You are here

function magic_extension_assets_theme_settings_form_submit in Magic 7

Submit handler for the theme settings form.

This form will take the theme settings, and for the css and js excludes, create the regex that will be required to remove the css and js files at will. It will then selectively clear the caches for those specific cache items.

1 string reference to 'magic_extension_assets_theme_settings_form_submit'
magic_form_system_theme_settings_alter in ./magic.module
Implements hook_form_alter().

File

./magic.module, line 367
Keep Frontend DRY; sprinkle it with MAGIC!

Code

function magic_extension_assets_theme_settings_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  $clear_caches = array(
    'css' => FALSE,
    'js' => FALSE,
  );
  foreach (array(
    'magic_css_excludes',
    'magic_js_excludes',
  ) as $item) {

    // Check to see if this has changed since the last save. If so, we have to
    // rebuild the regex and clear our caches.
    $current_settings = variable_get($values['var'], FALSE);
    if (!isset($current_settings[$item . '_regex']) || $values[$item] !== $current_settings[$item]) {

      // Since we are updating the script, we must clear our caches after. We
      // set it this way, so that we don't clear them twice if both the CSS and
      // JS changed.
      $type = $item == 'magic_css_excludes' ? 'css' : 'js';
      $clear_caches[$type] = TRUE;

      // Explode and trim the values for the exclusion rules.
      $excludes = array_filter(array_map('trim', explode("\n", $values[$item])));
      if (!empty($excludes)) {
        module_load_include('inc', 'magic', 'includes/magic');

        // Now we get the regex and set that.
        $excludes = magic_generate_exclude_full($excludes);
        $excludes['exclude'] = magic_generate_path_regex($excludes['exclude']);
        $excludes['include'] = magic_generate_path_regex($excludes['include']);
        if ($item == 'magic_css_excludes') {

          // Make sure that RTL styles are excluded as well when a file name has been
          // specified with it's full .css file extension.
          $excludes['exclude'] = preg_replace('/\\\\.css$/', '(\\.css|-rtl\\.css)', $excludes['exclude']);
          $excludes['include'] = preg_replace('/\\\\.css$/', '(\\.css|-rtl\\.css)', $excludes['include']);
        }

        // Last check, if we didnt actually have anything in excludes, we don't
        // save a thing.
        if ($excludes['exclude'] == FALSE) {
          $excludes = FALSE;
        }
      }
      else {

        // We have nothing we are excluding, ignore the setting.
        $excludes = FALSE;
      }
    }
    else {

      // If we dont re-set the form value, it will get removed.
      $excludes = $current_settings[$item . '_regex'];
    }
    form_set_value(array(
      '#parents' => array(
        $item . '_regex',
      ),
    ), $excludes, $form_state);
  }

  // Now we will actually clear the caches.
  if (!empty($clear_caches)) {
    _magic_clear_cache(substr($values['var'], '6', '-9'), $clear_caches);
  }
}