123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- /**
- *
- * Cube Framework $Id$ qk/6J+GPF1Pt28aLNyv9zsYTG46MYjjBSsTUc7l7TFs=
- *
- * @link http://codecu.be/framework
- * @copyright Copyright (c) 2014 CodeCube SRL
- * @license http://codecu.be/framework/license Commercial License
- *
- * @version 1.0
- */
- /**
- * hidden form element generator class
- */
- namespace Cube\Form\Element;
- use Cube\Form\Element;
- class Hidden extends Element
- {
- /**
- * new line character
- */
- const NL = "\n";
- /**
- *
- * type of element - override the variable from the parent class
- *
- * @var string
- */
- protected $_element = 'hidden';
- /**
- *
- * whether to add keys to multiple elements or not
- *
- * @var bool
- */
- protected $_forceCountMultiple = false;
- /**
- *
- * class constructor
- *
- * @param string $name
- */
- public function __construct($name)
- {
- parent::__construct($this->_element, $name);
- }
- /**
- *
- * set force count multiple flag
- *
- * @param boolean $forceCountMultiple
- *
- * @return $this
- */
- public function setForceCountMultiple($forceCountMultiple)
- {
- $this->_forceCountMultiple = (bool)$forceCountMultiple;
- return $this;
- }
- /**
- *
- * get force count multiple flag
- *
- * @return boolean
- */
- public function getForceCountMultiple()
- {
- return $this->_forceCountMultiple;
- }
- /**
- *
- * renders the html form element
- * the method is run by all subclasses who don't have it overridden
- * if a post value is an array, we will render a multiple element
- * for serialized data, we change the brackets to ' rather than "
- *
- * @return string the html code of the element
- */
- public function render()
- {
- $render = array();
- $value = $this->getValue();
- if (is_array($value)) {
- $this->setMultiple(true);
- }
- else {
- $value = array($value);
- }
- foreach ($value as $key => $val) {
- $forceCountMultiple = $this->getForceCountMultiple();
- $multiple = ($this->getMultiple() === true) ?
- ((is_string($key) || $forceCountMultiple) ? '[' . $key . ']' : $this->_brackets) : '';
- if (is_array($val)) {
- $element = new Hidden($this->_name . $multiple);
- $element->setForceCountMultiple($forceCountMultiple)
- ->setValue($val);
- $render[] = $element->render();
- }
- else {
- $render[] = '<input type="' . $this->_type . '" '
- . 'name="' . $this->_name . $multiple . '" '
- . $this->renderAttributes()
- . "value='" . $val . "' "
- . $this->_endTag;
- }
- }
- return implode(self::NL, $render);
- }
- }
|