You are here

page_theme.module in Page Theme 7.2

Same filename and directory in other branches
  1. 6 page_theme.module
  2. 7 page_theme.module

Allows to use different themes than the site default on specific pages.

File

page_theme.module
View source
<?php

/**
 * @file
 * Allows to use different themes than the site default on specific pages.
 */

/**
 * Implements hook_help().
 */
function page_theme_help($path, $arg) {
  switch ($path) {
    case 'admin/help#page_theme':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Page Theme module is a simple and easy module to use which allows you to use different themes than the site default on specific pages. For more information, see the online handbook entry for <a href="@page_theme">Page Theme module</a>.', array(
        '@page_theme' => 'https://www.drupal.org/project/page_theme',
      )) . '</p>';
      return $output;
    case 'admin/appearance/page-theme':
      $output = '<p>' . t('If pages are several defined, the first rule in the list will be used.') . '</p>';
      $output .= '<p>' . t('Only themes, which are enabled in the <a href="@themes_section">themes section</a>, will be used otherwise the site default theme.', array(
        '@themes_section' => url('admin/appearance'),
      )) . '</p>';
      return $output;
  }
}

/**
 * Implements hook_menu().
 */
function page_theme_menu() {
  $items['admin/appearance/page-theme'] = array(
    'title' => 'Page theme',
    'description' => 'Configure which theme is used on which pages.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'page_theme_admin_overview',
    ),
    'access arguments' => array(
      'administer themes',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 50,
    'file' => 'page_theme.admin.inc',
  );
  $items['admin/appearance/page-theme/add'] = array(
    'title' => 'Add rule',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'page_theme_admin_add',
    ),
    'access arguments' => array(
      'administer themes',
    ),
    'type' => MENU_LOCAL_ACTION,
    'file' => 'page_theme.admin.inc',
  );
  $items['admin/appearance/page-theme/list'] = array(
    'title' => 'List rules',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/appearance/page-theme/manage/%page_theme_rule'] = array(
    'title' => 'Configure rule',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'page_theme_admin_edit',
      4,
    ),
    'access arguments' => array(
      'administer themes',
    ),
    'file' => 'page_theme.admin.inc',
  );
  $items['admin/appearance/page-theme/manage/%page_theme_rule/configure'] = array(
    'title' => 'Configure',
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/appearance/page-theme/manage/%page_theme_rule/delete'] = array(
    'title' => 'Delete',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'page_theme_admin_delete',
      4,
    ),
    'access arguments' => array(
      'administer themes',
    ),
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
    'file' => 'page_theme.admin.inc',
  );
  return $items;
}
function page_theme_rule_load($rule) {
  $rule = strtr($rule, array(
    '-' => '_',
  ));
  return db_query('SELECT * FROM {page_theme} WHERE rule = :rule', array(
    ':rule' => $rule,
  ))
    ->fetchObject();
}

/**
 * Implements hook_theme().
 */
function page_theme_theme() {
  $themes['page_theme_admin_overview'] = array(
    'render element' => 'form',
    'file' => 'page_theme.theme.inc',
  );
  return $themes;
}

/**
 * Implements hook_custom_theme().
 */
function page_theme_custom_theme() {
  global $user;
  $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));
  $rids = array_keys($user->roles);
  $result = db_query("SELECT DISTINCT pt.theme, pt.pages \n                        FROM {page_theme} pt LEFT JOIN {page_theme_role} ptr ON pt.ptid = ptr.ptid \n                        WHERE pt.status = 1 AND (ptr.rid IN (:rids) OR ptr.rid IS NULL) \n                        ORDER BY pt.weight, pt.theme", array(
    ':rids' => $rids,
  ));
  foreach ($result as $page_theme) {
    $pages = drupal_strtolower($page_theme->pages);
    $page_match = drupal_match_path($path, $pages);
    if ($path != $_GET['q']) {
      $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
    }
    if ($page_match) {
      return $page_theme->theme;
    }
  }
}

/**
 *  Helper functions for themes.
 */
function page_theme_get_themes() {
  static $themes = array();
  if (!$themes) {
    $result = db_query('SELECT name, status, info FROM {system} WHERE type = :type', array(
      ':type' => 'theme',
    ));
    foreach ($result as $theme) {
      $theme->info = unserialize($theme->info);
      if (empty($theme->info['hidden']) || !$theme->info['hidden']) {
        $themes[$theme->name] = array(
          'theme' => $theme->name,
          'name' => $theme->info['name'],
          'status' => $theme->status,
        );
      }
    }
  }
  return $themes;
}
function page_theme_get_themes_list() {
  static $themes = array();
  if (!$themes) {
    foreach (page_theme_get_themes() as $theme) {
      $themes[$theme['theme']] = $theme['name'];
    }
    natcasesort($themes);
  }
  return $themes;
}
function page_theme_get_themes_options() {
  $options['0'] = '- ' . t('Select theme') . ' -';
  $options += page_theme_get_themes_list();
  return $options;
}
function page_theme_get_theme_name($theme, $expand = FALSE) {
  $themes = page_theme_get_themes();
  if (isset($themes[$theme])) {
    $name = $expand && !$themes[$theme]['status'] ? check_plain($themes[$theme]['name']) . '<br><small>' . t('(Disabled)') . '</small>' : $themes[$theme]['name'];
  }
  else {
    $name = $expand ? $theme . '<br><small>' . t('(Not available)') . '</small>' : $theme;
  }
  return $name;
}
function page_theme_get_rule_roles($rule) {
  static $roles = array();
  if (!$roles) {
    $roles = user_roles();
  }
  $rule_roles = db_query('SELECT rid FROM {page_theme_role} WHERE ptid = :ptid', array(
    'ptid' => $rule->ptid,
  ))
    ->fetchAllKeyed(0, 0);
  return array_intersect_key($roles, $rule_roles);
}