You are here

domain_theme.module in Domain Access 7.3

Domain Theme module for the Domain Access module group.

Originally written by canen; http://drupal.org/user/16188.

File

domain_theme/domain_theme.module
View source
<?php

/**
 * @defgroup domain_theme Domain Theme: manage themes
 *
 * Switch themes based on active domain.
 */

/**
 * @file
 * Domain Theme module for the Domain Access module group.
 *
 * Originally written by canen; http://drupal.org/user/16188.
 *
 * @ingroup domain_theme
 */

/**
 * Implements hook_custom_theme().
 */
function domain_theme_custom_theme() {
  global $conf;
  $_domain = domain_get_domain();

  // Assign the theme selected, based on the active domain.
  $theme = domain_theme_lookup($_domain['domain_id']);

  // Check for a custom admin theme.
  if (isset($conf['admin_theme']) && path_is_admin(current_path()) && user_access('view the administration theme')) {
    $theme = domain_theme_lookup($_domain['domain_id'], $conf['admin_theme']);

    // No custom settings, but use the admin theme.
    if ($theme == -1) {
      return $conf['admin_theme'];
    }
  }

  // The above returns -1 on failure.
  if ($theme != -1) {
    domain_theme_set_variables($theme);
    return $theme['theme'];
  }
}

/**
 * Set the stored variables for a domain.
 */
function domain_theme_set_variables($theme) {
  global $conf;
  if (!empty($theme['settings'])) {
    $settings = domain_unserialize($theme['settings']);
    $conf['theme_' . $theme['theme'] . '_settings'] = $settings;

    // Account for color module.
    $vars = array(
      'palette',
      'stylesheets',
      'logo',
      'files',
      'screenshot',
    );

    // In some cases, where the domain uses the default color palette
    // and the primary theme does not, we may only have the palette
    // stored, in which case, we have to load that data and ignore the rest.
    $palette_var = 'color_' . $theme['theme'] . '_palette';
    if (!isset($settings[$palette_var])) {
      if (isset($settings['palette'])) {
        $conf[$palette_var] = $settings['palette'];
      }
    }
    else {
      foreach ($vars as $variable) {
        $name = 'color_' . $theme['theme'] . '_' . $variable;
        if (isset($settings[$name])) {
          $conf[$name] = $settings[$name];
        }
      }
    }
  }
}

/**
 * Implements hook_menu()
 */
