You are here

domain_theme.module in Domain Access 7.2

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();

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

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

  // 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/theme/%domain'] = array(
    'title' => 'Domain theme settings',
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
    'access arguments' => array(
      'administer domains',
    ),
    'page callback' => 'domain_theme_page',
    'page arguments' => array(
      4,
    ),
    'file' => 'domain_theme.admin.inc',
  );
  $items['admin/structure/domain/theme-reset/%domain'] = 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/theme/%/%domain/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,
      5,
    ),
    '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_domainlinks()
 */
function domain_theme_domainlinks($domain) {
  if ($domain['domain_id'] > 0) {
    $links[] = array(
      'title' => t('theme'),
      'path' => 'admin/structure/domain/theme/' . $domain['domain_id'],
    );
    return $links;
  }
  return FALSE;
}

/**
 * Implements hook_domainform()
 */
function domain_theme_domainform(&$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.'),
  );
}

/**
 * Implements hook_domainbatch()
 */
function domain_theme_domainbatch() {
  $batch = array();

  // Allows the deletion of all Domain Theme rows.
  $batch['domain_theme'] = array(
    '#form' => array(
      '#title' => t('Reset themes'),
      '#type' => 'checkbox',
      '#options' => array(
        0 => 1,
        1 => t('Reset'),
      ),
      '#description' => t('Delete custom theme settings for this domain.'),
    ),
    '#permission' => 'administer themes',
    '#domain_action' => 'domain_delete',
    '#system_default' => 0,
    '#meta_description' => t('Delete custom theme settings for domains as supplied by Domain Theme.'),
    '#table' => 'domain_theme',
    '#weight' => -10,
  );

  // Change themes for sites.
  $themes = list_themes();
  $options = array();
  foreach ($themes as $theme) {
    if ($theme->status == 1) {
      $options[$theme->name] = $theme->name;
    }
  }
  $batch['site_theme'] = array(
    '#form' => array(
      '#title' => t('Theme settings'),
      '#type' => 'select',
      '#options' => $options,
      '#description' => t('Select the theme for this domain.'),
    ),
    '#domain_action' => 'custom',
    '#lookup' => 'domain_theme_batch_lookup',
    '#submit' => 'domain_theme_batch_submit',
    '#system_default' => variable_get('theme_default', 'garland'),
    '#variable' => 'theme_default',
    '#meta_description' => t('Change the themes for all domains.'),
    '#data_type' => 'string',
    '#update_all' => TRUE,
    '#weight' => -9,
  );
  foreach ($batch as $key => $value) {
    $batch[$key]['#module'] = t('Domain Theme');
  }
  return $batch;
}

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

      // 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 a theme setting for batch processing.
 *
 * @param $domain
 *   The domain being processed.
 * @return
 *   A valid theme name or -1 on failure.
 */
function domain_theme_batch_lookup($domain) {
  $theme = domain_theme_lookup($domain['domain_id']);
  if (isset($theme['theme'])) {
    return $theme['theme'];
  }
  return -1;
}

/**
 * Implements hook_domainwarnings()
 */
function domain_theme_domainwarnings() {
  $forms = array(
    'system_themes_form',
    'system_theme_settings',
  );
  $return = array();
  foreach ($forms as $form) {
    $return[$form] = 'admin/structure/domain/theme/%domain_id';
  }
  return $return;
}

/**
 * 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 additonal form elemtns 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(4);
  $domain_id = arg(5);
  $themes = list_themes();
  $domain = domain_load($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_domainupdate().
 */
function domain_theme_domainupdate($op, $domain, $form_values = array()) {
  if ($op == 'delete') {
    db_delete('domain_theme')
      ->condition('domain_id', $domain['domain_id'])
      ->execute();
  }
}

Related topics

Functions

Namesort descending Description
domain_theme_batch_lookup Return a theme setting for batch processing.
domain_theme_batch_submit Custom handler for domain_theme_domainbatch().
domain_theme_custom_theme Implements hook_custom_theme().
domain_theme_domainbatch Implements hook_domainbatch()
domain_theme_domainform Implements hook_domainform()
domain_theme_domainlinks Implements hook_domainlinks()
domain_theme_domainupdate Implements hook_domainupdate().
domain_theme_domainwarnings Implements hook_domainwarnings()
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()