You are here

themekey_ui.module in ThemeKey 6.3

ThemeKey UI is an extension for ThemeKey

ThemeKey UI adds a form element to node create and edit forms to assign a theme to a node.

ThemeKey UI adds a theme option to the 'URL aliases' administration if the "Path" module is enabled.

ThemeKey UI adds a form element to the user profile for themeing all nodes created by this user.

@author Markus Kalkbrenner | Cocomore AG

@author profix898

File

themekey_ui.module
View source
<?php

/**
 * @file
 * ThemeKey UI is an extension for ThemeKey
 *
 * ThemeKey UI adds a form element to node create and edit forms
 * to assign a theme to a node.
 *
 * ThemeKey UI adds a theme option to the 'URL aliases' administration
 * if the "Path" module is enabled.
 *
 * ThemeKey UI adds a form element to the user profile for themeing all
 * nodes created by this user.
 *
 * @see themekey.module
 *
 * @author Markus Kalkbrenner | Cocomore AG
 *   @see http://drupal.org/user/124705
 *
 * @author profix898
 *   @see http://drupal.org/user/35192
 */

/**
 * Implements hook_themekey_properties()
 *
 * Provides additional properties for the ThemeKey module:
 *   themekey_ui:node_triggers_theme
 *
 * @return
 *   array of themekey properties
 */
function themekey_ui_themekey_properties() {

  // Attributes for properties
  $attributes = array();
  $attributes['themekey_ui:node_triggers_theme'] = array(
    'description' => t("Property themekey_ui:node_triggers_theme could not be selected from the property drop down. You get this static property by activating !link. Afterwards, you can move the property to any position in the rule chain. When done, it triggers the switch to the theme assigned to the current node using ThemeKey UI.", array(
      '!link' => l(t('Show theme option in create/edit node forms'), 'admin/settings/themekey/settings/ui'),
    )),
    'page cache' => THEMEKEY_PAGECACHE_SUPPORTED,
    'static' => TRUE,
  );
  $attributes['themekey_ui:node_author_triggers_theme'] = array(
    'description' => t("Property themekey_ui:node_author_triggers_theme could not be selected from the property drop down. You get this static property by activating !link. Afterwards, you can move the property to any position in the rule chain. When done, it triggers the switch to the theme the author selected for his nodes in his user profile.", array(
      '!link' => l(t('Show theme option in user profile'), 'admin/settings/themekey/settings/ui'),
    )),
    'page cache' => THEMEKEY_PAGECACHE_SUPPORTED,
    'static' => TRUE,
  );

  // Mapping functions
  $maps = array();
  $maps[] = array(
    'src' => 'node:nid',
    'dst' => 'themekey_ui:node_triggers_theme',
    'callback' => 'themekey_ui_nid2theme',
  );
  $maps[] = array(
    'src' => 'node:nid',
    'dst' => 'themekey_ui:node_author_triggers_theme',
    'callback' => 'themekey_ui_author2theme',
  );
  return array(
    'attributes' => $attributes,
    'maps' => $maps,
  );
}

/**
 * This function implements the interface of a ThemeKey
 * mapping function but doesn't set a ThemeKey property's
 * value. It sets the global variable $custom_theme to a
 * theme directly, which will cause ThemeKey to use this
 * theme.
 *
 * @param $nid
 *   a node id, current value of ThemeKey property node:nid
 *
 * @return
 *   string "static" if global custom theme has been set
 *   or NULL if no theme has been assigned to the node
 */
function themekey_ui_nid2theme($nid) {
  global $custom_theme;

  // node_load() must not be called from hook_init().
  // Therefor we have to execute SQL here using hook_nodeapi().
  $node = new stdClass();
  $node->nid = $nid;
  $node->vid = themekey_node_get_simple_node_property($nid, 'vid');
  $node->type = themekey_node_get_simple_node_property($nid, 'type');
  themekey_ui_nodeapi($node, 'load');
  if (!empty($node->themekey_ui_theme) && 'default' != $node->themekey_ui_theme && themekey_check_theme_enabled($node->themekey_ui_theme)) {
    $custom_theme = $node->themekey_ui_theme;
    return 'static';
  }
  return NULL;
}

