Source for file CLI_STAFF.phpclass

Documentation is available at CLI_STAFF.phpclass

  1. <?php
  2. /**
  3.   * Class file CLI_STAFF.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 pk-06-10-02
  13.   * @version $Id: CLI_STAFF.phpclass,v 1.16 2008/10/06 21:08:59 pitlinz Exp $
  14.   */
  15.  
  16.  
  17.     // ---------------------------------------------------------
  18.     // requirements
  19.     // ---------------------------------------------------------
  20.  
  21.     pcf_require_interface('INFA_CLI_STAFF',dirname(__FILE__"/");
  22.  
  23.     pcf_require_class('DBMS_TABLEOBJ',"db/");
  24.     pcf_require_class('CLIENT',dirname(__FILE__"/");
  25.     pcf_require_class('CLI_STAFF_TYPE',dirname(__FILE__"/");
  26.     
  27.     require_once __OCSP_DEFAULTCONFPATH__."client.conf.phpinc";
  28.  
  29. /**
  30.   * Class file CLI_STAFF.phpclass
  31.   *
  32.   * @project    Open CSP-Management
  33.   * @package    client
  34.   *
  35.   * @author     Peter Krebs <pitlinz@users.sourceforge.net>
  36.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  37.   *
  38.   * @version pk-03-12-23
  39.   * @version pk-06-10-02
  40.   * @version $Id: CLI_STAFF.phpclass,v 1.16 2008/10/06 21:08:59 pitlinz Exp $
  41.   */
  42. class CLI_STAFF extends DBMS_TABLEOBJ implements INFA_CLI_STAFF
  43. {
  44.  
  45.     // ---------------------------------------------------------------------------
  46.     // constants
  47.     // ---------------------------------------------------------------------------
  48.     
  49.     /**
  50.       * @constant string CLASS_SRC_FILE
  51.       * @since pk-08-02-04
  52.       */
  53.     const CLASS_SRC_FILE = __FILE__;       
  54.     
  55.     // ---------------------------------------------------------------------------
  56.     // class (static)
  57.     // ---------------------------------------------------------------------------
  58.     
  59.     /*** class vars ------------------------------------------------------ */
  60.     
  61.     /**
  62.      * identity map
  63.      *
  64.      * @staticvar array $loadedStaff 
  65.      */
  66.     protected static $loadedStaff array();
  67.     
  68.     /*** class methods --------------------------------------------------- */
  69.     
  70.     /**
  71.      * returns an instance out of the identity map
  72.      *
  73.      * @param int $aId 
  74.      * @param boolean $debug 
  75.      * 
  76.      * @return CLI_STAFF 
  77.      * 
  78.      * @version pk-08-10-06 bugfix $aId
  79.      */
  80.     public static function &getInstance($aId,$debug=False)
  81.     {
  82.         if (!isset(self::$loadedStaff[$aId]))
  83.         {
  84.             self::$loadedStaff[$aIdself::factoryFromId($aId,$debug);
  85.         
  86.  
  87.         return self::$loadedStaff[$aId];
  88.     }
  89.     
  90.     /**
  91.      * adds a staff to the identity map
  92.      *
  93.      * @param CLI_STAFF $aStaff 
  94.      * @param boolean $debug 
  95.      */
  96.     public static function setInstance(&$aStaff,$debug=False)
  97.     {
  98.         if ($debugechoDebugMethod(__FILE__,"static","CLI_STAFF::setInstance(" get_class($aStaff.");");
  99.         
  100.         if (pcf_is_instance_of($aStaff,"CLI_STAFF"))
  101.         {
  102.             self::$loadedStaff[$aStaff->getId()$aStaff;
  103.         }
  104.     }
  105.     
  106.     /**
  107.      * returns if a staff has been added to the identity map
  108.      *
  109.      * @param int $aId 
  110.      * @return boolean 
  111.      */
  112.     public static function isInstanced($aId)
  113.     {
  114.         return isset(self::$loadedStaff[intval($aId)]);
  115.     }    
  116.     
  117.     // ---------------------------------------------------------------------------
  118.     // object vars
  119.     // ---------------------------------------------------------------------------
  120.     
  121.     /*** compostion --------------------------------------------------- */
  122.     
  123.      /**
  124.       * @var CLIENT myClient
  125.       */
  126.     protected $myClient=Null;
  127.  
  128.     /*** attributes  -------------------------------------------------- */
  129.     
  130.     /**
  131.       * @var string $myClassSrcFile 
  132.       */
  133.     protected $myClassSrcFile=__FILE__;
  134.  
  135.     /**
  136.       * @staticvar string $myTable name of the db table
  137.       */
  138.     protected $myTable="T_CLI_STAFF";    
  139.     
  140.     /**
  141.       * @var double $typeIsSet (time())
  142.       ***/
  143.     protected $typeIsSet=0;
  144.  
  145.     /**
  146.       * @var CLI_STAFF_TYPE $myTypeObj 
  147.       * @since pk-06-10-02
  148.       ***/
  149.     protected $myTypeObj=NULL;
  150.  
  151.     // ---------------------------------------------------------------------------
  152.     // factory / construct
  153.     // ---------------------------------------------------------------------------
  154.         
  155.     /**
  156.       * init the object and populate if an id is submitted
  157.       *
  158.       * @param mixed $client (CLIENT object or client id)
  159.       * @param int $cstId 
  160.       * @param bool $debug 
  161.       *
  162.       * @version pk-05-11-23
  163.       * @version pk-08-02-04
  164.       *
  165.       * @deprecated since pk-08-08-19
  166.       */
  167.     function CLI_STAFF($client=NULL,$cstId=0,$debug=False
  168.     {
  169.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::CLI_STAFF(CST_ID$cstId)");
  170.         $this->init($debug);
  171.                 
  172.         if (intval($cstId)) {
  173.             $this->setId($cstId);
  174.             $this->dbPopulate($debug);
  175.         else {
  176.             $this->setDefaults();
  177.         }
  178.         
  179.         // <pk-08-02-04>
  180.         if (pcf_is_instance_of($client,'CLIENT')) 
  181.         {
  182.             $this->setClient($client,$debug);    
  183.         else if (intval($client)) {
  184.             $this->setCliId(intval($client));
  185.         }
  186.         // </pk-08-02-04>        
  187.     }
  188.  
  189.     /**
  190.      * returns a client staff object
  191.      *
  192.      * @param int $typeId 
  193.      * @param mixed $client (CLIENT object or client id)
  194.      * @param int $cstId 
  195.      * @param boolean $debug 
  196.      * 
  197.      * @return CLI_STAFF 
  198.      * 
  199.      * @since pk-08-02-03
  200.      */
  201.     static public function factoryFromType($typeId,$client=null,$cstId=0,$debug=False)
  202.     {
  203.         return CLI_STAFF_TYPE::getInstance($typeId,$debug)->factoryStaff($client,$cstId,$debug);
  204.     }
  205.     
  206.     
  207.     /**
  208.      * returns a client staff object
  209.      *
  210.      * @param array $staffData 
  211.      * @param boolean $debug 
  212.      * 
  213.      * @return CLI_STAFF 
  214.      * 
  215.      * @since pk-08-02-03
  216.      */
  217.     static public function factoryFromRow($staffData,$debug=False)
  218.     {
  219.         if ($debugechoDebugMethod(__FILE__,"static","CLI_STAFF::factoryFromRow()");
  220.  
  221.         $obj_type CLI_STAFF_TYPE::getInstance(intval($staffData['CSTTYPE_ID']));
  222.         
  223.         return $obj_type->factoryStaffFromRow($staffData,$debug);
  224.     }
  225.     
  226.     
  227.     /**
  228.      * returns a client staff object
  229.      *
  230.      * @param int $cstId 
  231.      * @param boolean $debug 
  232.      * 
  233.      * @return CLI_STAFF 
  234.      * 
  235.      * @since pk-08-04-04
  236.      */
  237.     static public function factoryFromId($cstId,$debug=False)
  238.     {
  239.         if ($debugechoDebugMethod(__FILE__,"static","CLI_STAFF::factoryFromId()");
  240.                 
  241.         if ($arr_row OCSP_OBJ::defaultReadDBObj()->getRow('T_CLI_STAFF',array('CST_ID' => intval($cstId)),$debug))
  242.         {
  243.             return self::factoryFromRow($arr_row,$debug);
  244.         else {
  245.             return null;
  246.         }        
  247.     }
  248.        
  249.     
  250.     /**
  251.       * set client table to config setting if set
  252.       *
  253.       * @param string $gDBIdx 
  254.       * @param boolean $debug 
  255.       *
  256.       * @since pk-05-03-15
  257.       *
  258.       ***/
  259.     function init($debug=False{
  260.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::init()");
  261.         if (!empty($GLOBALS['CLIENT']['TABLES']['T_CLI_STAFF'])) {
  262.             $this->myTable=$GLOBALS['CLIENT']['TABLES']['T_CLI_STAFF'];
  263.         }
  264.         return parent::init($debug);
  265.     }
  266.  
  267.     /**
  268.       * returns the id
  269.       * @return int 
  270.       ***/
  271.     function getId({
  272.         return $this->CST_ID;
  273.     }
  274.  
  275.     /**
  276.       * sets the id
  277.       * @param int $aId 
  278.       ***/
  279.     function setId($aId{
  280.         $this->CST_ID=intval($aId);
  281.     }
  282.  
  283.     /**
  284.       * returns the name of the staff
  285.       * @return string 
  286.       ***/
  287.     function getName({
  288.         return $this->CST_LASTNAME." ".$this->CST_FIRSTNAME;
  289.     }
  290.  
  291.  
  292.     // #############################################################################
  293.     //  TYPE METHODS
  294.     // #############################################################################
  295.  
  296.     /**
  297.       * returns the client type id
  298.       *
  299.       * @param boolean $debug 
  300.       *
  301.       * @return int 
  302.       *
  303.       * @since pk-06-01-16
  304.       * @version pk-06-10-02
  305.       *
  306.       */
  307.     function get_TypeId($debug=False{
  308.         return intval($this->getDBField('CSTTYPE_ID'));
  309.     }
  310.  
  311.     /**
  312.       * sets the client type id
  313.       * @param int $aType 
  314.       * @since pk-06-01-16
  315.       * @version pk-06-10-02
  316.       ***/
  317.     function set_TypeId($aType,$debug=False{
  318.         if ($debugecho "<p><b>CLI_STAFF::setType(\$aType=$aType)</b> (".get_class($this).")</p>";
  319.         $this->setDBField('CSTTYPE_ID',intval($aType));
  320.     }
  321.  
  322.     /**
  323.       * checks if $this->myTypeObj is set proper and return it
  324.       *
  325.       * @param boolean $debug 
  326.       *
  327.       * @return CLI_STAFF_TYPE 
  328.       *
  329.       * @since pk-06-10-02
  330.       * @version pk-08-07-30
  331.       */
  332.     function &getTypeObj($debug=False{
  333.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::getTypeObj()");
  334.  
  335.         if ((!pcf_is_instance_of($this->myTypeObj,'CLI_STAFF_TYPE')) || ($this->myTypeObj->getId(!= $this->get_TypeId())) {
  336.             $this->myTypeObj = CLI_STAFF_TYPE::getInstance($this->get_TypeId());
  337.         }
  338.         if ($debugechoDebugLine(__FILE__,__LINE__,"<pre>Type Obj:".print_r($this->myTypeObj->getDBVal(),True)."</pre>");
  339.         return $this->myTypeObj;
  340.     }
  341.  
  342.     /**
  343.       * sets the type object
  344.       *
  345.       * @param CLI_STAFF_TYPE $aTypeObj 
  346.       *
  347.       * @since pk-06-10-02
  348.       ***/
  349.     function setTypeObj(&$aTypeObj{
  350.         if (pcf_is_instance_of($aTypeObj,'CLI_STAFF_TYPE')) {
  351.             $this->myTypeObj=$aTypeObj;
  352.             $this->set_TypeId($this->myTypeObj->getId());
  353.         }
  354.     }
  355.  
  356.  
  357.     /**
  358.       * returns the class specified by the staff type
  359.       *
  360.       * @param boolean $debug 
  361.       *
  362.       * @returns string
  363.       *
  364.       * @since pk-05-11-22
  365.       * @version pk-06-01-16
  366.       * @version pk-06-10-02
  367.       *
  368.       ***/
  369.     function getTypeClass($debug=False{
  370.         if ($debugecho "<p><b>CLI_STAFF::getTypeClass()</b> (".get_class($this).")</p>\n";
  371.  
  372.         $this->getTypeObj()// to ensur $this->myTypeObj is set
  373.         return $this->myTypeObj->getCstClassName($debug);
  374.     }
  375.  
  376.     /**
  377.       * checks if this is type of class CSTTYPE_CLASS
  378.       * and returns the object ($this if ok else a new object of the right type)
  379.       *
  380.       * @param boolean $debug 
  381.       *
  382.       * @returns CLI_STAFF
  383.       *
  384.       * @since pk-06-01-16
  385.       * @version pk-06-10-02
  386.       *
  387.       ***/
  388.     function &checkTypeClass($debug=False{
  389.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::checkTypeClass()");
  390.  
  391.         $s_className=$this->getTypeClass($debug);
  392.         if (strtoupper(get_class($this)) != $s_className{
  393.             return $this->myTypeObj->getCstObjectFromArray($this->getDBVal(),$debug);
  394.         else {
  395.             return $this;
  396.         }
  397.     }
  398.  
  399.     /**
  400.       * returns the name of the type (not the class !!!)
  401.       *
  402.       * @returns boolean
  403.       *
  404.       * @since pk-06-10-04
  405.       *
  406.       ***/
  407.     function getTypeName({
  408.         $this->getTypeObj()// ensure $this->myTypeObj is set
  409.         return $this->myTypeObj->getName();
  410.     }
  411.  
  412.     // #############################################################################
  413.     //  DATABASE METHODS
  414.     // #############################################################################
  415.  
  416.     /**
  417.       * returns the the data array of a join
  418.       *
  419.       * @param array $tables 
  420.       * @param array $filter 
  421.       * @param boolean $withObjVars 
  422.       * @param boolean $debug 
  423.       *
  424.       * @returns array
  425.       *
  426.       * @since pk-05-10-25
  427.       * @version pk-07-10-10
  428.       *
  429.       * @todo CLI_STAFF::dbGetDataArray() filter
  430.       ***/
  431.     function dbGetDataArray($tables,$filter=NULL,$withObjVars=True,$debug=False{
  432.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::dbGetDataArray(".$tables.")");
  433.  
  434.         if (!$this->isConnected(True))
  435.         {
  436.             ocsp_logError(__FILE__,__LINE__,"no database connection",E_ERROR);
  437.             return array();
  438.         }
  439.  
  440.         $a_ret=$this->getDBVal(False,$debug,$withObjVars);
  441.         if (is_array($tables)) {
  442.             $a_cliTables=array();
  443.             foreach($tables as $tbl{
  444.                 if ($debugecho "<blockquote><p>getting data for: ".$tbl."</p>\n";
  445.                 switch($tbl{
  446.                     case "T_CLI_STAFF":
  447.                         // do nothing
  448.                     case "T_CLI_CLIENT":
  449.                         $this->getClient($debug);
  450.                         $a_ret=array_merge($this->myClient->getDBVal(),$a_ret);
  451.                         break;
  452.                     case "T_ADDRESS":
  453.                         $a_tmp=NULL;
  454.                         if (intval($this->getDBField('ADR_ID'))) {
  455.                             $s_query="SELECT * FROM T_ADDRESS WHERE ADR_ID=".intval($this->getDBField('ADR_ID'));
  456.                             $a_tmp=$this->myDBObj->quickQuery($s_query);
  457.                         }
  458.                         if (!is_array($a_tmp)) {
  459.                             $this->getClient();
  460.                             $o_addr=$this->myClient->getDefaultAddress();
  461.                             $a_ret=array_merge($o_addr->getDBVal(),$a_ret);
  462.                         else {
  463.                             $a_ret=array_merge($a_tmp,$a_ret);
  464.                         }
  465.                         break;
  466.                     case "T_SYS_USER":
  467.                         $s_query ="SELECT u.* FROM T_SYS_USER u JOIN T_CLI_USER c USING(USR_ID)";
  468.                         $s_query.=" WHERE c.CLI_ID=".$this->getCliId();
  469.                         $s_query.="   AND c.CST_ID=".$this->getId();
  470.                         if ($a_tmp=$this->myDBObj->quickQuery($s_query)) {
  471.                             $a_ret=array_merge($a_tmp,$a_ret);
  472.                         }
  473.                     default:
  474.                         $s_query="SELECT * FROM ".$tbl." WHERE CST_ID=".$this->getId();
  475.                         if ($debugecho "<p>$s_query</p>\n";
  476.                         if ($a_tmp=$this->myDBObj->quickQuery($s_query)) {
  477.                             $a_ret=array_merge($a_tmp,$a_ret);
  478.                         else if (!$this->myDBObj->tblHasColumn($tbl,'CST_ID',$debug)) {
  479.                             $a_cliTables[]=$tbl;
  480.                         }
  481.                 }
  482.                 if (sizeof($a_cliTables)) {
  483.                     foreach($a_cliTables as $tbl{
  484.                         $s_query="SELECT * FROM ".$tbl." WHERE CLI_ID=".$this->getCliId();
  485.                         if ($debugecho "<p>$s_query</p>\n";
  486.                         if ($a_tmp=$this->myDBObj->quickQuery($s_query)) {
  487.                             $a_ret=array_merge($a_tmp,$a_ret);
  488.                         }
  489.                     }
  490.                 }
  491.                 if ($debugecho "</blockquote>";
  492.             }
  493.         }
  494.         return $a_ret;
  495.     }
  496.  
  497.     /**
  498.       * save the Object
  499.       * checks if we have a client
  500.       *
  501.       * @param string $gDBIDX GLOBAL ARRAY INDEX OF DB OBJECT
  502.       * @param bool   $debug  SHOW DEBUG INFO
  503.       *
  504.       * @global array $OCSP_OBJ 
  505.       *
  506.       * @return mixed 
  507.       *
  508.       * @version pk-05-11-07
  509.       * @version pk-06-08-07
  510.       * @version pk-07-10-10
  511.       *
  512.       ***/
  513.     function dbSave($debug=False{
  514.         global $OCSP_OBJ;
  515.  
  516.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::dbSave()");
  517.         if (!$this->getCliId()) {
  518.             $this->setError("NO CLIENT SET (CLI_ID)");
  519.             return False;       // each CLI_STAFF needs a client
  520.         }
  521.  
  522.         if (!intval($this->getDBField('CSTTYPE_ID'))) {
  523.             // set the type
  524.             $this->set_TypeId(0,$debug);
  525.         }
  526.  
  527.         // <pk-06-08-07>
  528.         $this->setDBField('CST_CHANGEDBY',$OCSP_OBJ['USER']->getId());
  529.         $this->setDBField('CST_LASTCHANGE','#NOW');
  530.         // </pk-06-08-07>
  531.  
  532.         return parent::dbSave($debug);
  533.     }
  534.  
  535.  
  536.     /**
  537.       * inserts a new row to the table and set the creator
  538.       *
  539.       * @param bool   $debug  show debug info
  540.       *
  541.       * @global array $OCSP_OBJ 
  542.       *
  543.       * @return int   returns the autoIncFld ID if exists or 1 on success
  544.       *
  545.       * @since pk-06-08-07
  546.       *
  547.       ***/
  548.     function dbInsert($debug=False
  549.     {
  550.         global $OCSP_OBJ;
  551.  
  552.         if($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::dbInsert()");
  553.         $this->setDBField('CST_CREATOR',OCSP_OBJ::currentUser()->getId());
  554.         $this->setDBField('CST_CREATION','#NOW');
  555.  
  556.         return parent::dbInsert($debug);
  557.     }
  558.     
  559.     /**
  560.      * updates change fields an call parent::dbReplace
  561.      *
  562.      * @param boolean $debug 
  563.      * @param boolean $ignorPopulated 
  564.      * 
  565.      * @return boolean 
  566.      */
  567.     function dbReplace($debug=False,$ignorPopulated=False)
  568.     {
  569.         $this->setDBField('CST_CHANGEDBY',OCSP_OBJ::currentUser()->getId());
  570.         $this->setDBField('CST_LASTCHANGE','#NOW');
  571.         
  572.         return parent::dbReplace($debug,$ignorPopulated);
  573.     }
  574.  
  575.  
  576.     /**
  577.       * deletes the row from the database
  578.       *
  579.       * @param boolean $backupData 
  580.       *
  581.       * @returns boolean
  582.       *
  583.       * @since pk-05-12-06
  584.       *
  585.       * @todo backupData
  586.       *
  587.       ***/
  588.     function dbDelete($backupData=True,$debug=False{
  589.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::dbDelete()");
  590.  
  591.         if (!$this->isConnected(true))
  592.         {
  593.             return false;
  594.         }
  595.         $s_query="SELECT CLP_TABLE FROM T_CLI_PROFILES WHERE CLP_KEYTABLE='".$this->myTable."'";
  596.         if ($cursor=$this->myDBObj->query($s_query)) {
  597.             while($row=$cursor->fetchArrayFld()) {
  598.                 $s_cmd="DELETE FROM ".$row['CLP_TABLE']." WHERE CST_ID=".$this->getId();
  599.                 if ($debugecho "<p>$s_cmd</p>";
  600.                 $this->myDBObj->executeCmd($s_cmd,$debug);
  601.             }
  602.         }
  603.  
  604.         $s_query ="SELECT * FROM T_CLI_USER WHERE CLI_ID=".$this->getCliId();
  605.         $s_query.=" AND CST_ID=".$this->getId();
  606.         if ($o_usrCursor=$this->myDBObj->query($s_query)) {
  607.             while($a_usr=$o_usrCursor->fetchArrayFld()) {
  608.                 $query="SELECT CLP_TABLE FROM T_CLI_PROFILES WHERE CLP_KEYTABLE='T_SYS_USER'";
  609.                 if ($cursor=$this->myDBObj->query($query)) {
  610.                     while($row=$cursor->fetchArrayFld()) {
  611.                         $cmd="DELETE FROM ".$row['CLP_TABLE']." WHERE USR_ID=".$a_usr['USR_ID'];
  612.                         $this->myDBObj->executeCmd($cmd,$debug);
  613.                     }
  614.                 }
  615.  
  616.                 $cmd="DELETE FROM T_SYS_GROUPMEMBER WHERE USR_ID=".$a_usr['USR_ID'];
  617.                 $this->myDBObj->executeCmd($cmd,$debug);
  618.  
  619.                 $cmd="DELETE FROM T_SYS_USER WHERE USR_ID=".$a_usr['USR_ID'];
  620.                 $this->myDBObj->executeCmd($cmd,$debug);
  621.             }
  622.         }
  623.  
  624.         return parent::dbDelete($backupData,$debug);
  625.     }
  626.  
  627.     // #############################################################################
  628.     //  FORM METHODS
  629.     // #############################################################################
  630.     
  631.     
  632.     /**
  633.       * returns the admin form
  634.       *
  635.       * @param int $frmMode 
  636.       * @param mixed $formIdName 
  637.       * @param param boolean $debug
  638.       *
  639.       * @return CLI_FORM 
  640.       *
  641.       * @requires CLI_FORM.phpclass
  642.       *
  643.       */
  644.     function getAdminForm($frmMode=FRM_MODE_READONLY,$formIdName=NULL,$debug=False)
  645.     {
  646.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::getAdminForm($frmMode,$formIdName)");
  647.  
  648.         require_once dirname(__FILE__)._OCSP_DIRSEP_."CLI_STAFF_FORM.phpclass";
  649.  
  650.         if (!empty($formIdName))
  651.         {
  652.             return $this->getForm($frmMode,$formIdName,'CLI_STAFF_FORM',$debug);
  653.         }
  654.  
  655.         if ($this->getTypeObj($debug))
  656.         {
  657.             if ($obj_form $this->myTypeObj->getAdminForm(False,$debug))
  658.             {
  659.                 $obj_form->setMyTblObj($this,$debug);
  660.                 $obj_form->setFrmMode($frmMode);
  661.                 return $obj_form;
  662.             }
  663.         }
  664.  
  665.         return $this->getForm($frmMode,NULL,'CLI_FORM',$debug);
  666.     }
  667.  
  668.  
  669.  
  670.     /**
  671.       * returns the user form
  672.       *
  673.       * @param boolean $frmMode 
  674.       * @param string $frmName default form if no type form is found
  675.       * @param boolean $debug 
  676.       *
  677.       * @return DBMS_FORM 
  678.       *
  679.       * @since pk-06-10-16
  680.       *
  681.       ***/
  682.     function &dbGetCliUserForm($frmMode,$frmName="",$debug=False{
  683.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::dbGetCliUserForm()");
  684.  
  685.         $this->getTypeObj();
  686.         return $this->myTypeObj->getCliUserForm($frmMode,$debug);
  687.     }
  688.  
  689.  
  690.     /**
  691.       * sets post array with form checks
  692.       *
  693.       * @param DBMS_FORM $frmObj a formObj as ref
  694.       * @param array $dbVal array with data
  695.       * @param boolean $debug 
  696.       * @param boolean $asPopulate 
  697.       * @param boolean $strict only set columns of $this->myTable and "OBJVALS_".$this->myTable
  698.       *
  699.       * @returns bool
  700.       *
  701.       * @since pk-06-10-11
  702.       *
  703.       * @todo sometimes CST_CREATOR is overwritten witch current user check why
  704.       *
  705.       ***/
  706.     function setFORMPOST(&$frmObj,$dbval,$debug=False,$asPopulate=True,$strict=False{
  707.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::setFORMPOST()");
  708.  
  709.         if (intval($this->getDBField['CST_CREATOR'])) // workaround for bug
  710.             $dbval['CST_CREATOR']   =$this->getDBField('CST_CREATOR');
  711.             $dbval['CST_CREATION']  =$this->getDBField('CST_CREATION');
  712.         }
  713.  
  714.         return parent::setFORMPOST($frmObj,$dbval,$debug,$asPopulate,$strict);
  715.     }
  716.  
  717.     // #############################################################################
  718.     //  CLIENT METHODS
  719.     // #############################################################################
  720.  
  721.     /**
  722.       * sets the client values to according staff values
  723.       * each CLI_xxx is set to CST_xxx
  724.       * and ADR_ID is set to CLI_DEFAULTADDR
  725.       *
  726.       * @param boolean $debug 
  727.       *
  728.       * @since pk-06-05-12
  729.       * @version pk-06-10-11
  730.       * @version pk-06-12-07
  731.       *
  732.       ***/
  733.     function setClientValuesToStaff($debug=False{
  734.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::setClientValuesToStaff()","",0);
  735.  
  736.         $this->getClient($debug)// ensure $this->myClient is set
  737.         $a_cliVal=$this->myClient->getDBVal();
  738.  
  739.         $this->setDBField('ADR_ID',$a_cliVal['CLI_DEFAULTADDR']);
  740.         unset($a_cliVal['CLI_ID'])// to avoid overwriting the ID
  741.         unset($a_cliVal['ADR_ID'])// we alread have it set
  742.  
  743.         foreach($a_cliVal as $s_key => $s_val{
  744.             if (!empty($s_val)) {
  745.                 $s_staKey=str_replace("CLI_","CST_",$s_key);
  746.                 $this->setDBField($s_key,$s_val,$debug,True);
  747.                 if (!$this->getDBField($s_staKey)) {
  748.                     $this->setDBField($s_staKey,$s_val,$debug,True);
  749.                 }
  750.             }
  751.         }
  752.     }
  753.  
  754.  
  755.     /**
  756.       * returns the Client ID
  757.       *
  758.       * @return int 
  759.       ***/
  760.     function getCliId({
  761.         return intval($this->CLI_ID);
  762.     }
  763.  
  764.     /**
  765.       * sets the client ID
  766.       * 
  767.       * if the new value differs from $this->myClient->getId()
  768.       * $this->myClient is set to NULL
  769.       *
  770.       * @param int $aId 
  771.       * @param boolean $debug 
  772.       * 
  773.       * @return boolean 
  774.       *
  775.       * @version pk-08-02-20
  776.       */
  777.     function setCliId($aId,$debug=False
  778.     {
  779.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::setCliId($aId)");
  780.         
  781.         if (intval($aId)) {
  782.             $this->setDBField('CLI_ID',intval($aId));
  783.             if (is_object($this->myClient)) {
  784.                 if ($this->myClient->getId(!= $this->getCliId()) {
  785.                     $this->myClient=NULL;
  786.                 }
  787.             }
  788.             return True;
  789.         else {
  790.             return False;
  791.         }
  792.     }
  793.  
  794.     /**
  795.       * returns the client object
  796.       * if $this->myClient is not an object a new one is created
  797.       *
  798.       * @param bool $debug 
  799.       *
  800.       * @return CLIENT 
  801.       */
  802.     function &getClient($debug=False{
  803.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::getClient()",get_class($this->myClient));
  804.         if (!is_object($this->myClient)) {
  805.             $this->myClient = CLIENT::factoryFromId($int_CliId);
  806.         }
  807.         return $this->myClient;
  808.     }
  809.  
  810.     /**
  811.       * sets the client object
  812.       *
  813.       * @param CIENT $aClient 
  814.       * @param boolean $debug 
  815.       *
  816.       * @since pk-06-10-04
  817.       * @version pk-08-02-20
  818.       * 
  819.       */
  820.     function setClient(&$aClient,$debug=False{
  821.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::setClient(" get_class($aClient")");
  822.         
  823.         if (pcf_is_instance_of($aClient,'CLIENT')) {
  824.             $this->myClient=$aClient;
  825.             $this->setCliId($aClient->getId(),$debug);
  826.             if (!pcf_is_instance_of($this->myDBObj,"OCSP_DB"))
  827.             {
  828.                 $this->myDBObj=$this->myClient->getDBObj();
  829.             }
  830.         }
  831.     }
  832.  
  833.  
  834.     /**
  835.       * returns the type id of the client
  836.       *
  837.       * @param boolean $debug 
  838.       *
  839.       * @return int 
  840.       *
  841.       * @since pk-05-11-22
  842.       *
  843.       */
  844.     function getClientType($debug=False{
  845.         if ($debugecho "<p><b>CLI_STAFF::getClientType()</b> (".get_class($this).")</p>";
  846.         if ($this->getClient($debug)) {
  847.             return $this->myClient->getType();
  848.         }
  849.     }
  850.  
  851.     
  852.     // #############################################################################
  853.     //  ADDRESS METHODS
  854.     // #############################################################################
  855.     
  856.     /**
  857.      * returns the address object for the staff
  858.      * 
  859.      * if the staff has no address assigned
  860.      * the default address of the client is returned
  861.      *
  862.      * @param boolean $debug 
  863.      * 
  864.      * @return ADDRESS 
  865.      * 
  866.      * @since pk-08-04-04
  867.      */
  868.     function getAddress($debug=false)
  869.     {
  870.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::getAddress()");
  871.                 
  872.         if (!class_exists('ADDRESS'))
  873.         {
  874.             require_once dirname(__FILE___OCSP_DIRSEP_ "ADDRESS.phpclass";
  875.         }
  876.         
  877.         if ($addr_id intval($this->getDBField('ADR_ID')))
  878.         {
  879.             return ADDRESS::factoryFromId($addr_id,$debug);
  880.         else {
  881.             return $this->getClient()->getDefaultAddress($debug);
  882.         }
  883.         
  884.         
  885.     }
  886.     
  887.     
  888.     // #############################################################################
  889.     //  USER METHODS
  890.     // #############################################################################
  891.  
  892.     
  893.     public function getUserId($debug=false)
  894.     {
  895.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::getUserId();");
  896.         
  897.         if ($int_id intval($this->getId()))
  898.         {
  899.             if (!$this->isConnected(true))
  900.             {
  901.                 throw new Exception(_OCSP_EXCEP_NODBCONN_);
  902.             }
  903.             return $this->myDBObj->getOne("SELECT USR_ID FROM T_CLI_USER WHERE CST_ID=".$this->getId());
  904.         else {
  905.             return false;
  906.         }
  907.     }
  908.     
  909.     /**
  910.       * sets the user id and updates the Client user Table
  911.       *
  912.       * @param int $aId 
  913.       * @param boolean $debug 
  914.       *
  915.       * @return boolean 
  916.       *
  917.       * @since pk-06-01-17
  918.       *
  919.       */
  920.     function setUSR_ID($aId,$debug=False{
  921.         if ($debugechoDebug(__FILE__,"<p><b>CLI_STAFF::setUSR_ID($aId,$debug)</b> (".get_class($this).")</p>",0);
  922.  
  923.         if (!$this->getId()) return False;
  924.         $query="REPLACE INTO T_CLI_USER(CLI_ID,CST_ID,USR_ID) SELECT ".$this->getCliId().",".$this->getId().",USR_ID FROM T_SYS_USER WHERE USR_ID=".intval($aId);
  925.         // select USR_ID form T_SYS_USER to ensure user exists
  926.         // use REPLACE to avoid errors if done twice
  927.         // and to keep old values
  928.         if ($debugechoDebug(__FILE__,"<blockquote><p>$query</p></blockquote>",2);
  929.         return $GLOBALS[$this->gDBIDX]->executeCmd($query);
  930.     }
  931.  
  932.     /**
  933.       * removes a user from the staff
  934.       *
  935.       * @param int $aId 
  936.       * @param boolean $debug 
  937.       *
  938.       * @return boolean 
  939.       *
  940.       * @since pk-06-09-15
  941.       *
  942.       ***/
  943.     function removeUSR_ID($aId,$debug=False{
  944.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::removeUSR_ID");
  945.  
  946.         if (!$this->getId()) return False;
  947.         $s_cmd="DELETE FROM T_CLI_USER WHERE USR_ID=".intval($aId)." AND CST_ID=".$this->getId();
  948.         return $GLOBALS[$this->get_gDBIDX()]->executeCmd($s_cmd);
  949.     }
  950.  
  951.     /**
  952.       * generates a user for the client
  953.       * and returns the user ID or False in case of error
  954.       *
  955.       * @param string $login 
  956.       * @param string $pwd 
  957.       * @param boolean $enabled 
  958.       * @param array $groups 
  959.       * @param boolean $debug 
  960.       * @param int $defaultGrp 
  961.       *
  962.       * @returns int
  963.       *
  964.       * @since pk-06-01-17
  965.       * @version pk-06-09-18
  966.       *
  967.       ***/
  968.     function createUser($login,$pwd,$enabled=True,$groups=NULL,$debug=False,$defaultGrp=0{
  969.         if ($debugechoDebug(__FILE__,"<p><b>CLI_STAFF::createUser($login,*****,....)</b> (".get_class($this).")</p>",0);
  970.  
  971.         if (empty($login|| empty($pwd)) {
  972.             // require both
  973.             return False;
  974.         }
  975.  
  976.         if (!$GLOBALS['USER']->isPublic(&& !$GLOBALS['USER']->canEditClient($this->getCliId())) {
  977.             //the current user is not allowed to edit the client
  978.             return False;
  979.         }
  980.  
  981.         $query="SELECT COUNT(*) FROM T_SYS_USER WHERE UPPER(USR_LOGIN)=UPPER(".$GLOBALS[$this->get_gDBIDX()]->qs_getSlashedValue($login).")";
  982.         if (intval($GLOBALS[$this->gDBIDX]->quickQuery($query,0))) {
  983.             // the login already exists ->
  984.             if ($debugechoDebug(__FILE__,"<p>Login exists</p>");
  985.             return False;
  986.         else {
  987.             // the login is not in use
  988.             // <pk-06-09-18>
  989.             $a_usrVals=array();
  990.             $a_usrVals['USR_LOGIN']     =$login;
  991.             $a_usrVals['USR_PWD']       =$pwd;   // the system also accepts clear text passwords
  992.             $a_usrVals['USR_ENABLED']   =intval($enabled);
  993.             $a_usrVals['USR_CREATION']  ="CURRENT_TIMESTAMP";
  994.             $a_usrVals['USR_MD5PWD']    =md5($pwd);
  995.  
  996.             if (!is_array($groups)) $groups=array();
  997.  
  998.             if (intval($defaultGrp0{
  999.                 if ($GLOBALS['USER']->isPublic()) {
  1000.                     $s_query="SELECT GRP_SELFSELECT FROM T_SYS_GROUP WHERE GRP_ID=".intval($defaultGrp);
  1001.                     if (intval($GLOBALS['USRDB']->quickQuery($s_query,0))) {
  1002.                         $a_usrVals['GRP_ID']=intval($defaultGrp);
  1003.                     }
  1004.                 else {  // another user creates a user
  1005.                     if ($GLOBALS['USER']->isGroupAdmin($defaultGrp)) {
  1006.                         $a_usrVals['GRP_ID']=intval($defaultGrp);
  1007.                     }
  1008.                 }
  1009.             }
  1010.  
  1011.             if ($debugechoDebug(__FILE__,"<pre>".print_r($a_usrVals,True)."</pre>",3);
  1012.             if ($GLOBALS[$this->get_gDBIDX()]->insertArray("T_SYS_USER",$a_usrVals)) {
  1013.                 $query="SELECT USR_ID FROM T_SYS_USER WHERE USR_LOGIN=".$GLOBALS[$this->gDBIDX]->qs_getSlashedValue($login);
  1014.                 if (!($i_usrId=$GLOBALS[$this->gDBIDX]->quickQuery($query,0))) // the user was not created
  1015.                     return False;
  1016.                 }
  1017.                 if (!$this->setUSR_ID($i_usrId,$debug)) // undo insert and return False
  1018.                     $GLOBALS[$this->gDBIDX]->executeCmd("DELETE FROM T_SYS_USER WHERE usrId=".$i_usrId);
  1019.                     return False;
  1020.                 }
  1021.                 if (isset($a_usrVals['GRP_ID'])) $groups[]=intval($a_usrVals['GRP_ID']);
  1022.                 if (is_array($groups)) {
  1023.                     foreach($groups as $i_grp{
  1024.                         if (intval($i_grp0{
  1025.                             $a_grpVals=array('GRP_ID'=>intval($i_grp),'USR_ID'=>$i_usrId);
  1026.                             $GLOBALS[$this->get_gDBIDX()]->replaceArray('T_SYS_GROUPMEMBER',$a_grpVals);
  1027.                         }
  1028.                     }
  1029.                 }
  1030.                 return $i_usrId;
  1031.             }
  1032.         }
  1033.  
  1034.     }
  1035.  
  1036.     /**
  1037.       * creates a user from post values
  1038.       *
  1039.       * @param DBMS_FORM $frmObj 
  1040.       * @param int $usrId becomes the userId
  1041.       * @param boolean $debug 
  1042.       *
  1043.       * @return boolean 
  1044.       *
  1045.       * @since pk-06-09-18
  1046.       * @version pk-06-10-04
  1047.       *
  1048.       ***/
  1049.     function createUserFromPost(&$frmObj,&$usrId,$debug=False{
  1050.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::createUserFromPost()");
  1051.  
  1052.         if (!pcf_is_instance_of($frmObj,"DBMS_FORM")) {
  1053.             if (!isset($_POST['DBMSFORMID']|| !intval($_POST['DBMSFORMID'])) {
  1054.                 return False;
  1055.             }
  1056.             $frmObj=DBMS_form_loadId(intval($_POST['DBMSFORMID']));
  1057.         }
  1058.  
  1059.         $frmObj->clearErrors();
  1060.         $s_arrName=(isset($_POST['FRMARRAYNAME']$_POST['FRMARRAYNAME'"DBVAL");
  1061.         if (is_array($_POST[$s_arrName])) {
  1062.             if ($a_postVals=$frmObj->getValueArray($_POST[$s_arrName],"",$s_arrName)) {
  1063.                 $b_enabled      =(isset($a_postVals['USR_ENABLED']intval($a_postVals['USR_ENABLED']True);
  1064.                 $i_defaultGrp   =(isset($a_postVals['GRP_ID']intval($a_postVals['GRP_ID']0);
  1065.                 if ($usrId=$this->createUser($a_postVals['USR_LOGIN'],$a_postVals['USR_PWD'],$b_enabled,NULL,$debug,$i_defaultGrp)) {
  1066.                     if ($frmObj->usesTable('T_CLI_USER')) {
  1067.                         $s_query="SELECT * FROM T_CLI_USER WHERE UPPER USR_ID=".$usrId." AND CST_ID=".$this->getId()." AND CLI_ID=".$this->getCliId();
  1068.                         $a_cliUsr=$GLOBALS[$this->get_gDBIDX()]->quickQuery($s_query);
  1069.                         foreach($a_cliUsr as $s_key => $m_val{
  1070.                             if (isset($a_postVals[$s_key])) {
  1071.                                 $a_cliUsr=$a_postVals[$s_key];
  1072.                             }
  1073.                         }
  1074.                         $GLOBALS[$this->get_gDBIDX()]->replaceArray('T_CLI_USER',$a_cliUsr);
  1075.                     }
  1076.                     return True;
  1077.                 else {
  1078.                     $frmObj->setError(array('LABEL'=>"Login",'MSG'=>"Benutzername bereits vorhanden"));
  1079.                     return False;
  1080.                 }
  1081.             else {
  1082.                 return False;
  1083.             }
  1084.         else {
  1085.             return False;
  1086.         }
  1087.     }
  1088.  
  1089.     /**
  1090.       * deletes a user
  1091.       *
  1092.       * @param int $aId 
  1093.       * @param boolean $debug 
  1094.       *
  1095.       * @return boolean 
  1096.       *
  1097.       * @since pk-06-10-05
  1098.       *
  1099.       ***/
  1100.     function deleteUser($aId,$debug=False{
  1101.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::deleteUser($aId)");
  1102.  
  1103.         $this->getTypeObj()// ensure $this->myTypeObj is set
  1104.  
  1105.         $b_canDelete=$this->myTypeObj->userCanDeleteStaff();
  1106.  
  1107.         if (!$b_canDelete{
  1108.             $this->getClient($debug)// ensure $this->myClient is set
  1109.             $b_canDelete=$this->myClient->userIsAdmin();
  1110.         }
  1111.  
  1112.         if ($b_canDelete{
  1113.             $s_query ="SELECT COUNT(*) FROM T_CLI_USER ";
  1114.             $s_query.=" WHERE CLI_ID=".$this->getCliId();
  1115.             $s_query.="   AND CST_ID=".$this->getId();
  1116.             $s_query.="   AND USR_ID=".intval($aId);
  1117.  
  1118.             if (intval($GLOBALS[$this->get_gDBIDX()]->quickQuery($s_query,0))) {
  1119.                 $o_user=new USER($this->get_gDBIDX(),False,$debug,False);
  1120.                 $o_user->loadUser($aId,$debug);
  1121.                 return $o_user->deleteUser();
  1122.             }
  1123.         else {
  1124.             return False;
  1125.         }
  1126.     }
  1127.  
  1128.     // ------------------------------------------------------
  1129.     // profiles
  1130.     // ------------------------------------------------------
  1131.     
  1132.     function getProfileById($aId,$debug=False)
  1133.     {
  1134.         require_once dirname(__FILE__)._OCSP_DIRSEP_."CLI_PROFILE.phpclass";
  1135.         return CLI_PROFILE::factory_fromId($aId);
  1136.     }
  1137.     
  1138.     
  1139.     // ------------------------------------------------------
  1140.     // rights
  1141.     // ------------------------------------------------------
  1142.  
  1143.     /**
  1144.       * returns if the current user can show the client data
  1145.       *
  1146.       * @param boolean $debug 
  1147.       *
  1148.       * @since pk-06-10-02
  1149.       *
  1150.       ***/
  1151.     function userCanShow($debug=False){
  1152.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::userCanShow()");
  1153.  
  1154.         if (!intval($this->getId())) 
  1155.         {
  1156.             // empty staff can be viewed by every one :-)
  1157.             return True;
  1158.         }
  1159.         
  1160.         if (OCSP_OBJ::currentUser(True)->isGroupMember(_OCSP_GROUP_CLIADMIN_))
  1161.         {
  1162.             // user is client admin no need to proceed with checks
  1163.             return True;
  1164.         }
  1165.         
  1166.         if ($this->getTypeObj()->userCanShowStaff($debug)) 
  1167.         {
  1168.             return True;
  1169.         }
  1170.  
  1171.         $this->getClient();
  1172.         return $this->myClient->userCanShow();
  1173.     }
  1174.  
  1175.     /**
  1176.       * returns if the current user can edit the client data
  1177.       *
  1178.       * @param boolean $debug 
  1179.       *
  1180.       * @since pk-06-10-26
  1181.       *
  1182.       ***/
  1183.     function userCanEdit($debug=False){
  1184.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::userCanEdit()");
  1185.  
  1186.         if (!intval($this->getId())) {
  1187.             // empty or new clients can be edited by every one as they are not in the DB
  1188.             return True;
  1189.         }
  1190.         if ($GLOBALS['USER']->isGroupMember($GLOBALS['GROUPS']['CLIENT-ADMIN'])) {
  1191.             // user is client admin no need to proceed with checks
  1192.             return True;
  1193.         }
  1194.  
  1195.         $this->getTypeObj()// to ensure $this->myTypeObj is set
  1196.         if ($this->myTypeObj->userCanEditStaff($debug)) return True;
  1197.  
  1198.         $this->getClient();
  1199.         if($this->myClient->userCanEdit())              return True;
  1200.  
  1201.         if ($this->myTypeObj->userCanSelfEdit($debug)) {
  1202.             // finaly check if the client is staff of the user
  1203.             $s_query ="SELECT COUNT(*) FROM T_CLI_USER WHERE CLI_ID=".$this->getCliId()." AND USR_ID=".$GLOBALS['USER']->getId();
  1204.             $s_query.=" AND CST_ID=".$this->getId();
  1205.             if ($i_count=intval($GLOBALS[$this->get_gDBIDX()])->quickQuery($s_query,0)) return True;
  1206.         }
  1207.         return False;
  1208.     }
  1209.  
  1210.     /**
  1211.       * returns if the current user can edit the client data
  1212.       *
  1213.       * @param boolean $debug 
  1214.       *
  1215.       * @since pk-06-10-26
  1216.       *
  1217.       ***/
  1218.     function userCanDelete($debug=False){
  1219.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_STAFF::userCanDelete()");
  1220.  
  1221.         if ($GLOBALS['USER']->isGroupMember($GLOBALS['GROUPS']['CLIENT-ADMIN'])) {
  1222.             // user is client admin no need to proceed with checks
  1223.             return True;
  1224.         }
  1225.  
  1226.         $this->getTypeObj()// to ensure $this->myTypeObj is set
  1227.         if ($this->myTypeObj->userCanDeleteStaff($debug)) return True;
  1228.  
  1229.         $this->getClient();
  1230.         if($this->myClient->userCanDelete())              return True;
  1231.  
  1232.         return False;
  1233.     }
  1234.  
  1235. }
  1236. ?>

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