Source for file ADDRESS.phpclass

Documentation is available at ADDRESS.phpclass

  1. <?php
  2. /**
  3.   * Class file ADDRESS.phpclass
  4.   *
  5.   * @project    Open CSP-Management
  6.   * @package    client
  7.   *
  8.   * @author     Peter Krebs <pitlinz@users.sourceforge.net>
  9.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  10.   *
  11.   * @version pk-03-12-23
  12.   * @version $Id: ADDRESS.phpclass,v 1.13 2008/11/30 13:35:30 pitlinz Exp $
  13.   */
  14.  
  15. if (!class_exists('DBMS_TABLEOBJ'))
  16. {
  17.     require_once __OCSP_PHPINCPATH__."db" _OCSP_DIRSEP_ "DBMS_TABLEOBJ.phpclass";    
  18. }
  19.  
  20. if (!class_exists('CLIENT'))
  21. {
  22.     require_once dirname(__FILE__)._OCSP_DIRSEP_."CLIENT.phpclass";
  23. }
  24.  
  25.  
  26. /**
  27.   * Class file ADDRESS.phpclass
  28.   *
  29.   * @project    Open CSP-Management
  30.   * @package    client
  31.   *
  32.   * @author     Peter Krebs <p.krebs@lvus.at>
  33.   * @copyright  (c) 2002-2003 by Peter Krebs and Landesverlag Unternehmensservice
  34.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  35.   *
  36.   * @version $Id: ADDRESS.phpclass,v 1.13 2008/11/30 13:35:30 pitlinz Exp $
  37.   * 
  38.   */
  39. class ADDRESS extends DBMS_TABLEOBJ 
  40. {
  41.     
  42.     /*** class constants  --------------------------------------------- */
  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.     /**
  56.      * unit of work address objects
  57.      *
  58.      * @var $addrObjLst array
  59.      */    
  60.     protected static $addrObjLst array();
  61.     
  62.     /*** class methods --------------------------------------------------- */
  63.         
  64.     /**
  65.      * returns an instance of the unit of work
  66.      *
  67.      * @param int $aId 
  68.      * 
  69.      * @return ADDRESS 
  70.      */
  71.     public static function &getInstance($aId)
  72.     {
  73.         if (!isset(self::$addrObjLst[$aId]|| pcf_is_instance_of(self::$addrObjLst[$aId],'ADDRESS'))
  74.         {
  75.             self::$addrObjLst[$aIdADDRESS::factoryFromId(intval($aId));
  76.         }
  77.         
  78.         return self::$addrObjLst[$aId];
  79.     }    
  80.     
  81.     /**
  82.       * @staticvar string $myTable name of the db table
  83.       */
  84.     var $myTable="T_ADDRESS";
  85.  
  86.     /**
  87.       * @staticvar string $myClassSrcFile if is set the DBMS_FORM get the class object
  88.       * @since pk-06-10-02
  89.       ***/
  90.     var $myClassSrcFile=__FILE__;
  91.  
  92.     /**
  93.       * @var CLIENT myClient
  94.       ***/
  95.     var $myClient=NULL;
  96.  
  97.     /**
  98.       * init the object and populate if an id is submitted
  99.       *
  100.       * @param int $cliId CLI_ID
  101.       * @param int $adrId 
  102.       * @param string $gDBIdx 
  103.       * @param bool $debug 
  104.       *
  105.       ***/
  106.     function ADDRESS($cliId=0,$adrId=0,$debug=FALSE
  107.     {
  108.         if ($debugechoDebugMethod(__FILE__,get_class($this),"ADDRESS::ADDRESS($cliId,$adrId)");
  109.                        
  110.         $this->init($debug);
  111.         if (($adrId&& ($cliId)) {
  112.             $this->setId($adrId);
  113.             $this->setCliId($cliId);
  114.             $this->dbPopulate($debug);
  115.         else {
  116.             $this->setDefaults();
  117.             $this->setCliId($cliId);
  118.         }
  119.     }
  120.     
  121.     
  122.     /**
  123.      * factory an address and sets dbRow
  124.      *
  125.      * @param array $row 
  126.      * @param boolean $debug 
  127.      * 
  128.      * @return boolean 
  129.      * 
  130.      * @since pk-08-03-07
  131.      * @version pk-08-07-31
  132.      * 
  133.      */
  134.     public static function factoryFromRow($row,$debug=false)
  135.     {
  136.         if (isset($row['ADR_ID']&& intval($row['ADR_ID']))
  137.         {
  138.             $obj_ret new ADDRESS();
  139.             $obj_ret->setDBRow($row);
  140.             return $obj_ret;
  141.         }
  142.         
  143.         $int_cliId (isset($row['CLI_ID']intval($row['CLI_ID']0);
  144.         return new ADDRESS($int_cliId,0,$debug);
  145.     }
  146.     
  147.     /**
  148.      * factory an address from the id
  149.      *
  150.      * @param int $aId 
  151.      * @param boolean $debug 
  152.      * 
  153.      * @return ADDRESS 
  154.      * 
  155.      * @since pk-08-03-07
  156.      * 
  157.      * @todo change calls to getInstance()
  158.      */
  159.     public static function &factoryFromId($aId,$debug=false)
  160.     {
  161.         if ($debugechoDebugMethod(__FILE__,"static","ADDRESS::factoryFromId()");
  162.         
  163.         if (isset(self::$addrObjLst[$aId]))
  164.         {
  165.             return self::$addrObjLst[$aId];
  166.         }
  167.         
  168.         $obj_ret new ADDRESS();
  169.         $obj_ret->setId($aId);
  170.         
  171.         if (intval($aId))
  172.         {
  173.             $obj_ret->dbPopulate($debug);
  174.                self::$addrObjLst[$aId$obj_ret;
  175.                return self::$addrObjLst[$aId];          
  176.         }
  177.         return $obj_ret;
  178.     }
  179.  
  180.  
  181.     /**
  182.       * sets column value
  183.       *
  184.       * replaces
  185.       *     CLI_NAME1 -> ADR_CO
  186.       *     CLI_NAME2 -> ADR_CO2
  187.       *
  188.       * @param string $col 
  189.       * @param mixed $val 
  190.       *
  191.       * @since pk-05-03-03
  192.       *
  193.       ***/
  194.     function setDBField($col,$val{
  195.         switch($col{
  196.             case "CLI_NAME1":
  197.                 return parent::setDBField("ADR_CO",$val);
  198.             case "CLI_NAME2":
  199.                 return parent::setDBField("ADR_CO2",$val);
  200.             default:
  201.                 return parent::setDBField($col,$val);
  202.         }
  203.     }
  204.  
  205.     /**
  206.       * returns the id
  207.       * @return int 
  208.       ***/
  209.     function getId({
  210.         return intval($this->getDBField('ADR_ID'));
  211.     }
  212.  
  213.     /**
  214.       * sets the id
  215.       * 
  216.       * @param int $aId 
  217.       * 
  218.       * @version pk-08-03-07
  219.       *  
  220.       */
  221.     function setId($aId{
  222.         $this->setDBField('ADR_ID',intval($aId));
  223.         self::$addrObjLst[$aId$this;
  224.     }
  225.  
  226.  
  227.     /**
  228.       * returns the Client ID
  229.       *
  230.       * @return int 
  231.       ***/
  232.     function getCliId({
  233.         return intval($this->getDBField('CLI_ID'));
  234.     }
  235.  
  236.     /**
  237.       * sets the client ID
  238.       * 
  239.       * if the new value differs from $this->myClient->getId()
  240.       * $this->myClient is set to NULL
  241.       *
  242.       * @param int $aId 
  243.       * 
  244.       * @return boolean 
  245.       * 
  246.       * @version pk-08-02-11
  247.       ***/
  248.     function setCliId($aId{
  249.         if (intval($aId)) {
  250.             $this->setDBField('CLI_ID',intval($aId));
  251.             if (is_object($this->myClient)) {
  252.                 if ($this->myClient->getId(!= $this->getCliId()) {
  253.                     $this->myClient=NULL;
  254.                 }
  255.             }
  256.             return TRUE;
  257.         else {
  258.             return FALSE;
  259.         }
  260.     }
  261.  
  262.  
  263.     /**
  264.      * returns if the address is the primary address
  265.      * 
  266.      * @return boolean 
  267.      * 
  268.      * @since pk-08-05-16
  269.      */
  270.     public function isPrimary()
  271.     {
  272.         return (intval($this->getDBField('ADR_ISPRIMARY')) True False);
  273.     }
  274.  
  275.     /**
  276.       * sets this to the clients primary address
  277.       *
  278.       * @param bool $saveClient 
  279.       *
  280.       ***/
  281.     function setPrimary($saveClient=FALSE,$debug=FALSE{
  282.         if ($debugecho "<p><b>ADDRESS::setPrimary($saveClient,$debug)</b> (".get_class($this).")</p>";
  283.         $this->setDBField("ADR_ISPRIMARY",1);
  284.         if (($saveClient&& ($this->getId()) && ($this->getClient($debug))) {     // make sure $this->myClient is populated
  285.             $this->myClient->setPriAddrId($this->getId());
  286.             $this->myClient->dbSave($this->gDBIDX,$debug);
  287.         }
  288.     }
  289.  
  290.  
  291.     /**
  292.       * returns the client object
  293.       * 
  294.       * if $this->myClient is not an object a new one is created
  295.       *
  296.       * @param bool $debug 
  297.       * 
  298.       * @return CLIENT 
  299.       *
  300.       * @version pk-07-10-10
  301.       * @version pk-08-02-11
  302.       * @version pk-08-11-29
  303.       */
  304.     function &getClient($debug=False{
  305.         if ($debugechoDebugMethod(__FILE__,get_class($this),"ADDRESS::getClient()");
  306.         
  307.         if (pcf_class_implements($this->myClient,"INFA_CLIENT"&& ($this->myClient->getId(== $this->getCliId()))
  308.         {
  309.             return $this->myClient;
  310.         }
  311.         
  312.         $this->myClient = CLIENT::getInstance($this->getCliId());        
  313.         return $this->myClient;
  314.     }
  315.  
  316.     /**
  317.      * sets the client object
  318.      *
  319.      * @param CLIENT $cliObj 
  320.      * @param boolean $debug 
  321.      *
  322.      * @return boolean 
  323.      *
  324.      * @since pk-05-11-01
  325.      * @version pk-08-02-11
  326.      * 
  327.      */
  328.     function setClient(&$cliObj,$debug=FALSE{
  329.         if ($debugechoDebugMethod(__FILE__,get_class($this),"ADDRESS::setClient()");
  330.         if (pcf_is_instance_of($cliObj,'CLIENT')) 
  331.         {
  332.             $this->myClient = $cliObj;            
  333.             return $this->setCliId($cliObj->getId());
  334.         else {
  335.             return FALSE;
  336.         }
  337.     }
  338.     
  339.     /**
  340.      * populates the address object and adds to self::$addrObjLst
  341.      *
  342.      * @param boolean $debug 
  343.      * 
  344.      * @return boolean 
  345.      * 
  346.      * @since pk-08-03-07
  347.      * 
  348.      */
  349.     function dbPopulate($debug=false
  350.     {
  351.         if ($debugechoDebugMethod(__FILE__,get_class($this),"ADDRESS::dbPopulate()");
  352.         
  353.         if (parent::dbPopulate($debug))
  354.         {
  355.             if ($this->getId())
  356.             {
  357.                 self::$addrObjLst[$this->getId()$this;
  358.             }
  359.             return true;
  360.         }
  361.         return false;
  362.     }
  363.  
  364.     /**
  365.       * save the Object
  366.       *
  367.       * if ADR_ISPRIMARY is set:
  368.       * - update client
  369.       * - make sure no other address is primary for the client
  370.       *
  371.       * @param bool   $debug  SHOW DEBUG INFO
  372.       *
  373.       * @return mixed 
  374.       *
  375.       * @version pk-08-02-11
  376.       * 
  377.       * @deprecated since pk-08-05-16
  378.       */
  379.     function dbSave($debug=False
  380.     {
  381.         return parent::dbSave($debug);
  382.         /*
  383.         if ($debug) echoDebugMethod(__FILE__,get_class($this),"ADDRESS::dbSave($debug)");
  384.                       
  385.         if (!$this->getCliId()) 
  386.         {
  387.             throw new Exception(_OCSP_EXCEP_NOID_ . ": CLI_ID IS NOT SET FOR ADDRESS"); 
  388.         }
  389.         
  390.         if (!$this->isConnected(True))
  391.         {
  392.             throw new Exectption(_OCSP_)
  393.         }
  394.         
  395.         if ($ret=parent::dbSave($debug)) {          // store the return value it could be the index of a new row
  396.             if ($this->isPrimary()) {               // otherwise the primary address has to check that it's the only one
  397.                 if ($this->getClient($debug)) {     // make sure it is populated
  398.                     $cmd ="UPDATE ".$this->myTable." SET ADR_ISPRIMARY=0 WHERE CLI_ID=".$this->myClient->getId();
  399.                     $cmd.=" AND ADR_ID <> ".$this->getId();
  400.                     $cmd.=" AND ADR_ISPRIMARY > 0 ";    // only update other primary addresses (timestamp :-)
  401.                     if ($debug) echoDebugLine(__FILE__,__LINE__,"Query: <p>$query</p>");
  402.                     $this->myDBObj->executeCmd($cmd);
  403.  
  404.                     if ($this->myClient->getPriAddrId() != $this->getId()) {
  405.                         if (!$this->myClient->getPopulateTs()) {
  406.                             if (!$this->myClient->dbPopulate($debug)) {
  407.                                 die("SYSTEM ERROR COULD NOT GET OWNER OF ADDRESS");
  408.                             }
  409.                         }
  410.                         $this->myClient->setPriAddrId($this->getId());
  411.                         $this->myClient->dbSave($debug);
  412.                     }
  413.                 }
  414.             }
  415.             return $ret;
  416.         } else {
  417.             return FALSE;
  418.         }
  419.         */
  420.     }
  421.  
  422.     /**
  423.       * inserts a new row to the table
  424.       *
  425.       * @param bool   $debug  show debug info
  426.       *
  427.       * @global array $OCSP_OBJ 
  428.       *
  429.       * @returns int   returns the autoIncFld ID if exists or 1 on success
  430.       *
  431.       * @since pk-05-11-30
  432.       *
  433.       ***/
  434.     function dbInsert($debug=FALSE
  435.     {
  436.         if ($debugechoDebugMethod(__FILE__,get_class($this),"ADDRESS::dbInsert($debug)");
  437.         
  438.         if (!$this->isConnected(True))
  439.         {
  440.             throw new Exception(_OCSP_EXCEP_NODBCONN_);
  441.         }
  442.         
  443.         $this->ADR_CREATION ="CURRENT_TIMESTAMP";
  444.         if (is_object($OCSP_OBJ['USER'])) {
  445.             $this->ADR_CREATOR    =OCSP_OBJ::currentUser()->getId()// <pk-05-11-03 />
  446.             $this->ADR_CHANGEDBY  =OCSP_OBJ::currentUser()->getId();
  447.         }
  448.         
  449.         if ($this->isPrimary())
  450.         {
  451.             $str_cmd "UPDATE " $this->myTable . " SET ADR_ISPRIMARY=0";
  452.             $str_cmd.= " WHERE CLI_ID=" $this->getCliId();
  453.             $this->myDBObj->executeCmd($str_cmd);
  454.         else {
  455.             $str_query "SELECT COUNT(*) FROM " $this->myTable . " WHERE CLI_ID=" .$this->getCliId();
  456.             $int_count=$this->myDBObj->getOne($str_query);
  457.             if (!intval($int_count))
  458.             {
  459.                 $this->setDBField('ADR_ISPRIMARY',1);
  460.             else {
  461.                 $this->setDBField('ADR_ISPRIMARY',0);
  462.             }
  463.         }
  464.         
  465.         if($int_ret parent::dbInsert($debug))
  466.         {
  467.             if ($this->isPrimary())
  468.             {
  469.                 $this->setPrimary(True);                
  470.             }
  471.         }
  472.         
  473.         return $int_ret;
  474.     }
  475.  
  476.     /**
  477.       * replaces a row in the table
  478.       *
  479.       * @param boolean $debug  show debug info
  480.       * @param boolean $ignorPopulated (if false the table MUST have been populated)
  481.       *
  482.       * @global array $OCSP_OBJ 
  483.       *
  484.       * @returns boolean
  485.       *
  486.       * @since pk-05-11-30
  487.       * @version pk-07-10-05
  488.       *
  489.       ***/
  490.     function dbReplace($debug=FALSE,$ignorPopulated=FALSE)
  491.     {
  492.         if ($debugechoDebugMethod(__FILE__,get_class($this),"ADDRESS::dbReplace($debug)");
  493.  
  494.         if (is_object(OCSP_OBJ::currentUser())) {
  495.             $this->ADR_CHANGEDBY  =OCSP_OBJ::currentUser()->getId();
  496.         }
  497.         
  498.         $this->ADR_LASTCHANGE ="CURRENT_TIMESTAMP";
  499.         if ($this->isPrimary())
  500.         {
  501.               $str_cmd "UPDATE " $this->myTable . " SET ADR_ISPRIMARY=0";
  502.             $str_cmd.= " WHERE CLI_ID=" $this->getCliId();
  503.             $this->myDBObj->executeCmd($str_cmd);          
  504.             $this->setPrimary(True);
  505.         }
  506.         return parent::dbReplace($debug,$ignorPopulated);
  507.     }
  508.  
  509.     /**
  510.      * returns an array of default values
  511.      *
  512.      * @param boolean $debug 
  513.      * 
  514.      * @return array 
  515.      * 
  516.      * @since pk-08-02-11
  517.      * 
  518.      */
  519.     function getDefaults($debug=FALSE)
  520.     {
  521.         if ($debugechoDebugMethod(__FILE__,get_class($this),"ADDRESS::getDefaults()");
  522.         
  523.         $arr_ret parent::getDefaults();
  524.         if ($int_cliId $this->getCliId())
  525.         {
  526.             $arr_ret['CLI_ID'$this->getCliId();
  527.         }
  528.         
  529.         return $arr_ret;
  530.     }
  531.  
  532.     /**
  533.       * tries to get the form from the client type
  534.       *
  535.       * @param int $mode 
  536.       * @param mixed $form (string) form name (int) form id
  537.       * @param boolean $debug 
  538.       *
  539.       * @return OCSP_FORM 
  540.       *
  541.       *  __OCSP_PHPINCPATH__
  542.       * @requires db/forms/OCSP_FORM.phpclass
  543.       *
  544.       * @since pk-08-11-29
  545.       */
  546.     function &getForm($mode=FRM_MODE_READONLY,$form='',$frmClass='OCSP_FORM',$debug=False)
  547.     {
  548.         if (empty($form|| !intval($form))
  549.         {
  550.             if ($this->getClient(&& ($obj_cliType $this->myClient->getTypeObj()))
  551.             {
  552.                 $form $obj_cliType->getAddressFormId();
  553.             }
  554.         }
  555.         return parent::getForm($mode,$form,$frmClass,$debug);
  556.     }
  557. }
  558. ?>

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