Zikula_Core
[ class tree: Zikula_Core ] [ index: Zikula_Core ] [ all elements ]

Source for file ThemeUtil.class.php

Documentation is available at ThemeUtil.class.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright (c) 2001, Zikula Development Team
  6.  * @link http://www.zikula.org
  7.  * @version $Id: pnTheme.php 20730 2006-12-09 15:37:53Z drak $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @package Zikula_Core
  10.  * @subpackage pnAPI
  11. */
  12.  
  13. /**
  14.  *  Theme filters
  15.  */
  16. define('PNTHEME_FILTER_ALL'0);
  17. define('PNTHEME_FILTER_USER'1);
  18. define('PNTHEME_FILTER_SYSTEM'2);
  19. define('PNTHEME_FILTER_ADMIN'3);
  20.  
  21. /**
  22.  *  Theme states
  23.  */
  24. define('PNTHEME_STATE_ALL'0);
  25. define('PNTHEME_STATE_ACTIVE'1);
  26. define('PNTHEME_STATE_INACTIVE'2);
  27.  
  28. /**
  29.  *  Theme types
  30.  */
  31. define('PNTHEME_TYPE_ALL'0);
  32. define('PNTHEME_TYPE_LEGACY'1);
  33. define('PNTHEME_TYPE_XANTHIA2'2);
  34. define('PNTHEME_TYPE_XANTHIA3'3);
  35. define('PNTHEME_TYPE_AUTOTHEME'4);
  36.  
  37. /**
  38.  * ThemeUtil
  39.  *
  40.  * @package Zikula_Core
  41.  * @subpackage ThemeUtil
  42.  */
  43. class ThemeUtil
  44. {
  45.     /**
  46.      * Load a theme
  47.      *
  48.      * include theme.php for the requested theme
  49.      *
  50.      * @return bool true if successful, false otherwiese
  51.      */
  52.     function load($theme)
  53.     {
  54.         static $loaded 0;
  55.     
  56.         if ($loaded{
  57.             return true;
  58.         }
  59.         // Lots of nasty globals for back-compatability with older themes
  60.         global $bgcolor1;
  61.         global $bgcolor2;
  62.         global $bgcolor3;
  63.         global $bgcolor4;
  64.         global $bgcolor5;
  65.         global $sepcolor;
  66.         global $textcolor1;
  67.         global $textcolor2;
  68.         global $postnuke_theme;
  69.         global $thename;
  70.     
  71.         // get the theme info
  72.         $themeinfo ThemeUtil::getInfo(ThemeUtil::getIDFromName($theme));
  73.     
  74.         // note - don't use dbutil here since we require the vars in theme.php to be globals
  75.         if ($themeinfo['type'!= 3{
  76.             $files[WHERE_IS_PERSO "themes/$themeinfo[directory]/theme.php";
  77.             $files["themes/$themeinfo[directory]/theme.php";
  78.             pnModLangLoad('Theme''userapi');
  79.             $result false;
  80.             foreach ($files as $file{
  81.                 if (file_exists($file&& Loader::includeOnce($file)) $result true;
  82.             }
  83.             if (!$resultreturn false;
  84.         else {
  85.             // initialise the engine
  86.             $GLOBALS['theme_engine'new Theme($themeinfo['name']true);
  87.             // we're a xanthia theme
  88.             $GLOBALS['xanthia_theme'true;
  89.     
  90.             // create some dummy functions - TODO fix this code deciding what to do with it
  91.             function opentable()   {  echo $GLOBALS['theme_engine']->themetable(true1);  }
  92.             function closetable()  {  echo $GLOBALS['theme_engine']->themetable(false1)}
  93.             function opentable2()  {  echo $GLOBALS['theme_engine']->themetable(true2);  }
  94.             function closetable2({  echo $GLOBALS['theme_engine']->themetable(false2)}
  95.         }
  96.     
  97.         // now lets load the themes language file
  98.         ThemeUtil::loadLanguage();
  99.     
  100.         // end of modification
  101.         $loaded 1;
  102.         return true;
  103.     }
  104.     
  105.     /**
  106.      * return a theme variable
  107.      *
  108.      * @return mixed theme variable value
  109.      */
  110.     function getVar($name null$default null)
  111.     {
  112.         static $themevars;
  113.     
  114.         if (!isset($themevars&& isset($GLOBALS['theme_engine']&& is_object($GLOBALS['theme_engine'])) {
  115.             // initialise the holding array
  116.             $themevars array();
  117.     
  118.             // get the theme info
  119.             $theme pnUserGetTheme();
  120.             $themeinfo ThemeUtil::getInfo(ThemeUtil::getIDFromName($theme));
  121.     
  122.             // note - don't use dbutil here since we require the vars in theme.php to be globals
  123.             if ($themeinfo['type'== 3{
  124.                 // load the config vars direct from the theme engine object
  125.                 $themevars $GLOBALS['theme_engine']->get_config_vars();
  126.             }
  127.     
  128.             // load the legacy globals
  129.             $globalthemevars array('bgcolor1''bgcolor2' 'bgcolor3''bgcolor4''bgcolor5''sepcolor''textcolor1''textcolor2');
  130.             foreach ($globalthemevars as $globalthemevar{
  131.                 if (isset($GLOBALS[$globalthemevar])) {
  132.                     $themevars[$globalthemevar== $GLOBALS[$globalthemevar];
  133.                 }
  134.             }
  135.         }
  136.     
  137.         // if no variable name is present then return all theme vars
  138.         if (!isset($name)) {
  139.             return $themevars;
  140.         }
  141.     
  142.         // if a name is present and the variable exists return its value
  143.         if (isset($themevars[$name])) {
  144.             return $themevars[$name];
  145.         }
  146.     
  147.         // not found the var so return the default
  148.         return $default;
  149.     }
  150.     
  151.     /**
  152.      * getAllThemes
  153.      *
  154.      * list all available themes
  155.      *
  156.      * possible values of filter are
  157.      * PNTHEME_FILTER_ALL - get all themes (default)
  158.      * PNTHEME_FILTER_USER - get user themes
  159.      * PNTHEME_FILTER_SYSTEM - get system themes
  160.      * PNTHEME_FILTER_ADMIN - get admin themes
  161.      *
  162.      * @param filter - filter list of returned themes by type
  163.      * @return array of available themes
  164.      ***/
  165.     function getAllThemes($filter PNTHEME_FILTER_ALL$state PNTHEME_STATE_ACTIVE$type PNTHEME_TYPE_ALL)
  166.     {
  167.         static $themesarray array();
  168.     
  169.         $key md5((string)$filter.(string)$state.(string)$type);
  170.     
  171.         if (empty($themesarray[$key])) {
  172.             $pntable pnDBGetTables();
  173.             $themescolumn $pntable['themes_column'];
  174.             $whereargs array();
  175.             if ($state != PNTHEME_STATE_ALL{
  176.                 $whereargs["$themescolumn[state] = 'DataUtil::formatForStore($state"'";
  177.             }
  178.             if ($type != PNTHEME_TYPE_ALL{
  179.                 $whereargs["$themescolumn[type] = '. (int)DataUtil::formatForStore($type"'";
  180.             }
  181.             if ($filter == PNTHEME_FILTER_USER{
  182.                 $whereargs["$themescolumn[user] = '1'";
  183.             }
  184.             if ($filter == PNTHEME_FILTER_SYSTEM{
  185.                 $whereargs["$themescolumn[system] = '1'";
  186.             }
  187.             if ($filter == PNTHEME_FILTER_ADMIN{
  188.                 $whereargs["$themescolumn[admin] = '1'";
  189.             }
  190.     
  191.             $where implode($whereargs' AND ');
  192.             $orderBy "ORDER BY $themescolumn[name]";
  193.             // define the permission filter to apply
  194.             $permFilter array(array('realm'          => 0,
  195.                                       'component_left' => 'Theme',
  196.                                       'instance_left'  => 'name',
  197.                                       'level'          => ACCESS_READ));
  198.             $themesarray[$keyDBUtil::selectObjectArray('themes'$where$orderBy0-1''$permFilter);
  199.             if (!$themesarray[$key]{
  200.                 return false;
  201.             }
  202.         }
  203.     
  204.         return $themesarray[$key];
  205.     }
  206.     
  207.     /*
  208.      * load the language file for a theme
  209.      *
  210.      * @author Patrick Kellum
  211.      * @return void
  212.      */
  213.     function loadLanguage($script 'global'$theme null)
  214.     {
  215.         $currentlang SessionUtil::getVar('lang');
  216.         $language pnConfigGetVar('language');
  217.         if ($theme == null{
  218.             $theme pnUserGetTheme();
  219.         }
  220.     
  221.         // get the theme info
  222.         $themeinfo ThemeUtil::getInfo(ThemeUtil::getIDFromName($theme));
  223.     
  224.         $_theme  DataUtil::formatForOS($themeinfo['directory']);
  225.         $_cLang  DataUtil::formatForOS($currentlang);
  226.         $_lang   DataUtil::formatForOS($language);
  227.         $_script DataUtil::formatForOS($script);
  228.     
  229.         $files array();
  230.         $files[WHERE_IS_PERSO.'themes/' $_theme'/lang/' $_cLang '/' $_script '.php';
  231.         $files[WHERE_IS_PERSO.'themes/' $_theme'/lang/' $_lang '/' $_script '.php';
  232.         $files['themes/' $_theme '/lang/' $_cLang '/' $_script '.php';
  233.         $files['themes/' $_theme '/lang/' $_lang '/' $_script '.php';
  234.         Loader::loadOneFile ($files);
  235.     
  236.         return;
  237.     }
  238.     
  239.     /*
  240.      * getIDFromName
  241.      *
  242.      * get themeID given its name
  243.      *
  244.      * @author Mark West
  245.      * @link http://www.markwest.me.uk
  246.      * @param 'theme' the name of the theme
  247.      * @return int theme ID
  248.      */
  249.     function getIDFromName($theme)
  250.     {
  251.         // define input, all numbers and booleans to strings
  252.         $theme (isset($themestrtolower((string)$theme'');
  253.     
  254.         // validate
  255.         if (!pnVarValidate($theme'theme')) {
  256.             return false;
  257.         }
  258.     
  259.         static $themeid;
  260.     
  261.         if (!is_array($themeid|| !isset($themeid[$theme])) {
  262.             $themes ThemeUtil::getThemesTable();
  263.     
  264.             if ($themes === false{
  265.                 return;
  266.             }
  267.     
  268.             foreach ($themes as $themeinfo{
  269.                 $tName  strtolower($themeinfo['name']);
  270.                 $themeid[$tName$themeinfo['id'];
  271.                 if (isset($themeinfo['displayname']&& $themeinfo['displayname'])
  272.                 {
  273.                     $tdName strtolower($themeinfo['displayname']);
  274.                     $themeid[$tdName$themeinfo['id'];
  275.                 }
  276.             }
  277.     
  278.             if (!isset($themeid[$theme])) {
  279.                 $themeid[$themefalse;
  280.                 return false;
  281.             }
  282.         }
  283.     
  284.         if (isset($themeid[$theme])) {
  285.             return $themeid[$theme];
  286.         }
  287.     
  288.         return false;
  289.     }
  290.     
  291.     /**
  292.      * getInfo
  293.      *
  294.      * Returns information about a theme.
  295.      *
  296.      * @author Mark West
  297.      * @param string $themeid Id of the theme
  298.      * @return array the theme information
  299.      ***/
  300.     function getInfo($themeid)
  301.     {
  302.         if ($themeid == || !is_numeric($themeid)) {
  303.             return false;
  304.         }
  305.     
  306.         static $themeinfo;
  307.     
  308.         if (!is_array($themeinfo|| !isset($themeinfo[$themeid])) {
  309.             $themeinfo ThemeUtil::getThemesTable();
  310.     
  311.             if (!$themeinfo{
  312.                 return;
  313.             }
  314.     
  315.             if (!isset($themeinfo[$themeid])) {
  316.                 $themeinfo[$themeidfalse;
  317.                 return $themeinfo[$themeid];
  318.             }
  319.         }
  320.     
  321.         return $themeinfo[$themeid];
  322.     }
  323.     
  324.     /**
  325.      * gets the themes table
  326.      *
  327.      * small wrapper function to avoid duplicate sql
  328.      * @access private
  329.      * @return array modules table
  330.     */
  331.     function getThemesTable()
  332.     {
  333.         static $themestable;
  334.         if (!isset($themestable|| defined('_PNINSTALLVER')) {
  335.             $themestable DBUtil::selectObjectArray('themes'''''-1-1'id');
  336.         }
  337.         return $themestable;
  338.     }
  339.     
  340.     /**
  341.      * get the modules stylesheet from several possible sources
  342.      *
  343.      *@access public
  344.      *@param string $modname    the modules name (optional, defaults to top level module)
  345.      *@param string $stylesheet the stylesheet file (optional)
  346.      *@return string path of the stylesheet file, relative to PN root folder
  347.      */
  348.     function getModuleStylesheet($modname=''$stylesheet='')
  349.     {
  350.         // default for the module
  351.         if (empty($modname)) {
  352.             $modname pnModGetName();
  353.         }
  354.  
  355.         // default for the style sheet
  356.         if (empty($stylesheet)) {
  357.             $stylesheet pnModGetVar($modname'modulestylesheet');
  358.             if (empty($stylesheet)) {
  359.                 $stylesheet 'style.css';
  360.             }
  361.         }
  362.  
  363.         $osstylesheet DataUtil::formatForOS($stylesheet);
  364.         $osmodname    DataUtil::formatForOS($modname);
  365.  
  366.         // config directory
  367.         $configstyledir 'config/styles';
  368.         $configpath     "$configstyledir/$osmodname";
  369.  
  370.         // theme directory
  371.         $theme          DataUtil::formatForOS(pnUserGetTheme());
  372.         $themepath      "themes/$theme/style/$osmodname";
  373.  
  374.         // module directory
  375.         $modinfo        pnModGetInfo(pnModGetIDFromName($modname));
  376.         $osmoddir       DataUtil::formatForOS($modinfo['directory']);
  377.         $modpath        "modules/$osmoddir/pnstyle";
  378.         $syspath        "system/$osmoddir/pnstyle";
  379.  
  380.         // search for the style sheet
  381.         $csssrc '';
  382.         foreach (array($configpath,
  383.                        $themepath,
  384.                        $modpath,
  385.                        $syspathas $path{
  386.             if (file_exists("$path/$osstylesheet"&& is_readable("$path/$osstylesheet")) {
  387.                 $csssrc "$path/$osstylesheet";
  388.                 break;
  389.             }
  390.         }
  391.         return $csssrc;
  392.     }
  393. }

Documentation generated on Fri, 18 Jul 2008 21:58:29 +0200 by phpDocumentor 1.4.1