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

Source for file pnTheme.class.php

Documentation is available at pnTheme.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.class.php 24433 2008-07-03 12:06:19Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @author Mark West
  10.  * @package Zikula_Core
  11.  * @subpackage pnAPI
  12.  */
  13.  
  14. /**
  15.  * pnTheme
  16.  *
  17.  * @package Zikula_Core
  18.  * @subpackage pnAPI
  19.  */
  20. if (class_exists('pnRender')) {
  21.     class Theme extends pnRender {
  22.  
  23.         // base theme info
  24.         var $id;           // id
  25.         var $name;         // name
  26.         var $displayname;  // display name
  27.         var $description;  // description
  28.         var $regid;        // registration id
  29.         var $type;         // type
  30.         var $directory;    // directory
  31.         var $version;      // version
  32.         var $official;     // official
  33.         var $author;       // author(s)
  34.         var $contact;      // contact(s)
  35.         var $admin;        // admin capable
  36.         var $user;         // user capable
  37.         var $system;       // system capable
  38.         var $state;        // state
  39.         var $credits;      // credits
  40.         var $changelog;    // changelog
  41.         var $help;         // help
  42.         var $license;      // license
  43.         var $xhtml;        // xhtml capable
  44.  
  45.         // base theme vars
  46.         var $themepath;      // Theme base path
  47.         var $imagepath;      // Theme image path
  48.         var $imagelangpath;  // Theme image path
  49.         var $stylepath;      // Theme stylesheet path
  50.         var $scriptpath;     // Theme stylesheet path
  51.  
  52.         // vars to identify our page
  53.         var $componentid;
  54.         var $pageid;
  55.         var $pagetype;
  56.         var $qstring;
  57.         var $requesturi;
  58.         var $permlevel;
  59.         var $isloggedin;
  60.  
  61.         /**
  62.          * Initialize our class
  63.          * @return                none 
  64.          */
  65.         function Theme($theme$usefilters true)
  66.         {
  67.             // first, get a native pnRender object object
  68.             $this->pnRender();
  69.  
  70.             // store our theme directory
  71.             $themeinfo ThemeUtil::getInfo(ThemeUtil::getIDFromName($theme));
  72.             foreach ($themeinfo as $key => $value{
  73.                 $this->$key $value;
  74.             }
  75.  
  76.             // change some base settings from our parent class
  77.             // template compilation
  78.             $this->compile_dir = pnConfigGetVar('temp''/Xanthia_compiled';
  79.             $this->compile_check = pnModGetVar('Theme''compile_check');
  80.             $this->force_compile = pnModGetVar('Theme''force_compile');
  81.             $this->compile_id = $theme;
  82.             // template caching
  83.             $this->cache_dir = pnConfigGetVar('temp''/Xanthia_cache';
  84.             $this->caching = pnModGetVar('Theme''enablecache');
  85.             $type FormUtil::getPassedValue('type'null'GETPOST');
  86.             if ($this->caching && strtolower($type!= 'admin'{
  87.                 $modulesnocache explode(',',pnModGetVar('Theme''modulesnocache'));
  88.                 if (in_array($this->toplevelmodule$modulesnocache)) {
  89.                     $this->caching = false;
  90.                 }
  91.             else {
  92.                  $this->caching = false;
  93.             }
  94.             $this->cache_lifetime = pnModGetVar('Theme''cache_lifetime');
  95.  
  96.             // assign all our base template variables
  97.             $this->_base_vars();
  98.  
  99.             // define the plugin directories
  100.             $this->_plugin_dirs();
  101.  
  102.             // load the theme configuration
  103.             $this->_load_config();
  104.  
  105.             // check for cached output
  106.             // turn on caching, check for cached output and then disable caching
  107.             // to prevent blocks from being cached
  108.             if ($this->caching && $this->is_cached($this->themeconfig['page']$this->pageid)) {
  109.                 $this->display($this->themeconfig['page']$this->pageid);
  110.                 pnShutDown();
  111.             }
  112.  
  113.             // Assign all theme variables
  114.             $this->assign($this->get_config_vars());
  115.  
  116.             if ($usefilters{
  117.                 // register page vars output filter
  118.                 $this->load_filter('output''pagevars');
  119.  
  120.                 // register short urls output filter
  121.                 if (pnConfigGetVar('shorturls')) {
  122.                     $this->load_filter('output''shorturls');
  123.                 }
  124.  
  125.                 // register trim whitespace output filter if requried
  126.                 if (pnModGetVar('Theme''trimwhitespace')) {
  127.                     $this->load_filter('output''trimwhitespace');
  128.                 }
  129.  
  130.                  // register output filter to make urls clickable if requried
  131.                 if (pnModGetVar('Theme''makelinks')) {
  132.                     $this->load_filter('output''makelinks');
  133.                 }
  134.  
  135.                 // register output filter to add MultiHook environment if requried
  136.                 if (pnModAvailable('MultiHook')) {
  137.                     $modinfo pnModGetInfo(pnModGetIDFromName('MultiHook'));
  138.                     if (version_compare($modinfo['version']'5.0''>='== 1{
  139.                         $this->load_filter('output''multihook');
  140.                         pnModAPIFunc('MultiHook''theme''preparetheme');
  141.                     }
  142.                 }
  143.             }
  144.  
  145.             // Start the output buffering to capture module output
  146.             ob_start();
  147.         }
  148.  
  149.         /**
  150.          * display the page output
  151.          * @access                private
  152.          * @return                none 
  153.          */
  154.         function themefooter()
  155.         {
  156.             // end output buffering and get module output
  157.             $maincontent ob_get_contents();
  158.             ob_end_clean();
  159.  
  160.             // add the module wrapper
  161.             if (!$this->system && (!isset($this->themeconfig['modulewrapper']|| $this->themeconfig['modulewrapper'])) {
  162.                  $maincontent '<div id="pn-maincontent" class="pn-module-'.DataUtil::formatForDisplay($this->toplevelmodule).'">'.$maincontent.'</div>';
  163.             }
  164.  
  165.             // Assign the main content area to the template engine
  166.             $this->assign_by_ref('maincontent'$maincontent);
  167.  
  168.             // render the page using the correct template
  169.             $this->display($this->themeconfig['page']$this->pageid);
  170.         }
  171.  
  172.         /**
  173.          * display a block
  174.          *
  175.          * @param postion 
  176.          */
  177.         function themesidebox($block)
  178.         {
  179.             // assign the block information
  180.             $this->assign($block);
  181.  
  182.             $bid $block['bid'];
  183.             $bkey $block['bkey'];
  184.             $position $block['position'];
  185.  
  186.             // fix block positions - for now....
  187.             if ($position == 'l'$position 'left';
  188.             if ($position == 'c'$position 'center';
  189.             if ($position == 'r'$position 'right';
  190.  
  191.             // HACK: Save/restore output filters - we don't want to output-filter blocks
  192.             $outputfilters $this->_plugins['outputfilter'];
  193.             $this->_plugins['outputfilter'array();
  194.             // HACK: Save/restore cache settings
  195.             $caching $this->caching;
  196.             $this->caching = false;
  197.  
  198.             // determine the correct template and construct the output
  199.             $return '';
  200.             if (isset($this->themeconfig['blockinstances'][$bid]&& !empty($this->themeconfig['blockinstances'][$bid])) {
  201.                 $return .= $this->fetch($this->themeconfig['blockinstances'][$bid]);
  202.             elseif (isset($this->themeconfig['blocktypes'][$bkey]&& !empty($this->themeconfig['blocktypes'][$bkey])) {
  203.                 $return .= $this->fetch($this->themeconfig['blocktypes'][$bkey]);
  204.             else if (isset($this->themeconfig['blockpositions'][$position]&& !empty($this->themeconfig['blockpositions'][$position])) {
  205.                 $return .= $this->fetch($this->themeconfig['blockpositions'][$position]);
  206.             else if (isset($this->themeconfig['block']&& !empty($this->themeconfig['block'])) {
  207.                 $return .= $this->fetch($this->themeconfig['block']);
  208.             else {
  209.                 if (!empty($block['title'])) {
  210.                     $return .= '<h4>'pnML($block['title']array()true' ' .$block['minbox'].'</h4>';
  211.                 }
  212.                 $return .= $block['content'];
  213.             }
  214.  
  215.             // HACK: Save/restore output filters
  216.             $this->_plugins['outputfilter'$outputfilters;
  217.             // HACK: Save/restore cache settings
  218.             $this->caching = $caching;
  219.  
  220.             if (!isset($this->themeconfig['blockwrapper']|| $this->themeconfig['blockwrapper']{
  221.                  $return '<div class="pn-block pn-blockposition-'.DataUtil::formatForDisplay($block['position'])
  222.                          .' pn-bkey-'.DataUtil::formatForDisplay($block['bkey'])
  223.                          .' pn-bid-'.DataUtil::formatForDisplay($block['bid']).'">'."\n"
  224.                          . $return "</div>\n";
  225.             }
  226.  
  227.             return $return;
  228.         }
  229.  
  230.         /**
  231.          * render the legacy open/close table functions
  232.          *
  233.          * @param string $template 
  234.          * @deprecated
  235.          */
  236.         function themetable($open true$tablenum 1)
  237.         {
  238.             $return '';
  239.             if ($open{
  240.                  // Start the output buffer
  241.                  ob_start();
  242.             else {
  243.                 // Get the buffer contents and end buffering
  244.                 $tablecontent ob_get_contents();
  245.                 ob_end_clean();
  246.  
  247.                 if (isset($this->themeconfig['table'.(int)$tablenum])) {
  248.                     // assign the table content
  249.                     $this->assign('tablecontent'$tablecontent);
  250.                     $return $this->fetch($this->themeconfig['table'.(int)$tablenum]);
  251.                 else {
  252.                     $return '<div class="pn-box'.(int)$tablenum.'">'.$tablecontent.'</div>';
  253.                 }
  254.             }
  255.  
  256.             return $return;
  257.          }
  258.  
  259.         /**
  260.          * Checks which path to use for required template
  261.          *
  262.          * @param string $template 
  263.          */
  264.         function get_template_path($template)
  265.         {
  266.             static $cache array();
  267.  
  268.             if (isset($cache[$template])) {
  269.                 return $cache[$template];
  270.             }
  271.  
  272.             // get the theme path to templates
  273.             $os_theme DataUtil::formatForOS($this->directory);
  274.  
  275.             // Define the locations in which we will look for templates
  276.             // (in this order)
  277.             // 1. Master template path
  278.             $masterpath "themes/$os_theme/templates";
  279.             // 2. The module template path
  280.             $modulepath "themes/$os_theme/templates/modules";
  281.             // 4. The block template path
  282.             $blockpath "themes/$os_theme/templates/blocks";
  283.  
  284.             $ostemplate DataUtil::formatForOS($template);
  285.  
  286.             $search_path array($masterpath,
  287.                                  $modulepath,
  288.                                  $blockpath);
  289.             foreach ($search_path as $path{
  290.                 if (file_exists("$path/$ostemplate"&& is_readable("$path/$ostemplate")) {
  291.                     $cache[$template$path;
  292.                     return $path;
  293.                 }
  294.             }
  295.  
  296.             // when we arrive here, no path was found
  297.             return false;
  298.         }
  299.  
  300.         /**
  301.          * define all our plugin directories
  302.          *
  303.          * @access private
  304.          */
  305.         function _plugin_dirs()
  306.         {
  307.             // add our basic engine plugin path
  308.             if (file_exists('system/Theme/plugins')) {
  309.                 array_push($this->plugins_dir'system/Theme/plugins');
  310.             }
  311.             if (file_exists('system/pnRender/plugins')) {
  312.                 array_push($this->plugins_dir'system/pnRender/plugins');
  313.             }
  314.  
  315.             // add theme specific plugins directories, if they exist
  316.             $themepath 'themes/'.$this->directory.'/plugins';
  317.             if (file_exists($themepath)) {
  318.                 array_push($this->plugins_dir$themepath);
  319.             }
  320.         }
  321.  
  322.         /**
  323.          * assign template vars for base theme paths and other useful variables
  324.          *
  325.          * @access private
  326.          */
  327.         function _base_vars()
  328.         {
  329.             // get variables from input
  330.             $name   FormUtil::getPassedValue('name'null'GETPOST');
  331.             $module FormUtil::getPassedValue('module'null'GETPOST');
  332.             $type   FormUtil::getPassedValue('type'null'GETPOST');
  333.             $func   FormUtil::getPassedValue('func'null'GETPOST');
  334.  
  335.             // set some basic class variables from the PN environemnt
  336.             $this->isloggedin pnUserLoggedIn();
  337.             $this->uid pnUserGetVar('uid');
  338.  
  339.             // Assign the query string
  340.             $this->qstring = isset($_SERVER['QUERY_STRING']$_SERVER['QUERY_STRING''';
  341.             // Assign the current script
  342.             $this->requesturi pnGetCurrentURI();
  343.  
  344.             // assign some basic paths for the engine
  345.             $this->template_dir $this->themepath.'/templates';        // default directory for templates
  346.  
  347.             // define the cache id
  348.             if ($this->isloggedin{
  349.                 $this->pageid $this->name.$this->requesturi.$this->qstring.$this->language.$this->uid;
  350.             else {
  351.                 $this->pageid $this->name.$this->requesturi.$this->qstring.$this->language;
  352.             }
  353.             // now strip any non-ascii characters from the page id
  354.             $this->pageid md5($this->pageid);
  355.  
  356.             $this->themepath      'themes/'.$this->directory;
  357.             $this->imagepath      $this->baseurl.$this->themepath.'/images';
  358.             $this->imagelangpath  $this->baseurl.$this->themepath.'/images'.$this->language;
  359.             $this->stylepath      $this->baseurl.$this->themepath.'/style';
  360.             $this->scriptpath     $this->baseurl.$this->themepath.'/javascript';
  361.  
  362.             // set vars based on the module structures
  363.             $this->type !empty($type$type 'user';
  364.             $this->func !empty($func$func 'main';
  365.             $this->home (empty($module&& empty($name)) true false;
  366.  
  367.             // identify and assign the page type
  368.             $this->pagetype 'module';
  369.             if ((stristr($_SERVER['PHP_SELF']'admin.php'|| strtolower($this->type== 'admin')) {
  370.                 $this->pagetype 'admin';
  371.             else if (empty($name&& empty($module)) {
  372.                 $this->pagetype 'home';
  373.             }
  374.             $this->assign('pagetype'$this->pagetype);
  375.  
  376.             // make the base vars available to all templates
  377.             $this->assign('lang'$this->language);
  378.             $this->assign('loggedin'$this->isloggedin);
  379.             $this->assign('uid'$this->uid);
  380.             $this->assign('themepath'$this->themepath);
  381.             $this->assign('imagepath'$this->imagepath);
  382.             $this->assign('imagelangpath'$this->imagelangpath);
  383.             $this->assign('stylepath'$this->stylepath);
  384.             $this->assign('scriptpath'$this->scriptpath);
  385.             $this->assign('module'$this->toplevelmodule);
  386.             $this->assign('type'$this->type);
  387.             $this->assign('func'$this->func);
  388.         }
  389.  
  390.         /**
  391.          * load the base theme configuration
  392.          *
  393.          * @access private
  394.          */
  395.         function _load_config()
  396.         {
  397.             // set the config directory
  398.             $this->_set_configdir();
  399.  
  400.             // load the page configurations
  401.             $pageconfigurations pnModAPIFunc('Theme''user''getpageconfigurations'array('theme' => $this->name));
  402.  
  403.             // parse the query string into individual arguments discarding common arguments
  404.             // common arguments are ones that we don't want affecting our url matching or ones that are
  405.             // already considered; These are same args defined as reserved by the MDG.
  406.             if (pnConfigGetVar('shorturls')) {
  407.                 if (pnConfigGetVar('shorturlstype'== 0{
  408.                     // remove the base uri, entrypoint, module name and, if it exists, the function name from the string
  409.                     $customargs str_replace(pnGetBaseURI()''$this->requesturi);
  410.                     $entrypoint pnConfigGetVar('entrypoint');
  411.                     $customargs str_replace("/{$entrypoint}/"'/'$customargs);
  412.                 else {
  413.                     $extension pnConfigGetVar('shorturlsext');
  414.                     $qstring str_replace(array(pnGetBaseURI().'/'".{$extension}""module-{$this->toplevelmodule}"$this->type$this->func)
  415.                                            '',
  416.                                            $this->requesturi);
  417.                     $qstring trim($qstring'-');
  418.                     $argsarray explode('-'$qstring);
  419.                     $argsarray array_chunk($argsarray2);
  420.                     foreach ($argsarray as $argarray{
  421.                         $customargs[implode('='$argarray);
  422.                     }
  423.                     $customargs '/' implode('/'$customargs);
  424.                 }
  425.             else {
  426.                 $queryparts explode('&'$this->qstring);
  427.                 $customargs '';
  428.                 foreach ($queryparts as $querypart{
  429.                     if (!stristr($querypart'module='&& !stristr($querypart'name='&&  !stristr($querypart'type='&&
  430.                         !stristr($querypart'func='&& !stristr($querypart'theme='&& !stristr($querypart'authid=')) {
  431.                         $customargs .= '/'.$querypart;
  432.                     }
  433.                 }
  434.             }
  435.  
  436.             // identify and load the correct module configuration
  437.             $this->cachepage true;
  438.             if (stristr($_SERVER['PHP_SELF']'user'&& isset($pageconfigurations['*user'])) {
  439.                 $file $pageconfigurations['*user']['file'];
  440.             else if (!stristr($_SERVER['PHP_SELF']'user'&& !stristr($_SERVER['PHP_SELF']'admin.php'&& $this->home && isset($pageconfigurations['*home'])) {
  441.                 $file $pageconfigurations['*home']['file'];
  442.             else if (stristr($_SERVER['PHP_SELF']'admin.php'&& isset($pageconfigurations['*admin'])) {
  443.                 $this->cachepage false;
  444.                 $file $pageconfigurations['*admin']['file'];
  445.             else {
  446.                 $customargs $this->toplevelmodule.'/'.$this->type.'/'.$this->func.$customargs;
  447.                 // find any page configurations that match in a sub string comparison
  448.                 $match '';
  449.                 $matchlength 0;
  450.                 foreach ($pageconfigurations as $pageconfiguration => $pageconfigurationarray{
  451.                     if (stristr($customargs$pageconfiguration&& $matchlength strlen($pageconfiguration)) {
  452.                         $match $pageconfiguration;
  453.                         $matchlength strlen($match);
  454.                     }
  455.                 }
  456.                 if((strtolower($this->type== 'admin'&& isset($pageconfigurations['*admin'])&&(!stristr($match,'admin'))) {
  457.                     $match '*admin';
  458.                 }
  459.                 if(strtolower($this->type== 'admin'{
  460.                     $this->cachepage false;
  461.                 }
  462.                 if (!empty($match)) {
  463.                     $file $pageconfigurations[$match]['file'];
  464.                 }
  465.             
  466.  
  467.             if (!isset($file)) {
  468.                 $file $pageconfigurations['master']['file'];
  469.             }
  470.  
  471.             // load the page configuration
  472.             $this->themeconfig pnModAPIFunc('Theme''user''getpageconfiguration'array('theme' => $this->name'filename' => $file));
  473.  
  474.             // check if we've not got a valid theme configation
  475.             if (!$this->themeconfig{
  476.                 $this->themeconfig pnModAPIFunc('Theme''user''getpageconfiguration'array('theme' => $this->name'filename' => 'master.ini'));
  477.             }
  478.  
  479.             // register any filters
  480.             if (isset($this->themeconfig['filters']&& !empty($this->themeconfig['filters'])) {
  481.                 if (isset($this->themeconfig['filters']['outputfilters']&& !empty($this->themeconfig['filters']['outputfilters'])) {
  482.                     $this->themeconfig['filters']['outputfilters'explode(','$this->themeconfig['filters']['outputfilters']);
  483.                     foreach ($this->themeconfig['filters']['outputfilters'as $filter{
  484.                         $this->load_filter('output'$filter);
  485.                     }
  486.         }
  487.                 if (isset($this->themeconfig['filters']['prefilters']&& !empty($this->themeconfig['filters']['prefilters'])) {
  488.                     $this->themeconfig['filters']['prefilters'explode(','$this->themeconfig['filters']['prefilters']);
  489.                     foreach ($this->themeconfig['filters']['prefilters'as $filter{
  490.                         $this->load_filter('pre'$filter);
  491.                     }
  492.                 }
  493.                 if (isset($this->themeconfig['filters']['postfilters']&& !empty($this->themeconfig['filters']['postfilters'])) {
  494.                     $this->themeconfig['filters']['postfilters'explode(','$this->themeconfig['filters']['postfilters']);
  495.                     foreach ($this->themeconfig['filters']['postfilters'as $filter{
  496.                         $this->load_filter('post'$filter);
  497.                     }
  498.                 }
  499.             }
  500.  
  501.             // load the theme settings
  502.             $this->config_load('file:themevariables.ini''variables');
  503.  
  504.             // load the palette
  505.             if (isset($this->themeconfig['palette'])) {
  506.                  $this->config_load('file:themepalettes.ini'$this->themeconfig['palette']);
  507.             }
  508.  
  509.             // assign the palette
  510.             $this->assign('palette'isset($this->themeconfig['palette']$this->themeconfig['palette'null);
  511.         }
  512.  
  513.         /**
  514.          * set the config directory for this theme
  515.          *
  516.          * @access private
  517.          */
  518.         function _set_configdir()
  519.         {
  520.             // check for a running configuration in the pnTemp/Xanthia_Config directory
  521.             if (is_dir($dir pnConfigGetVar('temp').'/Xanthia_Config/'.DataUtil::formatForOS($this->name))) {
  522.                 $this->config_dir $dir;
  523.             else {
  524.                 $this->config_dir $this->themepath.'/templates/config';
  525.             }
  526.         }
  527.     }
  528. }

Documentation generated on Fri, 18 Jul 2008 21:54:34 +0200 by phpDocumentor 1.4.1