Source for file CMS_MENU.phpclass

Documentation is available at CMS_MENU.phpclass

  1. <?php
  2. /**
  3.   * openCSP class file CMS_MENU.phpclass
  4.   *
  5.   * @project Open CSP-Management
  6.   * @package default
  7.   *
  8.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  9.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.   *
  11.   * @since pk-16.06.2008
  12.   * @version $Id: CMS_MENU.phpclass,v 1.10 2008/11/24 23:30:33 pitlinz Exp $
  13.   */
  14.  
  15. // ---------------------------------------------------------
  16. // requirements
  17. // ---------------------------------------------------------
  18.     
  19.     pcf_require_class('OCSP_OBJ',__OCSP_PHPINCPATH__ "common/OCSP_OBJ.phpclass");
  20.     pcf_require_class('OCSP_CMS_CACHE',__OCSP_PHPINCPATH__ "cms/OCSP_CMS_CACHE.phpclass");
  21.     pcf_require_class('OCSP_CMS_CACHE',__OCSP_PHPINCPATH__ "cms/OCSP_CMS_CACHE.phpclass");
  22.     
  23.     pcf_require_class('DBMS_TREE',__OCSP_PHPINCPATH__."db/DBMS_TREE.phpclass");
  24.  
  25.  
  26. /**
  27.   * openCSP class CMS_MENU
  28.   *
  29.   * singleton object
  30.   * 
  31.   * @project Open CSP-Management
  32.   * @package cms
  33.   *
  34.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  35.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  36.   *
  37.   * 
  38.   * @since pk-16.06.2008
  39.   * @version $Id: CMS_MENU.phpclass,v 1.10 2008/11/24 23:30:33 pitlinz Exp $
  40.   */
  41. class CMS_MENU extends DBMS_TREE
  42. {
  43.     // ---------------------------------------------------------------------------
  44.     // constants
  45.     // ---------------------------------------------------------------------------
  46.     
  47.     /**
  48.      * @constant string CLASS_SRC_FILE
  49.      *
  50.      * @abstract
  51.      */
  52.     const CLASS_SRC_FILE = __FILE__;
  53.     
  54.     // ---------------------------------------------------------------------------
  55.     // class (static)
  56.     // ---------------------------------------------------------------------------
  57.     
  58.     /*** class vars ------------------------------------------------------ */
  59.  
  60.     /**
  61.      * @staticvar CMS_MENU $myInstance 
  62.      */
  63.     protected static $loadedMenus array();
  64.     
  65.     
  66.     /*** class methods   ---------------------------------------------- */
  67.             
  68.     /**
  69.      * returns the singleton instance
  70.      *
  71.      * @return OCSP_MENU 
  72.      */
  73.     public static function &getInstance($projId)
  74.     {
  75.         $projId intval($projId);
  76.         if (!pcf_is_instance_of(self::$loadedMenus[intval($projId)],'CMS_MENU'))
  77.         {
  78.             self::$loadedMenus[$projIdnew CMS_MENU();
  79.         }
  80.         
  81.         return self::$loadedMenus[$projId];
  82.     }
  83.     
  84.     /**
  85.      * returns an array with all available node classes
  86.      *
  87.      * @return array 
  88.      */
  89.     public static function getAvailableNodeClasses()
  90.     {
  91.         $arr_ret array();
  92.         //"SELECT MEN_OBJCLASS,MET_TITLE FROM T_CMS_MENUTYPE ORDER BY MET_ISDEFAULT DESC"
  93.         if ($arr_row OCSP_OBJ::defaultReadDBObj()->getArray('T_CMS_MENUTYPE',Null,0,0,"MET_ISDEFAULT DESC,MET_TITLE"))
  94.         {
  95.             $arr_ret[$arr_row['MEN_OBJCLASS']] $arr_row;
  96.         }
  97.         return $arr_ret;
  98.     }
  99.     
  100.     // ---------------------------------------------------------------------------
  101.     // object vars
  102.     // ---------------------------------------------------------------------------
  103.     
  104.     /*** compostion --------------------------------------------------- */
  105.     
  106.     /**
  107.      * @var array myNodes
  108.      */
  109.     protected $myNodes = array();
  110.     
  111.     /**
  112.      * array of child nodes
  113.      * 
  114.      * array(key = array($key => node))
  115.      * 
  116.      * @var array $nodeChildKeys 
  117.      */
  118.     protected $childNodes = array();
  119.     
  120.     /**
  121.      * array of the first level children
  122.      *
  123.      * @var array $myFirstLevelChildren 
  124.      */
  125.     protected $myFirstLevelChildren = array();
  126.     
  127.     /**
  128.      * project object
  129.      * 
  130.      * @var CMS_PROJECT $myProject 
  131.      */
  132.     protected $myProject = Null;
  133.     
  134.     /*** attributes  -------------------------------------------------- */
  135.     
  136.     /**
  137.      * the root Id in T_CMS_MENU
  138.      *
  139.      * @var int 
  140.      */
  141.     protected $menRootId = 0;
  142.     
  143.     /*** factory / construct  ----------------------------------------- */    
  144.         
  145.     /*** getter / setter ---------------------------------------------- */
  146.  
  147.     /**
  148.      * returns the project object
  149.      * 
  150.      * @param boolean $debug 
  151.      * 
  152.      * @return CMS_PROJECT 
  153.      */
  154.     public function &getMyProject()
  155.     {
  156.         return $this->myProject;
  157.     }
  158.     
  159.     /**
  160.      * returns the project Id
  161.      *
  162.      * @return int 
  163.      * 
  164.      * @todo ask tree nodes
  165.      */
  166.     public function getProjId()
  167.     {
  168.         $debug=False;
  169.         if ($this->myProject)
  170.         {
  171.             return $this->myProject->getId();
  172.         }
  173.         if ($debugechoDebugLine(__FILE__,__LINE__,"Project not set");
  174.         return 0;
  175.     }
  176.     
  177.     /**
  178.      * sets the CMS Project
  179.      *
  180.      * @param CMS_PROJECT $aProject 
  181.      */
  182.     public function setMyProject(&$aProject)
  183.     {
  184.         $this->myProject = $aProject;
  185.     }
  186.     
  187.         
  188.     /*** node get/factory methods ------------------------------------- */
  189.             
  190.     /**
  191.      * returns the menu node with key $aKey
  192.      * 
  193.      * a key can be seperated with '-'
  194.      *
  195.      * @param mixed $aKey (
  196.      * @param boolean $forceObj 
  197.      * @param boolean $debug 
  198.      * 
  199.      * @return CMS_MENU_NODE 
  200.      */
  201.     public function &getNode($aKey,$forceObj=False,$debug=False)
  202.     {
  203.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CMS_MENU::getNode(".$aKey.")");
  204.         
  205.         if (empty($aKey&& (!$forceObj))
  206.         {
  207.             $obj_node Null;
  208.             return $obj_node;
  209.         }
  210.         
  211.         do {
  212.             if (isset($this->myNodes[$aKey]))
  213.             {
  214.                 if ($debugechoDebugLine(__FILE__,__LINE__,"returning already loaded node");
  215.                 return $this->myNodes[$aKey];
  216.             }
  217.  
  218.             if ($arr_menRow OCSP_CMS_CACHE::getInstance('MENU')->getMenuRow($aKey))
  219.             {
  220.                 if ($debugechoDebugLine(__FILE__,__LINE__,"returning node from cache row");
  221.                 return $this->getNodeFromRow($arr_menRow,False,$debug);
  222.             }            
  223.             
  224.             if (strstr($aKey,"-"))
  225.             {
  226.                 $arr_childKeys  explode("-",$aKey);
  227.                 $int_menId         $arr_childKeys[0];             
  228.             else {
  229.                 $int_menId        intval($aKey);
  230.                 $arr_childKeys  Null;
  231.             }
  232.         
  233.             if ($debugechoDebugLine(__FILE__,__LINE__,"key array: <pre>" print_r($arr_childKeys,True"</pre>");
  234.             
  235.             if (!intval($int_menId))
  236.             {
  237.                 if ($forceObj)
  238.                 {
  239.                     CMS_TABLEOBJ::menuType_requireClass('CMS_MENU_NODE');
  240.                     $obj_node new CMS_MENU_NODE();
  241.                 else {
  242.                     $obj_node Null;
  243.                 }
  244.                 return $obj_node
  245.             }
  246.             
  247.             if (!isset($this->myNodes[$int_menId]|| !intval($this->myNodes[$int_menId]->getId()))
  248.             {            
  249.                 $bol_storeToCache False;
  250.                 if (!($arr_menRow OCSP_CMS_CACHE::getInstance('MENU')->getMenuRow($int_menId)))
  251.                 {
  252.                     if ($arr_menRow OCSP_OBJ::defaultReadDBObj()->getRow('T_CMS_MENU',array('MEN_ID' => intval($int_menId))))
  253.                     {
  254.                         $bol_storeToCache True;
  255.                     else {
  256.                         break// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  257.                     }
  258.                 }
  259.                 if (!CMS_TABLEOBJ::menuType_requireClass($arr_menRow['MEN_OBJCLASS']))
  260.                 {
  261.                     throw new Exception(_OCSP_EXCEP_CLASSNOTFOUND_ ":" $arr_menRow['MEN_OBJCLASS']);
  262.                 }                
  263.                 
  264.                 $obj_node $this->getNodeFromRow($arr_menRow,False,$debug);                
  265.                 $this->myNodes[$int_menId]->setTreeObj($this);
  266.                 
  267.                 if ($bol_storeToCache)
  268.                 {
  269.                     OCSP_CMS_CACHE::getInstance('MENU')->storeMenuObj($this->myNodes[$int_menId],$debug);    
  270.                 }
  271.             }
  272.             
  273.             if ($int_menId == $aKey)
  274.             {
  275.                 if ($debugechoDebugLine(__FILE__,__LINE__,"returning CMS Menu " $int_menId " Node for key: " $aKey);
  276.                 return $this->myNodes[$int_menId];
  277.             }
  278.             
  279.             if (method_exists($this->myNodes[$int_menId],'factoryChildNode'))
  280.             {                
  281.                 if ($debugechoDebugLine(__FILE__,__LINE__,"factoryChildNode");
  282.                 $this->myNodes[$aKey$this->myNodes[$int_menId]->factoryChildNode($aKey,$debug);
  283.                 $this->myNodes[$aKey]->setTreeObj($this);
  284.                 OCSP_CMS_CACHE::getInstance('MENU')->storeMenuObj($this->myNodes[$aKey],$debug);
  285.                 return $this->myNodes[$aKey];
  286.             else {
  287.                 if ($debugechoDebugLine(__FILE__,__LINE__,"Object: " get_class($this->myNodes[$int_menId]" does not have method factoryChildNode");
  288.             }
  289.             
  290.             if ($forceObj)
  291.             {
  292.                 $str_cmd "\$obj_node = new " $arr_menRow['MEN_OBJCLASS'"();";
  293.                 $obj_node->setTreeObj($this);
  294.                 $obj_node->setParentObj($this->myNodes[$int_menId]);
  295.                 return $obj_node;
  296.             }
  297.  
  298.             
  299.         while(False);    
  300.         
  301.         if ($forceObj)
  302.         {
  303.             CMS_TABLEOBJ::menuType_requireClass('CMS_MENU_NODE');
  304.             $this->myNodes[$aKeynew CMS_MENU_NODE();
  305.             $this->myNodes[$aKey]->setTreeObj($this);
  306.             return $this->myNodes[$aKey]
  307.         else {
  308.             $obj_node Null;
  309.             return $obj_node;
  310.         }
  311.  
  312.     }
  313.     
  314.     public function addNode(&$aNode)
  315.     {
  316.         if ($mix_key $aNode->getKey())
  317.         {
  318.             $this->myNodes[$mix_key$aNode;
  319.         }
  320.     }
  321.     
  322.     /**
  323.      * returns the menu node from $_GET
  324.      *
  325.      * @param boolean $debug 
  326.      * 
  327.      * @return CMS_MENU_NODE 
  328.      */
  329.     public function &getNodeFromEnv($debug=False)
  330.     {
  331.         if (isset($_GET[CMS_TABLEOBJ::getMenuIdGetName()]))
  332.         {
  333.             if ($obj_ret $this->getNode($_GET[CMS_TABLEOBJ::getMenuIdGetName()],False,$debug))
  334.             {
  335.                 return $obj_ret;
  336.             }
  337.         }
  338.         
  339.         if (isset($_GET[CMS_TABLEOBJ::getMenuIdColName()]))
  340.         {
  341.             if ($obj_ret $this->getNode($_GET[CMS_TABLEOBJ::getMenuIdColName()],False,$debug))
  342.             {
  343.                 return $obj_ret;
  344.             }
  345.         }
  346.         
  347.         $obj_ret=Null;
  348.         return $obj_ret;
  349.         
  350.     }
  351.     
  352.     /**
  353.      * faktories a node form a dbRow
  354.      *
  355.      * @param array $nodeRow 
  356.      * @param boolean $forceOverload 
  357.      * @param boolean $debug 
  358.      * 
  359.      * @return CMS_MENU_NODE 
  360.      * 
  361.      */
  362.     public function &getNodeFromRow($nodeRow,$forceOverload=False,$debug=False)
  363.     {
  364.         if($debugechoDebugMethod(__FILE__,get_class($this),"CMS_MENU::getNodeFromRow();");
  365.                 
  366.         if (CMS_TABLEOBJ::menuType_requireClass($nodeRow['MEN_OBJCLASS']))
  367.         {
  368.             $str_cmd "\$obj_node = new " $nodeRow['MEN_OBJCLASS'"();";
  369.             if ($debugechoDebugLine(__FILE__,__LINE__$str_cmd "<p style=\"font-size:75%\">" print_r($nodeRow,True"</p>")
  370.             eval($str_cmd);
  371.             $obj_node->setTreeObj($this);
  372.             $obj_node->setDBRow($nodeRow,True);
  373.             
  374.             if (!isset($this->myNodes[$obj_node->getKey()]|| $forceOverload)
  375.             {
  376.                 $this->myNodes[$obj_node->getKey()$obj_node;    
  377.             else {
  378.                 if (get_class($this->myNodes[$obj_node->getKey()]!= $nodeRow['MEN_OBJCLASS')
  379.                 {
  380.                     $this->myNodes[$obj_node->getKey()$obj_node;
  381.                 else {
  382.                     $this->myNodes[$obj_node->getKey()]->setDBRow($nodeRow,True,$debug);
  383.                 }
  384.             }
  385.             return $this->myNodes[$obj_node->getKey()]
  386.         else {
  387.             throw new Exception(_OCSP_EXCEP_CLASSNOTFOUND_ ":" $nodeRow['MEN_OBJCLASS']);
  388.         }
  389.     }
  390.     
  391.          
  392.     /*** node children get/factory methods ----------------------------- */
  393.  
  394.     /**
  395.      * returns previously set children array
  396.      *
  397.      * @param string $aKey 
  398.      * @param boolean $debug 
  399.      * 
  400.      * @return array 
  401.      */
  402.     public function getChildNodes($aKey,$debug)
  403.     {
  404.         if (isset($this->childNodes[$aKey]))
  405.         {
  406.             return $this->childNodes[$aKey];
  407.         }
  408.     }
  409.     
  410.     /**
  411.      * sets the child array of $aKey
  412.      *
  413.      * @param string $aKey 
  414.      * @param array $arr_nodes 
  415.      * @param boolean $debug 
  416.      */
  417.     public function setChildNodes($aKey,$arr_nodes,$debug)
  418.     {
  419.         if (!empty($aKey))
  420.         {
  421.             $this->childNodes[$aKey$arr_nodes;
  422.         }
  423.     }
  424.     
  425.     /**
  426.       * returns a array of pointers to the child objects
  427.       *
  428.       * @param bool $debug 
  429.       * @param bool $filtered 
  430.       *
  431.       * @returns array
  432.       *
  433.       */
  434.     function getChildrenList($debug=False,$filtered=False
  435.     {       
  436.         if ($debugechoDebugMethod(__FILE__,get_class($this),'CMS_MENU::getChildrenList()');
  437.         if (isset($this->chldNodes['ROOT']&& sizeof($this->childNodes['ROOT']))
  438.         {
  439.             return $this->childNodes['ROOT'];    
  440.         }
  441.         
  442.         $this->childNodes['ROOT'array();
  443.         
  444.         $int_projectId $this->getProjId();
  445.            if ($arr_childRows OCSP_CMS_CACHE::getInstance('MENU')->getMenuChildrenRowArray('ROOT_' $int_projectId)) 
  446.            {
  447.                foreach($arr_childRows as $arr_row)
  448.                {
  449.                    $int_sort ((isset($arr_row['MEN_SORTORDER']&& intval($arr_row['MEN_SORTORDER'])) intval($arr_row['MEN_SORTORDER']1);
  450.                 while(isset($this->childNodes['ROOT'][$int_sort])) $int_sort++// fix order
  451.                 $arr_row['MEN_SORTORDER'$int_sort;
  452.                 $this->childNodes['ROOT'][$int_sort&$this->getNodeFromRow($arr_row,$debug);                            
  453.                }     
  454.  
  455.                return $this->childNodes['ROOT'];
  456.            }
  457.            
  458.            $arr_filter array(
  459.                    'PROJ_ID'     => $int_projectId,
  460.                    'MEN_PARENT'=> $this->menRootId
  461.                );
  462.  
  463.         if ($debugechoDebugLine(__FILE__,__LINE__,'Menu Filter: ' print_r($arr_filter));               
  464.         if ($arr_childRows OCSP_OBJ::defaultReadDBObj()->getArray('T_CMS_MENU',$arr_filter,0,0,'MEN_SORTORDER',$debug))
  465.         {            
  466.                foreach($arr_childRows as $arr_row)
  467.                {
  468.                    if ($debugechoDebugLine(__FILE__,__LINE__,"adding node: <pre>" print_r($arr_row,True)) "</pre>";
  469.                    $int_sort ((isset($arr_row['MEN_SORTORDER']&& intval($arr_row['MEN_SORTORDER'])) intval($arr_row['MEN_SORTORDER']1);
  470.                 while(isset($this->childNodes['ROOT'][$int_sort])) $int_sort++// fix order
  471.                 $arr_row['MEN_SORTORDER'$int_sort;
  472.                 $this->childNodes['ROOT'][$int_sort&$this->getNodeFromRow($arr_row,$debug);
  473.                }               
  474.             return $this->childNodes['ROOT'];                   
  475.         else {
  476.             return array();
  477.         }        
  478.            
  479.         
  480.     }
  481.         
  482.     /*** tree methods -------------------------------------------------- */
  483.     
  484.     
  485.     /**
  486.       * returns an array of pointers to the elements till root with all
  487.       *
  488.       * in case of an error false is return (use '===' type equal )
  489.       *
  490.       * @param mixed $aNode (DMBS_TREE_NODE) a node object or (int) a node id
  491.       * @param bool $useCache 
  492.       * @param bool $debug 
  493.       *
  494.       * @returns array  array of pointers to nodes key=DBMS_TREE_NODE::getId()
  495.       *
  496.       */
  497.     function getRootPath($aNode,$useCache=TRUE,$debug=FALSE{
  498.         if ($debugechoDebugMethod(__FILE__,get_class($this),"DBMS_TREE::getRootPath()","NODE$aNode");
  499.  
  500.         if ($debugecho "<blockquote style=\"backgroud-color: #febf00;font-size:80%\">";
  501.  
  502.         if (pcf_is_instance_of($aNode,'DBMS_TREE_NODE'))
  503.         {
  504.             if ($debugecho "</blockquote>";
  505.             return $aNode->getRootPath($useCache,$debug);
  506.         }
  507.  
  508.         if (empty($aNode)) return array();
  509.         if ($o_node=$this->getNode($aNode,$debug))
  510.         {
  511.             if ($debugecho "</blockquote>";
  512.             return $o_node->getRootPath($useCache,$debug);
  513.         }
  514.         ocsp_logError(__FILE__,__LINE__,"$aNode is not in the tree",E_WARNING);
  515.         if ($debugecho "</blockquote>";
  516.         return array();
  517.     }    
  518.  
  519. }
  520.  
  521. ?>

Documentation generated on Thu, 08 Jan 2009 17:39:08 +0100 by phpDocumentor 1.4.0a2