function domain_theme_menu() {
  $items = array();

  // Menu items for configuring themes.
  $items['admin/structure/domain/view/%domain/theme'] = array(
    'title' => 'Theme',
    'type' => MENU_LOCAL_TASK,
    'access arguments' => array(
      'administer domains',
    ),
    'page callback' => 'domain_theme_page',
    'page arguments' => array(
      4,
    ),
    'file' => 'domain_theme.admin.inc',
  );
  $items['admin/structure/domain/view/%domain/theme-reset'] = array(
    'title' => 'Domain theme settings',
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
    'access arguments' => array(
      'administer domains',
    ),
    'page callback' => 'domain_theme_reset',
    'page arguments' => array(
      4,
    ),
    'file' => 'domain_theme.admin.inc',
  );
  $items['admin/structure/domain/view/%domain/theme/%/theme-settings'] = array(
    'title' => 'Configure domain theme settings',
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
    'access arguments' => array(
      'administer domains',
    ),
    'page callback' => 'domain_theme_settings',
    'page arguments' => array(
      4,
      6,
    ),
    'file' => 'domain_theme.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_theme()
 */
function domain_theme_theme() {
  $themes = array(
    'domain_theme_reset' => array(
      'variables' => array(
        'domain' => array(),
      ),
    ),
    'domain_theme_form' => array(
      'render element' => 'form',
      'file' => 'domain_theme.admin.inc',
    ),
  );
  return $themes;
}

/**
 * Get domain theme information
 *
 * @param $domain_id
 *  The domain_id taken from {domain}.
 * @param $theme
 *  The string representation of a {domain_theme} entry. Optional.
 *  If this value is NULL, the default theme for this domain will be returned.
 * @param $reset
 *  A boolean flag to clear the static variable if necessary. Not used.  Here for consistency.
 * @return
 *  An array containing the requested row from the {domain_theme} table.
 *  Returns -1 on failure.
 */
function domain_theme_lookup($domain_id, $theme = NULL, $reset = FALSE) {
  if (!is_null($theme)) {
    $return = db_query("SELECT domain_id, theme, settings, status FROM {domain_theme} WHERE domain_id = :domain_id AND theme= :theme", array(
      ':domain_id' => $domain_id,
      ':theme' => $theme,
    ))
      ->fetchAssoc();
  }
  else {
    $return = db_query("SELECT domain_id, theme, settings, status FROM {domain_theme} WHERE domain_id = :domain_id AND status = 1", array(
      ':domain_id' => $domain_id,
    ))
      ->fetchAssoc();
  }
  if (empty($return)) {
    $return = -1;
  }
  return $return;
}

/**
 * Implements hook_domain_form()
 */
function domain_theme_domain_form(&$form) {

  // Set the module weight for Domain Theme,
  $module_weight = variable_get('domain_theme_weight', 0);
  db_update('system')
    ->fields(array(
    'weight' => $module_weight,
  ))
    ->condition('name', 'domain_theme')
    ->condition('type', 'module')
    ->execute();

  // Add the form element to the main screen.
  $form['domain_theme'] = array(
    '#type' => 'fieldset',
    '#title' => t('Theme settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $options = drupal_map_assoc(array(
    -100,
    -25,
    -10,
    -5,
    -1,
    0,
    1,
    5,
    10,
    25,
    100,
  ));
  $form['domain_theme']['domain_theme_weight'] = array(
    '#type' => 'select',
    '#title' => t('Domain Theme execution order'),
    '#options' => $options,
    '#default_value' => $module_weight,
    '#description' => t('If you use other modules that allow custom user or group themes, you may experience conflicts
      with the Domain Theme module.  Use this setting to vary the execution order of the Domain Theme module.  Lower
      (negative) values will execute earlier in the Drupal page building process.'),
  );
}

/**
 * Custom handler for domain_theme_domain_batch().
 *
 * @param $values
 * The form values passed by the FormsAPI.
 */
function domain_theme_batch_submit($values) {
  foreach ($values['domain_batch'] as $key => $value) {
    if ((int) $key != domain_default_id()) {

      // Clear out the old theme.
      db_update('domain_theme')
        ->fields(array(
        'status' => 0,
      ))
        ->condition('domain_id', $key)
        ->execute();
      $data = db_query("SELECT theme FROM {domain_theme} WHERE theme = :theme AND domain_id = :domain_id", array(
        ':theme' => $value,
        ':domain_id' => $key,
      ))
        ->fetchField();
      if (!empty($data) && $data == $value) {
        db_update('domain_theme')
          ->fields(array(
          'status' => 1,
        ))
          ->condition('domain_id', $key)
          ->condition('theme', $value)
          ->execute();
      }
      else {
        db_insert('domain_theme')
          ->fields(array(
          'domain_id' => $key,
          'theme' => $value,
          'status' => 1,
        ))
          ->execute();
      }
    }
    else {
      variable_set('theme_default', $value);
    }
  }
}

/**
 * Return the unique string path element used by color.module.
 *
 * @param $path
 *  A path to a color module file, such as 'default/files/garland-00123451/style.css'.
 * @ return
 *  A string indicating the color module's filepath.
 */
function domain_theme_get_color_path($path) {
  return current(array_slice(array_reverse(explode('/', $path)), 1, 1));
}

/**
 * Implements hook_form_alter().
 *
 * This function is a helper to a normal hook_form_alter implementation,
 * where we add additional form elements if we are dealing with domain-specific
 * form settings.
 */
function domain_theme_form_alter(&$form, &$form_state, $form_id) {

  // We cannot use a named form_alter here because of color module.
  // Color submit must happen first, and a named function destroys that.
  if ($form_id != 'system_theme_settings' || arg(2) != 'domain') {
    return;
  }
  $theme = arg(6);
  $domain_id = arg(4);
  $themes = list_themes();
  $domain = domain_lookup($domain_id);
  if ($domain == -1 || !array_key_exists($theme, $themes)) {
    return drupal_access_denied();
  }

  // Which domain are we editing?
  $form['domain_id'] = array(
    '#type' => 'value',
    '#value' => $domain['domain_id'],
  );
  $form['theme'] = array(
    '#type' => 'value',
    '#value' => $theme,
  );

  // We have to remove the core submit handler, but keep other handlers.
  // Otherwise, our changes get saved to the {variables} table.
  $form['#submit'][100] = 'domain_theme_settings_submit';
  foreach ($form['#submit'] as $key => $value) {
    if ($value == 'system_theme_settings_submit') {
      unset($form['#submit'][$key]);
    }
  }

  // Check for the presence of color.module. If it exists, we have to account
  // for it and run a check to ensure we don't delete the main site's theme
  // folder if we set a domain theme to the default scheme.
  $color_info = NULL;
  if (module_exists('color') && ($color_info = color_get_info($theme))) {
    $form['#submit'][-100] = 'domain_theme_color_submit';
  }

  // Order of operation becomes:
  // domain_theme_color_submit()
  // color_form_submit()
  // domain_theme_settings_submit()
  ksort($form['#submit']);

  // If no color module, we are done.
  if (empty($color_info)) {
    return;
  }

  // Label the scheme correctly.
  // Since may have added a 'domain_default' element to the palette,
  // color module might think we are not using the default theme.
  $current_scheme = variable_get('color_' . $theme . '_palette', array());
  if (isset($current_scheme['domain_default'])) {
    unset($current_scheme['domain_default']);
    $info = color_get_info($theme);
    if (isset($info['schemes']['default']['colors']) && implode(',', $info['schemes']['default']['colors']) == implode(',', $current_scheme)) {
      $form['color']['scheme']['#default_value'] = 'default';
    }
  }

  // Color module will reset the values in {variable}. which we don't
  // want to happen. So we have to grab the existing values and store
  // them so that we can set the {variable} table correctly.
  // TODO: Make this work with Domain Prefix.
  $vars = array(
    'palette',
    'stylesheets',
    'logo',
    'files',
    'screenshot',
  );
  foreach ($vars as $variable) {
    $name = 'color_' . $theme . '_' . $variable;
    $value = db_query("SELECT value FROM {variable} WHERE name = :name", array(
      ':name' => $name,
    ))
      ->fetchField();
    $color_settings[$name] = isset($value) ? $value : NULL;
  }
  $form['domain_color_defaults'] = array(
    '#type' => 'value',
    '#value' => $color_settings,
  );
}

/**
 * Implements hook_features_api().
 */
function domain_theme_features_api() {
  $components = array(
    'domain_theme' => array(
      'name' => t('Domain themes'),
      'default_hook' => 'domain_theme_default_themes',
      'default_file' => FEATURES_DEFAULTS_CUSTOM,
      'default_filename' => 'domains',
      'feature_source' => TRUE,
      'file' => drupal_get_path('module', 'domain_theme') . '/domain_theme.features.inc',
    ),
  );
  return $components;
}

Related topics

Functions

Namesort descending Description
domain_theme_batch_submit Custom handler for domain_theme_domain_batch().
domain_theme_custom_theme Implements hook_custom_theme().
domain_theme_domain_form Implements hook_domain_form()
domain_theme_features_api Implements hook_features_api().
domain_theme_form_alter Implements hook_form_alter().
domain_theme_get_color_path Return the unique string path element used by color.module.
domain_theme_lookup Get domain theme information
domain_theme_menu Implements hook_menu()
domain_theme_set_variables Set the stored variables for a domain.
domain_theme_theme Implements hook_theme()