Source for file TMPL_CHAPTER.phpclass

Documentation is available at TMPL_CHAPTER.phpclass

  1. <?php
  2. /**
  3.   * openCSP class file TMPL_CHAPTER.phpclass
  4.   *
  5.   * @project Open CSP-Management
  6.   * @package default
  7.   *
  8.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  9.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.   *
  11.   * @since pk-24.09.2008
  12.   * @version $Id: TMPL_CHAPTER.phpclass,v 1.11 2008/12/17 16:28:52 peterkrebs Exp $
  13.   */
  14.  
  15.     // ---------------------------------------------------------
  16.     // constants 
  17.     // ---------------------------------------------------------
  18.     
  19.     if (!defined('OCSP_CMS_CHATMPL_DUMMYTABLE')) define('OCSP_CMS_CHATMPL_DUMMYTABLE','CHAOBJVAL');
  20.  
  21.  
  22.     // ---------------------------------------------------------
  23.     // requirements
  24.     // ---------------------------------------------------------
  25.  
  26.     pcf_require_interface('INFA_TMPL_CHAPTER',dirname(__FILE__"/");
  27.  
  28.     pcf_require_class('DBMS_TABLEOBJ',"db/");
  29.     pcf_require_class('CMS_PAGE_TEMPLATE',"cms/template/page/");
  30.     pcf_require_class('CMS_CHAPTER',"cms/chapter/");
  31.     pcf_require_class('OCSP_TEMPLATE',"common/");
  32.     pcf_require_class('DBMS_FIELD',"db/frmClasses/");
  33.     
  34.     
  35. /**
  36.   * openCSP class TMPL_CHAPTER
  37.   *
  38.   * @project Open CSP-Management
  39.   * @package default
  40.   *
  41.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  42.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  43.   *
  44.   * @since pk-24.09.2008
  45.   * @version $Id: TMPL_CHAPTER.phpclass,v 1.11 2008/12/17 16:28:52 peterkrebs Exp $
  46.   */
  47. class TMPL_CHAPTER extends DBMS_TABLEOBJ implements INFA_TMPL_CHAPTER
  48. {
  49.     // ---------------------------------------------------------------------------
  50.     // constants
  51.     // ---------------------------------------------------------------------------
  52.     
  53.     /**
  54.      * @constant string CLASS_SRC_FILE
  55.      */
  56.     const CLASS_SRC_FILE = __FILE__;
  57.  
  58.     // ---------------------------------------------------------------------------
  59.     // class (static)
  60.     // ---------------------------------------------------------------------------
  61.     
  62.     /*** class vars ------------------------------------------------------ */
  63.     
  64.     /**
  65.      * unit of work
  66.      *
  67.      * @staticvar array  $loadedCMSChapterTmpls 
  68.      */
  69.     protected static $loadedCMSChapterTmpls array();
  70.     
  71.     /*** class methods --------------------------------------------------- */
  72.     
  73.     /**
  74.      * returns an instance of the unit of work
  75.      *
  76.      * @param int $aId 
  77.      * 
  78.      * @return TMPL_CHAPTER 
  79.      */
  80.     public static function &getInstance($aId,$debug=False)
  81.     {
  82.         if ($debugechoDebugMethod(__FILE__,"static","TMPL_CHAPTER::getInstance({$aId})");
  83.         $aId intval($aId);
  84.         if (
  85.             !isset(self::$loadedCMSChapterTmpls[$aId]|| 
  86.             !pcf_class_implements(self::$loadedCMSChapterTmpls[$aId],'INFA_TMPL_CHAPTER')
  87.         )
  88.         {
  89.             self::$loadedCMSChapterTmpls[$aIdTMPL_CHAPTER::factoryFromId(intval($aId));
  90.         }
  91.         
  92.         return self::$loadedCMSChapterTmpls[$aId];
  93.     }    
  94.     
  95.     /**
  96.      * returns if we already have an instance of $aId
  97.      *
  98.      * @param int $aId 
  99.      * @return boolean 
  100.      */
  101.     public static function isInstanced($aId)
  102.     {
  103.         if (isset(self::$loadedCMSChapterTmpls[$aId]))
  104.         {
  105.             return pcf_class_implements(self::$loadedCMSChapterTmpls[$aId],'INFA_TMPL_CHAPTER');
  106.         }
  107.         return False;
  108.     }
  109.  
  110.     /**
  111.      * adds a newsletter to the unit of work
  112.      *
  113.      * @param CMS_CHAPTER $aChapter 
  114.      */
  115.     public static function setInstance(&$aChapter)
  116.     {
  117.         if (pcf_class_implements($aChapter,'INFA_TMPL_CHAPTER'))
  118.         {
  119.             if (intval($aChapter->getId()))
  120.             {
  121.                 self::$loadedCMSChapterTmpls[$aChapter->getId()$aChapter;
  122.             }
  123.         }
  124.     }    
  125.     
  126.     // ---------------------------------------------------------------------------
  127.     // object vars
  128.     // ---------------------------------------------------------------------------
  129.     
  130.     /*** compostion --------------------------------------------------- */
  131.     
  132.     /**
  133.      * table of the input fields
  134.      * 
  135.      * @var string $myTmplFieldsTable 
  136.      */
  137.     protected $myTmplFieldsTable = "T_CMS_TMPL_CHAPTER_FIELDS";
  138.     
  139.     /**
  140.      * array of template form fields
  141.      *
  142.      * @var array 
  143.      */
  144.     protected $myTmplFields = array();
  145.     
  146.     /**
  147.      * temlate form fields populate timestamp
  148.      *
  149.      * @var timestamp 
  150.      */
  151.     protected $myTmplFieldsPopulateTS = 0;
  152.     
  153.     /*** attributes  -------------------------------------------------- */
  154.  
  155.     /**
  156.      * the database table
  157.      *
  158.      * @var string 
  159.      */
  160.     protected $myTable = "T_CMS_TMPL_CHAPTER";    
  161.         
  162.     /**
  163.       * @var  string  $contTable   the table holding the content
  164.       */
  165.     protected $contTable      = "T_CMS_CHAPTER";     
  166.     
  167.     // ---------------------------------------------------------------------------
  168.     // factory / construct
  169.     // ---------------------------------------------------------------------------
  170.     
  171.     /**
  172.      * factories a new TMPL_CHAPTER object
  173.      *
  174.      * @param int $aId 
  175.      * 
  176.      * @return TMPL_CHAPTER 
  177.      */
  178.     public static function factoryFromId($aId)
  179.     {
  180.         $obj_ret new TMPL_CHAPTER();
  181.         $obj_ret->populateFromKeys(array('CTMP_ID' => intval($aId)));
  182.         return $obj_ret;
  183.     }        
  184.     
  185.     /**
  186.      * returns a new newsletter from a db row
  187.      *
  188.      * @param array $row 
  189.      *
  190.      * @return TMPL_CHAPTER 
  191.      */
  192.     public static function factoryFromRow($row)
  193.     {
  194.         $obj_ret new TMPL_CHAPTER();
  195.         $obj_ret->setDBRow($row,True);
  196.         return $obj_ret;
  197.     }    
  198.     
  199.     // ---------------------------------------------------------------------------
  200.     // getter / setter
  201.     // ---------------------------------------------------------------------------    
  202.  
  203.     /**
  204.      * returns the template id
  205.      *
  206.      * @return int 
  207.      */
  208.     public function getId()
  209.     {
  210.         return intval($this->getDBField('CTMP_ID'));
  211.     }
  212.  
  213.     /**
  214.      * returns the template name
  215.      *
  216.      * @return string 
  217.      */
  218.     public function getName()
  219.     {
  220.         return $this->getDBField('CTMP_NAME');
  221.     }
  222.         
  223.     /**
  224.      * returns the html template
  225.      *
  226.      * @return string 
  227.      */
  228.     public function getHTMLCode()
  229.     {
  230.         return $this->getDBField('CTMP_HTMLCODE');
  231.     }
  232.     
  233.     /**
  234.      * returns if the template is a seperator (no fields)
  235.      *
  236.      * @return boolean 
  237.      */
  238.     public function isSeperator()
  239.     {
  240.         return (intval($this->getDBField('CTMP_ISSEP')) True False);
  241.     }
  242.     
  243.     /**
  244.      * (abstract) returns 'OCSP'
  245.      *
  246.      * @return string 
  247.      */
  248.     public function getModuleKey()
  249.     {
  250.         return 'OCSP';
  251.     }
  252.     
  253.     /**
  254.       * sets the content table (name)
  255.       *
  256.       * @param string $aTable name of the content table
  257.       *
  258.       * @since pk-05-10-10
  259.       *
  260.       ***/
  261.     public function setConttable($aTable
  262.     {
  263.         if (!empty($aTable)) $this->contTable=$aTable;
  264.     }
  265.  
  266.     /**
  267.       * returns the content table (name)
  268.       *
  269.       * @return string 
  270.       *
  271.       * @since pk-05-10-10
  272.       *
  273.       ***/
  274.     public function getConttable(
  275.     {
  276.         return $this->contTable;
  277.     }        
  278.     
  279.     // ---------------------------------------------------------------------------
  280.     // DB methods
  281.     // ---------------------------------------------------------------------------    
  282.     
  283.     /**
  284.       * inserts a new row to the table
  285.       *
  286.       * @param boolean   $debug  show debug info
  287.       *
  288.       * @returns int   returns the autoIncFld ID if exists or 1 on success
  289.       *
  290.       */
  291.     public function dbInsert($debug=False)
  292.     {     
  293.         if ($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::dbInsert()");
  294.                 
  295.         $this->setDBField('CTMP_LASTCHANGE',Null);
  296.         $this->setDBField('CTMP_LASTCHANGE_BY',OCSP_OBJ::currentUser()->getId());
  297.         $this->setDBField('CTMP_CREATED_AT',"CURRENT_TIMESTAMP");
  298.         $this->setDBField('CTMP_CREATED_BY',OCSP_OBJ::currentUser()->getId());
  299.         $this->setDBFieldIfEmpty('CTMP_VERSION',1);                
  300.         if ($int_id intval(parent::dbInsert($debug)))
  301.         {
  302.             $this->extractTmplFields($debug);
  303.             $this->saveTmplFields($debug);
  304.         }
  305.         
  306.         return $int_id;
  307.     }    
  308.     
  309.     /**
  310.       * replaces a row in the table
  311.       *
  312.       * @param boolean $debug  show debug info
  313.       * @param boolean $ignorPopulated (if False the table MUST have been populated)
  314.       *
  315.       * @returns boolean
  316.       *
  317.       */
  318.     public function dbReplace($debug=False,$ignorPopulated=False
  319.     {
  320.         if ($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::dbReplace()");
  321.                 
  322.         $this->setDBField('CTMP_LASTCHANGE',"CURRENT_TIMESTAMP");
  323.         $this->setDBField('CTMP_LASTCHANGE_BY',OCSP_OBJ::currentUser()->getId());
  324.         $this->setDBField('CTMP_VERSION',(intval($this->getDBField('CTMP_VERSIOn')) 1));                
  325.         if (parent::dbReplace($debug,$ignorPopulated))
  326.         {
  327.             $this->extractTmplFields($debug);
  328.             $this->saveTmplFields($debug);
  329.             return True;
  330.         else {
  331.             return False;
  332.         }
  333.     }
  334.     
  335.     // ---------------------------------------------------------------------------
  336.     // page template
  337.     // ---------------------------------------------------------------------------    
  338.     
  339.     /**
  340.      * returns the page template id
  341.      *
  342.      * @return int 
  343.      */
  344.     public function getPageTemplateId()
  345.     {
  346.         return $this->getDBField('PTMP_ID');
  347.     }    
  348.     
  349.     /**
  350.      * sets the page template id
  351.      *
  352.      * @param int $aId 
  353.      */
  354.     public function setPageTemplateId($aId)
  355.     {
  356.         $this->setDBField('PTMP_ID',intval($aId));
  357.     }
  358.  
  359.     /**
  360.      * returns the page template
  361.      *
  362.      * @return CMS_PAGE_TEMPLATE 
  363.      * 
  364.      */
  365.     public function &getPageTemplate($debug=False)
  366.     {
  367.         if ($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::getPageTemplate()");
  368.         return CMS_PAGE_TEMPLATE::getInstance($this->getPageTemplateId(),$debug);
  369.     }
  370.         
  371.     
  372.     /**
  373.      * sets the page template
  374.      *
  375.      * @param CMS_PAGE_TEMPLATE $aTemplate 
  376.      */
  377.     public function setPageTemplate(&$aTemplate)
  378.     {
  379.         $this->setPageTemplateId($aTemplate->getId());
  380.     }    
  381.     
  382.     // ---------------------------------------------------------------------------
  383.     // template fields
  384.     // ---------------------------------------------------------------------------    
  385.     
  386.     /**
  387.      * returns if the template fields are populated
  388.      *
  389.      * @return boolean 
  390.      */
  391.     public function tmplFieldsArePopulated()
  392.     {
  393.         return ($this->myTmplFieldsPopulateTS >= (time(ini_get('max_execution_time')))
  394.     }
  395.     
  396.     /**
  397.      * resets $this->myTmplFieldsPopulateTS and repopulates it if $populate
  398.      *
  399.      * @param boolean $populate 
  400.      * @param boolean $debug 
  401.      */
  402.     public function tmplFieldsHasChanged($populate=False,$debug=False)
  403.     {
  404.         if ($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::tmplFieldsHasChanged()");
  405.         $this->myTmplFieldsPopulateTS = 0;
  406.         if ($populate)
  407.         {
  408.             $this->populateTmplFields($debug);
  409.         }
  410.     }
  411.     
  412.     /**
  413.      * populates the input fields from the database
  414.      * 
  415.      * @param boolean $debug 
  416.      * 
  417.      * @requires OCSP_FORM
  418.      */
  419.     protected function populateTmplFields($debug=False)
  420.     {        
  421.         if ($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::populateTmplFields()");
  422.         
  423.         pcf_require_class('OCSP_FORM',"db/forms/");
  424.         
  425.         $arr_filterarray(
  426.             'CTMP_ID' => $this->getId()
  427.         );
  428.         
  429.         $int_sortorder 1;
  430.         $this->myTmplFields = array();
  431.         if ($arr_fields OCSP_OBJ::defaultReadDBObj()->getArray($this->myTmplFieldsTable,$arr_filter,0,0,"FLD_SORTORDER",$debug))
  432.         {
  433.             $int_sortorder 1;
  434.             foreach($arr_fields as $arr_row)
  435.             {
  436.                    $arr_objData unserialize(base64_decode($arr_row['FLD_B64OBJECT']));
  437.                    //if ($debug) echoDebugLine(__FILE__,__LINE__,"fldObjVal: <pre>" . print_r($arr_objData,True) . "</pre>");
  438.                    OCSP_FORM::requireFieldClass($arr_row['FLD_CLASS']);
  439.                     
  440.                    $this->myTmplFields[$arr_row['FLD_KEY']] new $arr_row['FLD_CLASS']();
  441.                    $this->myTmplFields[$arr_row['FLD_KEY']]->setObjectVars($arr_objData['OBJVAL'])
  442.                    $this->myTmplFields[$arr_row['FLD_KEY']]->setOrderNr($int_sortorder);
  443.                    /*
  444.                    if (intval($arr_row['FLD_SORTORDER']) != $int_sortorder)
  445.                    {
  446.                        if ($debug) echoDebugLine(__FILE__,__LINE__,"updateing sortorder of " . $this->myTmplFields[$arr_row['FLD_KEY']]->getName() . "[" . $arr_row['FLD_KEY'] . "]");
  447.                        $this->myTmplFields[$arr_row['FLD_KEY']]->save();                      
  448.                    }
  449.                 */
  450.                    $int_sortorder++;
  451.             }
  452.         
  453.  
  454.         $this->myTmplFieldsPopulateTS = time();
  455.         if ($int_sortorder == 1)
  456.         {
  457.             $this->extractTmplFields($debug);
  458.         }        
  459.     }
  460.     
  461.     /**
  462.      * extracts the input fields from the te
  463.      *
  464.      * @param boolean $debug 
  465.      */
  466.     protected function extractTmplFields($debug=False)
  467.     {
  468.         if ($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::extractTmplFields()");
  469.         
  470.         $str_code $this->getDBField('CTMP_HTMLCODE'$this->getDBField('CTMP_TXTCODE');
  471.         
  472.         if (!$this->tmplFieldsArePopulated())
  473.         {
  474.             $this->populateTmplFields($debug);
  475.         }
  476.         
  477.         $arr_cmsValues OCSP_OBJ::defaultReadDBObj()->getDBTblDesc('T_CMS_CHAPTER');
  478.         if ($arr_pageValues OCSP_OBJ::defaultReadDBObj()->getDBTblDesc('T_CMS_PAGE'))
  479.         {
  480.             $arr_cmsValues array_merge($arr_pageValues,$arr_cmsValues);        
  481.         }
  482.         
  483.         if (!empty($str_code&& $arr_tmplVars=OCSP_TEMPLATE::getVars($str_code)) 
  484.         {
  485.             if ($debugechoDebugLine(__FILE__,__LINE__,"Vars found in code: <br />".(print_r($arr_tmplVars,TRUE)));
  486.  
  487.             $arr_illegalchars=array(" ","\n","<",">","&","?");
  488.             foreach($arr_tmplVars as $str_key => $arr_fld
  489.             {
  490.                 if (!isset($arr_cmsValues[$str_key])) 
  491.                 {
  492.                     $bol_illegal False;
  493.                     reset($arr_illegalchars);
  494.                     while(!$bol_illegal && list($idx,$cha_i)=each($arr_illegalchars))
  495.                     {
  496.                         if (strstr($str_key,$cha_i)) {
  497.                             if ($debugechoDebugLine(__FILE__,__LINE__,"ignore field:".htmlspecialchars($str_key));
  498.                             $bol_illegal True;
  499.                         }                    
  500.                     }
  501.                                     
  502.                     
  503.                     if (!$bol_illegal && !empty($str_key)) // <pk-05-02-08 /> 
  504.                     
  505.                         if (!isset($this->myTmplFields[$str_key]|| !pcf_class_implements($this->myTmplFields[$str_key],'INFA_DBMS_FIELD'))
  506.                         {
  507.                             if ($debugechoDebugLine(__FILE__,__LINE__,"checking$str_key");
  508.                         
  509.                             switch($arr_fld['TYPE']{
  510.                                 case PCF_TMPLVAR_ARR:
  511.                                 case PCF_TMPLVAR_IF:
  512.                                     $this->myTmplFields[$str_key]=new DBMS_FIELD('CHAOBJVAL',$str_key,array('LEN'=>256,'TYPE'=>'string'),sizeof($this->myTmplFields)+1)//<pk 05-08-02 />
  513.                                     if ($debugechoDebugLine(__FILE__,__LINE__,"addinged field: {$str_key}");
  514.                                                                     
  515.                                     break;
  516.                                 default:
  517.                                     if ($debugechoDebug(__FILE__,"<p style=\"font-size:80%\">no Form Field (".print_r($fld,TRUE)."</p>");
  518.         
  519.                             }
  520.                         else if($debug{
  521.                             echoDebugLine(__FILE__,__LINE__,"fld: {$str_key} already exists ");
  522.                         }
  523.                     else {
  524.                         if ($debugechoDebugLine(__FILE__,__LINE__,"<p>EMPTY FIELD</p>");
  525.                     }
  526.                 }
  527.             }
  528.             //echoDebugLine(__FILE__,__LINE__,print_r($this->myTmplFields,True));
  529.         }                
  530.     }
  531.  
  532.     /**
  533.      * saves one template field to the db
  534.      * 
  535.      * @param string $fldKey 
  536.      * @param DBMS_FIELD $fld 
  537.      * @param boolean $debug 
  538.      * 
  539.      * @return boolean 
  540.      * 
  541.      */
  542.     public function saveTmplField($fldKey,&$fld,$debug=False
  543.     {
  544.         if ($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::saveInpField($fldKey,...)");
  545.         
  546.         $a_ObjData=$fld->getObjectData($debug);
  547.         if (!($fld->getName()))     $fld->setName($fldKey);
  548.         if (!($fld->getTable()))    $fld->setTable(OCSP_CMS_CHATMPL_DUMMYTABLE);
  549.         
  550.         $a_fldRow=array(
  551.             "CTMP_ID"               => $this->getId(),
  552.             'FLD_KEY'               => $fldKey,
  553.             'FLD_TABLE'             => $fld->getTable(),
  554.             'FLD_NAME'              => $fld->getName(),
  555.             'FRM_MODULE'            => $this->getModuleKey(),
  556.             'FLD_IS_HIDDEN'         => intval($fld->isHidden('all')),
  557.             'FLD_ENABLE_NEW'        => intval($fld->isEnabled(FRM_MODE_NEW)),
  558.             'FLD_ENABLE_EDIT'       => intval($fld->isEnabled(FRM_MODE_EDIT)),
  559.             'FLD_SHOW_IN_LIST'      => intval($fld->showInList()),
  560.             'FLD_SORTORDER'         => intval($fld->getOrderNr()),
  561.             'FLD_CLASS'                => get_class($fld),
  562.             'FLD_B64OBJECT'         => base64_encode(serialize($a_ObjData))
  563.         );
  564.         if ($debugechoDebugLine(__FILE__,__LINE__,"<pre style=\"font-size:80%\">".print_r($a_fldRow,True)."</pre>");
  565.         
  566.         $b_ret OCSP_OBJ::defaultWriteDBObj()->replace($this->myTmplFieldsTable,$a_fldRow,$debug);
  567.         if (!$b_retOCSP_OBJ::defaultWriteDBObj()->errorMsg();
  568.  
  569.         return $b_ret;
  570.     }
  571.     
  572.     /**
  573.      * saves the template fields to the db
  574.      *
  575.      * @param boolean $debug 
  576.      * 
  577.      * @todo inline SQL
  578.      */
  579.     public function saveTmplFields($debug=False)
  580.     {
  581.         if($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::saveTmplFields()");
  582.         
  583.         if ($this->tmplFieldsArePopulated())
  584.         {
  585.             // mark existing 
  586.             $str_cmd "UPDATE " $this->myTmplFieldsTable . " SET FLD_SORTORDER = -99";
  587.             $str_cmd.= " WHERE CTMP_ID=" $this->getId();
  588.             OCSP_OBJ::defaultWriteDBObj()->executeCmd($str_cmd);
  589.             
  590.             foreach($this->myTmplFields as $str_key => &$obj_field)
  591.             {
  592.                 $this->saveTmplField($str_key,$obj_field,$debug);
  593.             }
  594.             
  595.             // delete untouched
  596.             $str_cmd "DELETE FROM " $this->myTmplFieldsTable;
  597.             $str_cmd.= " WHERE CTMP_ID=" $this->getId();
  598.             $str_cmd.= "   AND FLD_SORTORDER= -99";
  599.             OCSP_OBJ::defaultWriteDBObj()->executeCmd($str_cmd);
  600.             
  601.         }        
  602.     }
  603.     
  604.     /**
  605.      * returns an array with the template fields
  606.      *
  607.      * @param boolean $debug 
  608.      * 
  609.      * @return array 
  610.      */
  611.     public function getTmplFields($debug=False)
  612.     {
  613.         if ($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::getTmplFields()");
  614.         
  615.         if (!$this->tmplFieldsArePopulated())
  616.         {
  617.             $this->populateTmplFields($debug);
  618.         }
  619.         
  620.         if ($debugechoDebugLine(__FILE__,__LINE__,sizeof($this->myTmplFields" fields found");
  621.         
  622.         return $this->myTmplFields;        
  623.     }
  624.     
  625.     /**
  626.      * returns the template field object
  627.      *
  628.      * @param string $fldKey 
  629.      * @param debug $debug 
  630.      * 
  631.      * @return DBMS_FIELD 
  632.      * 
  633.      */
  634.     public function &getTmplField($fldKey,$debug=False)
  635.     {
  636.         if ($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::getTmplField({$fldKey})");
  637.         
  638.         if (!$this->tmplFieldsArePopulated())
  639.         {
  640.             $this->populateTmplFields($debug);
  641.         }
  642.         
  643.         if (isset($this->myTmplFields[$fldKey]))
  644.         {
  645.             return $this->myTmplFields[$fldKey];
  646.         else {
  647.             $obj_ret Null;
  648.             return $obj_ret;
  649.         }        
  650.     }
  651.  
  652.     /**
  653.      * moves a template field to $intPos
  654.      *
  655.      * @param string $fldKey 
  656.      * @param int $toPos 
  657.      * @param boolean $debug 
  658.      */
  659.     public function moveTmplField($fldKey,$toPos,$debug=False)
  660.     {
  661.         if ($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::moveTmplField({$fldKey},{$toPos});");
  662.         
  663.         if ($toPos 1$toPos 1;
  664.         
  665.         if ($this->getTmplFields($debug))
  666.         {
  667.             $int_sort 1;
  668.             $bol_set  False;
  669.             foreach($this->myTmplFields as $str_key => $obj_fld)
  670.             {
  671.                 if ($debugechoDebugLine(__FILE__,__LINE__,"checking {$str_key} {$int_sort} -- $obj_fld->getOrderNr());
  672.                 if ($fldKey != $str_key)
  673.                 {
  674.                     if ($int_sort == $toPos)
  675.                     {                                    
  676.                         $int_sort++;    
  677.                     
  678.                     
  679.                     if ($obj_fld->getOrderNr(!= $int_sort{
  680.                         if ($debugechoDebugLine(__FILE__,__LINE__,"moving {$str_key} to {$int_sort}");
  681.                         $obj_fld->setOrderNr($int_sort);
  682.                         $this->saveTmplField($str_key,$obj_fld,$debug);    
  683.                     }
  684.                     $int_sort++;
  685.                 else {
  686.                     if ($debugechoDebugLine(__FILE__,__LINE__,"moving {$str_key} to {$toPos}");
  687.                     $obj_fld->setOrderNr($toPos);
  688.                     $this->saveTmplField($str_key,$obj_fld,$debug);
  689.                     $bol_set True;    
  690.                 }                                
  691.             }
  692.             
  693.             if (!$bol_set && ($obj_fld $this->getTmplField($fldKey)))
  694.             {
  695.                 $obj_fld->setOrderNr($int_sort);
  696.                 $this->saveTmplField($fldKey,$obj_fld,$debug);        
  697.             }
  698.             
  699.             $this->tmplFieldsHasChanged(True,$debug);
  700.         else if($debug{
  701.             echoDebugLine(__FILE__,__LINE__,"could not get fields");        
  702.         }
  703.     }
  704.  
  705.     /**
  706.      * deletes a template field
  707.      * 
  708.      * note: if the field is still used in the template it will
  709.      * be added again when the fields are extracted
  710.      *
  711.      * @param string $fldKey 
  712.      * @param debug $debug 
  713.      */
  714.     public function deleteTmplField($fldKey,$debug=False)
  715.     {    
  716.         if ($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::deleteTmplField({$fldKey});");
  717.         
  718.         if ($this->getTmplFields($debug))
  719.         {
  720.             $int_sort 1;
  721.             $bol_set  False;
  722.             $arr_fields array();
  723.             foreach($this->myTmplFields as $str_key => &$obj_fld)
  724.             {
  725.                 if ($debugechoDebugLine(__FILE__,__LINE__,"checking field: {$str_key}");
  726.                 if ($fldKey != $str_key)
  727.                 {
  728.                     $arr_fields[$str_key$obj_fld;
  729.                     if ($obj_fld->getOrderNr(!= $int_sort
  730.                     {
  731.                         $obj_fld->setOrderNr($int_sort);    
  732.                     }
  733.                     $this->saveTmplField($str_key,$obj_fld,$debug);
  734.                     $int_sort++;
  735.                 else {
  736.                     $str_cmd "DELETE FROM " $this->myTmplFieldsTable;
  737.                     $str_cmd.= " WHERE CTMP_ID=" $this->getId();
  738.                     $str_cmd.= "   AND FLD_KEY=" OCSP_OBJ::defaultReadDBObj()->qs_getSlashedValue($str_key);
  739.                     if ($debugechoDebugLine(__FILE__,__LINE__,"Cmd: " $str_cmd);
  740.                     OCSP_OBJ::defaultWriteDBObj()->executeCmd($str_cmd);                    
  741.                 }
  742.             }
  743.             
  744.             $this->myTmplFields = $arr_fields;
  745.         }
  746.     }
  747.     
  748.     // ---------------------------------------------------------------------------
  749.     // chapters
  750.     // ---------------------------------------------------------------------------    
  751.     
  752.     /**
  753.      * returns a new not populated chapter
  754.      *
  755.      * @return CMS_CHAPTER 
  756.      */
  757.     public function &getNewChapter()
  758.     {
  759.         $obj_ret new CMS_CHAPTER();
  760.         $obj_ret->setMyTemplate($this);
  761.         return $obj_ret;
  762.     }
  763.     
  764.     /**
  765.      * get's a chapter instance
  766.      *
  767.      * @param int $aId 
  768.      * @param boolean $debug 
  769.      * 
  770.      * @return CMS_CHAPTER 
  771.      */
  772.     public function &getChapterInstance($aId,$debug=False)
  773.     {
  774.         if ($debugechoDebugMethod(__FILE__,get_class($this),'NWL_TMPL_CHPATER::getChapter('.$aId.')');
  775.         
  776.         if (!intval($aId))
  777.         {
  778.             return $this->getNewChapter($debug);    
  779.         }
  780.         
  781.         if (CMS_CHAPTER::isInstanced($aId))
  782.         {
  783.             $obj_ret CMS_CHAPTER::getInstance($aId);
  784.             $obj_ret->setMyTemplate($this);
  785.             return $obj_ret;
  786.         }
  787.  
  788.         $obj_ret $this->getNewChapter();
  789.         $obj_ret->populateFromKeys(array('CHA_ID' => intval($aId)));
  790.         $obj_ret->setMyTemplate($this);
  791.         CMS_CHAPTER::setInstance($obj_ret);
  792.         return $obj_ret;
  793.  
  794.     }
  795.     
  796.     /**
  797.      * returns a chapter object from a T_CMS_CHAPTER row
  798.      *
  799.      * @param array $aRow 
  800.      * @param boolean $debug 
  801.      * 
  802.      * @return CMS_CHAPTER 
  803.      */
  804.     public function &getChaObjFromRow($aRow,$debug=False)
  805.     {
  806.         if ($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::getChaObjFromRow()");
  807.         
  808.         $obj_ret $this->getChapterInstance(intval($aRow['CHA_ID']));
  809.         $obj_ret->setDBRow($aRow);
  810.         return $obj_ret;        
  811.     }
  812.     
  813.     
  814.     /**
  815.      * adds the template input fields to $aForm
  816.      *
  817.      * @param CMS_CHAPTER_FORM $aForm 
  818.      * @param boolean $debug 
  819.      */
  820.     public function addInputFieldsToForm(&$aForm,$debug=False)
  821.     {
  822.         if ($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::addInputFieldsToForm();");
  823.         
  824.         if ($arr_fields $this->getTmplFields($debug))
  825.         {
  826.             if ($obj_tmplValFld $aForm->getField('CTMP_ID'))
  827.             {                
  828.                 if ($obj_block $obj_tmplValFld->getMyBlock())
  829.                 {    
  830.                     foreach($arr_fields as &$obj_fld)
  831.                     {
  832.                         $obj_block->addFieldObj($obj_fld,$debug);
  833.                     }
  834.                 }
  835.             }
  836.         }        
  837.     }
  838.     
  839.     /**
  840.      * returns the chapter input form
  841.      *
  842.      * @param mixed $form (string) form name (int) form id
  843.      * @param boolean $debug 
  844.      * 
  845.      * @return OCSP_FORM 
  846.      */
  847.     public function &getChapterForm($form='',$frmClass='CMS_CHAPTER_FORM',$debug=False)
  848.     {
  849.         if ($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::getChapterForm()");
  850.                 
  851.           if (empty($form))
  852.         {
  853.             $form='T_CMS_CHAPTER';                
  854.         }
  855.                   
  856.         if (empty($frmClass|| ($frmClass == 'OCSP_FORM'|| ($frmClass == 'CMS_CHAPTER_FORM')) // ensure form source is included     
  857.         {
  858.             pcf_require_class('CMS_CHAPTER_FORM',"cms/chapter/");
  859.             $frmClass='CMS_CHAPTER_FORM';
  860.         }
  861.  
  862.  
  863.         if (intval($form))
  864.         {
  865.             $obj_form=OCSP_FORM::factory_from_id(intval($form),$frmClass,$debug);
  866.             $frmName="";
  867.         else {
  868.             $obj_form=OCSP_FORM::factory_from_Name($form,$frmClass,$debug);
  869.             $frmName=$form;
  870.         }
  871.         
  872.         if (!pcf_is_instance_of($obj_form,"OCSP_FORM"))
  873.         {
  874.             throw new Exception(_OCSP_EXCEP_NOFORM_ "::{$form}");
  875.         }
  876.                 
  877.         $this->addInputFieldsToForm($obj_form,$debug);
  878.         return $obj_form;
  879.     }
  880.     
  881.     // ---------------------------------------------------------------------------
  882.     // rendering
  883.     // ---------------------------------------------------------------------------    
  884.     
  885.     /**
  886.      * renders the template to html
  887.      *
  888.      * @param array $values 
  889.      * @param boolean $debug 
  890.      * 
  891.      * @return string (html)
  892.      */
  893.     public function renderToHtml($values,$debug=False)
  894.     {
  895.         if ($debugechoDebugMethod(__FILE__,get_class($this),"TMPL_CHAPTER::renderToHtml()",nl2br(htmlspecialchars(print_r($values,True))));
  896.         
  897.         if ($str_templ $this->getHtmlCode())
  898.         {
  899.             pcf_require_class('OCSP_TEMPLATE',"common/");
  900.             $str_ret OCSP_TEMPLATE::parse($str_templ,$values);
  901.             if ($debugechoDebugLine(__FILE__,__LINE__,"Template: <hr />" str_replace('$','{}',$str_templ"<hr />returns: " htmlspecialchars($str_ret));
  902.             return $str_ret;
  903.         }
  904.     }    
  905.  
  906. }
  907.  
  908. ?>

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