cvs: pear /HTML_Template_Flexy Flexy.php /HTML_Template_Flexy/Flexy QuickForm.php /HTML_Template_Flexy/Flexy/Token Tag.php

From: Date: Thu, 03 Apr 2003 08:56:14 +0000
Subject: cvs: pear /HTML_Template_Flexy Flexy.php /HTML_Template_Flexy/Flexy QuickForm.php /HTML_Template_Flexy/Flexy/Token Tag.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-9715@lists.php.net to get a copy of this message
alan_k Thu Apr 3 03:56:14 2003 EDT Modified files: /pear/HTML_Template_Flexy Flexy.php /pear/HTML_Template_Flexy/Flexy QuickForm.php /pear/HTML_Template_Flexy/Flexy/Token Tag.php Log: major overhall of flexy to quickforms handling - now stores arguments to generate html_quickforms, rather than serialized view of quickforms

Index: pear/HTML_Template_Flexy/Flexy.php diff -u pear/HTML_Template_Flexy/Flexy.php:1.22 pear/HTML_Template_Flexy/Flexy.php:1.23 --- pear/HTML_Template_Flexy/Flexy.php:1.22 Fri Mar 14 04:13:50 2003 +++ pear/HTML_Template_Flexy/Flexy.php Thu Apr 3 03:56:13 2003 @@ -42,7 +42,7 @@ * - heavily focused on displaying objects as pages. * (so you can document your tags.) * -* @version $Id: Flexy.php,v 1.22 2003/03/14 09:13:50 alan_k Exp $ +* @version $Id: Flexy.php,v 1.23 2003/04/03 08:56:13 alan_k Exp $ */ class HTML_Template_Flexy { @@ -383,8 +383,12 @@ if($this->quickform && ($cfp = fopen( $this->quickformFile, 'w') ) ) { - fwrite($cfp,serialize($this->quickform)); + //echo "<PRE>";print_r($this->quickform);echo "</PRE>"; + + fwrite($cfp,serialize($this->quickform->elementDefArray)); fclose($cfp); + // now clear it. + $this->quickform = false; } return true; @@ -646,21 +650,17 @@ return false; } require_once 'HTML/Template/Flexy/QuickForm.php'; - if (!$this->quickform) { - $this->quickform = HTML_Template_Flexy_QuickForm::loadFromSerialFile($this->quickformFile); + if (is_object($data)) { + $data = (array)$data; } - - if ($data === false) { - return; + if (!is_array($data)) { + $data = array(); } - if (is_array($data)) { - $this->quickform->setDefaults($data); - } - if (is_object($data)) { - $this->quickform->setDefaults((array)$data); - } + $this->quickform = HTML_Template_Flexy_QuickForm::loadFromSerialFile($this->quickformFile,$data); + + } Index: pear/HTML_Template_Flexy/Flexy/QuickForm.php diff -u pear/HTML_Template_Flexy/Flexy/QuickForm.php:1.3 pear/HTML_Template_Flexy/Flexy/QuickForm.php:1.4 --- pear/HTML_Template_Flexy/Flexy/QuickForm.php:1.3 Fri Mar 14 03:09:06 2003 +++ pear/HTML_Template_Flexy/Flexy/QuickForm.php Thu Apr 3 03:56:13 2003 @@ -24,7 +24,7 @@ * @abstract * Provides useful public methods for HTML_Template_Flexy to work with HTML_QuickForm * -* @version $Id: QuickForm.php,v 1.3 2003/03/14 08:09:06 alan_k Exp $ +* @version $Id: QuickForm.php,v 1.4 2003/04/03 08:56:13 alan_k Exp $ */ class HTML_Template_Flexy_QuickForm extends HTML_QuickForm { @@ -38,12 +38,41 @@ * @access private (although this doesnt make much sense..) */ var $_elementTemplate = - '<!-- BEGIN required --><div class="QuickFormRequired">*</div><!-- END required --> - <!-- BEGIN error --><div class="QuickFormError">{error}</div><br><!-- END error --> - {element}'; + '{label}{element} + <!-- BEGIN required --><span class="QuickFormRequired">*</span><!-- END required --> <!-- BEGIN error --><div class="QuickFormError">{error}</div><!-- END error -->'; + + /* ----------------------- PUBLIC METHODS - used outside engine. ---------------------------- */ + + /** + * get a reference to the Elements (so you can modify them/ set stuff) + * + * @return array associative array of elementsname => element (by reference) + * @access public + */ + function &getElements() + { + return $this->_elements; + } + + /** + * flag an element as hidden (so do not display it) + * + * @param string element name - name of element to hide + * @return none + * @access public + */ + function hideElement($elementname) + { + $this->_elements[$this->_elementIndex[$elementname]]->hide = true;; + } + + + /* ----------------------- semi private methods - used by engine. ---------------------------- */ + + /** * get the HTML for the the form head (including javascript and hidden elements) * @@ -75,18 +104,30 @@ function elementToHtml($elementname) { + if (isset($this->_elements[$this->_elementIndex[$elementname]]->hide) && + $this->_elements[$this->_elementIndex[$elementname]]->hide) { + return ''; + } return $this->_buildElement($this->_elements[$this->_elementIndex[$elementname]]); } + + + var $elementDefArray = array(); /** - * get a reference to the Elements (so you can modify them/ set stuff) + * get the HTML for an element by name. * - * @return array associative array of elementsname => element (by reference) + * @param string name of element to get HTML for + * + * @return string * @access public */ - function &getElements() + + function addElementDef() { - return $this->_elements; + $this->elementDefArray[] = func_get_args(); } + + /** * load cached quickform (this) from a file - and automatically reload the classes required * for the elements @@ -99,32 +140,62 @@ * @return object HTML_Template_Flexy_QuickForm * @access public */ - function loadFromSerialFile($filename) + function loadFromSerialFile($filename,$defaults) { // does our file exist. if (!file_exists($filename)) { return PEAR::raiseError('Flexy Quickform wrapper attempted to load non existent file :'. $filename,null,PEAR_ERROR_DIE); } + $ret = false; + // double load defintion for quickform.. $data = unserialize(file_get_contents($filename)); - foreach($data->_elements as $e=>$object) { - $dd = (array) $object; - if (!isset($dd['__PHP_Incomplete_Class_Name'])) { + //echo "<PRE>LOAD:";print_r($data);echo "</PRE>"; + foreach($data as $array) { + //echo "<PRE>PARSE:";print_r($array);echo "</PRE>"; + if ($array[0][0] == 'form') { + $ret = new HTML_Template_Flexy_QuickForm; + array_shift($array[0]); + call_user_func_array(array($ret,'HTML_QuickForm'), $array[0]); + $ret->setDefaults($defaults); + continue; + } + if (!$ret) { + // technically this is an error condition. continue; } - $class = $dd['__PHP_Incomplete_Class_Name']; - if (class_exists($class)) { + if ($array[0][0] == 'addRule' || $array[0][0] == 'addFilter' ) { + $method = array_shift($array[0]); + //echo "<PRE>addrule";print_r(array(array($method), $array[0]));echo "</PRE>"; + $rr = call_user_func_array(array(&$ret,$method), $array[0]); + //echo "<PRE>addrule";print_r($rr);echo "</PRE>"; continue; } - //print_r($object); - $classFilename = str_replace('html_quickform_','',$class) . '.php'; - //echo "class $class fileme : $filename"; - require_once 'HTML/QuickForm/' . $classFilename; + + + $e = call_user_func_array(array($ret,'createElement'),$array[0]); + //array_pop($ret->_elements); + if (isset($array[2])) { // options.. + foreach ($array[2] as $v) { + $e->addOption($v[0],$v[1]); + } + } + if (!isset($array[1])) { + $ret->addElement($e); + continue; + } + foreach ($array[1] as $k=>$v) { + //echo "<PRE>USR FUNC:";print_r(array( array($e,$k),$v));echo "</PRE>"; + call_user_func(array($e,$k),$v); + } + $ret->addElement($e); } - return unserialize(file_get_contents($filename)); + // echo "<PRE>RETURN:";print_r($ret);echo "</PRE>"; + + return $ret; } } Index: pear/HTML_Template_Flexy/Flexy/Token/Tag.php diff -u pear/HTML_Template_Flexy/Flexy/Token/Tag.php:1.27 pear/HTML_Template_Flexy/Flexy/Token/Tag.php:1.28 --- pear/HTML_Template_Flexy/Flexy/Token/Tag.php:1.27 Wed Apr 2 21:18:53 2003 +++ pear/HTML_Template_Flexy/Flexy/Token/Tag.php Thu Apr 3 03:56:13 2003 @@ -16,7 +16,7 @@ // | Authors: Alan Knowles <alan@akbkhome> | // +----------------------------------------------------------------------+ // -// $Id: Tag.php,v 1.27 2003/04/03 02:18:53 alan_k Exp $ +// $Id: Tag.php,v 1.28 2003/04/03 08:56:13 alan_k Exp $ $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN_TAG']['activeSelect'] = false; @@ -369,52 +369,33 @@ switch ($type) { case "CHECKBOX": - $e = &$_HTML_TEMPLATE_FLEXY['quickform']->addElement( - 'checkbox', - $name, - '' , // label? - '' , // test - $this->getAttributes() // wrapper needed... + $e = &$_HTML_TEMPLATE_FLEXY['quickform']->addElementDef( + array('checkbox', $name, $this->getAttribute('FLEXY:LABEL') , '' , $this->getAttributes() ), + array('setChecked' => $this->getAttribute('CHECKED')) ); - // technically this should be a bit more complex - // it needs to compare the 'value' field of the checkbox - // against the current value. - $e->setChecked($this->getAttribute('CHECKED')); break; case "RESET": - $e = &$_HTML_TEMPLATE_FLEXY['quickform']->addElement( - 'reset' , - $name, - $this->getAttribute('VALUE') , - $this->getAttributes() // wrapper needed... + $e = &$_HTML_TEMPLATE_FLEXY['quickform']->addElementDef( + array( 'reset' , $name, $this->getAttribute('VALUE') , $this->getAttributes() ) ); break; case "SUBMIT": - $e = &$_HTML_TEMPLATE_FLEXY['quickform']->addElement( - 'submit' , - $name, - $this->getAttribute('VALUE') , // the text. - $this->getAttributes() // wrapper needed... + $e = &$_HTML_TEMPLATE_FLEXY['quickform']->addElementDef( + array( 'submit' , $name, $this->getAttribute('VALUE') , $this->getAttributes() ) ); break; case "BUTTON": - $e = &$_HTML_TEMPLATE_FLEXY['quickform']->addElement( - 'button' , - $name, - $this->getAttribute('VALUE') , - $this->getAttributes() // wrapper needed... + $e = &$_HTML_TEMPLATE_FLEXY['quickform']->addElementDef( + array('button' , $name, $this->getAttribute('VALUE') , $this->getAttributes() ) ); break; case "PASSWORD": - $e = &$_HTML_TEMPLATE_FLEXY['quickform']->addElement( - 'password' , - $name, - '' , - $this->getAttributes() // wrapper needed... + $e = &$_HTML_TEMPLATE_FLEXY['quickform']->addElementDef( + array('password' , $name, '' , $this->getAttributes()) ); break; @@ -423,27 +404,23 @@ case "HIDDEN": - $e = &$_HTML_TEMPLATE_FLEXY['quickform']->addElement( - 'hidden' , - $name, - $this->getAttribute('VALUE'), - $this->getAttributes() // wrapper needed... + $e = &$_HTML_TEMPLATE_FLEXY['quickform']->addElementDef( + array('hidden' , $name,$this->getAttribute('VALUE'),$this->getAttributes()), + array('setValue' => $this->getAttribute('VALUE')) ); - $e->setValue($this->getAttribute('VALUE')); // hidden elements are displayed after the form tag. return ''; default: - $e = &$_HTML_TEMPLATE_FLEXY['quickform']->addElement( - 'text' , - $name, - '' , // the text. - $this->getAttributes() // wrapper needed... + $e = &$_HTML_TEMPLATE_FLEXY['quickform']->addElementDef( + array('text' ,$name,'', $this->getAttributes()), + array( + 'setSize' => $this->getAttribute('SIZE'), + 'setMaxLength' => $this->getAttribute('MAXLENGTH'), + 'setValue' => $this->getAttribute('VALUE') + ) ); - $e->setSize($this->getAttribute('SIZE')); - $e->setMaxLength($this->getAttribute('MAXLENGTH')); - $e->setValue($this->getAttribute('VALUE')); break; } @@ -471,16 +448,15 @@ if ($GLOBALS['_HTML_TEMPLATE_FLEXY']['quickform']) { - $e = &$GLOBALS['_HTML_TEMPLATE_FLEXY']['quickform']->addElement( - 'textarea', - $this->getAttribute('NAME'), - '' , // the text. - $this->getAttributes() // wrapper needed... + $e = &$GLOBALS['_HTML_TEMPLATE_FLEXY']['quickform']->addElementDef( + array( 'textarea', $this->getAttribute('NAME'),'', $this->getAttributes()), + array( + 'setValue' => $this->childrenToString(), + 'setWrap' => $this->getAttribute('WRAP'), + 'setRows' => $this->getAttribute('ROWS'), + 'setCols' => $this->getAttribute('COLS') + ) ); - $e->setValue($this->childrenToString()); - $e->setWrap($this->getAttribute('WRAP')); - $e->setRows($this->getAttribute('ROWS')); - $e->setCols($this->getAttribute('COLS')); } else { return false; } @@ -507,24 +483,28 @@ $name = $this->getAttribute('NAME'); // this ones for quickforms... - + $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN_TAG']['selectOptions'] = false; if ($GLOBALS['_HTML_TEMPLATE_FLEXY']['quickform']) { - - $e = &$GLOBALS['_HTML_TEMPLATE_FLEXY']['quickform']->addElement( - 'select', - $this->getAttribute('NAME'), - '' // , // the text. - //$this->getAttributes() // wrapper needed... - ); - $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN_TAG']['activeSelect'] = &$e; - $e->setSize($this->getAttribute('SIZE')); - $e->setMultiple($this->getAttribute('MULTIPLE')); + + + $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN_TAG']['selectOptions'] = array(); + $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN_TAG']['selectOptionSelected'] = ''; } else { return false; } // build the options. $this->childrenToString(); - + + $GLOBALS['_HTML_TEMPLATE_FLEXY']['quickform']->addElementDef( + array('select', $this->getAttribute('NAME'), ''), + array( + 'setSize' => $this->getAttribute('SIZE'), + 'setMultiple' => $this->getAttribute('MULTIPLE'), + 'SetSelected' => $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN_TAG']['selectOptionSelected'] + ), + $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN_TAG']['selectOptions'] // options.. + ); + // hopefully this will clear the reference and not the original.. unset($GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN_TAG']['activeSelect']); $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN_TAG']['activeSelect'] = false; @@ -555,10 +535,10 @@ $value = $text; } - if (!empty($_HTML_TEMPLATE_FLEXY_TOKEN_TAG['activeSelect'])) { - $_HTML_TEMPLATE_FLEXY_TOKEN_TAG['activeSelect']->addOption($text, $value); + if ($_HTML_TEMPLATE_FLEXY_TOKEN_TAG['selectOptions'] !== false) { + $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN_TAG']['selectOptions'][] = array($text, $value); if ($this->getAttribute('SELECTED')) { - $_HTML_TEMPLATE_FLEXY_TOKEN_TAG['activeSelect']->setSelected( $value); + $GLOBALS['_HTML_TEMPLATE_FLEXY_TOKEN_TAG']['selectOptionSelected'] = $value; } } else { return false; @@ -590,21 +570,24 @@ require_once 'HTML/Template/Flexy/QuickForm.php'; - $GLOBALS['_HTML_TEMPLATE_FLEXY']['quickform'] = new HTML_Template_Flexy_QuickForm( - $this->getAttribute('NAME'), - $this->getAttribute('METHOD'), - $this->getAttribute('ACTION'), - $this->getAttribute('TARGET') , - $this->getAttributes() // need to do some filtering on this.. + $GLOBALS['_HTML_TEMPLATE_FLEXY']['quickform'] = new HTML_Template_Flexy_QuickForm; + $GLOBALS['_HTML_TEMPLATE_FLEXY']['quickform']->addElementDef( + array( + 'form', + $this->getAttribute('NAME'), + $this->getAttribute('METHOD'), + $this->getAttribute('ACTION'), + $this->getAttribute('TARGET') , + $this->getAttributes() // need to do some filtering on this.. + ) ); - return '<?php echo $this->quickform->formHeadToHtml(); ?>' . $this->childrenToString() . '</form>'; - + //print_r($GLOBALS['_HTML_TEMPLATE_FLEXY']['quickform']); } @@ -718,6 +701,9 @@ function getAttributes() { $ret = array(); foreach($this->attributes as $k=>$v) { + if (substr($k,0,6) == 'FLEXY:') { + continue; + } $ret[strtolower($k)] = $this->getAttribute($k); } return $ret; @@ -743,13 +729,23 @@ $name = '__ALL__'; } foreach($this->attributes as $k=>$v) { - if (substr($k,0,4) == 'RULE') { + if (substr($k,0,6) != 'FLEXY:') { + continue; + } + $kk = substr($k,6); + + if (substr($kk,0,4) == 'RULE') { $args = explode('|',$this->getAttribute($k)); array_unshift($args,$name); - call_user_func_array(array(&$_HTML_TEMPLATE_FLEXY['quickform'],'addRule'), $args); + array_unshift($args,'addRule'); + + $_HTML_TEMPLATE_FLEXY['quickform']->addElementDef($args); } - if (substr($k,0,4) == 'FILTER') { - $_HTML_TEMPLATE_FLEXY['quickform']->addFilter($name,$this->getAttribute($k)); + if (substr($kk,0,4) == 'FILTER') { + $_HTML_TEMPLATE_FLEXY['quickform']->addElementDef( + array('addFilter',$name,$this->getAttribute($k)) + ); + } } }
« previous php.pear.cvs (#9715) next »