You are here

omega_tools.module in Omega Tools 7.2

File

omega_tools.module
View source
<?php

/**
 * Implementation of hook_menu
 */
function omega_tools_menu() {
  foreach (list_themes() as $theme) {
    $items['admin/appearance/settings/' . $theme->name . '/reset'] = array(
      'title' => $theme->info['name'],
      'page callback' => array(
        '_omega_tools_theme_reset',
      ),
      'page arguments' => array(
        $theme->name,
      ),
      'type' => MENU_CALLBACK,
      'access callback' => '_system_themes_access',
      'access arguments' => array(
        $theme,
      ),
    );
  }
  return $items;
}

/**
 * Implementation of hook_form_alter()
 * Functionality:
 *  - system_theme_settings form
 *    For this form the form_alter organizes the form a bit better, adding fieldsets
 *    to the default items, making them collapsible, and changing the default size of
 *    the file uploads for logo and favicon
 */
function omega_tools_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'system_theme_settings':
      $theme = arg(3);
      $themes = list_themes();

      // if we have an active theme settings page, not the default one
      if (isset($theme) && $theme != 'templates') {

        // we have a specific theme settings page defined, so we are going to render
        // a custom fieldset and submit button/handler that will allow a user to reset
        // the theme settings stored in the system.
        if (isset($themes[$theme]->base_theme)) {
          $base_themes = system_find_base_themes($themes, $theme, $used_keys = array());
          $omega_base = in_array('Omega', $base_themes) ? TRUE : FALSE;
        }
        else {
          $omega_base = FALSE;
        }

        //krumo($themes);
        if ($omega_base || $themes[$theme]->name == 'omega') {
          $form['omega_general']['omega_tools_reset'] = array(
            '#type' => 'fieldset',
            '#title' => t('Revert Theme Settings'),
            '#collapsible' => TRUE,
            '#collapsed' => FALSE,
            '#description' => '<p><strong>Use this feature with caution!!!</strong></p><p>Using this feature will allow you to revert your theme settings to anything that is in code, and erase the database stored settings in the <em>system</em> table. This is accomplished by removing the record in the table, and then re-enabling the theme. All settings from the .info file will be used rather than any stored changes in the database.</p><p>If you would like to save the theme settings you have now, and put them in code, use the export feature, and paste the settings[] array in your .info file replacing the current values for that array. Then it will be safe to revert without destroying changes made to the theme settings for your subtheme.</p><p>If you have manually edited the settings in the .info file, and are unconcerned with the settings in the database, or the settings in the database are out of date, then it is also safe to revert.</p>',
            '#weight' => 50,
          );
          $form['omega_general']['omega_tools_reset']['theme_reset'] = array(
            '#markup' => l('Revert Theme Settings for ' . $theme, 'admin/appearance/settings/' . $theme . '/reset'),
          );
          $form['omega_general']['omega_tools_export'] = array(
            '#type' => 'fieldset',
            '#title' => t('Export Theme Settings'),
            '#collapsible' => TRUE,
            '#collapsed' => FALSE,
            '#description' => '<p>This feature will dump the settings[] array for your .info file, so you may safely paste this data over the appropriate section in your themes .info, and then use the revert feature without actually using your settings <strong>&lt;/theme-export-awesomesauce&gt;</strong>.</p>',
            '#weight' => 49,
          );

          // gather theme information
          $overwritten_settings = variable_get('theme_' . $theme . '_settings', FALSE);
          if ($overwritten_settings) {
            $form['omega_general']['omega_tools_export']['theme_settings_status'] = array(
              '#markup' => 'Current Theme Settings status: <strong>Overwritten</strong>',
            );
            $s = $overwritten_settings;
            unset($s['toggle_logo'], $s['toggle_name'], $s['toggle_slogan'], $s['toggle_node_user_picture'], $s['toggle_comment_user_picture'], $s['toggle_comment_user_verification'], $s['toggle_favicon'], $s['toggle_main_menu'], $s['toggle_secondary_menu'], $s['default_logo'], $s['logo_path'], $s['logo_upload'], $s['default_favicon'], $s['favicon_path'], $s['favicon_upload'], $s['theme_settings_export_code']);
            $export_vars = '';
            foreach ($s as $key => $val) {
              if (isset($val) && !empty($val)) {
                if (is_array($val)) {
                  foreach ($val as $k => $v) {
                    $export_vars .= "settings[" . $key . "][{$k}] = '" . $v . "'\n";
                  }
                }
                else {
                  $export_vars .= "settings[" . $key . "] = '" . $val . "'\n";
                }
              }
              else {
                $export_vars .= "settings[" . $key . "] = ''\n";
              }
            }
            $form['omega_general']['omega_tools_export']['theme_settings_export_code'] = array(
              '#type' => 'textarea',
              '#title' => t('Export Code'),
              '#default_value' => $export_vars,
              '#description' => 'You will want to paste this code in the .info file of your subtheme and overwrite the entire settings[] section to properly export this code.',
            );
          }
          else {
            $form['omega_general']['omega_tools_export']['theme_settings_status'] = array(
              '#markup' => 'Current Theme Settings status: <strong>In Code (.info)</strong>',
            );
          }
        }
      }
      break;
  }
}

/**
 * Create a form to reset the theme settings in the database
 */
function _omega_tools_theme_reset_form($form, &$form_state, $theme) {
  $form = array();
  $form['theme_reset'] = array(
    '#markup' => '<p><strong>WARNING:</strong> You will be fully removing the theme settings stored in the database for this theme (' . $theme . '). This will revert to whatever information is set in the .info file, and erase any customizations made after installation of this theme.</p>',
  );
  $form['theme'] = array(
    '#type' => 'hidden',
    '#value' => $theme,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Revert Theme Settings'),
  );
  $form['#submit'][] = '_omega_tools_theme_reset_submit';

  //krumo($form);
  return $form;
}

/**
 * Callback for generating the reset form for theme settings
 */
function _omega_tools_theme_reset($theme = '') {
  $form = drupal_get_form('_omega_tools_theme_reset_form', $theme);
  return $form;
}

/**
 * Submit handler for theme reset
 */
function _omega_tools_theme_reset_submit($form, $form_state) {
  $theme = $form_state['values']['theme'];

  // set our redirect
  $form_state['redirect'] = '/admin/appearance/settings/' . $theme;

  // remove the stored DB copy of the theme settings that have been modified
  variable_del('theme_' . $theme . '_settings');

  // clear the cache in case that will help
  drupal_flush_all_caches();

  // rebuild the theme information
  system_rebuild_theme_data();

  // clear the cache in case that will help
  drupal_flush_all_caches();

  // report to the log
  watchdog('theme', 'Theme settings for %theme reset to default values.', array(
    '%theme' => $theme,
  ));

  // make a nice message to say it happened, and log to the system
  drupal_set_message('Theme settings for <strong>' . $form_state['values']['theme'] . '</strong> have been reset...');

  // go back to the primary settings page for this theme
  drupal_redirect_form($form_state);
}

Functions

Namesort descending Description
omega_tools_form_alter Implementation of hook_form_alter() Functionality:
omega_tools_menu Implementation of hook_menu
_omega_tools_theme_reset Callback for generating the reset form for theme settings
_omega_tools_theme_reset_form Create a form to reset the theme settings in the database
_omega_tools_theme_reset_submit Submit handler for theme reset