Source for file CLI_PROFILE_ENTRY.phpclass

Documentation is available at CLI_PROFILE_ENTRY.phpclass

  1. <?php
  2. /**
  3.   * Class file CLI_PROFILE_ENTRY.phpclass
  4.   *
  5.   * @project    Open CSP-Management
  6.   * @package    client
  7.   *
  8.   * @author     Peter Krebs <pitlinz@sourceforge.net>
  9.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  10.   *
  11.   * @since pk-06-10-08
  12.   * @version $Id:$
  13.   */
  14.  
  15.     // ---------------------------------------------------------
  16.     // requirements
  17.     // ---------------------------------------------------------
  18.  
  19.     pcf_require_class('DBMS_TABLEOBJ',"db/");
  20.     pcf_require_class('CLI_PROFILE',"client/");
  21.  
  22. /**
  23.   * Class file CLI_PROFILE_ENTRY.phpclass
  24.   *
  25.   * @project    Open CSP-Management
  26.   * @package    client
  27.   *
  28.   * @author     Peter Krebs <pitlinz@sourceforge.net>
  29.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  30.   *
  31.   * @version pk-03-12-23
  32.   * @version pk-07-10-06
  33.   * @version $Id:$
  34.   */
  35. class CLI_PROFILE_ENTRY extends DBMS_TABLEOBJ {
  36.  
  37.     // ---------------------------------------------------------------------------
  38.     // constants
  39.     // ---------------------------------------------------------------------------
  40.     
  41.     /**
  42.      * @constant string CLASS_SRC_FILE
  43.      */
  44.     const CLASS_SRC_FILE = __FILE__;
  45.  
  46.     // ---------------------------------------------------------------------------
  47.     // class (static)
  48.     // ---------------------------------------------------------------------------
  49.     
  50.     /*** class vars ------------------------------------------------------ */
  51.     
  52.     /*** class methods --------------------------------------------------- */
  53.     
  54.     // ---------------------------------------------------------------------------
  55.     // object vars
  56.     // ---------------------------------------------------------------------------
  57.     
  58.     /*** compostion --------------------------------------------------- */
  59.     
  60.     /**
  61.       * @var CLI_PROFILE $myProfile 
  62.       ***/
  63.     protected $myProfile=NULL;
  64.  
  65.     /*** attributes  -------------------------------------------------- */
  66.     
  67.     // ---------------------------------------------------------------------------
  68.     // factory / construct
  69.     // ---------------------------------------------------------------------------
  70.     
  71.     /**
  72.       * init the object and populate if an id is submitted
  73.       *
  74.       * @param int $clpId Profile ID
  75.       * @param int $id  the row id
  76.       * @param string $gDBIDX 
  77.       * @param bool $debug 
  78.       *
  79.       */
  80.     function CLI_PROFILE_ENTRY($clpId=0,$debug=FALSE{       
  81.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_PROFILE_ENTRY::CLI_PROFILE_ENTRY($clpId)");
  82.  
  83.         if (intval($clpId)) 
  84.         {
  85.             $this->myProfile=CLI_PROFILE::getInstance(intval($clpId));
  86.             $this->setMyTable($this->myProfile->getEntryTbl());
  87.         }
  88.     }
  89.  
  90.     /**
  91.       * sets the profile object
  92.       *
  93.       * @param CLI_PROFILE $profile 
  94.       * @param boolean $debug 
  95.       *
  96.       ***/
  97.     function setProfileObj(&$profile,$debug=False
  98.     {
  99.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_PROFILE_ENTRY::setProfileObj()");
  100.         
  101.         if (pcf_class_implements($profile,'INFA_CLI_PROFILE'))
  102.         {
  103.             $this->myProfile=$profile;
  104.             if ($this->myTable != $this->myProfile->getEntryTbl()) {
  105.                 if ($debugechoDebug(__FILE__,"<blockqoute><b>CHANGING TABLE</b></blockqoute>");
  106.                 $this->myTable=$this->myProfile->getEntryTbl();
  107.                 $this->init($debug,TRUE);
  108.             }
  109.             $this->setDBField('CLP_ID',$profile->getId());
  110.         }
  111.     }
  112.  
  113.     /**
  114.       * returns the profle object
  115.       * if a new object is created this->myTable is set and $this->init is called
  116.       *
  117.       * @param boolean $debug 
  118.       *
  119.       * @return CLI_PROFILE 
  120.       *
  121.       ***/
  122.     function &getProfileObj($debug=False
  123.     {
  124.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_PROFILE_ENTRY::getProfileObj()");
  125.         
  126.         if (pcf_class_implements($this->myProfile,'INFA_CLI_PROFILE'|| ($this->myProfile->getId(!= $this->getProfileId())) 
  127.         {
  128.             if ($this->myProfile = CLI_PROFILE::getInstance(intval($this->getProfileId())))
  129.             {
  130.                 if ($this->myProfile->getEntryTbl(!= $this->myTable)
  131.                 {
  132.                     $this->setMyTable($this->myProfile->getEntryTable,$debug);                
  133.                 }
  134.             }
  135.         }
  136.         return $this->myProfile;
  137.     }
  138.  
  139.     /**
  140.       * returns the id of the profile
  141.       *
  142.       * @return int 
  143.       *
  144.       * @since pk-06-10-22
  145.       ***/
  146.     function getProfileId({
  147.         return intval($this->getDBField('CLP_ID'));
  148.     }
  149.  
  150.     /**
  151.       * @return int 
  152.       */
  153.     function getId(
  154.     {
  155.         return intval($this->getDBField('CLP_IDX'));
  156.     }
  157.  
  158.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  159.     // form methods
  160.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  161.  
  162.  
  163.     /**
  164.       * sets post array with form checks
  165.       *
  166.       * @param DBMS_FORM $frmObj a formObj as ref
  167.       * @param array $dbVal array with data
  168.       * @param boolean $debug 
  169.       * @param boolean $asPopulate 
  170.       * @param boolean $strict only set columns of $this->myTable and "OBJVALS_".$this->myTable
  171.       *
  172.       * @returns bool
  173.       *
  174.       ***/
  175.     function setFORMPOST(&$frmObj,$dbval,$debug=FALSE,$asPopulate=TRUE,$strict=FALSE{
  176.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_PROFILE_ENTRY::setFORMPOST()");
  177.  
  178.         if (!intval($this->getProfileId())) {
  179.             if (isset($dbval['CLP_ID']&& (intval($dbval['CLP_ID']))) {
  180.                 $this->setDBField('CLP_ID',intval($dbval['CLP_ID']));
  181.             else if (isset($_POST['PFFORM_CLP_ID']&& intval($_POST['PFFORM_CLP_ID'])) {
  182.                 $this->setDBField('CLP_ID',intval($_POST['PFFORM_CLP_ID']));
  183.             }
  184.         }
  185.  
  186.         $this->getProfileObj()// ensure it is set propper and the object is initialized
  187.  
  188.         if ((!pcf_is_instance_of($frmObj,'CLI_PROFILE_FORM')) || (!intval($frmObj->getId()))) {
  189.             $o_frmObj=new CLI_PROFILE_FORM($col,$this->getProfileId(),$this->getDBField('CLP_TABLE'));
  190.             $o_frmObj->setDBMSValues($frmObj);
  191.             $GLOBALS['DBMS_FORMS'][$o_frmObj->getId()]=$o_frmObj;
  192.             $frmObj=&$GLOBALS['DBMS_FORMS'][$o_frmObj->getId()];
  193.             $frmObj->setProfileObj($this->myProfile);
  194.         }
  195.  
  196.         if (!intval($this->myProfile->getId())) {
  197.             $frmObj->setError(array('LABEL'=>"Profil",'MSG'=>"Profil konnte nicht ermittelt werden"),'CLP_ID');
  198.             return FALSE;
  199.         }
  200.  
  201.         // check if the foreign key is set
  202.         if (!isset($dbval[$this->myProfile->getKeyColName()]|| !intval($dbval[$this->myProfile->getKeyColName()])) {
  203.             if (isset($_POST['PFFORM_FK_VAL']&& intval($_POST['PFFORM_FK_VAL'])) {
  204.                 $this->setDBField($this->myProfile->getKeyColName(),intval($_POST['PFFORM_FK_VAL']));
  205.                 $dbval[$this->myProfile->getKeyColName()]=intval($_POST['PFFORM_FK_VAL']);
  206.             }
  207.         }
  208.  
  209.         return parent::setFORMPOST($frmObj,$dbval,$debug,$asPopulate,$strict);
  210.     }
  211.  
  212.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  213.     // db methods
  214.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  215.  
  216.     /**
  217.       * returns the key array
  218.       * an array of all key fields
  219.       *
  220.       * @param bolean $debug 
  221.       *
  222.       * @returns array
  223.       *
  224.       * @since pk-05-06-23
  225.       *
  226.       ***/
  227.     function getKeyArr($debug=FALSE{
  228.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_PROFILE_ENTRY::getKeyArr()");
  229.         $arr_ret=parent::getKeyArr($debug);
  230.  
  231.         if (!isset($arr_ret['CLP_ID']&& isset($this->myFields['CLP_ID']))
  232.         {
  233.             if ($debugechoDebugLine(__FILE__,__LINE__,"adding key CLP_ID");
  234.             $arr_ret['CLP_ID']=array(
  235.                     'NAME'  =>$this->myFields['CLP_ID']['COLNAME'],
  236.                     'TYPE'  =>$this->myFields['CLP_ID']['TYPE'],
  237.                     'AUTO_INCREMENT' => FALSE
  238.                 );
  239.         }
  240.         if (!isset($arr_ret[$this->myProfile->getKeyColName()]&& isset($this->myFields[$this->myProfile->getKeyColName()]))
  241.         {
  242.             if ($debugechoDebugLine(__FILE__,__LINE__,"adding key ".$this->myProfile->getKeyColName());
  243.             $arr_ret[$this->myProfile->getKeyColName()]=array(
  244.                     'NAME'  =>$this->myFields[$this->myProfile->getKeyColName()]['COLNAME'],
  245.                     'TYPE'  =>$this->myFields[$this->myProfile->getKeyColName()]['TYPE'],
  246.                     'AUTO_INCREMENT' => FALSE
  247.                 );
  248.         }
  249.         return $arr_ret;
  250.     }
  251.  
  252.     /**
  253.       * @param boolean $debug 
  254.       *
  255.       * @return boolean 
  256.       *
  257.       * @since pk-07-10-07
  258.       */
  259.     function checkProfileForeignKey($debug=FALSE)
  260.     {
  261.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_PROFILE_ENTRY::checkProfileForeignKey()");
  262.         
  263.         if (!intval($this->getDBField($this->myProfile->getKeyColName()))) {
  264.             if (!intval($this->myProfile->getForeignKey())) {
  265.                 // the key is not set
  266.                 if ($debugocsp_logError(__FILE__,__LINE__,"<p>KEY: ".$this->myProfile->getKeyColName()." NOT SET</p>",E_ERROR);
  267.                 return FALSE;
  268.             else {
  269.                 $this->setDBField($this->myProfile->getKeyColName(),$this->myProfile->getForeignKey());
  270.             }
  271.         }
  272.         return TRUE;
  273.     }
  274.  
  275.     /**
  276.       * save the Object
  277.       *
  278.       * @param string $gDBIDX GLOBAL ARRAY INDEX OF DB OBJECT
  279.       * @param boolean   $debug  SHOW DEBUG INFO
  280.       *
  281.       * @returns mixed
  282.       *
  283.       ***/
  284.     function dbSave($debug=FALSE{
  285.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_PROFILE_ENTRY::dbSave()");
  286.  
  287.         if (intval($this->getDBField('CLP_IDX'))) {
  288.             // we have an update
  289.             return $this->dbReplace($debug);
  290.         }
  291.  
  292.         // does we have an insert or update or overwrite of a row
  293.         $this->getProfileObj()// ensure $this->myProfile is set propper
  294.  
  295.         if (!intval($this->myProfile->getId())) {
  296.             // no profile set
  297.             if ($debugecho "<p>ERROR: no profile set</p>";
  298.             return FALSE;
  299.         }
  300.  
  301.         if (!$this->myProfile->isMultiple()) {
  302.             $s_query ="SELECT * FROM ".$this->myTable;
  303.             $s_query.=" WHERE ".$this->myProfile->getKeyColName()."=".intval($this->getDBField($this->myProfile->getKeyColName()));
  304.             if ($this->myProfile->checkProfileIdInEntryQuery()) {
  305.                 $s_query.=" AND CLP_ID=".$this->myProfile->getId();
  306.             }
  307.             if ($a_row=$this->myDBObj->quickQuery($s_query)) {
  308.                 $dbRow=array_merge($a_row,$this->getDBVal());
  309.                 $this->setDBRow($dbRow,TRUE);
  310.                 return $this->dbReplace($debug);
  311.             }
  312.             return $this->dbInsert($debug);
  313.         else {
  314.             return $this->dbInsert($debug);
  315.         }
  316.     }
  317.  
  318.     /**
  319.       * inserts a new row to the table
  320.       *
  321.       * @param string $gDBIDX Index of $GLOBALS to the db object
  322.       * @param bool   $debug  show debug info
  323.       *
  324.       * @global array $OCSP_OBJ 
  325.       *
  326.       * @returns int   returns the autoIncFld ID if exists or 1 on success
  327.       *
  328.       * @since pk-05-11-30
  329.       *
  330.       ***/
  331.     function dbInsert($debug=FALSE{
  332.         global $OCSP_OBJ;
  333.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_PROFILE_ENTRY::dbInsert()");
  334.  
  335.         if (!$this->checkProfileForeignKey($debug)) return FALSE;
  336.                 
  337.         $this->setDBField('CLP_CTIME',"CURRENT_TIMESTAMP");
  338.         $this->setDBField('CLP_CUSER',$OCSP_OBJ['USER']->getId());
  339.         $this->setDBField('CLP_UTIME',"CURRENT_TIMESTAMP");
  340.         $this->setDBField('CLP_UUSER',$OCSP_OBJ['USER']->getId());
  341.  
  342.         $this->setDBField('CLP_RADDR',$_SERVER['REMOTE_ADDR']);
  343.         $this->setDBField('CLP_HVIA',$_SERVER['HTTP_VIA']);
  344.         $this->setDBField('CLP_HX_FORWD',$_SERVER['HTTP_X_FORWARDED_FOR']);
  345.  
  346.         if (!intval($this->getProfileId()) && pcf_is_instance_of($this->myProfile,'CLI_PROFILE')) {
  347.             $this->setDBField('CLP_ID',$this->myProfile->getId());
  348.         else if (!intval($this->getProfileId())) {
  349.             $this->setError("KEIN PROFIL GESETZT");
  350.             return FALSE;
  351.         }
  352.  
  353.         return parent::dbInsert($debug);
  354.     }
  355.  
  356.     /**
  357.       * replaces a row in the table
  358.       *
  359.       * @param boolean $debug  show debug info
  360.       * @param boolean $ignorPopulated (if false the table MUST have been populated)
  361.       *
  362.       * @returns boolean
  363.       *
  364.       * @since pk-05-11-30
  365.       *
  366.       ***/
  367.     function dbReplace($debug=FALSE,$ignorPopulated=FALSE{
  368.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_PROFILE_ENTRY::dbReplace($gDBIDX,$debug)");
  369.  
  370.         if (!$this->checkProfileForeignKey($debug)) return FALSE;
  371.         
  372.         $this->setDBField('CLP_UTIME',"CURRENT_TIMESTAMP");
  373.         $this->setDBField('CLP_UUSER',$GLOBALS['USER']->getId());
  374.         $this->setDBField('CLP_RADDR',$_SERVER['REMOTE_ADDR']);
  375.         $this->setDBField('CLP_HVIA',$_SERVER['HTTP_VIA']);
  376.         $this->setDBField('CLP_HX_FORWD',$_SERVER['HTTP_X_FORWARDED_FOR']);
  377.         $this->setDBField('CLP_ID',$this->myProfile->getId());
  378.  
  379.         return parent::dbReplace($debug,$ignorPopulated);
  380.     }
  381.  
  382.     /**
  383.       * deletes the row from the database
  384.       *
  385.       * @param string $gDBIDX 
  386.       * @param boolean $backupData 
  387.       *
  388.       * @returns boolean
  389.       *
  390.       * @since pk-06-10-24
  391.       *
  392.       ***/
  393.     function dbDelete($backupData=FALSE,$debug=FALSE{
  394.         if ($debugechoDebugMethod(__FILE__,get_class($this),"CLI_PROFILE_ENTRY::dbDelete($gDBIDX,$debug)");
  395.  
  396.         if (!intval($this->getProfileId())) {
  397.             if (isset($dbval['CLP_ID']&& (intval($dbval['CLP_ID']))) {
  398.                 $this->setDBField('CLP_ID',intval($dbval['CLP_ID']));
  399.             else if (isset($_POST['PFFORM_CLP_ID']&& intval($_POST['PFFORM_CLP_ID'])) {
  400.                 $this->setDBField('CLP_ID',intval($_POST['PFFORM_CLP_ID']));
  401.             }
  402.         }
  403.  
  404.         $this->getProfileObj()// ensure it is set propper and the object is initialized
  405.         return parent::dbDelete($backupData,$debug);
  406.     }
  407.     
  408.     
  409.     /**
  410.       * returns the form
  411.       *
  412.       * @param int $mode 
  413.       * @param mixed $form (string) form name (int) form id
  414.       * @param boolean $debug 
  415.       *
  416.       * @return CLI_PROFILE_FORM 
  417.       *
  418.       * @requires CLI_PROFILE_FORM.phpclass
  419.       *
  420.       * @since pk-07-08-16
  421.       *
  422.       */
  423.     function &getForm($mode=FRM_MODE_READONLY,$form='',$frmClass='CLI_PROFILE_FORM',$debug=FALSE)
  424.     {
  425.         if ($debugechoDebugMethod(__FILE__,get_class($this),"DBMS_TABLEOBJ::getForm($mode,$form,$frmClass)");
  426.         pcf_require_class('CLI_PROFILE_FORM'dirname(__FILE__"/");
  427.         
  428.         if (!$obj_profile=$this->getProfileObj())
  429.         {
  430.             ocsp_logError(__FILE__,__LINE__,"could not get profile",E_ERROR);
  431.             $obj_ret=NULL;
  432.             return $obj_ret;
  433.         }
  434.  
  435.         if (empty($form))
  436.         {
  437.             $form=$this->myProfile->getEntryTblFormIdName($mode);
  438.         }
  439.  
  440.         $obj_form=parent::getForm($mode,$form,$frmClass,$debug);
  441.  
  442.         if (method_exists($obj_form,'setProfileObj'))
  443.         {
  444.             $obj_form->setProfileObj($this->myProfile);
  445.         }
  446.         return $obj_form;
  447.     }
  448. }

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