[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/themes/rootcandy/ -> template.php (source)

   1  <?php
   2  // $Id: template.php,v 1.36.2.50 2010/06/07 07:19:59 sign Exp $
   3  
   4  /**
   5   * @file
   6   * The theme template.php file
   7   */
   8  
   9  function rootcandy_body_class($left = NULL, $right = NULL) {
  10    $class = '';
  11    if ($left != '' AND $right) {
  12      $class = 'sidebars';
  13    }
  14    else if ($left != '' AND $right == '') {
  15      $class = 'sidebar-left';
  16    }
  17    else if ($left == '' AND $right != '') {
  18      $class = 'sidebar-right';
  19    }
  20  
  21    // Add classes describing the menu trail of the page.
  22    $class .= rootcandy_get_page_classes();
  23  
  24    if (isset($class)) {
  25      return ' class="'. $class .'"';
  26    }
  27  }
  28  
  29  function _rootcandy_admin_navigation() {
  30    $path = base_path() . path_to_theme();
  31    $base = path_to_theme();
  32  
  33    // get users role
  34    global $user;
  35  
  36    if ($user->uid != 1) {
  37      // get sorted roles
  38      $role_menu = _rootcandy_init_role_menu();
  39      if ($role_menu) {
  40        $rootcandy_navigation = theme_get_setting('rootcandy_navigation_source_'. $role_menu);
  41      }
  42    }
  43    else {
  44      $rootcandy_navigation = theme_get_setting('rootcandy_navigation_source_admin');
  45      if (!isset($rootcandy_navigation)) {
  46        $rootcandy_navigation = '_rootcandy_default_navigation';
  47      }
  48    }
  49  
  50    $menu_tree = array();
  51    if (!$rootcandy_navigation) {
  52      if (!$user->uid) {
  53        $menu_tree[] = array('href' => 'user/login', 'title' => t('User login'));
  54      }
  55    }
  56    elseif ($rootcandy_navigation == '_rootcandy_default_navigation') {
  57      // build default menu
  58      $menu_tree[] = array('href' => 'admin', 'title' => t('Dashboard'));
  59      $menu_tree[] = array('href' => 'admin/content', 'title' => t('Content'));
  60      if (variable_get('node_admin_theme', '0')) {
  61        $menu_tree[] = array('href' => 'node/add', 'title' => t('Create content'));
  62      }
  63      $menu_tree[] = array('href' => 'admin/build', 'title' => t('Building'));
  64      $menu_tree[] = array('href' => 'admin/settings', 'title' => t('Configuration'));
  65      $menu_tree[] = array('href' => 'admin/user', 'title' => t('Users'));
  66      $menu_tree[] = array('href' => 'admin/reports', 'title' => t('Reports'));
  67      if (module_exists('help')) {
  68        $menu_tree[] = array('href' => 'admin/help', 'title' => t('Help'));
  69      }
  70    }
  71    else {
  72      $menu_tree = menu_navigation_links($rootcandy_navigation);
  73    }
  74  
  75    if ($menu_tree) {
  76      $size = theme_get_setting('rootcandy_navigation_icons_size');
  77      $icons_disabled = theme_get_setting('rootcandy_navigation_icons');
  78      $list_class = 'i'. $size;
  79  
  80      $custom_icons = rootcandy_custom_icons();
  81      if (!isset($custom_icons)) {
  82        $custom_icons = '';
  83      }
  84  
  85      $match = _rootcandy_besturlmatch($_GET['q'], $menu_tree);
  86      $items = array();
  87      foreach ($menu_tree as $key => $item) {
  88        $router_item = menu_get_item($item['href']);
  89        if (!$router_item['access']) {
  90          continue;
  91        }
  92        $id = '';
  93        $icon = '';
  94        $class= '';
  95        // icons
  96        if (!$icons_disabled) {
  97          $size = theme_get_setting('rootcandy_navigation_icons_size');
  98          if (!isset($size)) $size = 24;
  99          $arg = explode("/", $item['href']);
 100          $icon = _rootcandy_icon($arg, $size, 'admin', $custom_icons);
 101          if ($icon) $icon = $icon .'<br />';
 102        }
 103        if ($key == $match) {
 104          $id = 'current';
 105          if (!$icons_disabled && $size) {
 106            $id = 'current-'. $size;
 107          }
 108        }
 109  
 110        // add a class to li
 111        $class = "";
 112        if (is_array($arg)) {
 113          $class = implode($arg, '-');
 114        }
 115  
 116        $item['data'] = l($icon . $item['title'], $item['href'], array('html' => TRUE));
 117        if (!empty($id)) $item['id'] = $id;
 118        if (!empty($class)) $item['class'] = $class;
 119        if (!empty($item['attributes'])) {
 120          unset($item['attributes']);
 121        }
 122        $items[] = $item;
 123  
 124      }
 125  
 126      $level = 1;
 127      if ($rootcandy_navigation == '_rootcandy_default_navigation') {
 128        $rootcandy_navigation = 'navigation';
 129        $level = 2;
 130      }
 131  
 132      return array(
 133        'navigation' => theme('admin_navigation', $items, $list_class),
 134        'menu' => $rootcandy_navigation,
 135        'level' => $level,
 136      );
 137    }
 138  }
 139  
 140  function rootcandy_admin_navigation($items, $class) {
 141    return theme('item_list', $items);
 142  }
 143  
 144  function _rootcandy_besturlmatch($needle, $menuitems) {
 145    $needle = drupal_get_path_alias($needle);
 146    $lastmatch = NULL;
 147    $lastmatchlen = 0;
 148    $urlparts = explode('/', $needle);
 149    $partcount = count($urlparts);
 150  
 151    foreach ($menuitems as $key => $menuitem) {
 152      $href = $menuitem['href'];
 153      $menuurlparts = explode('/', $href);
 154      $matches = _rootcandy_countmatches($urlparts, $menuurlparts);
 155      if (($matches > $lastmatchlen) || (($matches == $lastmatchlen) && (($lastmatch && drupal_strlen($menuitems[$lastmatch]['href'])) > drupal_strlen($href)) )) {
 156        $lastmatchlen = $matches;
 157        $lastmatch = $key;
 158      }
 159    }
 160    return $lastmatch;
 161  }
 162  
 163  /**
 164   * Override or insert PHPTemplate variables into the templates.
 165   */
 166  function rootcandy_preprocess_page(&$vars) {
 167    // get secondary links
 168    $vars['tabs2'] = menu_secondary_local_tasks();
 169  
 170    // color.module integration
 171    if (module_exists('color')) {
 172      _color_page_alter($vars);
 173    }
 174  
 175    // notify users that they can change settings in theme settings
 176    $admin_theme = variable_get('admin_theme', 'garland');
 177    if (arg(0) == 'admin' AND arg(1) == 'settings' AND arg(2) == 'admin' AND ($admin_theme == 'rootcandy' OR $admin_theme == 'rootcandy_fixed' OR $admin_theme == 'rootcandy_dark')) {
 178      $message = t('Thank you for using RootCandy.<br />Did you know, that Root Candy has advanced settings (Theme-specific settings fieldset)? You can change these settings at <a href="@configure-page">theme configuration page</a>.', array('@configure-page' => url('admin/build/themes/settings/'. $admin_theme)));
 179      $vars['messages'] .= '<div class="messages rootcandy">'. $message .'</div>';
 180      // display a warning message if the theme has not been enabled
 181      global $theme_info;
 182      if (!$theme_info->status) {
 183        $warning_message = t('RootCandy theme has not been enabled. You won\'t be able to see theme updates on update page! Please enable the theme on <a href="@themes-page">themes page</a>.', array('@themes-page' => url('admin/build/themes')));
 184        $vars['messages'] .= '<div class="messages warning">'. $warning_message .'</div>';
 185      }
 186    }
 187  
 188    if (arg(0) == 'admin' || ((arg(0) == 'node' AND is_numeric(arg(1)) AND arg(2) == 'edit') || (arg(0) == 'node' AND arg(1) == 'add'))) {
 189      $vars['go_home'] = '<a href="'. url() .'">'. t('Go Back to Homepage') .'</a>';
 190    }
 191  
 192    // get theme settings
 193    $vars['hide_header'] = theme_get_setting('rootcandy_header_display');
 194    $vars['hide_panel'] = theme_get_setting('rootcandy_hide_panel');
 195  
 196    // append legal notice
 197    if (!theme_get_setting('rootcandy_hide_author')) {
 198      $vars['closure'] .= '<div id="legal-notice">Theme created by <a href="http://sotak.co.uk" rel="external">Marek Sotak</a></div>';
 199    }
 200  
 201    $vars['hide_content'] = '';
 202  
 203  
 204    // check whether help is disabled
 205    if (theme_get_setting('rootcandy_help_display')) {
 206      unset($vars['help']);
 207    }
 208  
 209    // dashboard
 210    if (arg(0) == 'admin' AND !arg(1)) {
 211      if (!theme_get_setting('rootcandy_dashboard_display')) {
 212        $vars['dashboard'] = 1;
 213  
 214        // display help and messages in regions
 215        switch (theme_get_setting('rootcandy_dashboard_help')) {
 216          case 'left':
 217            $vars['dashboard_left'] = $vars['help'] . $vars['dashboard_left'];
 218            unset($vars['help']);
 219            break;
 220          case 'right':
 221            $vars['dashboard_right'] = $vars['help'] . $vars['dashboard_right'];
 222            unset($vars['help']);
 223            break;
 224        }
 225  
 226        switch (theme_get_setting('rootcandy_dashboard_messages')) {
 227          case 'left':
 228            $vars['dashboard_left'] = $vars['messages'] . $vars['dashboard_left'];
 229            unset($vars['messages']);
 230            break;
 231          case 'right':
 232            $vars['dashboard_right'] = $vars['messages'] . $vars['dashboard_right'];
 233            unset($vars['messages']);
 234            break;
 235        }
 236      }
 237      if (theme_get_setting('rootcandy_dashboard_content_display')) {
 238        $vars['hide_content'] = theme_get_setting('rootcandy_dashboard_content_display');
 239      }
 240    }
 241  
 242    $vars['panel_navigation'] = '<a id="open" class="open" href="#"><span class="panel-open">'. t('Open Panel') .'</span></a>';
 243    $vars['panel_navigation'] .= '<a id="close" style="display: none;" class="close" href="#"><span class="panel-close">'. t('Close Panel') .'</span></a>';
 244  
 245    $rootcandy_nav = _rootcandy_admin_navigation();
 246    $vars['rootcandy_navigation'] = $rootcandy_nav['navigation'];
 247  
 248    // get admin links into the region
 249    if (arg(0) == 'admin' AND arg(2)) {
 250      $menu = menu_navigation_links($rootcandy_nav['menu'], $rootcandy_nav['level']);
 251      $menu_links = _rootcandy_links($menu, array('id' => 'rootcandy-menu'));
 252      if ($vars['language']->direction) {
 253        $vars['admin_right'] = $menu_links . $vars['admin_right'];
 254      }
 255      else {
 256        $vars['admin_left'] = $menu_links . $vars['admin_left'];
 257      }
 258    }
 259  
 260    $vars['body_class'] = rootcandy_body_class($vars['admin_left'], $vars['admin_right']);
 261  
 262    $icons_disabled = theme_get_setting('rootcandy_navigation_icons');
 263    $rootcandy_navigation_class = array();
 264  
 265    if (!$icons_disabled) {
 266      $rootcandy_navigation_class[] = 'i'.theme_get_setting('rootcandy_navigation_icons_size');
 267    }
 268  
 269    if (!$vars['hide_header']) {
 270      $rootcandy_navigation_class[] = 'header-on';
 271    }
 272  
 273    $vars['rootcandy_navigation_class'] = '';
 274  
 275    if ($rootcandy_navigation_class) {
 276      $vars['rootcandy_navigation_class'] .= ' '. implode(' ', $rootcandy_navigation_class);
 277    }
 278  
 279    global $user;
 280    if ($user->uid) {
 281      $links[] = '<a href="'. url('user') .'" class="user-name">'. $user->name .'</a>';
 282      $links[] = '<a href="'. url('logout') .'">'. t('Logout') .'</a>';
 283      $links = implode(' | ', $links);
 284  
 285      $vars['rootcandy_user_links'] = $links;
 286    }
 287  }
 288  
 289  function rootcandy_admin_block_content($content) {
 290    if (!$content) {
 291      return '';
 292    }
 293  
 294    if (system_admin_compact_mode()) {
 295      drupal_add_js(drupal_get_path('theme', 'rootcandy') .'/compact-icon.js');
 296      drupal_add_css(drupal_get_path('theme', 'rootcandy') .'/compact-icon.css');
 297      $counter = 1;
 298      $output = '<ul class="admin-menu-compact">';
 299      foreach ($content as $item) {
 300        $additional_class = $counter % 4 == 0 ? ' last' : ''; //Append class last to every 4th item signifying a new row
 301        $item['localized_options']['html'] = TRUE;
 302  
 303        if ($image = _rootcandy_menu_icon($item['href'])) {
 304          $link = l($image .'<br />'. $item['title'], $item['href'], $item['localized_options']);
 305        }
 306        else {
 307          $link = l(theme('image', drupal_get_path('theme', 'rootcandy') .'/icons/i32/misc/unknown.png') .'<br />'. $item['title'], $item['href'], $item['localized_options']);
 308        }
 309  
 310        $output .= '<li class="leaf'. $additional_class .'">'. $link .'</li>';
 311        $counter++;
 312      }
 313      $output .= '</ul>';
 314    }
 315    else {
 316      $output = '<dl class="admin-list">';
 317      foreach ($content as $item) {
 318        $output .= '<dt>'. l($item['title'], $item['href'], $item['localized_options']) .'</dt>';
 319        $output .= '<dd>'. $item['description'] .'</dd>';
 320      }
 321      $output .= '</dl>';
 322    }
 323  
 324    return $output;
 325  }
 326  
 327  function _rootcandy_menu_icon($href) {
 328    $filename = drupal_get_path('theme', 'rootcandy') .'/icons/i32/admin/'. str_replace('/', '-', drupal_strtolower($href)) .'.png';
 329    return theme('image', $filename);
 330  }
 331  
 332  /**
 333   * Returns the rendered local tasks. The default implementation renders
 334   * them as tabs. Overridden to split the secondary tasks.
 335   *
 336   * @ingroup themeable
 337   */
 338  function rootcandy_menu_local_tasks() {
 339    return menu_primary_local_tasks();
 340  }
 341  
 342  function _rootcandy_icon($name, $size = '16', $subdir = '', $icons = '') {
 343    $url = implode("/", $name);
 344    $alias = drupal_get_path_alias($url);
 345    $name = implode("-", $name);
 346    $path = path_to_theme();
 347    if ($subdir) {
 348      $subdir = $subdir .'/';
 349    }
 350  
 351    if (isset($icons[$url])) {
 352      $icon = $icons[$url];
 353    }
 354    else if (isset($icons[$alias])) {
 355      $icon = $icons[$alias];
 356    }
 357    else {
 358      $icon = $path .'/icons/i'. $size .'/'. $subdir . $name .'.png';
 359    }
 360  
 361    $output = theme('image', $icon);
 362  
 363    if (!$output) {
 364      $icon = $path .'/icons/i'. $size .'/misc/unknown.png';
 365      $output = theme('image', $icon);
 366    }
 367  
 368    return $output;
 369  }
 370  
 371  function rootcandy_custom_icons() {
 372    $custom_icons = theme_get_setting('rootcandy_navigation_custom_icons');
 373    if (isset($custom_icons)) {
 374      $list = explode("\n", $custom_icons);
 375      $list = array_map('trim', $list);
 376      $list = array_filter($list, 'strlen');
 377      foreach ($list as $opt) {
 378        // Sanitize the user input with a permissive filter.
 379        $opt = rootcandy_filter_xss($opt);
 380        if (strpos($opt, '|') !== FALSE) {
 381          list($key, $value) = explode('|', $opt);
 382          $icons[$key] = $value ? $value : $key;
 383        }
 384        else {
 385          $icons[$opt] = $opt;
 386        }
 387      }
 388    }
 389    if (isset($icons)) {
 390      return $icons;
 391    }
 392  }
 393  
 394  function rootcandy_filter_xss($string) {
 395    return filter_xss($string);
 396  }
 397  
 398  /**
 399   * Read the theme settings' default values from the .info and save them into the database.
 400   *
 401   * @param $theme
 402   *   The actual name of theme that is being checked.
 403   */
 404  function rootcandy_settings_init($theme) {
 405    $themes = list_themes();
 406  
 407    // Get the default values from the .info file.
 408    $defaults = (is_array($themes[$theme]->info['settings'])) ? $themes[$theme]->info['settings'] : array();
 409  
 410    // Get the theme settings saved in the database.
 411    $settings = theme_get_settings($theme);
 412    // Don't save the toggle_node_info_ variables.
 413    if (module_exists('node')) {
 414      foreach (node_get_types() as $type => $name) {
 415        unset($settings['toggle_node_info_'. $type]);
 416      }
 417    }
 418  
 419    // Save default theme settings.
 420    variable_set(
 421      str_replace('/', '_', 'theme_'. $theme .'_settings'),
 422      array_merge($defaults, $settings)
 423    );
 424    // Force refresh of Drupal internals.
 425    theme_get_setting('', TRUE);
 426  }
 427  
 428  /*
 429   * In addition to initializing the theme settings during HOOK_theme(), init them
 430   * when viewing/resetting the admin/build/themes/settings/THEME forms.
 431   */
 432  if (arg(0) == 'admin' && arg(2) == 'themes' && arg(4)) {
 433    global $theme_key;
 434    rootcandy_settings_init($theme_key);
 435  }
 436  
 437  function rootcandy_get_page_classes($path = NULL) {
 438    if (!isset($path)) $path = $_GET['q'];
 439    if ($path) {
 440      $path = ' '. rootcandy_id_safe($path);
 441    }
 442    return $path;
 443  }
 444  
 445  /**
 446   * Converts a string to a suitable html ID attribute. Borrowed from Zen theme
 447   *
 448   * http://www.w3.org/TR/html4/struct/global.html#h-7.5.2 specifies what makes a
 449   * valid ID attribute in HTML. This function:
 450   *
 451   * - Ensure an ID starts with an alpha character by optionally adding an 'id'.
 452   * - Replaces any character except alphanumeric characters with dashes.
 453   * - Converts entire string to lowercase.
 454   *
 455   * @param $string
 456   *   The string
 457   * @return
 458   *   The converted string
 459   */
 460  function rootcandy_id_safe($string) {
 461    // Replace with dashes anything that isn't A-Z, numbers, dashes, or underscores.
 462    return check_plain(strtolower(preg_replace('/[^a-zA-Z0-9-]+/', '-', $string)));
 463  }
 464  
 465  function rootcandy_breadcrumb($breadcrumb) {
 466    if (!empty($breadcrumb)) {
 467      /*
 468       * Modern browsers (Firefox 1.5 and above, Internet Explorer 5.0 and above,
 469       * Safari not checked) would automatically change the double-arrows (») to
 470       * the other direction once it detects a right-to-left page. So no need to
 471       * reverse the array, and no need to use reversed-direction arrows.
 472       */
 473      return '<div class="breadcrumb">'. str_replace(t('Administer'), t('Dashboard'), implode(' » ', $breadcrumb)) .'</div>';
 474    }
 475  }
 476  
 477  function _rootcandy_links($links, $attributes = array('class' => 'links')) {
 478    $output = '';
 479  
 480    if (count($links) > 0) {
 481      $output = '<ul'. drupal_attributes($attributes) .'>';
 482  
 483      $num_links = count($links);
 484      $i = 1;
 485  
 486      foreach ($links as $key => $link) {
 487        $class = $key;
 488  
 489        // Add first, last and active classes to the list of links to help out themers.
 490        if ($i == 1) {
 491          $class .= ' first';
 492        }
 493        if ($i == $num_links) {
 494          $class .= ' last';
 495        }
 496  
 497        $check_path = $_GET['q'];
 498        $check_path = explode("/", $check_path);
 499        $q_path = $check_path[0] .'/'. $check_path[1] .'/'. $check_path[2];
 500        if (isset($link['href']) && ($link['href'] == $q_path || ($link['href'] == '<front>' && drupal_is_front_page()))) {
 501          $class .= ' active';
 502        }
 503        $output .= '<li'. drupal_attributes(array('class' => $class)) .'>';
 504  
 505        if (isset($link['href'])) {
 506          // Pass in $link as $options, they share the same keys.
 507          $output .= l($link['title'], $link['href'], $link);
 508        }
 509        else if (!empty($link['title'])) {
 510          // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
 511          if (empty($link['html'])) {
 512            $link['title'] = check_plain($link['title']);
 513          }
 514          $span_attributes = '';
 515          if (isset($link['attributes'])) {
 516            $span_attributes = drupal_attributes($link['attributes']);
 517          }
 518          $output .= '<span'. $span_attributes .'>'. $link['title'] .'</span>';
 519        }
 520  
 521        $i++;
 522        $output .= "</li>\n";
 523      }
 524  
 525      $output .= '</ul>';
 526    }
 527  
 528    return $output;
 529  }
 530  
 531  function rootcandy_menu_item_link($link) {
 532    if ($link['href'] == 'admin') {
 533      $link['title'] = t('Dashboard');
 534    }
 535  
 536    return theme_menu_item_link($link);
 537  }
 538  
 539  function _rootcandy_countmatches($arrayone, $arraytwo) {
 540    $matches = 0;
 541    foreach ($arraytwo as $i => $part) {
 542      if (!isset($arrayone[$i])) break;
 543      if ($arrayone[$i] == $part) $matches = $i+1;
 544    }
 545    return $matches;
 546  }
 547  
 548  function rootcandy_system_settings_form($form) {
 549    $themes = list_themes();
 550    $enabled_theme = arg(4);
 551    if ($form['#id'] == 'system-theme-settings' AND ($enabled_theme == 'rootcandy' || $themes[$enabled_theme]->base_theme == 'rootcandy')) {
 552  
 553      foreach ($form['theme_specific']['rows'] as $rid => $row) {
 554        //we are only interested in numeric keys
 555        if (intval($rid)) {
 556          $this_row = $row['data']['#value'];
 557          //Add the weight field to the row
 558          $weight = $form['theme_specific']['rows'][$rid]['role-weight-'. $rid]['#value'];
 559          $this_row[] = drupal_render($form['theme_specific']['navigation']['nav-by-role']['rootcandy_navigation_source_'. $rid]);
 560          $this_row[] = drupal_render($form['theme_specific']['rows'][$rid]['role-weight-'. $rid]);
 561          //Add the row to the array of rows
 562          $table_rows[$weight] = array('data' => $this_row, 'class' => 'draggable');
 563        }
 564      }
 565      ksort($table_rows);
 566  
 567      $header = array(
 568        "Role", "Navigation menu", "Order"
 569      );
 570  
 571      $form['theme_specific']['navigation']['role-weights']['content']['#value'] = theme('table', $header, $table_rows, array('id' => 'rootcandy-settings-table'));
 572      $output = drupal_render($form);
 573  
 574      drupal_add_tabledrag('rootcandy-settings-table', 'order', 'sibling', 'weight');
 575    }
 576    else {
 577      $output = drupal_render($form);
 578    }
 579    return $output;
 580  }
 581  
 582  function rootcandy_theme() {
 583    return array(
 584      'system_settings_form' => array(
 585        'arguments' => array('form' => NULL),
 586      ),
 587      'admin_navigation' => array(
 588        'arguments' => array('items' => NULL, 'class' => NULL),
 589      ),
 590    );
 591  }
 592  
 593  function _rootcandy_init_role_menu() {
 594    global $theme_key;
 595    global $user;
 596    $i = 100;
 597    $settings = theme_get_settings($theme_key);
 598    $menu = array();
 599  
 600    $roles = user_roles(FALSE);
 601  
 602    foreach ($user->roles as $rid => $role) {
 603      if (!$weight = $settings['role-weight-'. $rid]) {
 604        $settings['role-weight-'. $rid] = $i++;
 605      }
 606      $menu[$settings['role-weight-'. $rid]] = $rid;
 607    }
 608    ksort($menu);
 609    return $menu[key($menu)];
 610  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7