Source for file PRO_PRODUCT.phpclass

Documentation is available at PRO_PRODUCT.phpclass

  1. <?php
  2. /**
  3.   * openCSP class file PRO_PRODUCT.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-18.07.2008
  12.   * @version $Id: PRO_PRODUCT.phpclass,v 1.5 2008/08/16 09:21:08 pitlinz Exp $
  13.   */
  14.  
  15.     // ---------------------------------------------------------
  16.     // requirements
  17.     // ---------------------------------------------------------
  18.  
  19.     pcf_require_interface('INFA_PRO_PRODUCT',dirname(__FILE__"/");
  20.  
  21.     pcf_require_class('DBMS_TABLEOBJ',"db/");
  22.     pcf_require_class('PRO_PROD_TYPE',dirname(__FILE__"/");
  23.     pcf_require_class('PRO_GROUP',dirname(__FILE__"/");
  24.     pcf_require_class('PRO_PART',dirname(__FILE__"/");    
  25.  
  26. /**
  27.   * openCSP class PRO_PRODUCT
  28.   *
  29.   * @project Open CSP-Management
  30.   * @package shop
  31.   *
  32.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  33.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  34.   *
  35.   * @since pk-18.07.2008
  36.   * @version $Id: PRO_PRODUCT.phpclass,v 1.5 2008/08/16 09:21:08 pitlinz Exp $
  37.   */
  38. class PRO_PRODUCT extends DBMS_TABLEOBJ implements INFA_PRO_PRODUCT
  39. {
  40.     // ---------------------------------------------------------------------------
  41.     // constants
  42.     // ---------------------------------------------------------------------------
  43.     
  44.     /**
  45.      * @constant string CLASS_SRC_FILE
  46.      */
  47.     const CLASS_SRC_FILE = __FILE__;
  48.  
  49.     // ---------------------------------------------------------------------------
  50.     // class (static)
  51.     // ---------------------------------------------------------------------------
  52.     
  53.     /*** class vars ------------------------------------------------------ */
  54.     
  55.     protected static $loadedProducts array()
  56.         
  57.     /*** class methods --------------------------------------------------- */
  58.     
  59.     /**
  60.      * returns an instance of a product stored in self::$loadedProducts
  61.      *
  62.      * @param int $aId 
  63.      * @param boolean $debug 
  64.      * 
  65.      * @return PRO_PRODUCT 
  66.      */
  67.     public static function &getInstance($aId,$debug=False)
  68.     {
  69.         if ($debugechoDebugMethod(__FILE__,"static","PRO_PRODUCT::getInstance({$aId})");
  70.         
  71.         if ($aId)
  72.         {
  73.             if (!isset(self::$loadedProducts[intval($aId)]))
  74.             {
  75.                 self::$loadedProducts[intval($aId)self::factoryFromId($aId,$debug);                
  76.             }
  77.             return self::$loadedProducts[intval($aId)];
  78.         
  79.     }
  80.     
  81.     public static function setInstance(&$aProduct,$debug=False)
  82.     {
  83.         if ($debugechoDebugMethod(__FILE__,"static","PRO_PRODUCT::setInstance()");
  84.         
  85.         if (method_exists($aProduct,'getId'))
  86.         {
  87.             self::$loadedProducts[$aProduct->getId()$aProduct;
  88.         }
  89.     }
  90.     
  91.     /**
  92.      * returns if the product has been instanced
  93.      *
  94.      * @param unknown_type $aId 
  95.      * @return unknown 
  96.      */
  97.     public static function isInstanced($aId)
  98.     {
  99.         return isset(self::$loadedProducts[intval($aId)]);
  100.     }
  101.     
  102.     
  103.     // ---------------------------------------------------------------------------
  104.     // object vars
  105.     // ---------------------------------------------------------------------------
  106.     
  107.     /*** compostion --------------------------------------------------- */
  108.         
  109.     /**
  110.      * array of parts
  111.      *
  112.      * @var array $myParts 
  113.      */
  114.     protected $myParts = array();
  115.     
  116.     /*** attributes  -------------------------------------------------- */
  117.     
  118.     /**
  119.       * @var string $myTable 
  120.       */
  121.     protected $myTable="T_PRO_PRODUCT";        
  122.     
  123.     /**
  124.      * populate timestamp of the parts
  125.      *
  126.      * @var double $partsPopulateTS 
  127.      */
  128.     protected $partsPopulateTS = 0;
  129.     
  130.     // ---------------------------------------------------------------------------
  131.     // factory / construct
  132.     // ---------------------------------------------------------------------------
  133.     
  134.     /**
  135.      * factories a new product
  136.      *
  137.      * @param int $aId 
  138.      * @param boolean $debug 
  139.      * 
  140.      * @return PRO_PRODUCT 
  141.      */
  142.     public static function &factoryFromId($aId,$debug=False)
  143.     {
  144.         if ($debugechoDebugMethod(__FILE__,"static","PRO_PRODUCT::factoryFromId({$aId})");
  145.         
  146.         if ($arr_row OCSP_OBJ::defaultReadDBObj()->getRow('T_PRO_PRODUCT',array('PRO_ID'=>$aId)))
  147.         {
  148.             $obj_ret PRO_PROD_TYPE::getInstance($arr_row['PRT_ID'])->factoryProductFromRow($arr_row,$debug);
  149.         else {
  150.             $obj_ret Null;
  151.         }
  152.         return $obj_ret;
  153.     }
  154.     
  155.     
  156.     // ---------------------------------------------------------------------------
  157.     // getter / setter
  158.     // ---------------------------------------------------------------------------    
  159.  
  160.     public function getName()
  161.     {
  162.         return $this->getDBField('PRO_NAME');
  163.     }
  164.     
  165.     public function getNumber()
  166.     {
  167.         return $this->getDBField('PRO_NUMBER');
  168.     }    
  169.     
  170.     /**
  171.      * returns the product id
  172.      *
  173.      * @return int 
  174.      */
  175.     public function getId()
  176.     {
  177.         return $this->getDBField('PRO_ID');
  178.     }
  179.     
  180.     /**
  181.      * sets the product id
  182.      *
  183.      * @param int $aId 
  184.      */
  185.     public function setId($aId)
  186.     {
  187.         $this->setDBField('PRO_ID',intval($aId));
  188.     }
  189.     
  190.     /**
  191.      * returns the sort order number
  192.      *
  193.      * @return int 
  194.      */
  195.     public function getOrderNr()
  196.     {
  197.         return intval($this->getDBField('PRO_SORTORDER'));
  198.     }
  199.     
  200.     /**
  201.      * sets the sort order number
  202.      *
  203.      * @param int $aNbr 
  204.      */
  205.     public function setOrderNr($aNbr)
  206.     {
  207.         $this->setDBField('PRO_SORTODER',intval($aNbr));
  208.     }
  209.     
  210.     /**
  211.      * returns $this->getDBField(PRO_PRICE) or the lowest price of the parts
  212.      *
  213.      * @param boolean $debug 
  214.      * 
  215.      * @return float 
  216.      */
  217.     public function getOnlinePrice($debug=False)
  218.     {
  219.         if ($flo_price floatval($this->getDBField('PRO_PRICE')))
  220.         {
  221.             return $flo_price;
  222.         }
  223.         
  224.         $flo_price 0.0;
  225.         if ($arr_parts $this->getPartList($debug))
  226.         {
  227.             foreach($arr_parts as &$obj_part)
  228.             {
  229.                 if ($obj_part->isOnline())
  230.                 {
  231.                     if (($obj_part->getPrice($flo_price|| ($flo_price == 0.0))
  232.                     {
  233.                         $flo_price $obj_part->getPrice();
  234.                     }
  235.                 }
  236.             }
  237.         }
  238.         return $flo_price;
  239.     }
  240.     
  241.     // -------------------------------------------------------
  242.     // product group
  243.     // -------------------------------------------------------
  244.     
  245.     /**
  246.      * returns the product group id
  247.      *
  248.      * @return int 
  249.      */
  250.     public function getProGroupId()
  251.     {
  252.         return intval($this->getDBField('PGP_ID'));
  253.     }
  254.     
  255.     /**
  256.      * retruns the product group
  257.      *
  258.      * @return PRO_GROUP 
  259.      */
  260.     public function getProGroup()
  261.     {
  262.            return PRO_GROUP::getInstance($this->getProGroupId());
  263.     }
  264.     
  265.     // -------------------------------------------------------
  266.     // db save methods (insert / replace (update) / delete)
  267.     // -------------------------------------------------------
  268.  
  269.     public function dbSave($debug=False)
  270.     {
  271.         if ($debugechoDebugMethod(__FILE__,get_class($this),"PRO_PRODUCT::dbSave()");
  272.         if ($this->getId())
  273.         {
  274.             OCSP_OBJ::defaultWriteDBObj()->executeCmd('DELETE FROM T_PRO_PRODGROUP WHERE PRO_ID=' $this->getId());
  275.         }
  276.          
  277.         if ($int_ret parent::dbSave($debug))
  278.         {
  279.             if (intval($this->getDBField('PRO_SHOW')))
  280.             {
  281.                 if ($arr_rootpath $this->getProGroup()->getRootPath())
  282.                 {
  283.                     $arr_prodgroupRow array(
  284.                         'PRO_ID'             => $this->getId(),
  285.                         'PRO_SORTORDER'     => intval($this->getOrderNr()),
  286.                         'PRO_INGROUPFROM'    => $this->getDBField('PRO_SHOWFROM'),
  287.                         'PRO_INGROUPTILL'    => $this->getDBField('PRO_SHOWTILL'),
  288.                         'PRO_PGP_ID'        => intval($this->getProGroupId())
  289.                     );
  290.                 }
  291.                 
  292.                 foreach($arr_rootpath as $int_pgpId)
  293.                 {
  294.                     $arr_prodgroupRow['PGP_ID'intval($int_pgpId);
  295.                     OCSP_OBJ::defaultWriteDBObj()->insertArray('T_PRO_PRODGROUP',$arr_prodgroupRow);
  296.                 }
  297.             }
  298.         }         
  299.     }
  300.     
  301.     /**
  302.       * deletes the row from the database
  303.       *
  304.       * @param boolean $backupData 
  305.       * @param boolean $debug 
  306.       *
  307.       * @global array $OCSP_CONF 
  308.       *
  309.       * @returns boolean
  310.       *
  311.       */
  312.     function dbDelete($backupData=False,$debug=False
  313.     {    
  314.         if ($this->getId())
  315.         {
  316.             OCSP_OBJ::defaultWriteDBObj()->executeCmd('DELETE FROM T_PRO_PRODGROUP WHERE PRO_ID=' $this->getId());
  317.         }        
  318.         return parent::debDelete($backupData,$debug);
  319.     }
  320.     
  321.     // ---------------------------------------------------------------------------
  322.     // DB Values
  323.     // ---------------------------------------------------------------------------
  324.         
  325.     /**
  326.      * returns the template values for a list
  327.      *
  328.      * @param boolean $debug 
  329.      * 
  330.      * @return array; 
  331.      */
  332.     public function getListTemplateValues($debug=False)
  333.     {
  334.         return $this->getDBVal();
  335.     }    
  336.  
  337.     // ---------------------------------------------------------------------------
  338.     // parts
  339.     // ---------------------------------------------------------------------------
  340.     
  341.     protected function populateParts($debug=False)
  342.     {
  343.         if ($debugechoDebugMethod(__FILE__,get_class($this),"PRO_PRODUCT::populateParts()");
  344.         
  345.         $sthis->myParts array();
  346.         if ($arr_parts OCSP_OBJ::defaultReadDBObj()->getArray('T_PRO_PART',array('PRO_ID'=>$this->getId()),0,0,'PAR_SORTORDER'))
  347.         {
  348.             foreach($arr_parts as $arr_row)
  349.             {
  350.                 if (PRO_PART::isInstanced(intval($arr_row['PAR_ID'])))
  351.                 {
  352.                     $this->myParts[intval($arr_row['PAR_ID'])PRO_PART::getInstance(intval($arr_row['PAR_ID']))
  353.                 else {
  354.                     $this->myParts[intval($arr_row['PAR_ID'])PRO_PART_TYPE::getInstance(intval($arr_row['PAT_ID']))->factoryPartFromRow($arr_row);
  355.                     PRO_PART::setInstance($this->myParts[intval($arr_row['PAR_ID'])],$debug);
  356.                 }
  357.             }
  358.         }
  359.         $this->partsPopulateTS = time();
  360.     }
  361.     
  362.     /**
  363.      * returns an array with all parts of a product
  364.      *
  365.      * @param boolean $debug 
  366.      * 
  367.      * @return array 
  368.      */
  369.     public function getPartList($debug=False)
  370.     {
  371.         if ($this->partsPopulateTS < (time(ini_get('max_execution_time')))
  372.         {
  373.             $this->populateParts($debug);
  374.         }       
  375.         return $this->myParts;
  376.     }    
  377.     
  378.     // ---------------------------------------------------------------------------
  379.     // online state / rights
  380.     // ---------------------------------------------------------------------------
  381.     
  382.     /**
  383.      * returns if a product is online or not
  384.      *
  385.      * @param boolean $checkParts (check if we have an online part)
  386.      * @param boolean $debug 
  387.      * 
  388.      * @return boolean 
  389.      * 
  390.      */
  391.     public function isOnline($checkParts=True,$debug=False)
  392.     {        
  393.         if ($debugechoDebugMethod(__FILE__,get_class($this),"PRO_PRODUCT::isOnline(" ($checkParts "CHECKPARTS" """)");
  394.         
  395.         if (!intval($this->getDBField('PRO_SHOW')))
  396.         {
  397.             return False;
  398.         }
  399.         
  400.           pcf_require_class('OCSP_DATE','common/');
  401.           $obj_date new OCSP_DATE();
  402.           $obj_date->setDbString($this->getDBField('PRO_SHOWFROM'));
  403.           if ($obj_date->isInFutur())
  404.           {
  405.               if ($debugechoDebugLine(__FILE__,__LINE__,"product will come in future");
  406.               return False;
  407.           }
  408.           $obj_date->setDbString($this->getDBField('PRO_SHOWTILL'));
  409.           if ($obj_date->isInPast())
  410.           {
  411.               if ($debugechoDebugLine(__FILE__,__LINE__,"product is out of date");
  412.               return False;
  413.           }        
  414.           
  415.           if ($checkParts
  416.           {
  417.               if ($arr_parts $this->getPartList($debug))
  418.               {
  419.                   foreach($arr_parts as $obj_part)
  420.                   {
  421.                       if ($obj_part->isOnline()) return True;
  422.                   }
  423.                   return False;
  424.               }
  425.           else {          
  426.               return True;
  427.           }
  428.     }
  429.     
  430.     
  431.     /**
  432.      * returns if the product is in a campain
  433.      * 
  434.      * @return boolean 
  435.      */
  436.     public function isCampained($debug=False)
  437.     {
  438.         if ($debugechoDebugMethod(__FILE__,get_class($this),"PRO_PRODUCT::isCampained() [ID: "$this->getId("]");
  439.         if (intval($this->getDBField('PRO_CAMPAINED')) 0)
  440.         {
  441.             return True;
  442.         }
  443.         
  444.         if ($arr_parts $this->getPartList($debug))
  445.         {
  446.             foreach($arr_parts as &$obj_part)
  447.             {
  448.                 if ($obj_part->isCampained())
  449.                 {
  450.                     return True;
  451.                 }
  452.             }
  453.         }
  454.         return False;
  455.     }    
  456.     
  457.     
  458. }
  459.  
  460. ?>

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