* @version 0.3 * @since 0.1 * @copyright GNU Lesser General Public License (LGPL) version 3 */ /** * HtmlFormHelper * * HTML form fields helper */ class HtmlFormHelper extends Helper { // protected $fc; public function __construct($obj=null) { parent::__construct($obj); // $this->fc = FrontController::getInstance(); } /** * generates a complete form for model object given to constructor */ public function iAutoForm($name, $method='post', $action='', $class='') { if (!isset($this->obj)) { throw AppException('Can not generate form : no object given in Helper'); } $str = self::iForm($name, $method, $action); $str .= $this->obj->iFields($class); $str .= ''; return $str; } /** * returns a
tag * @param string $name name of form (id will be i_$name) * @param string $method post, get, or file (post by default) * @param string $action URL to which form is sent (current page by default) */ public static function iForm($name, $method='post', $action='', $class='') { $fc = FrontController::getInstance(); if (!$action) { $action = $fc->thisUrl(); } $str = ''; return $str; } /** * generates form field (only selected fields) */ public function iFields($class='side') { if (!isset($this->obj)) { throw AppException('Can not generate fields : no object given in Helper'); } $str = '
    '; foreach ($this->obj->getFields() as $key => $type) { $type = substr($type,0,3); // error_log(get_class($this->obj).' iFields : '.$key.' / '.$type); if ($type == 'UID') { // put UID in hidden field before
      $str = $this->iHidden($key).$str; continue; } else if ($this->obj->isObjectProperty($key, $class, $nkey)) { // get a simple input field for the ID // error_log(get_class($this->obj).' iFields found '.$key.' as an object of '.$class.' ('.$nkey.')'); $obj = new $class; $obj->addHelper('html_form'); $str .= '
    1. ' .$this->obj->iNested('id', $nkey); } else if ($sf = $this->iFieldLabelled($key, $key, $type)) { $str .= $sf; } } $str .= '
    2. '; $str .= '
    '; return $str; } /** * returns a field along with LABEL, all wrapped in a LI * @todo missing types */ public function iFieldLabelled($key, $label='', $type='', $wrap='li') { if (empty($type)) { $type = $this->obj->getPropertyType($key); } $sf = ''; switch($type) { case 'UID': //-TODO- hidden ? case 'INT': case 'NUM': case 'DEC': case 'STR': case 'EML': case 'URL': case 'TIM': // -TODO- $sf = $this->iText($key); break; case 'DTE': $sf = $this->iDate($key); break; case 'DTM': $sf = $this->iDateTime($key); break; case 'USR': $sf = $this->iText($key); break; case 'PSS': $sf = $this->iPass($key); break; case 'TXT': case 'BBS': // -TODO- case 'HTM': // -TODO- case 'XML': // -TODO- $sf = $this->iTextArea($key); break; case 'BOL': $sf = $this->iCheckBox($key); break; case 'LVL': // -TODO- case 'DOC': // -TODO- case 'IMG': // -TODO- case 'OBJ': // -TODO- break; } if (!$sf) { return false; } if (empty($label)) { $label = str_replace('_',' ',$key); } return $this->_htmlWrapBegin($wrap) .'' .$sf .$this->_htmlWrapEnd($wrap); } /* * generates a hidden field */ public function iHidden($key) { $str = ''; return $str; } /* * generates a text field */ public function iText($key) { $str = ''; if ($err = $this->_htmlError($key)) { $str .= $err; } return $str; } /* * generates a text field */ public function iNested($key, $nested) { $str = ''; if ($err = $this->_htmlError($key)) { $str .= $err; } return $str; } /** * generates date field * @todo real date field */ public function iDate($key) { $str = ''; if ($err = $this->_htmlError($key)) { $str .= $err; } return $str; } /** * generates date time field * @todo real date time field */ public function iDateTime() { $args = func_get_args(); return call_user_func_array(array($this,'iText'),$args); } /** * generates a password field */ public function iPass($key, $autocomplete=true) { $str = '_htmlError($key)) { $str .= $err; } return $str; } /** * generates a simple text area */ public function iTextArea($key) { $str = ''; if ($err = $this->_htmlError($key)) { $str .= $err; } return $str; } /** * generates a check box */ public function iCheckBox($key, $label='') { $str = '_value($key)) { $str .= ' checked="checked"'; } $str .= ' value="1" />'; if (empty($label)) { $options = $this->obj->getPropertyOptions($key); if (!empty($options['label'])) { // -TODO- translate $label = $options['label']; } } if (!empty($label)) { $str .= ''; } if ($err = $this->_htmlError($key)) { $str .= $err; } return $str; } /** * generates a series of radio inputs */ public function iRadio($key, $options='', $wrap='ul', $subwrap='li') { if (isset($this->obj)) { $options = $this->obj->getPropertyOptions($key); } if (empty($options['options'])) { FC::log_warn("iRadio $key does not provide options"); return $key.' (no option)'; } $str = $this->_htmlWrapBegin($wrap); $i = 1; foreach($options['options'] as $val => $lbl) { $str .= $this->_htmlWrapBegin($subwrap); $str .= ''; $str .= ''; $str .= $this->_htmlWrapEnd($subwrap); $i++; } return $str.$this->_htmlWrapEnd($wrap); } /** * generates a select (drop down) */ public function iSelect($key, $options='') { if (isset($this->obj)) { $options = $this->obj->getPropertyOptions($key); } if (empty($options['options'])) { FC::log_warn("iRadio $key does not provide options"); return $key.' (no option)'; } $str = ''; } /** * generates a select (drop down) */ public function iSelectTranslate($key, $section, $options='') { if (isset($this->obj)) { $options = $this->obj->getPropertyOptions($key); } if (empty($options['options'])) { FC::log_warn("iRadio $key does not provide options"); return $key.' (no option)'; } $str = ''; } /** * generates a time zone drop down list */ public function iTimeZone($key, $options='') { if (isset($this->obj)) { $default = $this->obj->get($key); // $options = $this->obj->getPropertyOptions($key); } $timezone_identifiers = DateTimeZone::listIdentifiers(); $arrTz = $arrCt = array(); $continent = ''; foreach( $timezone_identifiers as $value ) { if (!preg_match( '/^(Africa|America|Antartica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)\//', $value )) { continue; } $arrZn = explode('/', $value); if ($continent != $arrZn[0]) { if (count($arrCt)) { ksort($arrCt); $arrTz[$continent] = $arrCt; } $continent = $arrZn[0]; $arrCt = array(); } $city = str_replace('_',' ',(count($arrZn)>2)?$arrZn[2]:$arrZn[1]); $arrCt[$city] = $value; } $str = ''; } /** * returns a form field formatted value */ protected function _value($key) { if (!isset($this->obj)) { return ''; } return $this->obj->value($key); } /** * returns error (HTML formatted) */ protected function _htmlError($key) { if (!isset($this->obj)) { return ''; } return $this->obj->htmlError($key); } /** * HTML wrapper begin tag */ protected function _htmlWrapBegin($wrap) { if ($wrap) { return '<'.$wrap.'>'; } else { return ''; } } /** * HTML wrapper end tag */ protected function _htmlWrapEnd($wrap) { if ($wrap) { if ($idx = strpos($wrap, ' ')) { $wrap = substr($wrap,0,$idx); } return ''; } else { return ''; } } }