You are here

domain_theme.module in Domain Access 6.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
 */

/**
 * Implement hook_init()
 */
function domain_theme_init() {

  // Assign the theme selected, based on the active domain.
  global $_domain, $custom_theme;

  // If the theme has already been set (such as an admin theme), only load settings.
  if (empty($custom_theme)) {
    $theme = domain_theme_lookup($_domain['domain_id']);
  }
  else {
    $theme = domain_theme_lookup($_domain['domain_id'], $custom_theme);
  }

  // The above returns -1 on failure.
  if ($theme != -1) {

    // Set our domain-specific theme.
    if (empty($custom_theme)) {
      $custom_theme = $theme['theme'];
    }
    domain_theme_set_variables($theme);
  }
}
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];
        }
      }
    }
  }
}

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

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

/**
 * Implement hook_theme()
 */
function domain_theme_theme() {
  $themes = array(
    'domain_theme_reset' => array(
      'arguments' => array(
        'domain' => array(),
      ),
    ),
  );
  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_fetch_array(db_query("SELECT domain_id, theme, settings, status FROM {domain_theme} WHERE domain_id = %d AND theme= '%s'", $domain_id, $theme));
  }
  else {
    $return = db_fetch_array(db_query("SELECT domain_id, theme, settings, status FROM {domain_theme} WHERE domain_id = %d AND status = 1", $domain_id));
  }
  if (empty($return)) {
    $return = -1;
  }
  return $return;
}

/**
 * Implement hook_domainlinks()
 */
function domain_theme_domainlinks($domain) {
  if ($domain['domain_id'] > 0) {
    $links[] = array(
      'title' => t('theme'),
      'path' => 'admin/build/domain/theme/' . $domain['domain_id'],
    );
    return $links;
  }
  return FALSE;
}

/**
 * Implement hook_domainform()
 */
function domain_theme_domainform(&$form) {

  // Set the module weight for Domain Theme,
  $module_weight = variable_get('domain_theme_weight', 0);
  db_query("UPDATE {system} SET weight = %d WHERE name = 'domain_theme' AND type = 'module'", $module_weight);

  // 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.'),
  );
}

/**
 * Implement 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_lookup',
    '#submit' => 'domain_theme_batch',
    '#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($values) {
  foreach ($values['domain_batch'] as $key => $value) {
    if ($key > 0) {

      // Clear out the old theme.
      db_query("UPDATE {domain_theme} SET status = 0 WHERE domain_id = %d", $key);
      $data = db_fetch_array(db_query("SELECT theme FROM {domain_theme} WHERE theme = '%s' AND domain_id = %d", $value, $key));
      if (isset($data['theme'])) {
        db_query("UPDATE {domain_theme} SET status = 1 WHERE theme = '%s' AND domain_id = %d", $value, $key);
      }
      else {
        db_query("INSERT INTO {domain_theme} (domain_id, theme, status) VALUES (%d, '%s', 1)", $key, $value);
      }
    }
    else {
      variable_set('theme_default', $value);
    }
  }
}

/**
 * Implement hook_domainwarnings()
 */
function domain_theme_domainwarnings() {
  $forms = array(
    'system_themes_form',
    'system_theme_settings',
  );
  $return = array();
  foreach ($forms as $form) {
    $return[$form] = 'admin/build/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_domainupdate().
 */
function domain_theme_domainupdate($op, $domain, $form_values = array()) {
  if ($op == 'delete') {
    db_query("DELETE FROM {domain_theme} WHERE domain_id = %d", $domain['domain_id']);
  }
}

Related topics