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

Source for file PageUtil.class.php

Documentation is available at PageUtil.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: AjaxUtil.class.php 20909 2006-12-26 12:23:12Z landseer $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  *
  10.  * @package Zikula_Core
  11.  */
  12.  
  13. /**
  14.  * Zikula page variables functions
  15.  *
  16.  * A <em>page variable</em> is an entity identified by a name that stores a value for the currently
  17.  * rendered page. They are used to set for example the title of the page, the stylesheets used etc.
  18.  * from the module.
  19.  *
  20.  * Page variables can be <em>single valued</em> or <em>multi valued</em>. In the first case, only
  21.  * one single value can be set; each new setting will overwrite the old one. The title is an example
  22.  * for a single values page variable (each page can have exactly one title). Multi valued variables
  23.  * can contain more than one value, and new values can be added to the variable. An example of a multi
  24.  * valued variable is stylesheet (a page can use more than one style sheet).
  25.  *
  26.  * Zikula offers a set of API functions to manipulate page variables.
  27.  *
  28.  * A module can register a new page variable by providing its metadata using the pnPageRegisterVar
  29.  * function.
  30.  *
  31.  * Zikula doesn't impose any restriction on the page variabl's name except for duplicate
  32.  * and reserved names. As of this writing, the list of reserved names consists of
  33.  * <ul>
  34.  * <li>title</li>
  35.  * <li>description</li>
  36.  * <li>keywords</li>
  37.  * <li>stylesheet</li>
  38.  * <li>javascript</li>
  39.  * <li>body</li>
  40.  * <li>rawtext</li>
  41.  * <li>footer</li>
  42.  * </ul>
  43.  *
  44.  * @author      Jörg Napp, Frank Schummertz
  45.  * @link        http://www.pn-cms.de   Zikula home page
  46.  * @package     Zikula_Core
  47.  * @subpackage  PageUtil
  48.  */
  49. class PageUtil
  50. {
  51.     /**
  52.      * registerVar()
  53.      *
  54.      * Registers a new page variable.
  55.      * Zikula doesn't impose any restriction on the page variabl's name except for duplicate
  56.      * and reserved names. As of this writing, the list of reserved names consists of
  57.      * <ul>
  58.      * <li>title</li>
  59.      * <li>keywords</li>
  60.      * <li>stylesheet</li>
  61.      * <li>javascript</li>
  62.      * <li>body</li>
  63.      * </ul>
  64.      *
  65.      * @param string $varname the name of the new page variable
  66.      * @param boolean $multivalue to define a single or a multi valued variable
  67.      * @param string $default to set the default value. This value is assigned to the variable at registration time.
  68.      * @return boolean success or not
  69.      * @author Jörg Napp
  70.      * @since Feb 04
  71.      * @see resetVar
  72.      ***/
  73.     function registerVar($varname$multivalue false$default null)
  74.     {
  75.         global $_pnPageVars;
  76.  
  77.         // check for $_pnPageVars sanity
  78.         if (!isset($_pnPageVars)) {
  79.             $_pnPageVars array();
  80.         elseif (!is_array($_pnPageVars)) {
  81.             return false;
  82.         }
  83.  
  84.         // if already registered, stop
  85.         if (isset($_pnPageVars[$varname])) {
  86.             return false;
  87.         }
  88.  
  89.         // define the page variable and it's default value
  90.         $_pnPageVars[$varnamecompact('multivalue''default');
  91.  
  92.         // always make the default value the contents (even if it's null - that will be filtered away)
  93.         PageUtil::resetVar($varname);
  94.  
  95.         return true;
  96.     }
  97.  
  98.     /**
  99.      * resetVar()
  100.      *
  101.      * Resets the pge variable back to its default value.
  102.      * All values assigned by addVar() or setVar()
  103.      * will get lost.
  104.      *
  105.      * @param string $varname the name of the page variable
  106.      * @return boolean true on success, false of the page variable is not registered.
  107.      * @author Jörg Napp
  108.      * @since Feb 04
  109.      * @see registerVar
  110.      ***/
  111.     function resetVar($varname)
  112.     {
  113.         global $_pnPageVars;
  114.  
  115.         // check for $_pnPageVars sanity
  116.         if (!isset($_pnPageVars)) {
  117.             $_pnPageVars array();
  118.         elseif (!is_array($_pnPageVars)) {
  119.             return false;
  120.         }
  121.  
  122.         if (!isset($_pnPageVars[$varname])) {
  123.             return false;
  124.         }
  125.  
  126.         if ($_pnPageVars[$varname]['multivalue']{
  127.             if (empty($_pnPageVars[$varname]['default'])) {
  128.                 $_pnPageVars[$varname]['contents'array();
  129.             else {
  130.                 $_pnPageVars[$varname]['contents'array($_pnPageVars[$varname]['default']);
  131.             }
  132.         else {
  133.             if (empty($_pnPageVars[$varname]['default'])) {
  134.                 $_pnPageVars[$varname]['contents'null;
  135.             else {
  136.                 $_pnPageVars[$varname]['contents'$_pnPageVars[$varname]['default'];
  137.             }
  138.         }
  139.         return true;
  140.     }
  141.  
  142.     /**
  143.      * getVar()
  144.      *
  145.      * returns the value(s) of a page variable. In the case of
  146.      * a mulit valued variable, this is an array containing all assigned
  147.      * values
  148.      *
  149.      * @param string $varname the name of the page variable
  150.      * @return mixed the contents of the variable
  151.      * @author Jörg Napp
  152.      * @since Feb 04
  153.      * @see setVar
  154.      * @see addVar
  155.      ***/
  156.     function getVar($varname$default=null)
  157.     {
  158.         global $_pnPageVars;
  159.  
  160.         // check for $_pnPageVars sanity
  161.         if (!isset($_pnPageVars)) {
  162.             $_pnPageVars array();
  163.         elseif (!is_array($_pnPageVars)) {
  164.             return false;
  165.         }
  166.  
  167.         if (isset($_pnPageVars[$varname]&& isset($_pnPageVars[$varname]['contents'])) {
  168.             return $_pnPageVars[$varname]['contents'];
  169.         elseif (isset($_pnPageVars[$varname]['default'])) {
  170.             return $_pnPageVars[$varname]['default'];
  171.         }
  172.  
  173.         return $default;
  174.     }
  175.  
  176.     /**
  177.      * setVar()
  178.      *
  179.      * Sets the page variable to a new value. In the case of
  180.      * a multi valued page variable, all previously added values
  181.      * will get lost. If you want to add a value to a multi valued
  182.      * page variable, use PageUtil::addVar.
  183.      *
  184.      * @param string $varname the name of the page variable
  185.      * @param mixed $value the new value
  186.      * @return boolean true on success, false of the page variable is not registered.
  187.      * @author Jörg Napp
  188.      * @since Feb 04
  189.      * @see addVar
  190.      ***/
  191.     function setVar($varname$value)
  192.     {
  193.         global $_pnPageVars;
  194.  
  195.         // check for $_pnPageVars sanity
  196.         if (!isset($_pnPageVars)) {
  197.             $_pnPageVars array();
  198.         elseif (!is_array($_pnPageVars)) {
  199.             return false;
  200.         }
  201.  
  202.         if (!isset($_pnPageVars[$varname])) {
  203.             return false;
  204.         }
  205.  
  206.         if ($_pnPageVars[$varname]['multivalue']{
  207.             $_pnPageVars[$varname]['contents'array($value);
  208.         else {
  209.             $_pnPageVars[$varname]['contents'$value;
  210.         }
  211.         return true;
  212.     }
  213.  
  214.     /**
  215.      * addVar()
  216.      *
  217.      * Adds a new vaule to a page variable. In the case of a single
  218.      * page variable, this functions acts exactly like PageUtil::setVar.
  219.      *
  220.      * @param string $varname the name of the page variable
  221.      * @param mixed $value the new value
  222.      * @return boolean true on success, false of the page variable is not registered.
  223.      * @author Jörg Napp
  224.      * @since Feb 04
  225.      * @see setVar
  226.      ***/
  227.     function addVar($varname$value)
  228.     {
  229.         global $_pnPageVars;
  230.         // check for $_pnPageVars sanity
  231.         if (!isset($_pnPageVars)) {
  232.             $_pnPageVars array();
  233.         elseif (!is_array($_pnPageVars)) {
  234.             return false;
  235.         }
  236.  
  237.         if (!isset($_pnPageVars[$varname])) {
  238.             return false;
  239.         }
  240.  
  241.         if ($_pnPageVars[$varname]['multivalue']{
  242.             if (is_array($value)) {
  243.                 $_pnPageVars[$varname]['contents'array_merge($_pnPageVars[$varname]['contents']$value);
  244.             else {
  245.                 $_pnPageVars[$varname]['contents'][$value;
  246.             }
  247.             // make values unique
  248.             $_pnPageVars[$varname]['contents'array_unique($_pnPageVars[$varname]['contents']);
  249.         else {
  250.             $_pnPageVars[$varname]['contents'$value;
  251.         }
  252.  
  253.         return true;
  254.     }
  255.  
  256. }

Documentation generated on Fri, 18 Jul 2008 21:48:04 +0200 by phpDocumentor 1.4.1