Source for file CLIENT.phpclass

Documentation is available at CLIENT.phpclass

  1. <?php
  2. /**
  3.   * Class file CLIENT.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 $Id: CLIENT.phpclass,v 1.24 2008/11/30 13:35:30 pitlinz Exp $
  12.   */
  13.  
  14.     // ---------------------------------------------------------
  15.     // requirements
  16.     // ---------------------------------------------------------
  17.     
  18.     require_once __OCSP_DEFAULTCONFPATH__."client.conf.phpinc";
  19.  
  20.     pcf_require_interface('INFA_CLIENT',dirname(__FILE__"/");
  21.     
  22.     pcf_require_class('DBMS_TABLEOBJ',"db/");
  23.     pcf_require_class('ADDRESS',dirname(__FILE__"/");
  24.     pcf_require_class('CLI_STAFF',dirname(__FILE__"/");
  25.     pcf_require_class('CLI_TYPE',dirname(__FILE__"/");
  26.     pcf_require_class('CLI_STATE',dirname(__FILE__"/");
  27.     pcf_require_class('CLI_FORM',dirname(__FILE__"/");
  28.  
  29.     
  30. /**
  31.   * Class CLIENT
  32.   *
  33.   * @project    Open CSP-Management
  34.   * @package    client
  35.   *
  36.   * @author     Peter Krebs <pitlinz@users.sourceforge.net>
  37.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  38.   *
  39.   * @version pk-04-12-02
  40.   * @version pk-05-10-25
  41.   * @version $Id: CLIENT.phpclass,v 1.24 2008/11/30 13:35:30 pitlinz Exp $
  42.   */
  43. class CLIENT extends DBMS_TABLEOBJ implements INFA_CLIENT 
  44. {
  45.  
  46.     // ---------------------------------------------------------------------------
  47.     // constants
  48.     // ---------------------------------------------------------------------------
  49.     
  50.     /**
  51.       * @constant string CLASS_SRC_FILE
  52.       */
  53.     const CLASS_SRC_FILE = __FILE__;
  54.     
  55.     // ---------------------------------------------------------------------------
  56.     // class (static)
  57.     // ---------------------------------------------------------------------------
  58.     
  59.     /*** class vars ------------------------------------------------------ */
  60.         
  61.     /**
  62.      * unit of work client objects
  63.      *
  64.      * @staticvar array $clientLst 
  65.      */
  66.     static protected $clientLst = array();
  67.     
  68.     /*** class methods --------------------------------------------------- */
  69.     
  70.     /**
  71.      * returns an unit of work client object
  72.      *
  73.      * @param int $aId 
  74.      * 
  75.      * @return CLIENT 
  76.      */
  77.     public static function &getInstance($aId)
  78.     {
  79.         if (!($aId intval($aId)))
  80.         {
  81.             $obj_ret Null;
  82.             return $obj_ret
  83.         }
  84.         
  85.         if (!isset(self::$clientLst[$aId]|| !is_object(self::$clientLst[$aId]))
  86.         {
  87.             self::$clientLst[$aIdself::factoryFromId($aId);
  88.         }
  89.         
  90.         return self::$clientLst[$aId];
  91.     }
  92.     
  93.     /**
  94.      * returns if a client with id $aId has already been instanced
  95.      * 
  96.      * @param int $aId 
  97.      * 
  98.      * @return boolean 
  99.      */
  100.     public static function isInstanced($aId)
  101.     {
  102.         if (isset(self::$clientLst[$aId]&& pcf_class_implements($aClient,'INFA_CLIENT'))
  103.         {
  104.             return True;
  105.         else {
  106.             return False;
  107.         }
  108.     }
  109.     
  110.     /**
  111.      * sets a client instance in the unit of work
  112.      * 
  113.      * @param CLIENT $aClient 
  114.      */
  115.     public static function setInstance(&$aClient$debug=False)
  116.     {
  117.         if (pcf_class_implements($aClient,'INFA_CLIENT'))
  118.         {
  119.             self::$clientLst[$aClient->getId()$aClient;
  120.         }
  121.     }
  122.     
  123.     // ---------------------------------------------------------------------------
  124.     // object vars
  125.     // ---------------------------------------------------------------------------
  126.     
  127.     /*** compostion --------------------------------------------------- */
  128.  
  129.     /**
  130.       * @var ADDRESS $myAddress 
  131.       ***/
  132.     protected $myAddress=Null;
  133.    
  134.     /**
  135.       * @var CLI_TYPE $myTypeObj 
  136.       * @since pk-06-10-02
  137.       ***/
  138.     protected $myTypeObj=NULL;    
  139.  
  140.     /*** attributes  -------------------------------------------------- */
  141.     
  142.     
  143.     /**
  144.       * @var string $myTable name of the db table
  145.       */
  146.     protected $myTable="T_CLI_CLIENT";
  147.  
  148.     /**
  149.       * @var string $myClassSrcFile if is set the DBMS_FORM get the class object
  150.       * @deprecated since pk-07-10-18
  151.       ***/
  152.     protected $myClassSrcFile=__FILE__;
  153.  
  154.     /**
  155.       * @var string $staffClass 
  156.       * @since pk-05-12-06
  157.       ***/
  158.     protected $staffClass="CLI_STAFF";
  159.  
  160.     /**
  161.       * @var string $myUserClass class name of the user object
  162.       * @since pk-06-03-20
  163.       ***/
  164.     protected $myUserClass="USER";
  165.     
  166.     // --------------------------------------------
  167.     // construct and factory
  168.     // --------------------------------------------
  169.     
  170.     /**
  171.       * init the object and populate if an id is submitted
  172.       *
  173.       * @param int $aId CLI_ID
  174.       * @param string $gDBIdx   database index in $GLOBALS
  175.       * @param bool $debug 
  176.       *
  177.       ***/
  178.     function CLIENT($aId=0,$debug=False
  179.     {
  180.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::CLIENT($aId)");
  181.         $this->init($debug);
  182.         if (intval($aId)) {
  183.             $this->db_SetKey('CLI_ID',$aId);
  184.             $this->dbPopulate($debug);            
  185.         else {
  186.             $this->setDefaults();
  187.         }
  188.     }
  189.     
  190.     /**
  191.      * factory a client object
  192.      *
  193.      * @param int $aId 
  194.      * @param boolean $debug 
  195.      * 
  196.      * @return CLIENT 
  197.      * 
  198.      * @since pk-08-02-03
  199.      * 
  200.      */
  201.     public static function factoryFromId($aId,$debug=False)
  202.     {
  203.         if ($debugechoDebugMethod(__FILE__,"static","CLIENT::factoryFormId()");
  204.         
  205.         if ($arr_cliRow OCSP_OBJ::defaultReadDBObj()->getRow('T_CLI_CLIENT',array('CLI_ID' => intval($aId))))
  206.         {
  207.             return self::factoryFromDBRow($arr_cliRow,$debug);
  208.         else {
  209.             $obj_ret new CLIENT();
  210.             $obj_ret->setId(intval($aId));
  211.             return $obj_ret;
  212.         }        
  213.     }
  214.     
  215.     
  216.     /**
  217.      * factory a client object from a client database row
  218.      *
  219.      * @param array $cliRow 
  220.      * @param boolean $debug 
  221.      * 
  222.      * @return client 
  223.      * 
  224.      * @since pk-08-02-03
  225.      */
  226.     public static function factoryFromDBRow($cliRow,$debug=False)
  227.     {
  228.         if ($debugechoDebugMethod(__FILE__,"static","CLIENT::factoryFromDBRow();");
  229.  
  230.         if (is_array($cliRow)) {
  231.             $o_cliType=CLI_TYPE::getInstance(intval($cliRow['CTY_ID']));            
  232.             return $o_cliType->getCliObjectFromArray($cliRow,$debug);
  233.         else {
  234.             return new CLIENT();
  235.         }
  236.         
  237.     }
  238.     
  239.     // --------------------------------------------
  240.     // getter and setter
  241.     // --------------------------------------------
  242.         
  243.     /**
  244.       * returns the client Id
  245.       * @return int 
  246.       * @since pk-03-12-23
  247.       ***/
  248.     function getId({
  249.         return intval($this->getDBField('CLI_ID'));
  250.     }
  251.  
  252.     /**
  253.       * @param int $aId 
  254.       * @since pk-05-09-20
  255.       * @version pk-06-07-21 bugfix
  256.       ***/
  257.     function setId($aId{
  258.         $this->setDBFieldIfDiff('CLI_ID',intval($aId));
  259.     }
  260.  
  261.     /**
  262.       * @return int 
  263.       ***/
  264.     function getTypeId({
  265.         return intval($this->getDBField('CTY_ID'));
  266.     }
  267.  
  268.     /**
  269.       * @param $aTypeId 
  270.       ***/
  271.     function setTypeId($aTypeId{
  272.         $this->setDBFieldIfDiff('CTY_ID',intval($aTypeId));
  273.     }
  274.  
  275.     /**
  276.       * gets the clients type object
  277.       *
  278.       * @return CLI_TYPE 
  279.       * @since pk-06-10-02
  280.       *
  281.       ***/
  282.     function &getTypeObj({
  283.         if ((pcf_is_instance_of($this->myTypeObj,'CLI_TYPE')) && ($this->myTypeObj->getId()==$this->getTypeId())) {
  284.             return $this->myTypeObj;
  285.         else {
  286.             $this->myTypeObj=CLI_TYPE::getInstance($this->getTypeId());
  287.             return $this->myTypeObj;
  288.         }
  289.     }
  290.  
  291.     /**
  292.       * sets the clients type object
  293.       *
  294.       * @param CLI_TYPE $aType 
  295.       * @since pk-06-10-02
  296.       *
  297.       ***/
  298.     function setTypeObj(&$aType{
  299.         if (pcf_is_instance_of($aType,'CLI_TYPE')) {
  300.             $this->myTypeObj=$aType;
  301.             $this->setTypeId($aType->getId());
  302.         }
  303.     }
  304.  
  305.     /**
  306.      * returns the state id
  307.      *
  308.      * @return int 
  309.      * 
  310.      * @since pk-08-02-11
  311.      */
  312.     function getStateId(
  313.     {
  314.         return intval($this->getDBField('CLS_ID'));
  315.     }
  316.     
  317.     /**
  318.      * retruns the state object
  319.      *
  320.      * @return CLI_STATE 
  321.      */
  322.     public function getStateObj()
  323.     {
  324.         return CLI_STATE::getInstance($this->getStateId());
  325.     }
  326.     
  327.     /**
  328.      * marks a client as deleted
  329.      *
  330.      * @return boolean (userCanEdit)
  331.      */
  332.     public function markDeleted()
  333.     {
  334.         if ($this->userCanEdit())
  335.         {
  336.             $this->setDBField('CLI_STATE',-1);
  337.             return True;
  338.         }
  339.         return False;
  340.     }
  341.     
  342.     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  343.  
  344.     /**
  345.       * returns the client name (CLI_NAME1 + CLI_NAME2)
  346.       *
  347.       * @return string 
  348.       * 
  349.       * @since pk-04-08-31
  350.       * @version pk-07-08-22
  351.       ***/
  352.     function getName({
  353.         return trim($this->getDBField('CLI_NAME1')." ".$this->getDBField('CLI_NAME2'));   /* <pk-04-12-02 /> */
  354.     }
  355.  
  356.     /**
  357.       * returns the client number or the ID if no number isset
  358.       *
  359.       * @returns string
  360.       * @since pk-06-10-11
  361.       *
  362.       ***/
  363.     function getNumber({
  364.         $s_ret=$this->getDBField('CLI_NUMBER');
  365.         if (empty($s_ret)) $s_ret="ID:".$this->getId();
  366.         return $s_ret;
  367.     }
  368.  
  369.     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  370.     /*      form methods                                                            */
  371.     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  372.  
  373.     /**
  374.       * returns the admin form
  375.       *
  376.       * @param boolean $frmMode 
  377.       * @param boolean $debug 
  378.       *
  379.       * @returns DBMS_FORM
  380.       *
  381.       * @since pk-05-10-26
  382.       * @version pk-06-10-25 use $this->myTypeObj->getAdminForm()
  383.       *
  384.       ***/
  385.     function &dbGetAdminForm($frmMode,$debug=False
  386.     {
  387.         if ($debugecho "<p><b>CLIENT::dbGetAdminForm(...)</b> (".get_class($this).")</p>";
  388.  
  389.         $this->getTypeObj()// ensure $this->myTypeObj is set propper;
  390.         $obj_ret $this->myTypeObj->getAdminForm($frmMode,$debug);
  391.         $obj_ret->setMyTblObj($this);
  392.         return $obj_ret;
  393.     }
  394.  
  395.     /**
  396.       * returns the admin form
  397.       *
  398.       * @param boolean $frmMode 
  399.       * @param boolean $debug 
  400.       *
  401.       * @returns DBMS_FORM
  402.       *
  403.       * @since pk-05-10-26
  404.       * @version pk-06-10-25 use $this->myTypeObj->getAdminForm()
  405.       *
  406.       ***/
  407.     function &dbGetUserForm($frmMode,$debug=False
  408.     {
  409.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::getUserForm($frmMode)");
  410.  
  411.         $this->getTypeObj()// ensure $this->myTypeObj is set propper;
  412.         $obj_ret $this->myTypeObj->getUserForm($frmMode,$debug);
  413.         $obj_ret->setMyTblObj($this);
  414.         return $obj_ret;
  415.         
  416.     }
  417.  
  418.     /**
  419.       * returns a database form
  420.       *
  421.       * @param int $mode        dbms form show mode
  422.       * @param string $frmName  dbms form name
  423.       * @param boolean $debug   since pk-05-01-11
  424.       * @param boolean $autonew generate a default form if no form is found
  425.       *
  426.       * @returns DBMS_FORM
  427.       *
  428.       * @since pk-07-01-15
  429.       *
  430.       * @deprecated since pk-07-10-03
  431.       *
  432.       ***/
  433.     function &dbGetForm($mode,$frmName="",$debug=False,$autoNew=False)  {
  434.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::dbGetForm()","Name$frmName");
  435.  
  436.         return $this->getForm($mode,$frmName,'CLI_FORM',$debug);
  437.     }
  438.  
  439.     /**
  440.       * replaces dbGetForm and returns a OCSP_FORM
  441.       *
  442.       * @param int $mode 
  443.       * @param mixed $form (string) form name (int) form id
  444.       * @param boolean $debug 
  445.       *
  446.       * @return OCSP_FORM 
  447.       *
  448.       * @requires CLI_FORM.phpclass
  449.       *
  450.       * @since pk-07-10-03
  451.       *
  452.       */
  453.     function &getForm($mode=FRM_MODE_READONLY,$form='',$frmClass='CLI_FORM',$debug=False)
  454.     {
  455.         if (empty($frmClass)) $frmClass='CLI_FORM';
  456.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::getForm($mode,$form,$frmClass)");
  457.         if (!empty($form))
  458.         {
  459.             return parent::getForm($mode,$form,$frmClass,$debug);
  460.         }
  461.  
  462.         if ($this->userIsAdmin(&& ($o_ret=$this->dbGetAdminForm($mode,$debug))) {
  463.             return $o_ret;
  464.         }
  465.  
  466.         return $this->dbGetUserForm($mode,$debug);
  467.  
  468.     }
  469.  
  470.     /**
  471.       * returns the the data array of a join
  472.       *
  473.       * @param array $tables 
  474.       * @param array $filter 
  475.       * @param boolean $withObjVars 
  476.       * @param boolean $debug 
  477.       *
  478.       * @returns array
  479.       *
  480.       * @since pk-05-10-25
  481.       * @version pk-07-08-27
  482.       *
  483.       ***/
  484.     function dbGetDataArray($tables,$filter=NULL,$withObjVars=TRUE,$debug=False{
  485.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::dbGetDataArray()",print_r($table,TRUE));
  486.  
  487.         $a_ret=$this->getDBVal(False,$debug,$withObjVars);
  488.         if (!$this->isConnected(True,$debug))
  489.         {
  490.             ocsp_logError(__FILE__,__LINE__,"no database connection",E_ERROR);
  491.             return $a_ret;
  492.         }
  493.  
  494.         if (is_array($tables)) {
  495.             foreach($tables as $tbl{
  496.                 if ($debugechoDebugLine(__FILE__,__LINE__,"<h3>getting data for: ".$tbl."</h3>\n");
  497.                 switch($tbl{
  498.                     case "T_CLI_CLIENT":
  499.                         // do nothing
  500.                         break;
  501.                     case "T_ADDRESS":
  502.                         if ($o_addr=$this->getDefaultAddress($debug)) {
  503.                             $a_ret=array_merge($o_addr->getDBVal(),$a_ret);
  504.                         }
  505.                     default:
  506.                         $s_query="SELECT * FROM ".$tbl." WHERE CLI_ID=".$this->getId();
  507.                         if ($debugecho "<p>$s_query</p>\n";
  508.                         if ($a_tmp=$this->myDBObj->quickQuery($s_query)) {
  509.                             $a_ret=array_merge($a_tmp,$a_ret);
  510.                         }
  511.                 }
  512.                 if ($debugecho "</blockquote>";
  513.             }
  514.         }
  515.         return $a_ret;
  516.     }
  517.  
  518.  
  519.     /**
  520.       * sets post array with form checks
  521.       *
  522.       * @param DBMS_FORM $frmObj a formObj as ref
  523.       * @param array $dbVal array with data
  524.       * @param bool $debug 
  525.       * @param bool $asPopulate 
  526.       *
  527.       * @returns bool
  528.       *
  529.       * @since pk-05-10-26
  530.       * @version pk-05-12-05
  531.       * @version pk-07-01-10
  532.       * @version pk-07-02-16 ensure we have a type
  533.       *
  534.       ***/
  535.     function setFORMPOST(&$frmObj,$dbval,$debug=False,$asPopulate=TRUE{
  536.         if ($debugecho "<p><b>CLIENT::setFORMPOST(...)</b> (".get_class($this).")</p><blockquote style=\"font-size: 75%\">\n";
  537.  
  538.         // <pk-07-02-16>
  539.         if (intval($dbval['CTY_ID'])) {
  540.             $this->setTypeId(intval($dbval['CTY_ID']));
  541.         }
  542.  
  543.         if (!intval($this->getTypeId()) && isset($_GET['CTY_ID'])) {
  544.             $this->setTypeId(intval($_GET['CTY_ID']));
  545.         }
  546.         // </pk-07-02-16>
  547.  
  548.  
  549.         $a_frmData=$frmObj->getTableValueArrays($dbval,$debug);
  550.  
  551.         if ($frmObj->hasErrors()) {
  552.             if ($debug{echo "<p style=\"color:red\"><b>FORM ".$frmObj->getName()." HAS ERRORS:</b></p><pre style=\"font-size:10px;\">";print_r($valArray);echo "</pre>";$frmObj->writeErros();echo "</blockquote>";}
  553.             return False;
  554.         }
  555.  
  556.         $this->setDBRow($a_frmData[$this->myTable],TRUE,$debug);
  557.         $this->setObjValArray($a_frmData['OBJVALS_'.$this->myTable],$debug)// <pk-07-01-10 /> bugfix in array index
  558.  
  559.         $a_editablePFStates=array();
  560.         foreach($GLOBALS['OCSP_CLI']['PROFILESTATE'as $i_statId => $a_state{
  561.             if (isset($a_state['EDITABLE']&& $a_state['EDITABLE']{
  562.                 $a_editablePFStates[]=$i_statId;
  563.             }
  564.         }
  565.  
  566.         foreach($a_frmData as $s_tbl => $a_values{
  567.             switch($s_tbl{
  568.                 case $this->myTable:
  569.                 case $this->myTable."_OV":
  570.                     // do nothing as already done above
  571.                     break;
  572.                 case "T_ADDRESS":
  573.                     if (!is_object($this->frmPostSubOjects['T_ADDRESS'])) {
  574.                         $this->frmPostSubOjects['T_ADDRESS']=new ADDRESS(0,0,$this->get_gDBIDX(),$debug);
  575.                     }
  576.                     $a_dbval=array_merge($a_values,$this->getDBVal());
  577.                     $this->frmPostSubOjects['T_ADDRESS']->setDBRow($a_dbval,TRUE,$debug);
  578.                     break;
  579.                 case "T_ADDRESS_OV":
  580.                     if (!is_object($this->frmPostSubOjects['T_ADDRESS'])) {
  581.                         $this->frmPostSubOjects['T_ADDRESS']=new ADDRESS(0,0,$this->get_gDBIDX(),$debug);
  582.                     }
  583.                     $this->setObjValArray($a_values,$debug);
  584.                     break;
  585.                 default// <pk-06-10-20>
  586.                     // first check for profile Data
  587.                     $s_query ="SELECT * FROM T_CLI_PROFILES";
  588.                     $s_query.=" WHERE CLP_KEYTABLE='T_CLI_CLIENT' AND CLP_TABLE='".$s_tbl."'";
  589.                     $s_query.=" AND CLP_STATE IN (".implode(',',$a_editablePFStates).")";
  590.                     $s_query.=" AND CTY_ID IN (0,".$this->getTypeId().")";
  591.                     $s_query.=" ORDER BY CLP_SORTORDER";
  592.  
  593.                     if (($a_pfRow=$GLOBALS['USRDB']->quickQuery($s_query)) && (intval($a_pfRow['CLP_ID']))) {
  594.                         if ($debugecho "<h1>Profiledata found</h1><blockquote><hr>";
  595.                         $s_tblKey=$s_tbl."-PF".$a_pfRow['CLP_ID'];
  596.                         require_once dirname(__FILE__)."/CLI_PROFILE_ENTRY.phpclass";
  597.                         $this->frmPostSubOjects[$s_tblKey]=new CLI_PROFILE_ENTRY(intval($a_pfRow['CLP_ID']),$this->get_gDBIDX(),$debug);
  598.                         $a_dbval=array_merge($a_values,$this->getDBVal());
  599.                         $a_dbval['CLP_ID']=$a_pfRow['CLP_ID'];
  600.                         $this->frmPostSubOjects[$s_tblKey]->setDBRow($a_dbval,TRUE,$debug);
  601.                         if ($debugecho "<hr></blockquote>";
  602.                     }
  603.  
  604.             }
  605.         }
  606.         if ($debugecho "</blockquote>";
  607.         return TRUE;
  608.     }
  609.  
  610.     /**
  611.       * adds an object to the frmPostSubOjects
  612.       *
  613.       * @param string $table 
  614.       * @param DBMS_TABLEOBJ $obj 
  615.       *
  616.       * @since pk-06-10-02
  617.       *
  618.       ***/
  619.     function addfrmPostSubObject($table,&$obj{
  620.         if (pcf_is_instance_of($obj,'DBMS_TABLEOBJ')) {
  621.             $this->frmPostSubOjects[$table]=$obj;
  622.         }
  623.     }
  624.  
  625.  
  626.     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  627.     /*      Database methods                                                        */
  628.     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  629.  
  630.     /**
  631.       * inserts a new row to the table
  632.       *
  633.       * @param boolean $debug (show debug info)
  634.       * 
  635.       * @global $OCSP_OBJ 
  636.       *
  637.       * @returns int   returns the autoIncFld ID if exists or 1 on success
  638.       *
  639.       * @since pk-05-08-24
  640.       * @version pk-05-09-15
  641.       * @version pk-05-11-03
  642.       * @version pk-05-11-09 $GLOBALS['CLIENT']['DEFAULT']['CTY_ID'] added
  643.       * @version pk-08-03-06
  644.       *
  645.       */
  646.     function dbInsert($debug=false
  647.     {
  648.         global $OCSP_OBJ;
  649.         
  650.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::dbInsert()");
  651.         
  652.         $this->setDBField('CLI_CREATION',"#NOW");
  653.         if (is_object($OCSP_OBJ['USER'])) {
  654.             $this->CLI_CREATOR  =$OCSP_OBJ['USER']->getId()// <pk-05-11-03 />
  655.             $this->CLI_CHANGEDBY=$OCSP_OBJ['USER']->getId()// <pk-05-11-03 />
  656.         }
  657.         if (!intval($this->CTY_ID)) {
  658.             $this->CTY_ID=intval($GLOBALS['CLIENT']['DEFAULT']['CTY_ID']);
  659.         }
  660.  
  661.         if (isset($this->myLinkedTableRows['T_ADDRESS']))
  662.         {
  663.             $this->myLinkedTableRows['T_ADDRESS']['ADR_ISPRIMARY'1;
  664.         }
  665.         
  666.         return parent::dbInsert($debug);        
  667.         
  668.     }
  669.  
  670.     /**
  671.       * replaces a row in the table
  672.       *
  673.       * if the object has not been populated (!$this->populateTS)
  674.       * False is returned
  675.       *
  676.       * @param string $gDBIDX Index of $GLOBALS to the db object
  677.       * @param boolean $debug  show debug info
  678.       *
  679.       * @returns boolean
  680.       *
  681.       * @since pk-05-11-03
  682.       * @version pk-05-11-25 bugfix return value
  683.       * @version pk-05-11-30
  684.       *
  685.       ***/
  686.     function dbReplace($debug=False,$ignorPopulated=False)  
  687.     {
  688.         if ($debugecho "<hr><p><b>CLIENT::dbReplace($gDBIDX,$debug)</b> (".get_class($this).")</p>";
  689.  
  690.         if (is_object($GLOBALS['USER'])) {
  691.             $this->CLI_CHANGEDBY=$GLOBALS['USER']->getId();
  692.             $this->CLI_LASTCHANGE=NULL;
  693.         }
  694.         if (parent::dbReplace($gDBIDX,$debug)) {
  695.             if (is_array($this->frmPostSubOjects)) {
  696.                 foreach($this->frmPostSubOjects as $s_tbl => $o_obj{
  697.                     // <pk-05-11-30>
  698.                     switch($s_tbl{
  699.                         case "T_ADDRESS":
  700.                             $o_obj->setClient(&$this,$debug);
  701.                             if (($o_obj->getId()==$this->CLI_DEFAULTADDR|| (!intval($this->CLI_DEFAULTADDR))) {
  702.                                 $o_obj->setDBField('ADR_ISPRIMARY',1);
  703.                             }
  704.                             if ($debugecho "<pre>".print_r($o_obj->getDBVal(),TRUE)."</pre>";
  705.                             $o_obj->dbSave($gDBIDX,$debug);
  706.                             if (!intval($this->CLI_DEFAULTADDR)) {
  707.                                 if ($this->CLI_DEFAULTADDR=$o_obj->getId()) {
  708.                                     $s_cmd ="UPDATE ".$this->myTable." SET CLI_DEFAULTADDR=".intval($this->CLI_DEFAULTADDR);
  709.                                     $s_cmd.=" WHERE CLI_ID=".$this->getId();
  710.                                     $GLOBALS[$gDBIDX]->executeCmd($s_cmd);
  711.                                 }
  712.                             }
  713.                             break;
  714.                         default:
  715.                             if (pcf_is_instance_of($o_obj,'DBMS_TABLEOBJ')) // <pk-06-10-08>
  716.                                 if ($debugecho "<p>Saving Table$s_tbl</p><blockquote>";
  717.                                 $o_obj->CLI_ID=$this->getId();
  718.                                 $o_obj->dbSave($gDBIDX,$debug);
  719.                                 if ($debugecho "</blockquote>";
  720.                             }
  721.                     }
  722.                     // </pk-05-11-30>
  723.                 }
  724.             }
  725.             return TRUE// <pk-05-11-25 />
  726.         }
  727.         return False// <pk-05-11-25 />
  728.     }
  729.  
  730.     /**
  731.       * deletes the row from the database
  732.       *
  733.       * @param string $gDBIDX 
  734.       * @param boolean $backupData 
  735.       *
  736.       * @returns boolean
  737.       *
  738.       * @since pk-05-07-06
  739.       * @version pk-05-12-06
  740.       *
  741.       * @todo move functionality from deleteAllData to this method
  742.       *
  743.       ***/
  744.     function dbDelete($backupData=TRUE,$debug=False
  745.     {
  746.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::dbDelete()");
  747.         
  748.         if (!$this->isConnected(TRUE,$debug))
  749.         {
  750.             return false;
  751.         }        
  752.         
  753.         if ($backupData{
  754.             $xmlFile=$GLOBALS['PROJECT']['BACKUPPATH']."client-".$this->getId().".xml";
  755.             $this->xmlExport($xmlFile,false,$debug);
  756.         }
  757.  
  758.         // staff
  759.         // <pk-05-12-06>
  760.         $s_query="SELECT * FROM T_CLI_STAFF WHERE CLI_ID=".$this->getId();
  761.         if ($o_staffCursor=$this->myDBObj->query($s_query)) {
  762.             while($a_staff $o_staffCursor->fetchArrayFld()) {
  763.                 $s_cmd="\$o_staff=new ".$this->staffClass."();";
  764.                 eval($s_cmd);
  765.                 $o_staff->setDBRow($a_staff,TRUE);
  766.                 $o_staff->dbDelete($backupData,$debug);
  767.             }
  768.         }
  769.         // </pk-05-12-06>
  770.  
  771.  
  772.         return $this->deleteAllData($debug);
  773.     }
  774.  
  775.     /**
  776.       * deletes all client data
  777.       *
  778.       * @param boolean $debug 
  779.       *
  780.       * @todo store deleted data to be able to undo
  781.       *
  782.       * @since pk-04-08-24
  783.       *
  784.       * @deprecated since pk-05-12-06
  785.       *
  786.       ***/
  787.     function deleteAllData($debug=False{
  788.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::deleteAllData()");
  789.  
  790.         if (!$this->isConnected(TRUE,$debug))
  791.         {
  792.             return false;
  793.         }
  794.         $query="SELECT CLP_TABLE FROM T_CLI_PROFILES WHERE CLP_KEYTABLE='T_CLI_CLIENT'";
  795.         if ($cursor=$this->myDBObj->query($query)) {
  796.             while($row=$cursor->fetchArrayFld()) {
  797.                 $cmd="DELETE FROM ".$row['CLP_TABLE']." WHERE CLI_ID=".$this->getId();
  798.                 if ($debugecho "<p>$cmd</p>";
  799.                 $this->myDBObj->executeCmd($cmd,$debug);
  800.             }
  801.         }
  802.  
  803.  
  804.  
  805.         // users
  806.         $query="SELECT USR_ID FROM T_CLI_USER WHERE CLI_ID=".$this->getId();
  807.         if ($users=$this->myDBObj->queryArray($query,-1)) {
  808.             foreach($users as $usrId{
  809.                 $query="SELECT CLP_TABLE FROM T_CLI_PROFILES WHERE CLP_KEYTABLE='T_SYS_USER'";
  810.                 if ($cursor=$this->myDBObj->query($query)) {
  811.                     while($row=$cursor->fetchArrayFld()) {
  812.                         $cmd="DELETE FROM ".$row['CLP_TABLE']." WHERE USR_ID=".$usrId;
  813.                         $this->myDBObj->executeCmd($cmd,$debug);
  814.                     }
  815.                 }
  816.  
  817.                 $cmd="DELETE FROM T_SYS_GROUPMEMBER WHERE USR_ID=".$usrId;
  818.                 $this->myDBObj->executeCmd($cmd,$debug);
  819.  
  820.                 $cmd="DELETE FROM T_SYS_USER WHERE USR_ID=".$usrId;
  821.                 $this->myDBObj->executeCmd($cmd,$debug);
  822.             }
  823.         }
  824.  
  825.         $cmd "DELETE FROM T_CLI_USER WHERE CLI_ID=".$this->getId();
  826.         $this->myDBObj->executeCmd($cmd,$debug);
  827.  
  828.         // address
  829.         $cmd "DELETE FROM T_ADDRESS WHERE CLI_ID=".$this->getId();
  830.         $this->myDBObj->executeCmd($cmd,$debug);
  831.  
  832.         // T_CLI_COSTCENTER
  833.         $cmd "DELETE FROM T_CLI_COSTCENTER WHERE CLI_ID=".$this->getId();
  834.         $this->myDBObj->executeCmd($cmd,$debug);
  835.  
  836.         // CLIENT
  837.         $cmd "DELETE FROM T_CLI_CLIENT WHERE CLI_ID=".$this->getId();
  838.         $this->myDBObj->executeCmd($cmd,$debug);
  839.  
  840.         // CLIENT_OV
  841.         $cmd "DELETE FROM T_CLI_CLIENT_OV WHERE CLI_ID=".$this->getId();
  842.         $this->myDBObj->executeCmd($cmd,$debug);
  843.  
  844.         return true;
  845.     }
  846.  
  847.     // ----------------------------------------------------------
  848.     //  linked tables (joins) 
  849.     // ----------------------------------------------------------
  850.     
  851.     /**
  852.      * saves a linked row due to $cmd
  853.      *
  854.      * @param string $table 
  855.      * @param array $row 
  856.      * @param string $cmd (_OCSP_DBCMD_*)
  857.      * @param boolean $debug 
  858.      * 
  859.      * @return boolean 
  860.      * 
  861.      * @since pk-08-03-07
  862.      */
  863.     public function saveLinkedRow($table,$row,$cmd,$debug=false)
  864.     {
  865.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::saveLinkedRow()");
  866.                 
  867.         if (!$this->getId())
  868.         {
  869.             throw new Exception(_OCSP_EXCEP_NOID_);
  870.             return false;
  871.         }
  872.         $row['CLI_ID'$this->getId();
  873.         
  874.         switch($table)
  875.         {
  876.             case "T_ADDRESS":
  877.                 if (!$obj_addr $this->getAddressObj((isset($row['ADR_ID'])) intval($row['ADR_ID']0))
  878.                 {
  879.                     $obj_addr new ADDRESS();
  880.                 }
  881.                 $obj_addr->setDBRow($row,TRUE,$debug);
  882.                 $obj_addr->setCliId($this->getId());
  883.                 switch($cmd)
  884.                 {
  885.                     case _OCSP_DBCMD_INSERT_
  886.                     case _OCSP_DBCMD_REPLACE_:
  887.                     case _OCSP_DBCMD_UPDATE_:
  888.                         return $obj_addr->dbSave($debug);                        
  889.                     case _OCSP_DBCMD_REPLACE_:
  890.                         return $obj_addr->dbDelete(TRUE,$debug);
  891.                     default:
  892.                         return false;
  893.                 }
  894.                 break;
  895.         }
  896.         
  897.         if (isset($row['CLP_ID']))
  898.         {            
  899.             if ($obj_profile $this->getProfileObj($row['CLP_ID']))
  900.             {
  901.                 $int_idx=(isset($row['CLP_IDX']intval($row['CLP_IDX']0);
  902.                 $obj_entry $obj_profile->getEntryObj($this->getId(),$int_idx,$debug);
  903.                 $obj_entry->setDBRow($row,TRUE,$debug);
  904.                 $obj_entry->setDBField('CLI_ID',$this->getId());
  905.                 switch($cmd)
  906.                 {
  907.                     case _OCSP_DBCMD_INSERT_
  908.                     case _OCSP_DBCMD_REPLACE_:
  909.                     case _OCSP_DBCMD_UPDATE_:
  910.                         return $obj_entry->dbSave($debug);
  911.                     case _OCSP_DBCMD_REPLACE_:
  912.                         return $obj_entry->dbDelete(TRUE,$debug);
  913.                     default:
  914.                         return false;
  915.                 }
  916.                 break;                    
  917.             }
  918.         else {            
  919.             return parent::saveLinkedRow($table,$row,$cmd,$debug);
  920.         }
  921.  
  922.         return false;
  923.               
  924.     }    
  925.     
  926.     
  927.     
  928.     /**
  929.       * returns the CLI_ID if a client is found with same addresse and cols filled in
  930.       *
  931.       *
  932.       * @param boolean $debug 
  933.       *
  934.       * @return int 0 if not found otherwise the id of the client
  935.       *
  936.       * @since pk-06-10-02
  937.       *
  938.       ***/
  939.     function chkDuplicate($debug=False{
  940.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::chkDuplicate()");
  941.  
  942.         $found=0;
  943.         $sFrom =" FROM T_CLI_CLIENT c";
  944.         $sWhere=" WHERE 1=1";
  945.         foreach($this->frmPostSubOjects as $s_key => $o_subObj{
  946.             if (is_object($o_subObj)) {
  947.                 if ($debugecho "<p>checking$s_key </p>";
  948.                 switch($s_key{
  949.                     case "T_ADDRESS":
  950.                         $aVals=$o_subObj->getDBVal();
  951.                         unset($aVals['CLI_ID']);
  952.                         unset($aVals['ADR_ISPRIMARY']);
  953.                         unset($aVals['ADR_OK']);
  954.                         unset($aVals['ADR_CREATOR']);
  955.                         unset($aVals['ADR_CHANGEDBY']);
  956.                         unset($aVals['ADR_LASTCHANGE']);
  957.                         unset($aVals['ADR_SEX']);
  958.  
  959.                         $s_query="SELECT ADR_ID FROM T_ADDRESS WHERE 1=1";
  960.                         foreach($aVals as $s_col => $s_val{
  961.                             $s_query.=" AND $s_col=".$GLOBALS['USRDB']->qs_getSlashedValue($s_val);
  962.                         }
  963.                         if ($debugecho "<p>T_ADDRESS$s_query</p>";
  964.                         if ($a_addr=$this->myDBObj->queryArray($s_query)) {
  965.                             if ($debugecho "<pre>".print_r($a_addr,TRUE)."</pre>";
  966.                             $sFrom .=" JOIN T_ADDRESS a USING(CLI_ID)";
  967.                             $sWhere.=" AND a.ADR_ID IN (".implode(',',$a_addr).")";
  968.                         }
  969.  
  970.                         break;
  971.                     default:
  972.                         echo "<p>chkDuplicate Object Key$s_key</p>";
  973.                         echo "<blockquote><pre>".print_r($o_subObj,TRUE)."</pre></blockquote>";
  974.                 }
  975.             }
  976.         }
  977.  
  978.         $aVals=$this->getDBVal();
  979.         if ($debugecho "<pre>".print_r($aVals,TRUE)."</pre>";
  980.         unset($aVals['CTY_ID']);
  981.         unset($aVals['CLI_DEFAULTADDR']);
  982.         unset($aVals['CLI_LASTCHANGE']);
  983.         unset($aVals['CLI_CREATION']);
  984.         unset($aVals['CLI_LASTVISIT']);
  985.         unset($aVals['CLI_NWLMODE']);
  986.         unset($aVals['CLI_DOUBLET']);
  987.         unset($aVals['CLS_ID']);
  988.         unset($aVals['CLI_STATE']);
  989.         unset($aVals['CLI_CREATOR']);
  990.         unset($aVals['CLI_CHANGEDBY']);
  991.         unset($aVals['CLI_AGENTGROUP']);
  992.         unset($aVals['CLI_CONSULTANT']);
  993.         unset($aVals['CLI_FOREIGNKEY']);
  994.  
  995.         foreach($aVals as $s_col => $s_val{
  996.             $sWhere.=" AND $s_col=".$GLOBALS['USRDB']->qs_getSlashedValue($s_val);
  997.         }
  998.  
  999.         $s_query="SELECT c.CLI_ID ".$sFrom.$sWhere;
  1000.         if ($debugecho "<p>".$s_query."</p>";
  1001.  
  1002.         return intval($this->myDBObj->quickQuery($s_query,0));
  1003.  
  1004.     }
  1005.  
  1006.  
  1007.     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1008.     /*      Address methods                                                         */
  1009.     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1010.     
  1011.     
  1012.     /**
  1013.       * returns a dbrow with the primary address
  1014.       *
  1015.       * @param boolean $withoutNull null values are not added to the array
  1016.       * @param boolean $debug 
  1017.       *
  1018.       * @returns array
  1019.       *
  1020.       * @since pk-05-03-04
  1021.       *
  1022.       ***/
  1023.     function getAddressDBRow($withoutNull=TRUE,$debug=False{
  1024.         if ($debugecho "<p><b>CLIENT::getAddressDBRow(...)</b> (".get_class($this).")</p>";
  1025.  
  1026.         if ($dbRow=$this->getDBRow($withoutNull,$debug)) {
  1027.             if ($addrObj=$this->getAddressObj(0,$debug)) {
  1028.                 $addrRow=$addrObj->getDBRow($withoutNull,$debug);
  1029.             }
  1030.             $addrRow['CLI_ID']=$this->getId();
  1031.             return array_merge($dbRow,$addrRow);
  1032.         else {
  1033.             return array();
  1034.         }
  1035.     }
  1036.  
  1037.     /**
  1038.       * returns an array with dbvalues for the client and the primary address
  1039.       *
  1040.       * @param boolean $debug 
  1041.       *
  1042.       * @returns array
  1043.       *
  1044.       * @since pk-05-09-23
  1045.       *
  1046.       * @var array $a_ret 
  1047.       * @var ADDRESS $o_addr 
  1048.       ***/
  1049.     function getCliAddrDBVal($debug=False{
  1050.         if ($debugecho "<p><b>CLIENT::getCliAddrDBVal(...)</b> (".get_class($this).")</p>";
  1051.  
  1052.         $a_ret=$this->getDBVal($debug);
  1053.         if ($o_addr=$this->getDefaultAddress($debug)) {
  1054.            $a_ret=array_merge($o_addr->getDBVal(),$a_ret);
  1055.         }
  1056.         return $a_ret;
  1057.     }
  1058.  
  1059.  
  1060.     /**
  1061.       * @param boolen $debug 
  1062.       * @return ADDRESS 
  1063.       * @since pk-05-09-23
  1064.       ***/
  1065.     function &getDefaultAddress($debug=False{
  1066.         if ($debugecho "<p><b>CLIENT::getDefaultAddress()</b> (".get_class($this).")</p>\n";
  1067.         return $this->getAddressObj(0,$debug);
  1068.     }
  1069.  
  1070.     
  1071.     /**
  1072.       * returns a address of the client
  1073.       *
  1074.       * @param int $addrId if 0 the default address is used
  1075.       * @param boolean $debug 
  1076.       *
  1077.       * @return ADDRESS 
  1078.       *
  1079.       * @since pk-05-02-22
  1080.       * @version pk-07-08-28
  1081.       * @version pk-08-11-29
  1082.       *
  1083.       ***/
  1084.     function &getAddressObj($addrId=0,$debug=False{
  1085.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::getAddressObj($addrId)");
  1086.  
  1087.         if (!intval($addrId)) {
  1088.             if (!($addrId=$this->getPriAddrId()))
  1089.             {
  1090.                 // we have no primary address set -> ask the db
  1091.                 if (!$this->getId())
  1092.                 {
  1093.                     $obj_addr Null;
  1094.                     return $obj_addr;
  1095.                 }
  1096.                                 
  1097.                 if ($arr_address =  $this->myDBObj->getArray('T_ADDRESS',array('CLI_ID' => $this->getId()),0,1,'ADR_ISPRIMARY DESC',$debug))
  1098.                 {
  1099.                     $obj_addr ADDRESS::factoryFromRow($arr_address[0]);
  1100.                     $this->setPriAddrId($obj_addr->getId());
  1101.                     return $obj_addr;    
  1102.                 else {
  1103.                     $obj_addr NULL;
  1104.                     return $obj_addr;
  1105.                 }
  1106.             }
  1107.         }
  1108.         
  1109.         $obj_addr ADDRESS::getInstance($addrId,$debug);
  1110.         
  1111.         // <pk-08-11-29>
  1112.         if (!$obj_addr->getCliId(== $this->getId())
  1113.         {
  1114.             $obj_addr Null;
  1115.             return $obj_addr;
  1116.         else {
  1117.             $obj_addr->setClient($this,$debug);
  1118.             return $obj_addr;
  1119.         }
  1120.         // </pk-08-11-29>
  1121.     }
  1122.  
  1123.     
  1124.     /**
  1125.       * returns the default Address ID
  1126.       * if column CLI_DEFAULTADDR is 0 the first ADR_ID in T_ADDRESS of the client is set as primary
  1127.       *
  1128.       * @param boolean $autosave (save changes to the database)
  1129.       * @param boolean $debug 
  1130.       *
  1131.       * @returns int
  1132.       *
  1133.       * @global $OCSP_OBJ 
  1134.       *
  1135.       * @since pk-03-12-23
  1136.       * @version pk-05-08-29 $autosave added
  1137.       * @version pk-07-08-28 $this->myDBObj
  1138.       *
  1139.       * @todo CLIENT::getPriAddrId() inline SQL Code
  1140.       ***/
  1141.     function getPriAddrId($autosave=TRUE,$debug=False{
  1142.         global $OCSP_OBJ;
  1143.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::getPriAddrId()");
  1144.  
  1145.         if ((!intval($this->getDBField('CLI_DEFAULTADDR'))) && ($this->getId()))
  1146.         {
  1147.             if (!$this->isConnected(TRUE,$debug))
  1148.             {
  1149.                 ocsp_logError(__FILE__,__LINE__,"no database connection",E_ERROR);
  1150.                 return 0;
  1151.             }
  1152.             $query ="SELECT ADR_ID FROM T_ADDRESS WHERE CLI_ID=".$this->getId();
  1153.             $query.=" ORDER BY ADR_ISPRIMARY DESC";
  1154.             $this->setDBField('CLI_DEFAULTADDR',intval($this->myDBObj->quickQuery($query,0,0)));
  1155.             if ($autosave// <pk-05-08-29>
  1156.                 $str_cmd="UPDATE T_CLI_CLIENT SET CLI_DEFAULTADDR=".intval($this->CLI_DEFAULTADDR).",CLI_CHANGEDBY=".($OCSP_OBJ['USER']->getId()*-1)." WHERE CLI_ID=".$this->getId();
  1157.                 $this->myDBObj->executeCmd($str_cmd);
  1158.                 // <pk-07-08-28>
  1159.                 $str_cmd="UPDATE T_ADDRESS SET ADR_ISPRIMARY=0,ADR_CHANGEDBY=".($OCSP_OBJ['USER']->getId()*-1)." WHERE CLI_ID=".$this->getId()." AND ADR_ISPRIMARY <> 0";
  1160.                 $this->myDBObj->executeCmd($str_cmd);
  1161.                 $str_cmd="UPDATE T_ADDRESS SET ADR_ISPRIMARY=1,ADR_CHANGEDBY=".($OCSP_OBJ['USER']->getId()*-1)." WHERE CLI_ID=".$this->getId()." AND ADR_ID=".$this->getDBField('CLI_DEFAULTADDR');
  1162.                 $this->myDBObj->executeCmd($str_cmd);
  1163.                 // /<pk-07-08-28>
  1164.             // </pk-05-08-29>
  1165.         }
  1166.         return intval($this->getDBField('CLI_DEFAULTADDR'));
  1167.     }
  1168.  
  1169.     
  1170. /**
  1171.       * returns the default address
  1172.       * @param bool $debug 
  1173.       * @returns ADDRESS
  1174.       *
  1175.       * @since pk-04-01-13
  1176.       * @version pk-05-02-22
  1177.       *
  1178.       ***/
  1179.     function &getPriAddr($debug=False{
  1180.         if ($debugecho "<p><b>CLIENT::getPriAddr($debug)</b> (".get_class($this).")</p>";
  1181.         /* <pk-05-02-22>
  1182.             require_once $GLOBALS['CLIENT']['PHPINCPATH']."ADDRESS.phpclass";
  1183.             return new ADDRESS($this->getId(),$this->getPriAddrId(),$this->gDBIDX,$debug);
  1184.         */
  1185.         return $this->getAddressObj(0,$debug);
  1186.     }
  1187.  
  1188.     
  1189.     /**
  1190.       * sets the default Address ID
  1191.       *
  1192.       * @param int $aId a Address ID
  1193.       *
  1194.       * @since pk-03-12-23
  1195.       *
  1196.       ***/
  1197.     function setPriAddrId($aId{
  1198.         $this->CLI_DEFAULTADDR=intval($aId);
  1199.     }
  1200.  
  1201.  
  1202.     /**
  1203.       * sets the default Address by object
  1204.       * @note if $addrObj->getId() returns null $address->dbSave() is executed
  1205.       *
  1206.       * @param ADDRESS $addrObj 
  1207.       * @param boolean $saveAddr execute $addrObj->dbSave() after updateing the object
  1208.       * @param boolean $debug 
  1209.       * @param boolean $autosave if true $this->dbSave() is called
  1210.       *
  1211.       * @returns boolean
  1212.       *
  1213.       * @since pk-05-02-25
  1214.       *
  1215.       ***/
  1216.     function setPriAddr($addrObj,$saveAddr=TRUE,$debug=False,$autosave=False{
  1217.         if ($debugecho "<p><b>CLIENT::setPriAddr(...)</b> (".get_class($this).")</p>";
  1218.  
  1219.         if (!$this->getId()) return False;  // save the client first to get an id
  1220.  
  1221.         require_once __OCSP_PHPINCPATH__."common/pcf.phpinc";
  1222.         if (pcf_is_instance_of($addrObj,"ADDRESS")) {
  1223.             $addrObj->setCliId($this->getId());
  1224.             $addrObj->setPrimary(False,$debug);
  1225.             if ((!$addrObj->getId()) || $saveAddr{
  1226.                 // object is not saved
  1227.                 $addrObj->dbSave();
  1228.             }
  1229.             $this->setPriAddrId($addrObj->getId());
  1230.             if ($autosave$this->dbSave();
  1231.             return TRUE;
  1232.         }
  1233.         return False;
  1234.     }
  1235.  
  1236.     /**
  1237.      * returns an array with all address objects
  1238.      *
  1239.      * @param string $orderBy 
  1240.      * @param boolean $debug 
  1241.      * 
  1242.      * @return array 
  1243.      */
  1244.     public function getAddrList($orderBy="",$debug=False)
  1245.     {
  1246.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::getAddrList()");
  1247.         
  1248.         $arr_filter array('CLI_ID' => $this->getId());
  1249.         if (empty($orderBy))
  1250.         {
  1251.             $orderBy 'ADR_ISPRIMARY DESC,ADR_COUNTRY,ADR_ZIP';
  1252.         }
  1253.         $arr_ret array();
  1254.         if ($arr_addrRow OCSP_OBJ::defaultReadDBObj()->getArray('T_ADDRESS',$arr_filter,0,0,$orderBy,$debug))
  1255.         {
  1256.             foreach($arr_addrRow as $arr_Addr)
  1257.             {
  1258.                 $arr_ret[ADDRESS::factoryFromRow($arr_Addr,$debug);
  1259.             }
  1260.         }
  1261.         
  1262.         return $arr_ret;        
  1263.     }
  1264.     
  1265.     
  1266.     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1267.     /*      user methods                                                            */
  1268.     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1269.  
  1270.     /**
  1271.       * sets the user id and updates the Client user Table
  1272.       *
  1273.       * @param int $aId 
  1274.       * @param boolean $debug 
  1275.       *
  1276.       * @since pk-05-06-17
  1277.       *
  1278.       ***/
  1279.     function setUSR_ID($aId,$debug=False{
  1280.         if ($debugecho "<p><b>CLIENT::setUSR_ID($aId,$debug)</b> (".get_class($this).")</p>";
  1281.  
  1282.         if (!$this->getId()) return False;
  1283.         $query="REPLACE INTO T_CLI_USER(CLI_ID,USR_ID) SELECT ".$this->getId().",USR_ID FROM T_SYS_USER WHERE USR_ID=".intval($aId);
  1284.         // select USR_ID form T_SYS_USER to ensure user exists
  1285.         // use REPLACE to avoid errors if done twice
  1286.         // and to keep old values
  1287.         if ($debugecho "<blockquote><p>$query</p></blockquote>";
  1288.         return $GLOBALS[$this->gDBIDX]->executeCmd($query);
  1289.     }
  1290.  
  1291.     /**
  1292.       * generates a user for the client
  1293.       * and returns the user ID or False in case of error
  1294.       *
  1295.       * @param string $login 
  1296.       * @param string $pwd the clear text version
  1297.       * @param boolean $enabled 
  1298.       * @param array $groups 
  1299.       * @param boolean $debug 
  1300.       * @param int $defaultGrp 
  1301.       *
  1302.       * @returns int
  1303.       *
  1304.       * @since pk-05-06-17
  1305.       * @version pk-06-03-20
  1306.       *
  1307.       ***/
  1308.     function createUser($login,$pwd,$enabled=TRUE,$groups=NULL,$debug=False,$defaultGrp=0{
  1309.         if ($debugecho "<p><b>CLIENT::createUser($login,*****,....)</b> (".get_class($this).")</p>";
  1310.  
  1311.         if (empty($login|| empty($pwd)) {
  1312.             // require both
  1313.             return False;
  1314.         }
  1315.  
  1316.  
  1317.         // create a new user object
  1318.         $s_cmd="\$o_User=new ".$this->myUserClass."();";
  1319.         eval($s_cmd);
  1320.         if (!is_object($o_User)) return False// something went wrong :-(
  1321.  
  1322.         $a_userData=array(
  1323.             'USR_LOGIN'     => $login,
  1324.             'USR_PWD'       => $pwd,
  1325.             'USR_ENABLED'   => intval($enabled),
  1326.             'GROUPS'        => $groups,
  1327.             'CLI_ID'        => $this->getId(),
  1328.             'CST_ID'        => 0
  1329.         );
  1330.  
  1331.         return $o_User->generateUser($a_userData,False,$debug,$defaultGrp)// (userDate,login,debug)
  1332.     }
  1333.  
  1334.     /**
  1335.       * creates a user from post values
  1336.       *
  1337.       * @param DBMS_FORM $frmObj 
  1338.       * @param int $usrId becomes the userId
  1339.       * @param boolean $debug 
  1340.       *
  1341.       * @return boolean 
  1342.       *
  1343.       * @since pk-06-10-24
  1344.       *
  1345.       ***/
  1346.     function createUserFromPost(&$frmObj,&$usrId,$debug=False{
  1347.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_CLIENT::createUserFromPost()");
  1348.  
  1349.         if (!pcf_is_instance_of($frmObj,"DBMS_FORM")) {
  1350.             if (!isset($_POST['DBMSFORMID']|| !intval($_POST['DBMSFORMID'])) {
  1351.                 return False;
  1352.             }
  1353.             $frmObj=DBMS_form_loadId(intval($_POST['DBMSFORMID']));
  1354.         }
  1355.  
  1356.         $frmObj->clearErrors();
  1357.         $s_arrName=(isset($_POST['FRMARRAYNAME']$_POST['FRMARRAYNAME'"DBVAL");
  1358.         if (is_array($_POST[$s_arrName])) {
  1359.             if ($a_postVals=$frmObj->getValueArray($_POST[$s_arrName],"",$s_arrName)) {
  1360.                 $b_enabled      =(isset($a_postVals['USR_ENABLED']intval($a_postVals['USR_ENABLED']TRUE);
  1361.                 $i_defaultGrp   =(isset($a_postVals['GRP_ID']intval($a_postVals['GRP_ID']0);
  1362.                 if ($usrId=$this->createUser($a_postVals['USR_LOGIN'],$a_postVals['USR_PWD'],$b_enabled,NULL,$debug,$i_defaultGrp)) {
  1363.                     if ($frmObj->usesTable('T_CLI_USER')) {
  1364.                         $s_query="SELECT * FROM T_CLI_USER WHERE UPPER USR_ID=".$usrId." AND CST_ID=".$this->getId()." AND CLI_ID=".$this->getCLI_ID();
  1365.                         $a_cliUsr=$this->myDBObj->quickQuery($s_query);
  1366.                         foreach($a_cliUsr as $s_key => $m_val{
  1367.                             if (isset($a_postVals[$s_key])) {
  1368.                                 $a_cliUsr=$a_postVals[$s_key];
  1369.                             }
  1370.                         }
  1371.                         $this->myDBObj->replaceArray('T_CLI_USER',$a_cliUsr);
  1372.                     }
  1373.                     return TRUE;
  1374.                 else {
  1375.                     $frmObj->setError(array('LABEL'=>"Login",'MSG'=>"Benutzername bereits vorhanden"));
  1376.                     return False;
  1377.                 }
  1378.             else {
  1379.                 return False;
  1380.             }
  1381.         else {
  1382.             return False;
  1383.         }
  1384.     }
  1385.  
  1386.  
  1387.  
  1388.     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1389.     /*  Profile methods                                                     */
  1390.     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
  1391.  
  1392.     /**
  1393.       * @param int $clpId 
  1394.       * @param boolean $debug 
  1395.       *
  1396.       * @requires CLI_PROFILE.phpclass
  1397.       *
  1398.       * @return CLI_PROFILE 
  1399.       *
  1400.       * @since pk-07-10-06
  1401.       *
  1402.       */
  1403.     function getProfileObj($clpId,$debug=False)
  1404.     {
  1405.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::getProfile($clpId)");
  1406.         
  1407.         if (!class_exists('CLI_PROFILE'))
  1408.         {
  1409.             require_once dirname(__FILE__)._OCSP_DIRSEP_."CLI_PROFILE.phpclass";
  1410.         }
  1411.  
  1412.         $obj_profile=CLI_PROFILE::factory_fromId($clpId);
  1413.         $obj_profile->setDBField('CLI_ID',$this->getId());
  1414.  
  1415.         return $obj_profile;
  1416.     }
  1417.        
  1418.     /**
  1419.       * stores profile data to the database
  1420.       * calls addProfileObj() for each value array
  1421.       *
  1422.       * @param int $clpId 
  1423.       * @param array $valArr 
  1424.       * @param boolean $multiArr    if the profile allows multiple entries is $valArr a single entry or an array of entries?
  1425.       * @param boolean $dataCheck   check the data by calling
  1426.       * @param boolean $debug 
  1427.       *
  1428.       * @returns int     number of rows added
  1429.       *
  1430.       * @since pk-05-02-26
  1431.       *
  1432.       ***/
  1433.     function addProfile($clpId,$valArr,$multiArr=False,$dataCheck=TRUE,$debug=False{
  1434.         if ($debugecho "<p><b>CLIENT::addProfile($clpId,....)</b> (".get_class($this).")</p>";
  1435.  
  1436.         if (!$this->getId())    return False;
  1437.         if (!intval($clpId))    return False;
  1438.         if (!is_array($valArr)) return False;
  1439.  
  1440.         require_once $GLOBALS['OCSP']['DEFAULTCONFPATH']."client.conf.phpinc";
  1441.         require_once $GLOBALS['CLIENT']['PHPINCPATH']."CLI_PROFILE.phpclass";
  1442.  
  1443.         if ($debugecho "<blockquote>";
  1444.         $noRows=0;
  1445.         if ($profile=new CLI_PROFILE(0,$clpId,0)) {
  1446.             if ((!$profile->isMultiple()) || (!$multiArr)) {
  1447.                 // signle data row processing
  1448.                 if ($this->addProfileObj($profile,$valArr,$dataCheck,$debug)) {
  1449.                     $noRows=1;
  1450.                 }
  1451.             else {
  1452.                 foreach($valArr as $profileData{
  1453.                     if ($this->addProfileObj($profile,$profileData,$dataCheck,$debug)) {
  1454.                         $noRows++;
  1455.                     }
  1456.                 }
  1457.             }
  1458.         }
  1459.  
  1460.         if ($debugecho "<p>Returns: <b>$noRows</b></p></blockquote>";
  1461.         return $noRows;
  1462.     }
  1463.  
  1464.     /**
  1465.       * stores profile data to the database
  1466.       * processes only one data row
  1467.       *
  1468.       * @param CLP_PROFILE $profile the profile
  1469.       * @param array $valArr the posted array
  1470.       * @param boolean $dataCheck check the data by calling
  1471.       * @param boolean $debug 
  1472.       *
  1473.       * @returns boolean
  1474.       *
  1475.       * @since pk-05-02-26
  1476.       * @version pk-05-09-23
  1477.       ***/
  1478.     function addProfileObj(&$profile,$valArr,$dataCheck=TRUE,$debug=False{
  1479.         if ($debugecho "<p><b>CLIENT::addProfileObj(...)</b> (".get_class($this).")</p>";
  1480.  
  1481.         if (!is_object($profile)) return False;  // no profile object
  1482.         if (!$profile->getId()) return False;   // empty profile
  1483.         if (!is_array($valArr)) return False;   // no data
  1484.  
  1485.         if ($debugecho "<blockquote><pre>".print_r($valArr,TRUE)."</pre></blockquote>";
  1486.  
  1487.         if (!empty($valArr['_DBMS_FIELDS'])) {
  1488.             if ($a_keys=explode(",",$valArr['_DBMS_FIELDS'])) {
  1489.                 $b_isempty=TRUE;
  1490.                 reset($a_keys);
  1491.                 while($b_isempty && (list($idx,$s_keyeach($a_keys))) {
  1492.                     if (!empty($valArr[$s_key])) $b_isempty=False;
  1493.                 }
  1494.                 if ($b_isempty{
  1495.                     if ($debugecho "<p>ROW is empty</p>";
  1496.                     return False;
  1497.                 }
  1498.             }
  1499.         }
  1500.         $b_useCurrentUser=($this->getId()==$GLOBALS['USER']->getCliId());
  1501.         if ($valArr[$profile->getKeyColName()]=$this->getProfileKey($profile,$b_useCurrentUser,$debug)) {
  1502.             if (($dataCheck&& (!$profile->checkEntryRowArr($valArr,$debug))) {
  1503.                 return False;
  1504.             }
  1505.  
  1506.             if ($profile->isMultiple()) {
  1507.                 return $profile->insertEntryRow($valArr,$debug);
  1508.             else {
  1509.                 return $profile->updateEntryRow($valArr,TRUE,$debug);
  1510.             }
  1511.         }
  1512.         return False;
  1513.     }
  1514.  
  1515.     /**
  1516.       * returns the key value to use in the profile
  1517.       *
  1518.       * @param CLI_PROFILE $profile 
  1519.       * @param boolean $useCurrentUser 
  1520.       * @param boolean $debug 
  1521.       *
  1522.       * @returns int
  1523.       *
  1524.       * @since pk-05-09-23
  1525.       *
  1526.       ***/
  1527.     function getProfileKey(&$profile,$useCurrentUser=TRUE,$debug=False{
  1528.         if ($debugecho "<p><b>CLIENT::getProfileData(...)</b> (".get_class($this)."</p>";
  1529.  
  1530.         if (!is_object($profile)) return NULL;
  1531.  
  1532.         $pfKey=0;
  1533.         switch($profile->getKeyColName()) {
  1534.             case "CLI_ID":
  1535.                 $pfKey=$this->getId();
  1536.                 break;
  1537.             case "USR_ID":
  1538.                 if (($useCurrentUser&& ($GLOBALS['USER']->getCliId()==$this->getId())) {
  1539.                     $pfKey=$GLOBALS['USER']->getId();
  1540.                 else {
  1541.                     if (!$this->isConnected(TRUE,$debug))
  1542.                     {
  1543.                         ocsp_logError(__FILE__,__LINE__,'no database connection',E_WARNING);
  1544.                         return 0;
  1545.                     }
  1546.                     $s_query="SELECT USR_ID FROM T_CLI_USER WHERE CLI_ID=".$this->getId()." ORDER BY CST_ID";
  1547.                     $pfKey=intval($this->myDBObj->quickQuery($s_query,0));
  1548.                 }
  1549.                 break;
  1550.             default:
  1551.                 ocsp_logError(__FILE__,__LINE__,"<p>CLIENT::getProfileData <b>".$profile->getKeyColName()." not implemented</p>");
  1552.                 return NULL;
  1553.         }
  1554.         return $pfKey;
  1555.     }
  1556.  
  1557.  
  1558.     /**
  1559.       * returns an array with the profileData of the client
  1560.       * retValue is array['ROW_NR'][COL]
  1561.       *
  1562.       * @param CLI_PROFILE $profile 
  1563.       * @param boolean $useCurrentUser 
  1564.       * @param boolean $debug 
  1565.       *
  1566.       * @returns array
  1567.       *
  1568.       * @since pk-05-09-23
  1569.       *
  1570.       ***/
  1571.     function getProfileData(&$profile,$useCurrentUser=TRUE,$debug=False{
  1572.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::getProfileData(...)");
  1573.  
  1574.         if (!pcf_is_instance_of($profile,'CLI_PROFILE')) return NULL;
  1575.  
  1576.         $pfKey=$this->getProfileKey($profile,$useCurrentUser,$debug);
  1577.         if (intval($pfKey)) {
  1578.             return $profile->getEntryRowArray($pfKey,$debug);
  1579.         else {
  1580.             return NULL;
  1581.         }
  1582.     }
  1583.  
  1584.     /**
  1585.       * returns a DB-RowArray of profile
  1586.       *
  1587.       * @param CLI_PROFILE $profile 
  1588.       * @param int $clpidx 
  1589.       * @param boolean $useCurrentUser 
  1590.       * @param boolean $debug 
  1591.       *
  1592.       * @returns array
  1593.       *
  1594.       * @since pk-06-12-22
  1595.       *
  1596.       ***/
  1597.     function getProfileEntryRow(&$profile,$clpidx=0,$useCurrentUser=TRUE,$debug=False{
  1598.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::getProfileDataArr(...)");
  1599.  
  1600.         if (!is_object($profile)) return NULL;
  1601.  
  1602.         $pfKey=$this->getProfileKey($profile,$useCurrentUser,$debug);
  1603.         return $profile->getEntryRow($pfKey,$clpidx,$debug);
  1604.     }
  1605.  
  1606.     /**
  1607.       * deletes all profile entries for the user
  1608.       *
  1609.       * @param CLP_PROFILE $profile 
  1610.       * @param boolean $useCurrentUser 
  1611.       * @param boolean $debug 
  1612.       *
  1613.       * @returns boolean
  1614.       *
  1615.       * @since pk-05-09-23
  1616.       ***/
  1617.     function clearProfileData(&$profile,$useCurrentUser=TRUE,$debug=False{
  1618.         if ($debugecho "<p><b>CLIENT::clearProfileData(".$profile->getId().")</b> (".get_class($this).")</p>";
  1619.  
  1620.         if (!is_object($profile)) return False;
  1621.  
  1622.         if (!$GLOBALS['USER']->canEditClient($this->getId(),$debug)) {
  1623.             return False;
  1624.         }
  1625.  
  1626.         if ($pfKey=$this->getProfileKey($profile,$useCurrentUser,$debug)) {
  1627.             $profile->deleteEntryRow($pfKey,0,$debug);
  1628.             return TRUE;
  1629.         else {
  1630.             return False;
  1631.         }
  1632.     }
  1633.  
  1634.  
  1635.     /**
  1636.       * replaces all profile data
  1637.       * @param CLP_PROFILE $profile 
  1638.       * @param array $pfDataArr 
  1639.       * @param boolean $useCurrentUser 
  1640.       * @param boolean $debug 
  1641.       *
  1642.       * @returns int
  1643.       *
  1644.       * @since pk-05-09-23
  1645.       *
  1646.       * @var int $i_noPf 
  1647.       ***/
  1648.     function replaceProfileData(&$profile,$pfDataArr,$useCurrentUser=TRUE,$debug=False{
  1649.         if ($debugecho "<p><b>CLIENT::replaceProfileData(".(is_object($profile$profile->getId("NO PROFILE").",...)</b> (".get_class($this).")</p>";
  1650.  
  1651.         $i_noPf=0;
  1652.         if (!is_array($pfDataArr)) return False;
  1653.         if (!is_object($profile))  return False;
  1654.  
  1655.         if ($debugecho "<blockquote>";
  1656.         if ($this->clearProfileData($profile,$useCurrentUser,$debug)) {
  1657.             foreach($pfDataArr as $i_pfNr => $a_pfData{
  1658.                 if ($this->addProfileObj($profile,$a_pfData,TRUE,$debug)) $i_noPf++;
  1659.             }
  1660.         }
  1661.         if ($debugecho "</blockquote>";
  1662.         return $i_noPf;
  1663.     }
  1664.     
  1665.     /**
  1666.      * returns an profile entry object
  1667.      *
  1668.      * @param int $clpId 
  1669.      * @param int $clpIdx 
  1670.      * @param boolean $debug 
  1671.      * 
  1672.      * @return CLI_PROFILE_ENTRY 
  1673.      * 
  1674.      * @since pk-08-02-11
  1675.      */
  1676.     function getProfileEntry($clpId,$clpIdx=0,$debug=False)
  1677.     {
  1678.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::getProfileEntry()");
  1679.         
  1680.         if (!class_exists('CLI_PROFILE'))
  1681.         {
  1682.             require_once dirname(__FILE___OCSP_DIRSEP_ .  "CLI_PROFILE.phpclass";
  1683.         }
  1684.         return CLI_PROFILE::factory_entryObj($clpId,$this->getId(),$clpIdx,$debug);
  1685.     }
  1686.     
  1687.  
  1688.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1689.     // xml data
  1690.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1691.  
  1692.     /**
  1693.       * adds TableObject Contents to domRoot
  1694.       *
  1695.       * @param mixed &$domObj           //reference to Parent DomObject
  1696.       * @param string $domRoot      //reference to Parent DomRoot
  1697.       * @param bool $base64 
  1698.       * @param bool $debug 
  1699.       *
  1700.       * @returns bool
  1701.       * @since sw-05-03-09
  1702.       *
  1703.       ***/
  1704.     function &xmladdDOMElement(&$domObj,&$domRoot,$base64=TRUE,$debug=False{
  1705.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::xmladdDOMEleement()");
  1706.         
  1707.         if (!$this->isConnected(TRUE))
  1708.         {
  1709.             return False;
  1710.         }
  1711.  
  1712.         if($domRoot parent::xmladdDOMElement($domObj,$domRoot,$base64,$debug)) {
  1713.             $query ="SELECT * FROM T_ADDRESS WHERE CLI_ID = ".$this->getId();
  1714.             
  1715.             if($cursor=$this->myDBObj->query($query)) {
  1716.                 require_once dirname(__FILE___OCSP_DIRSEP_ ."ADDRESS.phpclass";
  1717.                     $valRoot $domObj->createElement("ADR_LST");
  1718.                     $domRoot->appendChild($valRoot);
  1719.                     while($AdrRow=$cursor->fetchArray()) {
  1720.                         $AdrObj new ADDRESS();
  1721.                         $AdrObj->setDBRow($AdrRow,TRUE,$debug);
  1722.                         $AdrObj->xmladdDOMElement($domObj,$valRoot,$base64,$debug);
  1723.                     }
  1724.             }
  1725.             $query ="SELECT * FROM T_CLI_STAFF WHERE CLI_ID = ".$this->getId();
  1726.             if($cursor=$this->myDBObj->query($query)) {
  1727.                 require_once $GLOBALS['CLIENT']['PHPINCPATH']."CLI_STAFF.phpclass";
  1728.                 $valRoot $domObj->createElement("STAFF_LST");
  1729.                 $domRoot->appendChild($valRoot);
  1730.                 while($AdrRow=$cursor->fetchArray()) {
  1731.                     $AdrObj new CLI_STAFF();
  1732.                     $AdrObj->setDBRow($AdrRow,TRUE,$debug);
  1733.                     $AdrObj->xmladdDOMElement($domObj,$valRoot,$base64,$debug);
  1734.                 }
  1735.             }
  1736.             $query ="SELECT * FROM T_CLI_USER WHERE CLI_ID = ".$this->getId();
  1737.             $query.= " AND CST_ID = 0";
  1738.             if($cursor=$this->myDBObj->query($query)) {
  1739.                 require_once $GLOBALS['CLIENT']['PHPINCPATH']."CLI_STAFF.phpclass";
  1740.                 $valRoot $domObj->createElement("USER_LST");
  1741.                 $domRoot->appendChild($valRoot);
  1742.                 while($UsrRow=$cursor->fetchArray()) {
  1743.                     $UsrObj new DBMS_TABLEOBJ();
  1744.                     $UsrObj->myTable "T_CLI_USER";
  1745.                     $UsrObj->setDBRow($UsrRow,TRUE,$debug);
  1746.                     $UsrObj->xmladdDOMElement($domObj,$valRoot,$base64,$debug);
  1747.                 }
  1748.             }
  1749.         }
  1750.         return $valRoot;
  1751.     }
  1752.  
  1753.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1754.     // state methods
  1755.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1756.  
  1757.     // since TBL_VERSION 17 (pk-05-09-20) the colum CLI_STATE is added
  1758.     //
  1759.     // CLI_STATE > 0 are treatened as active clients
  1760.     // CLI_STATE < 0 are treatened as disabled/deleted clients
  1761.  
  1762.     /**
  1763.       * activates the client (state=1 if < 1)
  1764.       * @since pk-05-09-23
  1765.       ***/
  1766.     function activate({
  1767.         if (intval($this->CLI_STATE1{
  1768.             $this->CLI_STATE=1;
  1769.         }
  1770.     }
  1771.  
  1772.     /**
  1773.       * @returns boolean
  1774.       * @since pk-05-09-23
  1775.       ***/
  1776.     function isActive({
  1777.         return (intval($this->CLI_STATE0);
  1778.     }
  1779.  
  1780.     /**
  1781.       * sets the client state
  1782.       *
  1783.       * @param int $clsId 
  1784.       * @param boolean $debug 
  1785.       *
  1786.       ***/
  1787.     function setState($clsId,$debug=False{
  1788.         $this->setDBField('CLS_ID',intval($clsId));
  1789.     }
  1790.  
  1791.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1792.     // staff methods
  1793.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1794.  
  1795.     /**
  1796.       * returns an array with staffId's
  1797.       *
  1798.       * @param int $ctyId 
  1799.       * @param int $clsId 
  1800.       * @param boolean $debug 
  1801.       *
  1802.       * @returns array
  1803.       *
  1804.       * @since pk-05-12-08
  1805.       * @version pk-06-05-15 debugging
  1806.       ***/
  1807.     function getStaffIdArr($ctyId=0,$clsId=0,$debug=False{
  1808.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::getStaffIdArr($ctyId,$clsId)","",0);
  1809.         $s_query ="SELECT CST_ID FROM T_CLI_STAFF";
  1810.         $s_query.=" WHERE CLI_ID=".$this->getId();
  1811.         if (intval($ctyId)) {
  1812.             $s_query.=" AND CTY_ID=".intval($ctyId);
  1813.         }
  1814.         if (intval($clsId)) {
  1815.             $s_query.=" AND CLS_ID=".intal($clsId);
  1816.         }
  1817.  
  1818.         if ($debugecho "<p>Query$s_query</p>";
  1819.  
  1820.         if (!$this->isConnected(TRUE,$debug))
  1821.         {
  1822.             ocsp_logError(__FILE__,__LINE__,"no database connection",E_ERROR);
  1823.             return array();
  1824.         }
  1825.  
  1826.         $a_ret=$this->myDBObj->queryArray($s_query,0,0);
  1827.         if ($debugecho "<p>Result: </p><pre>".print_r($a_ret,TRUE)."</pre>";
  1828.  
  1829.         return $a_ret;
  1830.     }
  1831.  
  1832.     
  1833.     /**
  1834.      * returns an array of staff objects
  1835.      *
  1836.      * @param int $csttypeId 
  1837.      * @param string $orderBy 
  1838.      * @param boolean $debug 
  1839.      * 
  1840.      * @return array (of CLI_STAFF* objects)
  1841.      * 
  1842.      * @version pk-08-07-30 $orderBy added
  1843.      */
  1844.     function getSaffList($csttypeId=0,$orderBy="",$debug=False)
  1845.     {       
  1846.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::getSaffList()");
  1847.                 
  1848.         $arr_filter array('CLI_ID' => $this->getId());
  1849.         if (intval($csttypeId))
  1850.         {
  1851.             $arr_filter['CSTTYPE_ID'$csttypeId;
  1852.             $str_table 'T_CLI_STAFF';
  1853.             $str_order '';
  1854.         else {
  1855.             $str_table 'T_CLI_STAFF LEFT OUTER JOIN T_CLI_STAFF_TYPE USING(CSTTYPE_ID)';
  1856.             $str_order 'CSTTYPE_SORTORDER,';
  1857.         }
  1858.         
  1859.         if (empty($orderBy))
  1860.         {
  1861.             $orderBy $str_order 'CST_FIRSTNAME,CST_LASTNAME';
  1862.         }
  1863.         $arr_ret array();
  1864.         if ($arr_cstRow OCSP_OBJ::defaultReadDBObj()->getArray($str_table,$arr_filter,0,0,$orderBy,$debug))
  1865.         {
  1866.             foreach($arr_cstRow as $arr_staff)
  1867.             {
  1868.                 if ($debugechoDebugLine(__FILE__,__LINE__,"Staff: <pre>" print_r($arr_staff,True"</pre>");                
  1869.                 if (!CLI_STAFF::isInstanced(intval($arr_staff['CST_ID'])))
  1870.                 {
  1871.                     CLI_STAFF::setInstance(CLI_STAFF::factoryFromRow($arr_staff,$debug));                    
  1872.                 
  1873.                 CLI_STAFF::getInstance(intval($arr_staff['CST_ID']))->setClient($this);
  1874.                 $arr_ret[CLI_STAFF::getInstance(intval($arr_staff['CST_ID']));
  1875.             }
  1876.         }
  1877.         
  1878.         return $arr_ret;
  1879.     }
  1880.     
  1881.     
  1882.     /**
  1883.       * returns a staff object
  1884.       *
  1885.       * @param int $cstId the id
  1886.       * @param boolean $useCliData if TRUE the data from the client fits into staff are copied
  1887.       * @param boolean $debug 
  1888.       *
  1889.       * @returns CLI_STAFF
  1890.       *
  1891.       * @since pk-06-10-02
  1892.       * @version pk-07-10-10
  1893.       * @version pk-08-05-16
  1894.       * @version pk-08-08-20 // use identity map
  1895.       */
  1896.     function getStaff($cstId=0,$useCliData=False,$debug=False
  1897.     {
  1898.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::getStaff($cstId)");
  1899.  
  1900.         pcf_require_class('CLI_STAFF',"client/");
  1901.         
  1902.         if (intval($cstId))
  1903.         {
  1904.             $obj_ret CLI_STAFF::getInstance($cstId,$debug);
  1905.             if ($obj_ret->getCliId(!= $this->getId())
  1906.             {
  1907.                 if ($debugechoDebugLine(__FILE__,__LINE__,"CLI_ID mismatch");
  1908.                 return Null;
  1909.             }
  1910.         else {
  1911.             $this->getTypeObj()// ensure $this->myTypeObj is set propper
  1912.             $obj_ret $this->myTypeObj->getDefaultStaffType($debug)->getCstObjectFromArray(array('CST_ID' => $this->getId()));            
  1913.         }
  1914.         
  1915.         $obj_ret->setClient($this);
  1916.         if ($useCliData{
  1917.             $obj_ret->setClientValuesToStaff($debug);
  1918.         }        
  1919.         
  1920.         return $obj_ret;
  1921.     }
  1922.  
  1923.     // ------------------------------------------------------
  1924.     // rights
  1925.     // ------------------------------------------------------
  1926.  
  1927.     /**
  1928.       * returns if the current user is allowed for mode
  1929.       *
  1930.       * @param int $frmMode 
  1931.       * @param boolean $debug 
  1932.       *
  1933.       * @return boolean 
  1934.       *
  1935.       * @since pk-06-10-02
  1936.       *
  1937.       ***/
  1938.     function userCan($frmMode=0,$debug=False{
  1939.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::userCan()");
  1940.  
  1941.         if ($GLOBALS['USER']->isGroupMember($GLOBALS['GROUPS']['CLIENT-ADMIN'])) {
  1942.             // user is client admin no need to proceed with checks
  1943.             return TRUE;
  1944.         }
  1945.         switch($frmMode{
  1946.             case FRM_MODE_DELETE:
  1947.                 return $this->userCanDelete($debug);
  1948.             case FRM_MODE_EDIT:
  1949.                 return $this->userCanEdit($debug);
  1950.             default:
  1951.                 return $this->userCanShow($debug);
  1952.         }
  1953.     }
  1954.  
  1955.     /**
  1956.       * returns if the current user can show the client data
  1957.       *
  1958.       * @param boolean $debug 
  1959.       *
  1960.       * @return boolean 
  1961.       *
  1962.       * @global array $OCSP_OBJ 
  1963.       *
  1964.       * @since pk-06-10-02
  1965.       * @version pk-07-01-15 return if the user can edit
  1966.       * @version pk-07-08-29
  1967.       *
  1968.       ***/
  1969.     function userCanShow($debug=False)
  1970.     {
  1971.         global $OCSP_OBJ;
  1972.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::userCanShow()");
  1973.  
  1974.         if (!intval($this->getId())) {
  1975.             // empty clients can be viewed by every one :-)
  1976.             return TRUE;
  1977.         }
  1978.         
  1979.         if (!OCSP_OBJ::currentUser()) 
  1980.         {
  1981.             return false;
  1982.         }        
  1983.         if (OCSP_OBJ::currentUser()->isGroupMember(_OCSP_GROUP_CLIADMIN_)) {
  1984.             // user is client admin no need to proceed with checks
  1985.             return TRUE;
  1986.         }
  1987.         $this->getTypeObj()// to ensure $this->myTypeObj is set
  1988.         if ($this->myTypeObj->userCanShowClients($debug)) return TRUE;
  1989.  
  1990.         // finaly check if the client is staff of the user
  1991.         if (!$this->isConnected(TRUE,$debug))
  1992.         {
  1993.             ocsp_logError(__FILE__,__LINE__,"no database connection",E_WARNING);
  1994.             return False;
  1995.         }
  1996.         $s_query="SELECT COUNT(*) FROM T_CLI_USER WHERE CLI_ID=".$this->getId()." AND USR_ID=".OCSP_OBJ::currentUser()->getId();
  1997.         if ($i_count=intval($this->myDBObj->quickQuery($s_query,0))) return TRUE;
  1998.  
  1999.         return $this->userCanEdit($debug)// <pk-07-01-15 />
  2000.     }
  2001.  
  2002.     /**
  2003.       * returns if the current user can edit the client data
  2004.       *
  2005.       * @param boolean $debug 
  2006.       *
  2007.       * @since pk-06-10-02
  2008.       *
  2009.       ***/
  2010.     function userCanEdit($debug=False)
  2011.     {
  2012.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::userCanEdit()");
  2013.  
  2014.         if (!intval($this->getId())) {
  2015.             // empty or new clients can be edited by every one as they are not in the DB
  2016.             return true;
  2017.         }
  2018.         
  2019.         if (!OCSP_OBJ::currentUser()) 
  2020.         {
  2021.             if ($debugechoDebugLine(__FILE__,__LINE__,"no user object");
  2022.             return False;
  2023.         }
  2024.         
  2025.         if (OCSP_OBJ::currentUser()->isGroupMember(_OCSP_GROUP_CLIADMIN_)) {
  2026.             // user is client admin no need to proceed with checks
  2027.             return true;
  2028.         }
  2029.  
  2030.         $this->getTypeObj()// to ensure $this->myTypeObj is set
  2031.         if ($this->myTypeObj->userCanEditClients($debug)) return TRUE;
  2032.  
  2033.         if ($this->myTypeObj->userCanSelfEdit($debug)) {
  2034.             // finaly check if the client is staff of the user
  2035.             if (!$this->isConnected(TRUE,$debug))
  2036.             {
  2037.                 ocsp_logError(__FILE__,__LINE__,"no database connection",E_NOTICE);
  2038.                 return False;
  2039.             }
  2040.             $s_query="SELECT COUNT(*) FROM T_CLI_USER WHERE CLI_ID=".$this->getId()." AND USR_ID=".OCSP_OBJ::currentUser()->getId();
  2041.             if ($i_count=intval($this->myDBObj->quickQuery($s_query,0))) return TRUE;
  2042.         }
  2043.         return $this->userIsAdmin($debug);
  2044.     }
  2045.  
  2046.     /**
  2047.       * returns if the current user can edit the client data
  2048.       * 
  2049.       * @param boolean $debug 
  2050.       *
  2051.       * @return boolean 
  2052.       * 
  2053.       * @since pk-06-10-02
  2054.       */
  2055.     function userCanDelete($debug=False)
  2056.     {
  2057.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::userCanDelete()");
  2058.  
  2059.             // user is client admin no need to proceed with checks
  2060.             return True;
  2061.         }
  2062.  
  2063.         $this->getTypeObj()// to ensure $this->myTypeObj is set
  2064.         if ($this->myTypeObj->userCanDeleteClients($debug)) return TRUE;
  2065.  
  2066.         return False;
  2067.     }
  2068.  
  2069.     /**
  2070.       * returns if the current user is an admin for the client
  2071.       * first checks common and type rights and then T_CLI_USER.CLU_ISADMIN
  2072.       *
  2073.       * @param boolean $debug 
  2074.       *
  2075.       * @return boolean 
  2076.       *
  2077.       ***/
  2078.     function userIsAdmin($debug=False{
  2079.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLIENT::userIsAdmin()");
  2080.  
  2081.             // user is client admin no need to proceed with checks
  2082.             return TRUE;
  2083.         }
  2084.  
  2085.         $this->getTypeObj()// to ensure $this->myTypeObj is set
  2086.         if ($this->myTypeObj->userIsAdmin($debug)) return TRUE;
  2087.  
  2088.         if ($this->myTypeObj->userCanSelfEdit($debug)) {
  2089.             $s_query ="SELECT COUNT(*) FROM T_CLI_USER ";
  2090.             $s_query.=" WHERE CLI_ID=".$this->getId();
  2091.             $s_query.="   AND USR_ID=".$GLOBALS['USER']->getId();
  2092.             $s_query.="   AND CLU_ISADMIN=1";
  2093.             if (intval($this->myDBObj->quickQuery($s_query,0))) return TRUE;
  2094.         }
  2095.         return False;
  2096.  
  2097.  
  2098.     }
  2099. }
  2100. ?>

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