Source for file PRO_GROUP.phpclass

Documentation is available at PRO_GROUP.phpclass

  1. <?php
  2. /**
  3.   * openCSP class file PRO_GROUP.phpclass
  4.   *
  5.   * @project Open CSP-Management
  6.   * @package shop
  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-17.07.2008
  12.   * @version $Id: PRO_GROUP.phpclass,v 1.9 2008/10/16 02:21:48 pitlinz Exp $
  13.   */
  14.  
  15.     // ---------------------------------------------------------
  16.     // requirements
  17.     // ---------------------------------------------------------
  18.  
  19.     pcf_require_class('DBMS_TABLEOBJ',"db/");
  20.     pcf_require_class('PRO_GROUP_TYPE',dirname(__FILE__"/");
  21.     pcf_require_class('OCSP_PRODUCT_CONF',dirname(__FILE__"/");
  22.     pcf_require_class('PRO_PROD_TYPE',dirname(__FILE__"/");
  23.     
  24. /**
  25.   * openCSP class PRO_GROUP
  26.   *
  27.   * @project Open CSP-Management
  28.   * @package shop
  29.   *
  30.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  31.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  32.   *
  33.   * @since pk-17.07.2008
  34.   * @version $Id: PRO_GROUP.phpclass,v 1.9 2008/10/16 02:21:48 pitlinz Exp $
  35.   */
  36. class PRO_GROUP extends DBMS_TABLEOBJ
  37. {
  38.     // ---------------------------------------------------------------------------
  39.     // constants
  40.     // ---------------------------------------------------------------------------
  41.     
  42.     /**
  43.      * @constant string CLASS_SRC_FILE
  44.      */
  45.     const CLASS_SRC_FILE = __FILE__;
  46.  
  47.     // ---------------------------------------------------------------------------
  48.     // class (static)
  49.     // ---------------------------------------------------------------------------
  50.     
  51.     /*** class vars ------------------------------------------------------ */
  52.     
  53.     
  54.     /**
  55.      * unit of work of loaded product groups
  56.      *
  57.      * @var array $loadedNodes 
  58.      */
  59.     protected static $loadedNodes=array();
  60.     
  61.     /*** class methods --------------------------------------------------- */
  62.     
  63.     // ---------------------------------------------------------------------------
  64.     // object vars
  65.     // ---------------------------------------------------------------------------
  66.     
  67.     /*** compostion --------------------------------------------------- */
  68.     
  69.     /**
  70.      * children array
  71.      *
  72.      * @var array $myChildren 
  73.      */
  74.     protected $myChildren = array();
  75.     
  76.     /**
  77.      * products assigned to the group
  78.      *
  79.      * @var array() 
  80.      */
  81.     protected $myProducts = array();
  82.     
  83.     /*** attributes  -------------------------------------------------- */
  84.     
  85.     /**
  86.       * @var string $myTable 
  87.       */
  88.     protected $myTable="T_PRO_GROUP";    
  89.     
  90.     /**
  91.      * @var double $childrenPopulateTS 
  92.      */
  93.     protected $childrenPopulateTS = 0;
  94.     
  95.     /**
  96.      * product table
  97.      *
  98.      * @var string $myProductTable 
  99.      */
  100.     protected $myProductTable = "T_PRO_PRODUCT";
  101.     
  102.     /**
  103.      * @var double $productPopulateTS 
  104.      */
  105.     protected $productPopulateTS = 0;
  106.     
  107.     // ---------------------------------------------------------------------------
  108.     // factory / construct
  109.     // ---------------------------------------------------------------------------
  110.     
  111.     /**
  112.      * factory a product group
  113.      *
  114.      * @param int $aId 
  115.      * 
  116.      * @return PRO_GROUP 
  117.      */
  118.     public static function factoryFromId($aId)
  119.     {        
  120.         if (intval($aId&& ($arr_row OCSP_OBJ::defaultReadDBObj()->getRow('T_PRO_GROUP',array('PGP_ID'=>intval($aId)))))        
  121.         {
  122.             return self::factoryFromDBRow($arr_row);
  123.         else {
  124.             $obj_ret Null;
  125.             return $obj_ret;
  126.         }        
  127.     }
  128.     
  129.     /**
  130.      * factory a product group from an db row
  131.      *
  132.      * @param array $aRow 
  133.      * @return PRO_GROUP 
  134.      */
  135.     public static function factoryFromDBRow($aRow)
  136.     {
  137.         return PRO_GROUP_TYPE::getInstance(intval($aRow['PGT_ID']))->factoryProGroupFromRow($aRow);        
  138.     }
  139.     
  140.     /**
  141.      * returns a product group in $self::loadedNodes
  142.      *
  143.      * @param int $aId 
  144.      * 
  145.      * @return PRO_GROUP 
  146.      */
  147.     public static function &getInstance($aId)
  148.     {
  149.         if (!isset(self::$loadedNodes[$aId]|| !pcf_is_instance_of(self::$loadedNodes[$aId],'PRO_GROUP'))
  150.         {
  151.             if (!(self::$loadedNodes[$aIdself::factoryFromId($aId)))
  152.             {
  153.                 $obj_ret new PRO_GROUP();
  154.                 return $obj_ret;
  155.             }            
  156.         }
  157.         return self::$loadedNodes[$aId];
  158.     }
  159.     
  160.     
  161.     // ---------------------------------------------------------------------------
  162.     // getter / setter
  163.     // ---------------------------------------------------------------------------    
  164.  
  165.     /**
  166.      * returns the pro group id
  167.      *
  168.      * @return int 
  169.      */
  170.     public function getId()
  171.     {
  172.         return $this->getDBField('PGP_ID');
  173.     }
  174.         
  175.     /**
  176.      * sets the product group id
  177.      * 
  178.      * @param int $aId 
  179.      */
  180.     public function setId($aId)
  181.     {
  182.         $this->setDBField('PGP_ID',intval($aId));
  183.     }
  184.  
  185.     /**
  186.      * returns the parent Id
  187.      *
  188.      * @return int 
  189.      */
  190.     public function getParentId()
  191.     {
  192.         return intval($this->getDBField('PGP_PARENT'));
  193.     }
  194.     
  195.     /**
  196.      * sets the parent Id
  197.      *
  198.      * @param int $aId 
  199.      */
  200.     public function setParentId($aId)
  201.     {
  202.         $this->setDBField('PGP_PARENT',intval($aId));
  203.     }
  204.     
  205.     /**
  206.      * returns the sort order number
  207.      *
  208.      * @return int 
  209.      */
  210.     public function getOrderNr()
  211.     {
  212.         return intval($this->getDBField('PGP_SORTORDER'));
  213.     }
  214.     
  215.     /**
  216.      * sets the order number
  217.      *
  218.      * @param int $aNbr 
  219.      */
  220.     public function setOrderNr($aNbr)
  221.     {
  222.         $this->setDBField('PGP_SORTORDER',intval($aNbr));
  223.     }
  224.     
  225.     
  226.     /**
  227.      * returns the title if set otherwise the name
  228.      *
  229.      * @param int $langId 
  230.      * @param boolean $debug 
  231.      * 
  232.      * @return string 
  233.      */
  234.     public function getTitle($langId=0,$debug=False)
  235.     {
  236.         if ($str_title $this->getDBField('PGP_TITLE'))
  237.         {
  238.             return $str_title;
  239.         else {
  240.             return $this->getDBField('PGP_NAME');
  241.         }
  242.     }
  243.     
  244.     // -------------------------------------------------------
  245.     // db save methods (insert / replace (update) / delete)
  246.     // -------------------------------------------------------
  247.     
  248.     /**
  249.       * inserts a new row to the table
  250.       *
  251.       * @param boolean   $debug  show debug info
  252.       *
  253.       * @returns int   returns the autoIncFld ID if exists or 1 on success
  254.       *
  255.       */
  256.     public function dbInsert($debug=False
  257.     {
  258.         if ($debugechoDebugMethod(__FILE__,get_class($this),"PRO_GROUP::dbInsert()");
  259.         
  260.         if (parent::dbInsert())
  261.         {
  262.             self::$loadedNodes[$this->getId()$this;
  263.             return $this->getId();    
  264.         else {
  265.             return False;
  266.         }
  267.         
  268.     }
  269.  
  270.     // -------------------------------------------------------
  271.     // tree methods
  272.     // -------------------------------------------------------
  273.  
  274.     public function getRootPath($debug=False)
  275.     {
  276.         if ($this->getParentId())
  277.         {
  278.             $arr_ret PRO_GROUP::getInstance($this->getParentId())->getRootPath();
  279.             $arr_ret[$this->getId();    
  280.         else {
  281.             $arr_ret array($this->getId());
  282.         }
  283.         return $arr_ret;
  284.     }
  285.     
  286.     /**
  287.      * returns if the product group has children
  288.      *
  289.      * @param boolean $debug 
  290.      * 
  291.      * @return boolean 
  292.      */
  293.     public function hasChildren($debug=False)
  294.     {
  295.         if ($this->childrenPopulateTS < (time(ini_get('max_execution_time')))
  296.         {
  297.             $this->populateChildren($debug);            
  298.         }
  299.         
  300.         return (sizeof($this->myChildrenTrue False);
  301.         
  302.     }
  303.     
  304.     /**
  305.      * populates $this->myChildren
  306.      *
  307.      * @param boolean $debug 
  308.      */
  309.     protected function populateChildren($debug=False)
  310.     {
  311.         if ($debugechoDebugMethod(__FILE__,get_class($this),"PRO_GROUP::populateChildren");
  312.         
  313.         if ($arr_children OCSP_OBJ::defaultReadDBObj()->getArray($this->myTable,array('PGP_PARENT' => intval($this->getId())),0,0,'PGP_SORTORDER'))
  314.         {
  315.             $this->myChildren = array();
  316.             foreach($arr_children as $arr_row)
  317.             {
  318.                 $int_sort (intval($arr_row['PGP_SORTORDER']intval($arr_row['PGP_SORTORDER']1);
  319.                 while(isset($this->myChildren[$int_sort])) $int_sort++;
  320.                 
  321.                 if (isset(self::$loadedNodes[$arr_row['PGP_ID']]))
  322.                 {
  323.                     $this->myChildren[$int_sort&self::$loadedNodes[$arr_row['PGP_ID']];
  324.                     $this->myChildren[$int_sort]->setDBField('PGP_SORTORDER',$int_sort);
  325.                 else {
  326.                     self::$loadedNodes[$arr_row['PGP_ID']] =  PRO_GROUP_TYPE::factoryFromId(intval($arr_row['PGT_ID']))->factoryProGroupFromRow($arr_row);
  327.                     $this->myChildren[$int_sort&self::$loadedNodes[$arr_row['PGP_ID']];
  328.                     $this->myChildren[$int_sort]->setDBField('PGP_SORTORDER',$int_sort);
  329.                 }
  330.                 
  331.             }
  332.         }
  333.         
  334.         if ($debugechoDebugLine(__FILE__,__LINE__,sizeof($this->myChildren" children populated");
  335.         
  336.         $this->childrenPopulateTS = time();        
  337.     }
  338.  
  339.     /**
  340.      * returns an array with the children
  341.      *
  342.      * @param boolean $debug 
  343.      * 
  344.      * @return array 
  345.      */
  346.     public function getChildren($debug=False)
  347.     {
  348.         if ($debugechoDebugMethod(__FILE__,get_class($this),"PRO_GROUP::getChildren(());");
  349.  
  350.         if ($this->childrenPopulateTS < (time(ini_get('max_execution_time')))
  351.         {
  352.             $this->populateChildren($debug);            
  353.         }        
  354.         
  355.         return $this->myChildren;
  356.     }
  357.     
  358.     // -------------------------------------------------------
  359.     // product methods
  360.     // -------------------------------------------------------
  361.     
  362.     /**
  363.      * populates the products and sets $this->productPopulateTS
  364.      *
  365.      * @param boolean $debug 
  366.      */
  367.     public function populateProducts($debug=False)    
  368.     {       
  369.         if ($debugechoDebugMethod(__FILE__,get_class($this),"PRO_GROUP::populateProducts()");
  370.  
  371.         $this->myProducts = array();
  372.         if ($arr_products OCSP_OBJ::defaultReadDBObj()->getArray($this->myProductTable,array('PGP_ID' => $this->getId()),0,0,'PRO_SORTORDER'))
  373.         {
  374.             foreach($arr_products as $arr_row)
  375.             {
  376.                 //if ($debug) echoDebugLine(__FILE__,__LINE__,"<pre>" . print_r($arr_row,True) . "</pre>");
  377.                 if (PRO_PRODUCT::isInstanced($arr_row['PRO_ID']))
  378.                 {
  379.                     $this->myProducts[$arr_row['PRO_ID']] PRO_PRODUCT::getInstance($arr_row['PRO_ID']);
  380.                 else {
  381.                     $this->myProducts[$arr_row['PRO_ID']] PRO_PROD_TYPE::getInstance(intval($arr_row['PRT_ID']),$debug)->factoryProductFromRow($arr_row);
  382.                     PRO_PRODUCT::setInstance($this->myProducts[$arr_row['PRO_ID']],$debug);
  383.                 }
  384.             }
  385.         }
  386.         
  387.         $this->productPopulateTS = time();        
  388.     }
  389.     
  390.     /**
  391.      * returns an array of all products in the product group
  392.      * and all child groups
  393.      *
  394.      * NOTE: this method uses the T_PRO_PRODGROUP table
  395.      *       take care this table is up to date
  396.      * 
  397.      * @param boolean $debug 
  398.      * 
  399.      * @return array of
  400.      */
  401.     public function getAllChildProducts($debug=False)
  402.     {
  403.         if ($debugechoDebugMethod(__FILE__,get_class($this),"PRO_GROUP::getAllChildProducts");
  404.         
  405.         $this->populateProducts($debug);
  406.         $arr_ret $this->myProducts;
  407.         
  408.         $str_query "SELECT * FROM T_PRO_PRODUCT ";
  409.         $str_query.= " WHERE PRO_ID IN (";
  410.         $str_query.= "     SELECT PRO_ID FROM T_PRO_PRODGROUP ";
  411.         $str_query.= "     WHERE PRO_ID = " $this->getId();
  412.         $str_query.= "     AND PRO_PGP_ID <> " $this->getId();
  413.         $str_query.= " )";
  414.         
  415.         if ($obj_cursor OCSP_OBJ::defaultReadDBObj()->query($str_query))
  416.         {
  417.             while ($arr_row $obj_cursor->fetchArrayFld())
  418.             {
  419.                 $arr_ret[$arr_row['PRO_ID']] PRO_PROD_TYPE::getInstance(intval($arr_row['PRT_ID']))->factoryProductFromRow($arr_row);
  420.             }
  421.         }
  422.         
  423.         return $arr_ret;
  424.     }
  425.     
  426.     /**
  427.      * returns a product
  428.      *
  429.      * @param int $aProId 
  430.      * @param boolean $debug 
  431.      * 
  432.      * @return PRO_PRODUCT 
  433.      */
  434.     public function &getProduct($aProId,$debug=False)
  435.     {
  436.         if ($debugechoDebugMethod(__FILE__,get_class($this),"PRO_GROUP::getProduct({$aProId})");        
  437.         
  438.         $aProId intval($aProId);
  439.         if (isset($this->myProducts[$aProId]))
  440.         {
  441.             return $this->myProducts[$aProId];
  442.         }
  443.         if ($this->productPopulateTS > (time(ini_get('max_execution_time')))
  444.         {
  445.             $boj_ret Null;
  446.             return $obj_ret;
  447.         }
  448.         
  449.         if ($arr_row OCSP_OBJ::defaultReadDBObj()->getRow($this->myProductTable,array('PRO_ID' => intval($aProId),'PGP_ID' => $this->getId())))
  450.         {
  451.             if ($debugechoDebugLine(__FILE__,__LINE__,"Product: {$arr_row['PRO_ID']} found with type {$arr_row['PRT_ID']}");
  452.             if ($obj_type PRO_PROD_TYPE::getInstance(intval($arr_row['PRT_ID'])))
  453.             {
  454.                 if ($debugechoDebugLine(__FILE__,__LINE__,"type class: " get_class($obj_type));
  455.                 $this->myProducts[$arr_row['PRO_ID']] $obj_type->factoryProductFromRow($arr_row);
  456.                 return $this->myProducts[$arr_row['PRO_ID']];
  457.             else {
  458.                 if ($debugechoDebugLine(__FILE__,__LINE__,"No Type for: <pre>" print_r($arr_row,True"</pre>");
  459.                 $obj_ret Null;
  460.                 return Null;
  461.             }
  462.         else {
  463.             $boj_ret Null;
  464.             return $obj_ret;
  465.            }        
  466.     }
  467.     
  468.     /**
  469.      * returns an array with all products in the group
  470.      *
  471.      * @param boolean $debug 
  472.      * 
  473.      * @return array 
  474.      */
  475.     public function getProductList($debug=False)
  476.     {
  477.         if ($this->productPopulateTS < (time(ini_get('max_execution_time')))
  478.         {
  479.             $this->populateProducts($debug);
  480.         }       
  481.         return $this->myProducts;
  482.     }
  483.     
  484.     /**
  485.      * adds a product
  486.      *
  487.      * @param PRO_PRODUCT $aProduct 
  488.      */
  489.     public function addProduct(&$aProduct)
  490.     {
  491.         $aProduct->setProGrpId($this->getId());
  492.         $this->myProducts[$aProduct->getId()$aProduct;        
  493.     }
  494.     
  495.     // ---------------------------------------------------------------------------
  496.     // online state / rights
  497.     // ---------------------------------------------------------------------------
  498.     
  499.     /**
  500.      * returns if a product is online or not
  501.      *
  502.      * @param boolean $checkProducts (check if we have an online part)
  503.      * @param boolean $checkParts (force to check product parts)
  504.      * @param boolean $debug 
  505.      */
  506.     public function isOnline($checkProducts=True,$checkParts=True,$debug=False)
  507.     {        
  508.         if ($debugechoDebugMethod(__FILE__,get_class($this),"PRO_GROUPS::isOnline(" ($checkProducts "CHECKPRODUCTS" """," ($checkParts "CHECKPARTS" """)");
  509.         
  510.         if (!intval($this->getDBField('PGP_SHOW')))
  511.         {
  512.             return False;
  513.         }
  514.         
  515.           pcf_require_class('OCSP_DATE','common/');
  516.           $obj_date new OCSP_DATE();
  517.           $obj_date->setDbString($this->getDBField('PGP_SHOWFROM'));
  518.           if ($obj_date->isInFutur())
  519.           {
  520.               if ($debugechoDebugLine(__FILE__,__LINE__,"product group will come in future");
  521.               return False;
  522.           }
  523.           $obj_date->setDbString($this->getDBField('PGP_SHOWTILL'));
  524.           if ($obj_date->isInPast())
  525.           {
  526.               if ($debugechoDebugLine(__FILE__,__LINE__,"product group is out of date");
  527.               return False;
  528.           }        
  529.           
  530.           if ($checkProducts
  531.           {
  532.               if ($debugechoDebugLine(__FILE__,__LINE__,"checking products");
  533.             if ($arr_products $this->getProductList($debug))
  534.             {
  535.                 foreach($arr_products as &$obj_product)
  536.                 {
  537.                     if ($obj_product->isOnline($checkParts,$debug))
  538.                     {
  539.                         return True;
  540.                     }
  541.                 
  542.                 return False;
  543.             else if ($arr_children $this->getChildren()) {
  544.                 foreach($arr_children as &$obj_child)
  545.                 {
  546.                     if ($obj_child->isOnline($checkProducts,$checkParts,$debug))
  547.                     {
  548.                         return True;
  549.                     }
  550.                 }
  551.             else {
  552.                 return False;
  553.             }
  554.           else {          
  555.               return True;
  556.           }
  557.     }
  558.            
  559.     /**
  560.       * returns if the current user can open the menu
  561.       * 
  562.       * NOTE: does not dates or PGP_SHOW
  563.       *
  564.       * @param boolean $debug 
  565.       *
  566.       * @return boolean 
  567.       *
  568.       */
  569.     function curUserCanOpen($debug=False)
  570.     {
  571.         if (intval($int_showGroup $this->getDBField('PGP_GROUP')))
  572.         {
  573.             return OCSP_OBJ::currentUser()->isGroupMember($int_showGroup);
  574.         else {
  575.             return True;
  576.         }
  577.     }
  578.     
  579.     /**
  580.       * returns if the current user can add a childmenu
  581.       *
  582.       * @param boolean $debug 
  583.       *
  584.       * @return boolean 
  585.       *
  586.       */
  587.     function curUserCanAddChild($debug=False)
  588.     {
  589.         return OCSP_OBJ::currentUser()->isGroupMember(_OCSP_GROUP_PRODUCTS_);    
  590.     }
  591.     
  592.     /**
  593.       * returns if the current user can add a page
  594.       *
  595.       * @param boolean $debug 
  596.       *
  597.       * @return boolean 
  598.       *
  599.       */
  600.     function curUserCanAddProduct($debug=False)
  601.     {
  602.         return OCSP_OBJ::currentUser()->isGroupMember(_OCSP_GROUP_PRODUCTS_);          
  603.     }
  604.  
  605.     /**
  606.       * returns if the current user can delete the menu
  607.       *
  608.       * @param boolean $debug 
  609.       *
  610.       * @return boolean 
  611.       *
  612.       */
  613.     function curUserCanDelete($debug=False)
  614.     {
  615.         return OCSP_OBJ::currentUser()->isGroupMember(_OCSP_GROUP_PRODUCTS_);                  
  616.     }
  617.  
  618.     /**
  619.       * returns if the current user can edit the menu
  620.       *
  621.       * @param boolean $debug 
  622.       *
  623.       * @return boolean 
  624.       *
  625.       * @since pk-06-11-06
  626.       *
  627.       ***/
  628.     function curUserCanEdit($debug=False)
  629.     {
  630.         return OCSP_OBJ::currentUser()->isGroupMember(_OCSP_GROUP_PRODUCTS_);                  
  631.     }
  632.     
  633. }
  634.  
  635. ?>

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