[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/skinr/ -> skinr.module (source)

   1  <?php
   2  // $Id: skinr.module,v 1.13.2.6.2.38 2010/10/08 00:41:55 jgirlygirl Exp $
   3  
   4  /**
   5   * Implementation of hook_help().
   6   */
   7  function skinr_help($path, $arg) {
   8    switch ($path) {
   9      case 'admin/help#skinr':
  10        if (module_exists('advanced_help')) {
  11          return t('Visit the <a href="@skinr-help">help page</a> for full documentation.', array('@skinr-help' => url('admin/advanced_help/skinr')));
  12        }
  13        else {
  14          return t('Please download and enable the <a href="http://drupal.org/project/advanced_help">Advanced Help</a> module for full Skinr documentation.');
  15        }
  16        break;
  17    }
  18  }
  19  
  20  /**
  21   * Implementation of hook_init().
  22   */
  23  function skinr_init() {
  24    static $run = FALSE;
  25    if (!$run) {
  26      module_load_include('inc', 'skinr', 'skinr.handlers');
  27      skinr_module_include('skinr.inc');
  28      $run = TRUE;
  29    }
  30  }
  31  
  32  /**
  33   * Implementation of hook_preprocess().
  34   */
  35  function skinr_preprocess(&$vars, $hook) {
  36    // Let's make sure this has been run. There have been problems where other
  37    // modules implement a theme function in their hook_init(), so we make doubly
  38    // sure that the includes are included.
  39    skinr_init();
  40  
  41    $skinr_config = skinr_fetch_config();
  42    $current_theme = skinr_current_theme();
  43    $theme_registry = theme_get_registry();
  44  
  45    $original_hook = $hook;
  46    if (isset($theme_registry[$hook]['original hook'])) {
  47      $original_hook = $theme_registry[$hook]['original hook'];
  48    }
  49  
  50    foreach ($skinr_config as $module => $settings) {
  51      if (!empty($settings['preprocess'][$original_hook])) {
  52        $preprocess_settings = $settings['preprocess'][$original_hook];
  53        $sids = skinr_handler('preprocess_index_handler', 'preprocess', $preprocess_settings['index_handler'], $vars);
  54  
  55        if ($extracted = skinr_skin_extract($module, $sids, $settings, $current_theme)) {
  56          foreach ($extracted['css'] as $file) {
  57            if ($file['enabled']) {
  58              _skinr_add_file($file['path'], 'css', $file['media']);
  59            }
  60          }
  61          foreach ($extracted['js'] as $file) {
  62            if ($file['enabled']) {
  63              _skinr_add_file($file['path'], 'js');
  64            }
  65          }
  66          if (!empty($extracted['template'])) {
  67            $vars['template_files'][] = $extracted['template'];
  68          }
  69          $vars['skinr'] = implode(' ', $extracted['classes']);
  70          $vars['skinr_array'] = $extracted['classes'];
  71  
  72          // Need to add a hook for skinr_ui to plug into.
  73          $data = array(
  74            'hook' => $hook,
  75            'vars' => &$vars,
  76            'skin' => &$extracted,
  77          );
  78          drupal_alter('skinr_preprocess', $data);
  79  
  80          // Reset styles to make sure all are included.
  81          $vars['styles'] = drupal_get_css();
  82          $vars['scripts'] = drupal_get_js();
  83        }
  84      }
  85    }
  86  }
  87  
  88  function skinr_skin_extract($module, $sids, $settings, $theme = NULL, $reset = FALSE) {
  89    if (empty($sids)) {
  90      return FALSE;
  91    }
  92    if (!is_array($sids)) {
  93      $sids = array($sids);
  94    }
  95    if (is_null($theme)) {
  96      $theme = skinr_current_theme();
  97    }
  98  
  99    $info = skinr_skin_data();
 100  
 101    $extracted = array(
 102      'module' => $module,
 103      'sids' => $sids,
 104      'classes' => array(),
 105      'css' => array(),
 106      'js' => array(),
 107      'template' => array(),
 108    );
 109  
 110    $skins = array();
 111    foreach ($sids as $sid) {
 112      if ($skinr = skinr_get($theme, $module, $sid, $reset)) {
 113        $skins = $skinr->skins + $skins;
 114      }
 115      // Only need to reset the first time.
 116      $reset = FALSE;
 117    }
 118  
 119    // Allow other modules to alter the skinr skins array.
 120    // @todo Fix this to work with skinr ui.
 121    drupal_alter('skinr_skins', $skins, $module, $sids, $settings);
 122  
 123    $extracted['css'] = skinr_skin_get_files($skins, 'css', $theme);
 124    $extracted['js'] = skinr_skin_get_files($skins, 'js', $theme);
 125  
 126    // Add template files.
 127    if (!empty($skins['_template'])) {
 128      $extracted['template'] = $skins['_template'];
 129      unset($skins['_template']);
 130    }
 131  
 132    $extracted['classes'] = skinr_flatten_skins_array($skins);
 133  
 134    return $extracted;
 135  }
 136  
 137  /**
 138   * Helper function to fetch all css or js files from an array of skins.
 139   *
 140   * @param $skins
 141   *   A an array of available skins.
 142   * @param $type
 143   *   Either 'css' or 'js', depending on which files you wish to retrieve from
 144   *   these skins.
 145   * @param $theme
 146   *   The theme from which to grab these files. Defaults to the current theme.
 147   *
 148   * @return
 149   *   An array of file data.
 150   */
 151  function skinr_skin_get_files($skins, $type, $theme = NULL) {
 152    if (empty($theme)) {
 153      $theme = skinr_current_theme();
 154    }
 155  
 156    $info = skinr_skin_data();
 157    $files = array();
 158  
 159    if ($type == 'css') {
 160      foreach ($skins as $skin => $classes) {
 161        // Add custom CSS files.
 162        if (isset($info[$theme]->skins[$skin])) {
 163          if (!empty($info[$theme]->skins[$skin]['stylesheets'])) {
 164            foreach ($info[$theme]->skins[$skin]['stylesheets'] as $media => $stylesheets) {
 165              foreach ($stylesheets as $file => $path) {
 166                $files[] = array(
 167                  'file' => $file,
 168                  'path' => $path,
 169                  'media' => $media,
 170                  'enabled' => TRUE,
 171                  'skin' => $skin,
 172                  'options' => 0,
 173                );
 174              }
 175            }
 176          }
 177          foreach ($info[$theme]->skins[$skin]['options'] as $option_id => $option) {
 178            if (!empty($option['stylesheets'])) {
 179              foreach ($option['stylesheets'] as $media => $stylesheets) {
 180                foreach ($stylesheets as $file => $path) {
 181                  $enabled = FALSE;
 182                  if (is_array($classes)) {
 183                    if (in_array($option['class'], $classes)) {
 184                      $enabled = TRUE;
 185                    }
 186                  }
 187                  else {
 188                    if ($option['class'] == $classes) {
 189                      $enabled = TRUE;
 190                    }
 191                  }
 192  
 193                  $files[] = array(
 194                    'file' => $file,
 195                    'path' => $path,
 196                    'media' => $media,
 197                    'enabled' => $enabled,
 198                    'skin' => $skin,
 199                    'options' => $option_id,
 200                  );
 201                }
 202              }
 203            }
 204          }
 205        }
 206      }
 207    }
 208    elseif ($type == 'js') {
 209      foreach ($skins as $skin => $classes) {
 210        // Add custom JS files.
 211        if (isset($info[$theme]->skins[$skin])) {
 212          if (isset($info[$theme]->skins[$skin]['scripts'])) {
 213            foreach ($info[$theme]->skins[$skin]['scripts'] as $file => $path) {
 214              $files[] = array(
 215                'file' => $file,
 216                'path' => $path,
 217                'enabled' => TRUE,
 218                'skin' => $skin,
 219                'options' => 0,
 220              );
 221            }
 222          }
 223          foreach ($info[$theme]->skins[$skin]['options'] as $option_id => $option) {
 224            if (isset($option['scripts'])) {
 225              foreach ($option['scripts'] as $file => $path) {
 226                $enabled = FALSE;
 227                  if (is_array($classes)) {
 228                    if (in_array($option['class'], $classes)) {
 229                      $enabled = TRUE;
 230                    }
 231                  }
 232                  else {
 233                    if ($option['class'] == $classes) {
 234                      $enabled = TRUE;
 235                    }
 236                  }
 237  
 238                $files[] = array(
 239                  'file' => $file,
 240                  'path' => $path,
 241                  'enabled' => $enabled,
 242                  'skin' => $skin,
 243                  'options' => $option_id,
 244                );
 245              }
 246            }
 247          }
 248        }
 249      }
 250    }
 251  
 252    return $files;
 253  }
 254  
 255  /**
 256   * Helper fuction to add CSS and JS files.
 257   *
 258   * The function checks an array of paths for the existence of the file to account for base themes.
 259   */
 260  function _skinr_add_file($filename, $type, $media = NULL) {
 261    if (file_exists($filename)) {
 262      if ($type == 'css') {
 263        drupal_add_css($filename, 'theme', $media);
 264      }
 265      else {
 266        drupal_add_js($filename, 'theme');
 267      }
 268    }
 269  }
 270  
 271  /**
 272   * Helper function to flatten an array of classes settings.
 273   */
 274  function skinr_flatten_skins_array($skins) {
 275    $return = array();
 276    foreach ($skins as $entry) {
 277      if (is_array($entry)) {
 278        foreach ($entry as $subentry) {
 279          if (!empty($subentry)) {
 280            $return[] = check_plain($subentry);
 281          }
 282        }
 283      }
 284      elseif (!empty($entry)) {
 285        $return[] = check_plain($entry);
 286      }
 287    }
 288  
 289    return $return;
 290  }
 291  
 292  // ------------------------------------------------------------------
 293  // Page rule functions.
 294  
 295  /**
 296   * Save a skinr page rule object.
 297   */
 298  function skinr_rule_save($rule) {
 299    drupal_write_record('skinr_rules', $rule, !empty($rule->rid) ? array('rid') : array());
 300  }
 301  
 302  /**
 303   * Load a skinr page rule object.
 304   */
 305  function skinr_rule_load($rid = NULL) {
 306    if (is_null($rid)) {
 307      $rules = array();
 308  
 309      $result = db_query("SELECT * FROM {skinr_rules}");
 310      while ($rule = db_fetch_object($result)) {
 311        $rule->roles = unserialize($rule->roles);
 312        $rules[] = $rule;
 313      }
 314  
 315      return $rules;
 316    }
 317    else {
 318      $result = db_query("SELECT * FROM {skinr_rules} WHERE rid = %d", $rid);
 319      if ($rule = db_fetch_object($result)) {
 320        $rule->roles = unserialize($rule->roles);
 321        return $rule;
 322      }
 323      return FALSE;
 324    }
 325  }
 326  
 327  /**
 328   * Delete a skinr page rule object.
 329   */
 330  function skinr_rule_delete($rid) {
 331    if ($rule = skinr_rule_load($rid)) {
 332      db_query("DELETE FROM {skinr_rules} WHERE rid = %d", $rule->rid);
 333      db_query("DELETE FROM {skinr} WHERE module = '%s' AND sid = '%s'", 'page', $rule->rid);
 334    }
 335  }
 336  
 337  function skinr_rule_visible($rid) {
 338    global $user;
 339  
 340    if ($rule = skinr_rule_load($rid)) {
 341      $page_match = TRUE;
 342  
 343      if (!empty($record['roles']) && ($user->uid != 1) && !count(array_intersect(array_keys($user->roles), $rule->roles))) {
 344        return FALSE;
 345      }
 346  
 347      // Match path if necessary
 348      if ($rule->pages) {
 349        if ($rule->visibility < 2) {
 350          $path = drupal_get_path_alias($_GET['q']);
 351          // Compare with the internal and path alias (if any).
 352          $page_match = drupal_match_path($path, $rule->pages);
 353          if ($path != $_GET['q']) {
 354            $page_match = $page_match || drupal_match_path($_GET['q'], $rule->pages);
 355          }
 356          // When $rule->visibility has a value of 0, the item is displayed on
 357          // all pages except those listed in $rule->pages. When set to 1, it
 358          // is displayed only on those pages listed in $rule->pages.
 359          $page_match = !($rule->visibility xor $page_match);
 360        }
 361        else {
 362          // PHP.
 363          $page_match = drupal_eval($rule->pages);
 364        }
 365      }
 366      return $page_match;
 367    }
 368    return FALSE;
 369  }
 370  
 371  // ------------------------------------------------------------------
 372  // Include file helpers.
 373  
 374  /**
 375   * Load skinr files on behalf of modules.
 376   */
 377  function skinr_module_include($file) {
 378    foreach (skinr_get_module_apis() as $module => $info) {
 379      if (file_exists("./$info[path]/$module.$file")) {
 380        require_once "./$info[path]/$module.$file";
 381      }
 382    }
 383  }
 384  
 385  /**
 386   * Get a list of modules that support skinr.
 387   */
 388  function skinr_get_module_apis() {
 389    static $cache = NULL;
 390  
 391    if (is_null($cache)) {
 392      $cache = array();
 393      foreach (module_implements('skinr_api') as $module) {
 394        $function = $module .'_skinr_api';
 395        $info = $function();
 396        if (isset($info['api']) && $info['api'] == 1.000) {
 397          if (!isset($info['path'])) {
 398            $info['path'] = drupal_get_path('module', $module);
 399          }
 400          $cache[$module] = $info;
 401        }
 402      }
 403    }
 404  
 405    return $cache;
 406  }
 407  
 408  // -----------------------------------------------------------------------
 409  // Skinr data handling functions.
 410  
 411  /**
 412   * Validate a skinr object.
 413   *
 414   * @param $skinr
 415   *   A skinr object.
 416   *
 417   * @return
 418   *   TRUE on success, FALSE on failure.
 419   */
 420  function skinr_validate(&$skinr) {
 421    if (!isset($skinr->theme) || !isset($skinr->module) || !isset($skinr->sid) || !isset($skinr->skins)) {
 422      return FALSE;
 423    }
 424    if (!isset($skinr->settings)) {
 425      $skinr->settings = array();
 426    }
 427    if (!is_array($skinr->skins) || !is_array($skinr->settings)) {
 428      return FALSE;
 429    }
 430  
 431    // Strip empty skins.
 432    $skinr->skins = _skinr_array_strip_empty($skinr->skins);
 433  
 434    return TRUE;
 435  }
 436  
 437  /**
 438   * Save a skinr object.
 439   *
 440   * @param $skinr
 441   *   A skinr object.
 442   *
 443   * @return
 444   *   TRUE on success, FALSE on failure.
 445   */
 446  function skinr_set($skinr) {
 447    // Make sure we're getting valid data.
 448    if (!skinr_validate($skinr)) {
 449      return FALSE;
 450    }
 451  
 452    if (empty($skinr->skins) && empty($skinr->settings)) {
 453      // Delete the db entry if it exists.
 454      db_query("DELETE FROM {skinr} WHERE theme = '%s' AND module = '%s' AND sid = '%s'", $skinr->theme, $skinr->module, $skinr->sid);
 455    }
 456    else {
 457      // Let's save the data.
 458      if (skinr_get($skinr->theme, $skinr->module, $skinr->sid) !== FALSE) {
 459        // Record exists, so let's update.
 460        drupal_write_record('skinr', $skinr, array('theme', 'module', 'sid'));
 461      }
 462      else {
 463        // Insert a new record.
 464        drupal_write_record('skinr', $skinr);
 465      }
 466    }
 467  
 468    return TRUE;
 469  }
 470  
 471  /**
 472   * Retrieves the desired skinr object.
 473   *
 474   * @return
 475   *   A skinr object if both $module and $sid are specified. An array of skinr
 476   *   objects if only $module is specified. An array of all skinr objects for a
 477   *   theme if neither $module nor $sid is specified. FALSE on failure.
 478   */
 479  function skinr_get($theme = NULL, $module = NULL, $sid = NULL, $reset = FALSE) {
 480    static $cache = array();
 481  
 482    if (is_null($theme)) {
 483      $theme = skinr_current_theme();
 484    }
 485  
 486    if ($reset) {
 487      $cache = array();
 488    }
 489  
 490    if (!isset($cache[$theme][$module][$sid])) {
 491      if (!isset($cache[$theme])) {
 492        $cache[$theme] = array();
 493      }
 494      if (!is_null($module) && !isset($cache[$theme][$module])) {
 495        $cache[$theme][$module] = array();
 496      }
 497  
 498      if (!is_null($module) && !is_null($sid)) {
 499        // Fetch just this sid.
 500        $result = db_query("SELECT theme, module, sid, settings, skins FROM {skinr} WHERE theme = '%s' AND module = '%s' AND sid = '%s'", $theme, $module, $sid);
 501      }
 502      elseif (!is_null($module)) {
 503        // Fetch all settings for this theme and module.
 504        $result = db_query("SELECT theme, module, sid, settings, skins FROM {skinr} WHERE theme = '%s' AND module = '%s'", $theme, $module);
 505      }
 506      else {
 507        // Fetch all settings for this theme.
 508        $result = db_query("SELECT theme, module, sid, settings, skins FROM {skinr} WHERE theme = '%s'", $theme);
 509      }
 510  
 511      while ($skinr = db_fetch_object($result)) {
 512        $skinr->settings = unserialize($skinr->settings);
 513        $skinr->skins = unserialize($skinr->skins);
 514        $cache[$skinr->theme][$skinr->module][$skinr->sid] = $skinr;
 515      }
 516    }
 517  
 518    if (is_null($sid) && is_null($module)) {
 519      // Return all the skinrs for the theme.
 520      if (isset($cache[$theme])) {
 521        return $cache[$theme];
 522      }
 523    }
 524    elseif(is_null($sid)) {
 525      // Return all the skinrs for the module.
 526      if (isset($cache[$theme][$module])) {
 527        return $cache[$theme][$module];
 528      }
 529    }
 530    elseif (isset($cache[$theme][$module][$sid])) {
 531      return $cache[$theme][$module][$sid];
 532    }
 533    return FALSE;
 534  }
 535  
 536  /**
 537   * Helper function to remove empty skins from an array.
 538   */
 539  function _skinr_array_strip_empty($array) {
 540    $new_array = array();
 541    foreach ($array as $key => $value) {
 542      if (is_array($value)) {
 543        $value = _skinr_array_strip_empty($value);
 544      }
 545      if (!empty($value)) {
 546        $new_array[$key] = $value;
 547      }
 548    }
 549    return $new_array;
 550  }
 551  
 552  /**
 553   * Helper function to retrieve the current theme.
 554   * The global variable $theme_key doesn't work for our purposes when an admin
 555   * theme is enabled.
 556   *
 557   * @param $exculde_admin_theme
 558   *   Optional. Set to TRUE to exclude the admin theme from posible themes to
 559   *   return.
 560   */
 561  function skinr_current_theme($exclude_admin_theme = FALSE) {
 562    global $user, $custom_theme;
 563  
 564    if (!empty($user->theme)) {
 565      $current_theme = $user->theme;
 566    }
 567    elseif (!empty($custom_theme) && !($exclude_admin_theme && $custom_theme == variable_get('admin_theme', '0'))) {
 568      // Don't return the admin theme if we're editing skinr settings.
 569      $current_theme = $custom_theme;
 570    }
 571    else {
 572      $current_theme = variable_get('theme_default', 'garland');
 573    }
 574    return $current_theme;
 575  }
 576  
 577  /**
 578   * Prepare defaults for skins.
 579   *
 580   * @return
 581   *   An array of default skinset settings.
 582   */
 583  function skinr_skins_default() {
 584    return array(
 585      'description' => '',
 586      'screenshot' => 'screenshot.png',
 587      'php' => DRUPAL_MINIMUM_PHP,
 588      'skinr' => array(),
 589    );
 590  }
 591  
 592  /**
 593   * Prepare defaults for skinr options.
 594   *
 595   * @return
 596   *   An array of default skinset options settings.
 597   */
 598  function skinr_group_default() {
 599    return array(
 600      'title' => '',
 601      'description' => '',
 602      'collapsible' => TRUE,
 603      'collapsed' => FALSE,
 604      'weight' => NULL,
 605    );
 606  }
 607  
 608  /**
 609   * Prepare defaults for skins.
 610   *
 611   * @return
 612   *   An array of default skins settings.
 613   */
 614  function skinr_skin_default() {
 615    return array(
 616      'title' => '',
 617      'type' => 'checkboxes',
 618      'description' => '',
 619      'features' => array('*'),
 620      'templates' => array(),
 621      'group' => '',
 622      'options' => array(),
 623      'stylesheets' => array(),
 624      'scripts' => array(),
 625      'weight' => NULL,
 626    );
 627  }
 628  
 629  /**
 630   * Retrieves all the Skinr skins from theme parents. Theme skins
 631   * will override any skins of the same name from its parents.
 632   */
 633  function skinr_inherited_skins($theme) {
 634    $themes = _system_theme_data();
 635  
 636    $all_skins = $skins = array();
 637    $base_theme = (!empty($themes[$theme]->info['base theme'])) ? $themes[$theme]->info['base theme'] : '';
 638    while ($base_theme) {
 639      // Add in path info here.
 640      $base_skins = (!empty($themes[$base_theme]->info['skinr'])) ? (array)$themes[$base_theme]->info['skinr'] : array();
 641      $base_path  = $path_root = dirname($themes[$base_theme]->filename);
 642      _skinr_add_paths_to_files($base_skins, $base_path);
 643  
 644      $all_skins[] = $base_skins;
 645      $base_theme = (!empty($themes[$base_theme]->info['base theme'])) ? $themes[$base_theme]->info['base theme'] : '';
 646    }
 647    array_reverse($all_skins);
 648    foreach ($all_skins as $new_skin) {
 649      $skins = array_merge($skins, $new_skin);
 650    }
 651    return $skins;
 652  }
 653  
 654  /**
 655   * Helper function to scan and collect skin .info data.
 656   *
 657   * @return
 658   *   An associative array of skins information.
 659   */
 660  function _skinr_skins_data() {
 661    static $skins_info = array();
 662  
 663    if (empty($skins_info)) {
 664      // Find skins.
 665      $mask = '\.info$';
 666      $directory = 'skins';
 667      $skinsets = drupal_system_listing($mask, $directory);
 668  
 669      // Find skins in theme folders.
 670      $themes = _system_theme_data();
 671      foreach ($themes as $theme) {
 672        $dir = dirname($theme->filename) .'/'. $directory;
 673        $skinsets = array_merge($skinsets, file_scan_directory($dir, $mask, array('.', '..', 'CVS'), 0, TRUE, 'name', 1));
 674      }
 675  
 676      // Find skins in module folders.
 677      foreach (skinr_get_module_apis() as $module => $info) {
 678        if (isset($info['skins']) && $info['skins'] == TRUE) {
 679          $dir = dirname($info['path'] .'/'. $directory);
 680          $skinsets = array_merge($skinsets, file_scan_directory($dir, $mask, array('.', '..', 'CVS'), 0, TRUE, 'name', 1));
 681        }
 682      }
 683  
 684      $defaults = skinr_skins_default();
 685  
 686      foreach ($skinsets as $key => $skinset) {
 687        $skinsets[$key]->info = drupal_parse_info_file($skinset->filename) + $defaults;
 688  
 689        // Give the screenshot proper path information.
 690        if (!empty($skinsets[$key]->info['screenshot'])) {
 691          $skinsets[$key]->info['screenshot'] = dirname($skinsets[$key]->filename) .'/'. $skinsets[$key]->info['screenshot'];
 692        }
 693  
 694        // Invoke hook_skinr_info_alter() to give installed modules a chance to
 695        // modify the data in the .info files if necessary.
 696        drupal_alter('skinr_info', $skinsets[$key]->info, $skinsets[$key]);
 697      }
 698  
 699      $skins_info = $skinsets;
 700    }
 701  
 702    return $skins_info;
 703  }
 704  
 705  /**
 706   * Helper function to process a skin or theme .info file.
 707   *
 708   * @return
 709   *    A skinset.
 710   */
 711  function _skinr_skinset($info) {
 712    $skinset = array(
 713      'options' => array('groups' => array()),
 714      'skins' => array(),
 715    );
 716  
 717    if (!empty($info->info['skinr'])) {
 718      $path_root = dirname($info->filename);
 719  
 720      $skinr_info = (array)$info->info['skinr'];
 721  
 722      // Store skinr options.
 723      if (!empty($skinr_info['options'])) {
 724        $skinset['options'] = $skinr_info['options'];
 725        unset($skinr_info['options']);
 726  
 727        if (!isset($skinset['options']['groups'])) {
 728          $skinset['options']['groups'] = array();
 729        }
 730  
 731        $defaults = skinr_group_default();
 732        foreach ($skinset['options']['groups'] as $id => $group) {
 733          $skinset['options']['groups'][$id] = array_merge($defaults, $skinset['options']['groups'][$id]);
 734          $skinset['options']['groups'][$id]['collapsible'] = (bool)$skinset['options']['groups'][$id]['collapsible'];
 735          $skinset['options']['groups'][$id]['collapsed'] = (bool)$skinset['options']['groups'][$id]['collapsed'];
 736          $skinset['options']['groups'][$id]['weight'] = $skinset['options']['groups'][$id]['weight'];
 737        }
 738      }
 739  
 740      // Add paths to $skinr_info.
 741      _skinr_add_paths_to_files($skinr_info, $path_root);
 742  
 743      // Inherit skins from parent theme, if inherit_skins is set to true.
 744      if (!empty($skinset['options']['inherit_skins'])) {
 745        // Paths get automatically added to base theme info.
 746        $base_info  = skinr_inherited_skins($info->name);
 747        // Merge base theme and current.
 748        $skinr_info = array_merge($base_info, $skinr_info);
 749      }
 750  
 751      $defaults = skinr_skin_default();
 752  
 753      foreach ($skinr_info as $id => $skin) {
 754        if (!is_array($skin)) {
 755          continue;
 756        }
 757        $skinset['skins'][$id] = array(
 758          'title' => isset($skin['title']) ? $skin['title'] : $defaults['title'],
 759          'type' => isset($skin['type']) ? $skin['type'] : $defaults['type'],
 760          'description' => isset($skin['description']) ? $skin['description'] : $defaults['description'],
 761          'features' => isset($skin['features']) ? $skin['features'] : $defaults['features'],
 762          'templates' => isset($skin['templates']) ? $skin['templates'] : $defaults['templates'],
 763          'group' => !empty($skin['group']) && !empty($skinset['options']['groups'][$skin['group']]) ? $skin['group'] : $defaults['group'],
 764          'options' => isset($skin['options']) ? $skin['options'] : $defaults['options'],
 765          'stylesheets' => isset($skin['stylesheets']) ? $skin['stylesheets'] : $defaults['stylesheets'],
 766          'scripts' => isset($skin['scripts']) ? $skin['scripts'] : $defaults['scripts'],
 767          'weight' => isset($skin['weight']) ? $skin['weight'] : $defaults['weight'],
 768        );
 769      }
 770    }
 771  
 772    return $skinset;
 773  }
 774  
 775  function _skinr_add_paths_to_files(&$skinr_info, $path_root) {
 776    foreach ($skinr_info as $id => $skin) {
 777      if (!is_array($skin)) {
 778        continue;
 779      }
 780  
 781      // Give the stylesheets proper path information.
 782      if (!empty($skin['stylesheets'])) {
 783        $skinr_info[$id]['stylesheets'] = _skinr_add_path_to_files($skin['stylesheets'], $path_root, 'css');
 784      }
 785  
 786      // Give the scripts proper path information.
 787      if (!empty($skin['scripts'])) {
 788        $skinr_info[$id]['scripts'] = _skinr_add_path_to_files($skin['scripts'], $path_root, 'js');
 789      }
 790  
 791      if (!empty($skin['options'])) {
 792        foreach ($skin['options'] as $oid => $option) {
 793          if (!empty($option['stylesheets'])) {
 794            $skinr_info[$id]['options'][$oid]['stylesheets'] = _skinr_add_path_to_files($option['stylesheets'], $path_root, 'css');
 795          }
 796          if (isset($option['scripts'])) {
 797            $skinr_info[$id]['options'][$oid]['scripts'] = _skinr_add_path_to_files($option['scripts'], $path_root, 'js');
 798          }
 799        }
 800      }
 801    }
 802  }
 803  
 804  /**
 805   * Helper function to prepend a path to an array of stylesheets or scripts in a .info file.
 806   *
 807   * @param $files
 808   *   A an array of filenames that need the path prepended.
 809   * @param $path
 810   *   The path to prepend.
 811   * @param $type
 812   *   Either 'css' or 'js', depending on which files you wish to retrieve from
 813   *   these skins.
 814   *
 815   * @return
 816   *    An array of files with the root path added.
 817   */
 818  function _skinr_add_path_to_files($files, $path, $type) {
 819    if ($type == 'css') {
 820      $pathed_stylesheets = array();
 821      foreach ($files as $media => $stylesheets) {
 822        foreach ($stylesheets as $stylesheet) {
 823          $pathed_stylesheets[$media][$stylesheet] = $path .'/'. $stylesheet;
 824        }
 825      }
 826      return $pathed_stylesheets;
 827    }
 828    elseif ($type == 'js') {
 829      $pathed_scripts = array();
 830      foreach ($files as $script) {
 831        $pathed_scripts[$script] = $path .'/'. $script;
 832      }
 833      return $pathed_scripts;
 834    }
 835  
 836    return FALSE;
 837  }
 838  
 839  /**
 840   * Helper function to process an array of skins or themes .info files.
 841   *
 842   * @param $type
 843   *   Either 'theme' or 'skinset'.
 844   * @param $refresh
 845   *   Whether to reload the list of skinsets from the database or not.
 846   *
 847   * @return
 848   *    An array of skinsets.
 849   */
 850  function skinr_skinsets($type, $refresh = FALSE) {
 851    static $skinsets = array('theme' => array(), 'skinset' => array());
 852  
 853    if ($refresh) {
 854      $skinsets[$type] = array();
 855    }
 856  
 857    if (empty($skinsets[$type])) {
 858      $themes = _system_theme_data();
 859  
 860      if ($type == 'theme') {
 861        foreach ($themes as $theme) {
 862          $skinset = new StdClass();
 863          $skinset->filename = $theme->filename;
 864          $skinset->name = $theme->name;
 865          $skinset->status = isset($theme->status) ? 1 : 0;
 866          $skinset->info = $theme->info;
 867  
 868          $skinsets[$type][$skinset->name] = $skinset;
 869        }
 870      }
 871      elseif ($type == 'skinset') {
 872        $result = db_query("SELECT * FROM {skinr_skinsets}");
 873        while ($skinset = db_fetch_object($result)) {
 874          if (file_exists($skinset->filename)) {
 875            $skinset->info = unserialize($skinset->info);
 876  
 877            $skinsets[$type][$skinset->name] = $skinset;
 878          }
 879        }
 880      }
 881  
 882      $default_status = array();
 883      foreach ($themes as $theme) {
 884        $default_status[$theme->name] = $theme->name;
 885      }
 886  
 887      foreach ($skinsets[$type] as $key => $skinset) {
 888        $skinset->type = $type;
 889  
 890        $additional = _skinr_skinset($skinset);
 891        $skinset->options = $additional['options'];
 892        $skinset->skins = $additional['skins'];
 893  
 894        $statuses = skinr_skinset_statuses($skinset->name);
 895        foreach ($skinset->skins as $skin_name => $skin) {
 896          $skinset->skins[$skin_name]['status'] = !empty($statuses[$skin_name]) ? $statuses[$skin_name] : $default_status;
 897        }
 898      }
 899    }
 900  
 901    return $skinsets[$type];
 902  }
 903  
 904  /**
 905   * Return an array of statuses of each skin in a skinset for each theme.
 906   *
 907   * @param $skinset_name
 908   *   The name of the skinset of which to load the statuses.
 909   * @param $refresh
 910   *   Whether to reload the list of statuses for a skin from the database or not.
 911   *
 912   * @return
 913   *   Array of statuses for individual skins in a skinset.
 914   */
 915  function skinr_skinset_statuses($skinset_name, $refresh = FALSE) {
 916    static $statuses = array();
 917  
 918    if (isset($refresh)) {
 919      $statuses[$skinset_name] = array();
 920    }
 921  
 922    if (empty($statuses[$skinset_name])) {
 923      $result = db_query("SELECT * FROM {skinr_skins} WHERE name = '%s'", $skinset_name);
 924      while ($skinr_skin = db_fetch_object($result)) {
 925        $statuses[$skinset_name][$skinr_skin->skin] = unserialize($skinr_skin->status);
 926      }
 927    }
 928  
 929    return $statuses[$skinset_name];
 930  }
 931  
 932  /**
 933   * Rebuild, save, and return data about all currently available skinsets.
 934   *
 935   * @return
 936   *   Array of all available skinsets and their data.
 937   */
 938  function skinr_rebuild_skinset_data() {
 939    $skinsets = _skinr_skins_data();
 940    skinr_get_files_database($skinsets);
 941  
 942    db_query("DELETE FROM {skinr_skinsets}");
 943  
 944    foreach ($skinsets as $skinset) {
 945      db_query("INSERT INTO {skinr_skinsets} (filename, name, status, info) VALUES ('%s', '%s', '%s', '%s')", $skinset->filename, $skinset->name, isset($skinset->status) ? $skinset->status : 0, serialize($skinset->info));
 946    }
 947  
 948    return $skinsets;
 949  }
 950  
 951  /**
 952   * Implementation of hook_flush_caches().
 953   */
 954  function skinr_flush_caches() {
 955    skinr_rebuild_skinset_data();
 956    return array();
 957  }
 958  
 959  /**
 960   * Retrieves the current status of an array of files in the skinr_skinsets table.
 961   *
 962   * @param $files
 963   *   An array of files to check.
 964   */
 965  function skinr_get_files_database(&$files) {
 966    // Extract current files from database.
 967    $result = db_query("SELECT filename, name, status FROM {skinr_skinsets}");
 968    while ($file = db_fetch_object($result)) {
 969      if (isset($files[$file->name]) && is_object($files[$file->name])) {
 970        $file->uri = $file->filename;
 971        foreach ($file as $key => $value) {
 972          if (!isset($files[$file->name]) || !isset($files[$file->name]->$key)) {
 973            $files[$file->name]->$key = $value;
 974          }
 975        }
 976      }
 977    }
 978  }
 979  
 980  /**
 981   * Themes are allowed to set Skinr skins in their .info files.
 982   *
 983   * @return
 984   *    An array of skinsets keyed by themename.
 985   *
 986   * @todo Use DB caching. No need to keep processing things every page load.
 987   */
 988  function skinr_skin_data() {
 989    static $cache = NULL;
 990  
 991    if (is_null($cache)) {
 992      $skins_skinsets  = skinr_skinsets('skinset');
 993      $themes_skinsets = skinr_skinsets('theme');
 994  
 995      // Need to merge all skins skinsets into a single list of skins.
 996      // Also merge in the groups information.
 997      $additional_skins = array();
 998      $groups = array();
 999      foreach ($skins_skinsets as $key => $skinset) {
1000        if (!empty($skinset->skins) && $skinset->status == 1) {
1001          $additional_skins += $skinset->skins;
1002        }
1003        if (!empty($skinset->options['groups'])) {
1004          $groups += $skinset->options['groups'];
1005        }
1006      }
1007  
1008      // Merge the additional skins into each theme, even if that theme has no
1009      // skinr data.
1010      $themes = list_themes();
1011      foreach ($themes as $theme) {
1012        if ($theme->status != 1) {
1013          continue;
1014        }
1015  
1016        if (isset($themes_skinsets[$theme->name])) {
1017          $cache[$theme->name] = $themes_skinsets[$theme->name];
1018          $cache[$theme->name]->skins += $additional_skins;
1019          $cache[$theme->name]->options['groups'] += $groups;
1020        }
1021        else {
1022          $cache[$theme->name] = array(
1023            'options' => array('groups' => $groups),
1024            'skins' => $additional_skins,
1025          );
1026        }
1027      }
1028    }
1029  
1030    return $cache;
1031  }
1032  
1033  /**
1034   * Fetch Skinr configuration data from modules.
1035   */
1036  function skinr_fetch_config() {
1037    static $cache = NULL;
1038  
1039    if (is_null($cache)) {
1040      $cache = module_invoke_all('skinr_config');
1041      foreach (module_implements('skinr_config_alter') as $module) {
1042        $function = $module .'_skinr_config_alter';
1043        $function($cache);
1044      }
1045    }
1046  
1047    return $cache;
1048  }
1049  
1050  /**
1051   * Fetch default configuration data for modules.
1052   */
1053  function _skinr_fetch_config_defaults($setting) {
1054    switch ($setting) {
1055      case 'form':
1056        $data = array(
1057          'access_handler' => 'skinr_access_handler',
1058          'data_handler' => 'skinr_data_handler',
1059          'submit_handler' => 'skinr_submit_handler',
1060          'submit_handler_attach_to' => array('#submit'),
1061          'skinr_title' => t('Skinr'),
1062          'skinr_weight' => 1,
1063          'title' => '',
1064          'description' => t('Manage which skins you want to apply to the hooks'),
1065          'collapsed' => TRUE,
1066          'weight' => 0,
1067        );
1068        return $data;
1069    }
1070  }
1071  
1072  /**
1073   * Execute a module's data handler.
1074   */
1075  function skinr_handler($type, $op, $handler, &$a3, $a4 = NULL, $a5 = NULL, $a6 = NULL, $a7 = NULL) {
1076    if (is_callable($handler)) {
1077      switch ($type) {
1078        case 'preprocess_index_handler':
1079          return $handler($a3);
1080        case 'preprocess_hook_callback':
1081          return $handler($a3, $a4);
1082        case 'data_handler':
1083        case 'submit_handler':
1084          return $handler($a3, $a4, $a5, $a6, $a7);
1085        default:
1086          return $handler($op, $a3, $a4);
1087      }
1088    }
1089  }


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