/**
 * This function implements the interface of a ThemeKey
 * mapping function but doesn't set a ThemeKey property's
 * value. It sets the global variable $custom_theme to a
 * theme directly which will cause ThemeKey to use this
 * theme.
 *
 * @param $nid
 *   a node id, current value of ThemeKey property node:nid
 *
 * @return
 *   string "static" if global custom theme has been set
 *   or NULL if no theme has been assigned to the node
 */
function themekey_ui_author2theme($nid) {
  global $custom_theme;

  // node_load() must not be called from hook_init().
  // Therefore, we have to execute SQL here.
  if ($theme = db_result(db_query("SELECT theme FROM {node} JOIN {themekey_ui_author_theme} USING (uid) WHERE nid = %d", $nid))) {
    if ('default' != $theme && themekey_check_theme_enabled($theme)) {
      $custom_theme = $theme;
      return 'static';
    }
  }
  return NULL;
}

/**
 * Implements hook_theme().
 */
function themekey_ui_theme() {
  return array(
    'themekey_ui_table' => array(
      'file' => 'themekey_ui_admin.inc',
      'arguments' => array(
        'form' => NULL,
      ),
    ),
    'themekey_ui_theme_select_form' => array(
      'file' => 'themekey_ui_admin.inc',
      'arguments' => array(
        'form' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_perm().
 */
function themekey_ui_perm() {
  return array(
    'assign node themes',
    'assign path alias themes',
    'assign theme to own nodes',
  );
}

/**
 * Implements hook_menu().
 */
function themekey_ui_menu() {
  $items = array();
  $items['admin/settings/themekey/settings/ui'] = array(
    'title' => 'User Interface',
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer themekey settings',
    ),
    'file' => 'themekey_ui_admin.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'themekey_ui_settings_form',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
  return $items;
}

/**
 * Implements hook_form_alter().
 */
function themekey_ui_form_alter(&$form, $form_state, $form_id) {
  if ('path_admin_form' == $form_id) {

    // path aliases form
    if (user_access('assign path alias themes') && variable_get('themekey_ui_pathalias', 0)) {
      module_load_include('inc', 'themekey_ui', 'themekey_ui_admin');
      themekey_ui_pathalias($form);
    }
  }
  elseif ('user_profile_form' == $form_id) {
    if (user_access('assign theme to own nodes') && variable_get('themekey_ui_author', 0)) {
      module_load_include('inc', 'themekey_ui', 'themekey_ui_admin');

      // to avoid a sql query to load his nodes themes every time a user is loaded, we do this query here
      $theme = FALSE;
      if (!empty($form['#uid'])) {
        $theme = db_result(db_query("SELECT theme FROM {themekey_ui_author_theme} WHERE uid = %d", $form['#uid']));
      }
      themekey_ui_theme_select_form($form, t('Theme configuration for my nodes'), t('Every node I create will be shown to other users using this theme.'), $theme ? $theme : 'default', $form['theme_select']['#weight'], FALSE);
    }
  }
  elseif ('themekey_help_tutorials_form' == $form_id) {
    module_load_include('inc', 'themekey_ui', 'themekey_ui_help');
    themekey_ui_help_tutorials($form);
  }
  else {

    // node form?
    if (variable_get('themekey_ui_nodeform', 0) && user_access('assign node themes')) {
      $type = isset($form['type']['#value']) ? $form['type']['#value'] : FALSE;
      if ($form_id == $type . '_node_form' && variable_get('themekey_ui_nodeform|' . $type, 1)) {
        module_load_include('inc', 'themekey_ui', 'themekey_ui_admin');
        $description = t('Theme configuration for this node');
        if (module_exists('og') && !og_is_omitted_type($type)) {
          $description .= '<p><b>' . t('Note:') . '</b> ' . t('This content type is used in organic groups. A Theme you select here will only be used to display this node if you set the theme for the organic group to %theme', array(
            '%theme' => variable_get('theme_default', 'garland') . ' ' . t('(site default theme)'),
          )) . '</p>';
        }
        themekey_ui_theme_select_form($form, t('Theme configuration for this node'), $description, !empty($form['#node']->themekey_ui_theme) ? $form['#node']->themekey_ui_theme : 'default');
      }
    }
  }
}

/**
 * Implements hook_nodeapi().
 */
function themekey_ui_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'load':
      if (variable_get('themekey_ui_nodeform', 0) && variable_get('themekey_ui_nodeform|' . $node->type, 1)) {
        if (!empty($node->vid)) {
          $theme = db_result(db_query('SELECT theme FROM {themekey_ui_node_theme} WHERE nid = %d AND vid = %d', $node->nid, $node->vid));
          if ($theme) {
            $node->themekey_ui_theme = $theme;
          }
          else {
            $node->themekey_ui_theme = 'default';
          }
        }
      }
      break;
    case 'insert':
      if (!empty($node->themekey_ui_theme)) {
        $item = array(
          'nid' => $node->nid,
          'vid' => $node->vid,
          'theme' => $node->themekey_ui_theme,
        );
        drupal_write_record('themekey_ui_node_theme', $item);
      }
      break;
    case 'update':
      if (!empty($node->themekey_ui_theme)) {
        $item = array(
          'nid' => $node->nid,
          'vid' => $node->vid,
          'theme' => $node->themekey_ui_theme,
        );
        $key = array();
        if (!$node->revision && db_result(db_query("SELECT 1 FROM {themekey_ui_node_theme} WHERE nid = %d AND vid = %d", $node->nid, $node->vid))) {
          $key = array(
            'nid',
            'vid',
          );
        }
        drupal_write_record('themekey_ui_node_theme', $item, $key);
      }
      break;
    case 'delete':
      db_query('DELETE FROM {themekey_ui_node_theme} WHERE nid = %d', $node->nid);
      break;
  }
}

/**
 * Implements hook_user()
 */
function themekey_ui_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'insert':
    case 'update':
      if (user_access('assign theme to own nodes') && variable_get('themekey_ui_author', 0) && !empty($edit['themekey_ui_theme'])) {
        $author_theme = array(
          'uid' => $account->uid,
          'theme' => $edit['themekey_ui_theme'],
        );
        if (db_result(db_query("SELECT 1 FROM {themekey_ui_author_theme} WHERE uid = %d", $account->uid))) {
          drupal_write_record('themekey_ui_author_theme', $author_theme, array(
            'uid',
          ));
        }
        else {
          drupal_write_record('themekey_ui_author_theme', $author_theme);
        }
        if ($edit['themekey_ui_theme'] != $account->themekey_ui_theme) {

          // theme settings changed
          // fast deletion of page cache (truncate)
          cache_clear_all('*', 'cache_page', TRUE);
        }
        $edit['themekey_ui_theme'] = NULL;
      }
      break;
    case 'delete':
      db_query("DELETE FROM {themekey_ui_author_theme} WHERE uid = %d", $account->uid);
      break;
  }
}

Functions

Namesort descending Description
themekey_ui_author2theme This function implements the interface of a ThemeKey mapping function but doesn't set a ThemeKey property's value. It sets the global variable $custom_theme to a theme directly which will cause ThemeKey to use this theme.
themekey_ui_form_alter Implements hook_form_alter().
themekey_ui_menu Implements hook_menu().
themekey_ui_nid2theme This function implements the interface of a ThemeKey mapping function but doesn't set a ThemeKey property's value. It sets the global variable $custom_theme to a theme directly, which will cause ThemeKey to use this theme.
themekey_ui_nodeapi Implements hook_nodeapi().
themekey_ui_perm Implements hook_perm().
themekey_ui_theme Implements hook_theme().
themekey_ui_themekey_properties Implements hook_themekey_properties()
themekey_ui_user Implements hook_user()