DisplayGroup.php 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Zend_Form_DisplayGroup
  22. *
  23. * @category Zend
  24. * @package Zend_Form
  25. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. * @version $Id$
  28. */
  29. class Zend_Form_DisplayGroup implements Iterator,Countable
  30. {
  31. /**
  32. * Group attributes
  33. * @var array
  34. */
  35. protected $_attribs = array();
  36. /**
  37. * Display group decorators
  38. * @var array
  39. */
  40. protected $_decorators = array();
  41. /**
  42. * Description
  43. * @var string
  44. */
  45. protected $_description;
  46. /**
  47. * Should we disable loading the default decorators?
  48. * @var bool
  49. */
  50. protected $_disableLoadDefaultDecorators = false;
  51. /**
  52. * Element order
  53. * @var array
  54. */
  55. protected $_elementOrder = array();
  56. /**
  57. * Elements
  58. * @var array
  59. */
  60. protected $_elements = array();
  61. /**
  62. * Whether or not a new element has been added to the group
  63. * @var bool
  64. */
  65. protected $_groupUpdated = false;
  66. /**
  67. * Plugin loader for decorators
  68. * @var Zend_Loader_PluginLoader
  69. */
  70. protected $_loader;
  71. /**
  72. * Group name
  73. * @var string
  74. */
  75. protected $_name;
  76. /**
  77. * Group order
  78. * @var int
  79. */
  80. protected $_order;
  81. /**
  82. * @var Zend_Translate
  83. */
  84. protected $_translator;
  85. /**
  86. * Is translation disabled?
  87. * @var bool
  88. */
  89. protected $_translatorDisabled = false;
  90. /**
  91. * @var Zend_View_Interface
  92. */
  93. protected $_view;
  94. /**
  95. * Constructor
  96. *
  97. * @param string $name
  98. * @param Zend_Loader_PluginLoader $loader
  99. * @param array|Zend_Config $options
  100. * @return void
  101. */
  102. public function __construct($name, Zend_Loader_PluginLoader $loader, $options = null)
  103. {
  104. $this->setName($name);
  105. $this->setPluginLoader($loader);
  106. if (is_array($options)) {
  107. $this->setOptions($options);
  108. } elseif ($options instanceof Zend_Config) {
  109. $this->setConfig($options);
  110. }
  111. // Extensions...
  112. $this->init();
  113. $this->loadDefaultDecorators();
  114. }
  115. /**
  116. * Initialize object; used by extending classes
  117. *
  118. * @return void
  119. */
  120. public function init()
  121. {
  122. }
  123. /**
  124. * Set options
  125. *
  126. * @param array $options
  127. * @return Zend_Form_DisplayGroup
  128. */
  129. public function setOptions(array $options)
  130. {
  131. $forbidden = array(
  132. 'Options', 'Config', 'PluginLoader', 'View',
  133. 'Translator', 'Attrib'
  134. );
  135. foreach ($options as $key => $value) {
  136. $normalized = ucfirst($key);
  137. if (in_array($normalized, $forbidden)) {
  138. continue;
  139. }
  140. $method = 'set' . $normalized;
  141. if (method_exists($this, $method)) {
  142. $this->$method($value);
  143. } else {
  144. $this->setAttrib($key, $value);
  145. }
  146. }
  147. return $this;
  148. }
  149. /**
  150. * Set options from config object
  151. *
  152. * @param Zend_Config $config
  153. * @return Zend_Form_DisplayGroup
  154. */
  155. public function setConfig(Zend_Config $config)
  156. {
  157. return $this->setOptions($config->toArray());
  158. }
  159. /**
  160. * Set group attribute
  161. *
  162. * @param string $key
  163. * @param mixed $value
  164. * @return Zend_Form_DisplayGroup
  165. */
  166. public function setAttrib($key, $value)
  167. {
  168. $key = (string) $key;
  169. $this->_attribs[$key] = $value;
  170. return $this;
  171. }
  172. /**
  173. * Add multiple form attributes at once
  174. *
  175. * @param array $attribs
  176. * @return Zend_Form_DisplayGroup
  177. */
  178. public function addAttribs(array $attribs)
  179. {
  180. foreach ($attribs as $key => $value) {
  181. $this->setAttrib($key, $value);
  182. }
  183. return $this;
  184. }
  185. /**
  186. * Set multiple form attributes at once
  187. *
  188. * Overwrites any previously set attributes.
  189. *
  190. * @param array $attribs
  191. * @return Zend_Form_DisplayGroup
  192. */
  193. public function setAttribs(array $attribs)
  194. {
  195. $this->clearAttribs();
  196. return $this->addAttribs($attribs);
  197. }
  198. /**
  199. * Retrieve a single form attribute
  200. *
  201. * @param string $key
  202. * @return mixed
  203. */
  204. public function getAttrib($key)
  205. {
  206. $key = (string) $key;
  207. if (!isset($this->_attribs[$key])) {
  208. return null;
  209. }
  210. return $this->_attribs[$key];
  211. }
  212. /**
  213. * Retrieve all form attributes/metadata
  214. *
  215. * @return array
  216. */
  217. public function getAttribs()
  218. {
  219. return $this->_attribs;
  220. }
  221. /**
  222. * Remove attribute
  223. *
  224. * @param string $key
  225. * @return bool
  226. */
  227. public function removeAttrib($key)
  228. {
  229. if (array_key_exists($key, $this->_attribs)) {
  230. unset($this->_attribs[$key]);
  231. return true;
  232. }
  233. return false;
  234. }
  235. /**
  236. * Clear all form attributes
  237. *
  238. * @return Zend_Form
  239. */
  240. public function clearAttribs()
  241. {
  242. $this->_attribs = array();
  243. return $this;
  244. }
  245. /**
  246. * Filter a name to only allow valid variable characters
  247. *
  248. * @param string $value
  249. * @return string
  250. */
  251. public function filterName($value)
  252. {
  253. return preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '', (string) $value);
  254. }
  255. /**
  256. * Set group name
  257. *
  258. * @param string $name
  259. * @return Zend_Form_DisplayGroup
  260. */
  261. public function setName($name)
  262. {
  263. $name = $this->filtername($name);
  264. if (('0' !== $name) && empty($name)) {
  265. require_once 'Zend/Form/Exception.php';
  266. throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty');
  267. }
  268. $this->_name = $name;
  269. return $this;
  270. }
  271. /**
  272. * Retrieve group name
  273. *
  274. * @return string
  275. */
  276. public function getName()
  277. {
  278. return $this->_name;
  279. }
  280. /**
  281. * Get fully qualified name
  282. *
  283. * Places name as subitem of array and/or appends brackets.
  284. *
  285. * @return string
  286. */
  287. public function getFullyQualifiedName()
  288. {
  289. return $this->getName();
  290. }
  291. /**
  292. * Get element id
  293. *
  294. * @return string
  295. */
  296. public function getId()
  297. {
  298. if (isset($this->id)) {
  299. return $this->id;
  300. }
  301. $id = $this->getFullyQualifiedName();
  302. // Bail early if no array notation detected
  303. if (!strstr($id, '[')) {
  304. return $id;
  305. }
  306. // Strip array notation
  307. if ('[]' == substr($id, -2)) {
  308. $id = substr($id, 0, strlen($id) - 2);
  309. }
  310. $id = str_replace('][', '-', $id);
  311. $id = str_replace(array(']', '['), '-', $id);
  312. $id = trim($id, '-');
  313. return $id;
  314. }
  315. /**
  316. * Set group legend
  317. *
  318. * @param string $legend
  319. * @return Zend_Form_DisplayGroup
  320. */
  321. public function setLegend($legend)
  322. {
  323. return $this->setAttrib('legend', (string) $legend);
  324. }
  325. /**
  326. * Retrieve group legend
  327. *
  328. * @return string
  329. */
  330. public function getLegend()
  331. {
  332. return $this->getAttrib('legend');
  333. }
  334. /**
  335. * Set description
  336. *
  337. * @param string $value
  338. * @return Zend_Form_DisplayGroup
  339. */
  340. public function setDescription($value)
  341. {
  342. $this->_description = (string) $value;
  343. return $this;
  344. }
  345. /**
  346. * Get description
  347. *
  348. * @return string
  349. */
  350. public function getDescription()
  351. {
  352. return $this->_description;
  353. }
  354. /**
  355. * Set group order
  356. *
  357. * @param int $order
  358. * @return Zend_Form_Element
  359. */
  360. public function setOrder($order)
  361. {
  362. $this->_order = (int) $order;
  363. return $this;
  364. }
  365. /**
  366. * Retrieve group order
  367. *
  368. * @return int
  369. */
  370. public function getOrder()
  371. {
  372. return $this->_order;
  373. }
  374. // Elements
  375. /**
  376. * Add element to stack
  377. *
  378. * @param Zend_Form_Element $element
  379. * @return Zend_Form_DisplayGroup
  380. */
  381. public function addElement(Zend_Form_Element $element)
  382. {
  383. $this->_elements[$element->getName()] = $element;
  384. $this->_groupUpdated = true;
  385. return $this;
  386. }
  387. /**
  388. * Add multiple elements at once
  389. *
  390. * @param array $elements
  391. * @return Zend_Form_DisplayGroup
  392. * @throws Zend_Form_Exception if any element is not a Zend_Form_Element
  393. */
  394. public function addElements(array $elements)
  395. {
  396. foreach ($elements as $element) {
  397. if (!$element instanceof Zend_Form_Element) {
  398. require_once 'Zend/Form/Exception.php';
  399. throw new Zend_Form_Exception('elements passed via array to addElements() must be Zend_Form_Elements only');
  400. }
  401. $this->addElement($element);
  402. }
  403. return $this;
  404. }
  405. /**
  406. * Set multiple elements at once (overwrites)
  407. *
  408. * @param array $elements
  409. * @return Zend_Form_DisplayGroup
  410. */
  411. public function setElements(array $elements)
  412. {
  413. $this->clearElements();
  414. return $this->addElements($elements);
  415. }
  416. /**
  417. * Retrieve element
  418. *
  419. * @param string $name
  420. * @return Zend_Form_Element|null
  421. */
  422. public function getElement($name)
  423. {
  424. $name = (string) $name;
  425. if (isset($this->_elements[$name])) {
  426. return $this->_elements[$name];
  427. }
  428. return null;
  429. }
  430. /**
  431. * Retrieve elements
  432. * @return array
  433. */
  434. public function getElements()
  435. {
  436. return $this->_elements;
  437. }
  438. /**
  439. * Remove a single element
  440. *
  441. * @param string $name
  442. * @return boolean
  443. */
  444. public function removeElement($name)
  445. {
  446. $name = (string) $name;
  447. if (array_key_exists($name, $this->_elements)) {
  448. unset($this->_elements[$name]);
  449. $this->_groupUpdated = true;
  450. return true;
  451. }
  452. return false;
  453. }
  454. /**
  455. * Remove all elements
  456. *
  457. * @return Zend_Form_DisplayGroup
  458. */
  459. public function clearElements()
  460. {
  461. $this->_elements = array();
  462. $this->_groupUpdated = true;
  463. return $this;
  464. }
  465. // Plugin loader (for decorators)
  466. /**
  467. * Set plugin loader
  468. *
  469. * @param Zend_Loader_PluginLoader $loader
  470. * @return Zend_Form_DisplayGroup
  471. */
  472. public function setPluginLoader(Zend_Loader_PluginLoader $loader)
  473. {
  474. $this->_loader = $loader;
  475. return $this;
  476. }
  477. /**
  478. * Retrieve plugin loader
  479. *
  480. * @return Zend_Loader_PluginLoader
  481. */
  482. public function getPluginLoader()
  483. {
  484. return $this->_loader;
  485. }
  486. /**
  487. * Add a prefix path for the plugin loader
  488. *
  489. * @param string $prefix
  490. * @param string $path
  491. * @return Zend_Form_DisplayGroup
  492. */
  493. public function addPrefixPath($prefix, $path)
  494. {
  495. $this->getPluginLoader()->addPrefixPath($prefix, $path);
  496. return $this;
  497. }
  498. /**
  499. * Add several prefix paths at once
  500. *
  501. * @param array $spec
  502. * @return Zend_Form_DisplayGroup
  503. */
  504. public function addPrefixPaths(array $spec)
  505. {
  506. if (isset($spec['prefix']) && isset($spec['path'])) {
  507. return $this->addPrefixPath($spec['prefix'], $spec['path']);
  508. }
  509. foreach ($spec as $prefix => $paths) {
  510. if (is_numeric($prefix) && is_array($paths)) {
  511. $prefix = null;
  512. if (isset($paths['prefix']) && isset($paths['path'])) {
  513. $this->addPrefixPath($paths['prefix'], $paths['path']);
  514. }
  515. } elseif (!is_numeric($prefix)) {
  516. if (is_string($paths)) {
  517. $this->addPrefixPath($prefix, $paths);
  518. } elseif (is_array($paths)) {
  519. foreach ($paths as $path) {
  520. $this->addPrefixPath($prefix, $path);
  521. }
  522. }
  523. }
  524. }
  525. return $this;
  526. }
  527. // Decorators
  528. /**
  529. * Set flag to disable loading default decorators
  530. *
  531. * @param bool $flag
  532. * @return Zend_Form_Element
  533. */
  534. public function setDisableLoadDefaultDecorators($flag)
  535. {
  536. $this->_disableLoadDefaultDecorators = (bool) $flag;
  537. return $this;
  538. }
  539. /**
  540. * Should we load the default decorators?
  541. *
  542. * @return bool
  543. */
  544. public function loadDefaultDecoratorsIsDisabled()
  545. {
  546. return $this->_disableLoadDefaultDecorators;
  547. }
  548. /**
  549. * Load default decorators
  550. *
  551. * @return void
  552. */
  553. public function loadDefaultDecorators()
  554. {
  555. if ($this->loadDefaultDecoratorsIsDisabled()) {
  556. return $this;
  557. }
  558. $decorators = $this->getDecorators();
  559. if (empty($decorators)) {
  560. $this->addDecorator('FormElements')
  561. ->addDecorator('HtmlTag', array('tag' => 'dl'))
  562. ->addDecorator('Fieldset')
  563. ->addDecorator('DtDdWrapper');
  564. }
  565. return $this;
  566. }
  567. /**
  568. * Instantiate a decorator based on class name or class name fragment
  569. *
  570. * @param string $name
  571. * @param null|array $options
  572. * @return Zend_Form_Decorator_Interface
  573. */
  574. protected function _getDecorator($name, $options = null)
  575. {
  576. $class = $this->getPluginLoader()->load($name);
  577. if (null === $options) {
  578. $decorator = new $class;
  579. } else {
  580. $decorator = new $class($options);
  581. }
  582. return $decorator;
  583. }
  584. /**
  585. * Add a decorator for rendering the group
  586. *
  587. * @param string|Zend_Form_Decorator_Interface $decorator
  588. * @param array|Zend_Config $options Options with which to initialize decorator
  589. * @return Zend_Form_DisplayGroup
  590. */
  591. public function addDecorator($decorator, $options = null)
  592. {
  593. if ($decorator instanceof Zend_Form_Decorator_Interface) {
  594. $name = get_class($decorator);
  595. } elseif (is_string($decorator)) {
  596. $name = $decorator;
  597. $decorator = array(
  598. 'decorator' => $name,
  599. 'options' => $options,
  600. );
  601. } elseif (is_array($decorator)) {
  602. foreach ($decorator as $name => $spec) {
  603. break;
  604. }
  605. if (is_numeric($name)) {
  606. require_once 'Zend/Form/Exception.php';
  607. throw new Zend_Form_Exception('Invalid alias provided to addDecorator; must be alphanumeric string');
  608. }
  609. if (is_string($spec)) {
  610. $decorator = array(
  611. 'decorator' => $spec,
  612. 'options' => $options,
  613. );
  614. } elseif ($spec instanceof Zend_Form_Decorator_Interface) {
  615. $decorator = $spec;
  616. }
  617. } else {
  618. require_once 'Zend/Form/Exception.php';
  619. throw new Zend_Form_Exception('Invalid decorator provided to addDecorator; must be string or Zend_Form_Decorator_Interface');
  620. }
  621. $this->_decorators[$name] = $decorator;
  622. return $this;
  623. }
  624. /**
  625. * Add many decorators at once
  626. *
  627. * @param array $decorators
  628. * @return Zend_Form_DisplayGroup
  629. */
  630. public function addDecorators(array $decorators)
  631. {
  632. foreach ($decorators as $decoratorName => $decoratorInfo) {
  633. if (is_string($decoratorInfo) ||
  634. $decoratorInfo instanceof Zend_Form_Decorator_Interface) {
  635. if (!is_numeric($decoratorName)) {
  636. $this->addDecorator(array($decoratorName => $decoratorInfo));
  637. } else {
  638. $this->addDecorator($decoratorInfo);
  639. }
  640. } elseif (is_array($decoratorInfo)) {
  641. $argc = count($decoratorInfo);
  642. $options = array();
  643. if (isset($decoratorInfo['decorator'])) {
  644. $decorator = $decoratorInfo['decorator'];
  645. if (isset($decoratorInfo['options'])) {
  646. $options = $decoratorInfo['options'];
  647. }
  648. $this->addDecorator($decorator, $options);
  649. } else {
  650. switch (true) {
  651. case (0 == $argc):
  652. break;
  653. case (1 <= $argc):
  654. $decorator = array_shift($decoratorInfo);
  655. case (2 <= $argc):
  656. $options = array_shift($decoratorInfo);
  657. default:
  658. $this->addDecorator($decorator, $options);
  659. break;
  660. }
  661. }
  662. } else {
  663. require_once 'Zend/Form/Exception.php';
  664. throw new Zend_Form_Exception('Invalid decorator passed to addDecorators()');
  665. }
  666. }
  667. return $this;
  668. }
  669. /**
  670. * Overwrite all decorators
  671. *
  672. * @param array $decorators
  673. * @return Zend_Form_DisplayGroup
  674. */
  675. public function setDecorators(array $decorators)
  676. {
  677. $this->clearDecorators();
  678. return $this->addDecorators($decorators);
  679. }
  680. /**
  681. * Retrieve a registered decorator
  682. *
  683. * @param string $name
  684. * @return false|Zend_Form_Decorator_Abstract
  685. */
  686. public function getDecorator($name)
  687. {
  688. if (!isset($this->_decorators[$name])) {
  689. $len = strlen($name);
  690. foreach ($this->_decorators as $localName => $decorator) {
  691. if ($len > strlen($localName)) {
  692. continue;
  693. }
  694. if (0 === substr_compare($localName, $name, -$len, $len, true)) {
  695. if (is_array($decorator)) {
  696. return $this->_loadDecorator($decorator, $localName);
  697. }
  698. return $decorator;
  699. }
  700. }
  701. return false;
  702. }
  703. if (is_array($this->_decorators[$name])) {
  704. return $this->_loadDecorator($this->_decorators[$name], $name);
  705. }
  706. return $this->_decorators[$name];
  707. }
  708. /**
  709. * Retrieve all decorators
  710. *
  711. * @return array
  712. */
  713. public function getDecorators()
  714. {
  715. foreach ($this->_decorators as $key => $value) {
  716. if (is_array($value)) {
  717. $this->_loadDecorator($value, $key);
  718. }
  719. }
  720. return $this->_decorators;
  721. }
  722. /**
  723. * Remove a single decorator
  724. *
  725. * @param string $name
  726. * @return bool
  727. */
  728. public function removeDecorator($name)
  729. {
  730. $decorator = $this->getDecorator($name);
  731. if ($decorator) {
  732. if (array_key_exists($name, $this->_decorators)) {
  733. unset($this->_decorators[$name]);
  734. } else {
  735. $class = get_class($decorator);
  736. unset($this->_decorators[$class]);
  737. }
  738. return true;
  739. }
  740. return false;
  741. }
  742. /**
  743. * Clear all decorators
  744. *
  745. * @return Zend_Form_DisplayGroup
  746. */
  747. public function clearDecorators()
  748. {
  749. $this->_decorators = array();
  750. return $this;
  751. }
  752. /**
  753. * Set view
  754. *
  755. * @param Zend_View_Interface $view
  756. * @return Zend_Form_DisplayGroup
  757. */
  758. public function setView(Zend_View_Interface $view = null)
  759. {
  760. $this->_view = $view;
  761. return $this;
  762. }
  763. /**
  764. * Retrieve view
  765. *
  766. * @return Zend_View_Interface
  767. */
  768. public function getView()
  769. {
  770. if (null === $this->_view) {
  771. require_once 'Zend/Controller/Action/HelperBroker.php';
  772. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  773. $this->setView($viewRenderer->view);
  774. }
  775. return $this->_view;
  776. }
  777. /**
  778. * Render display group
  779. *
  780. * @return string
  781. */
  782. public function render(Zend_View_Interface $view = null)
  783. {
  784. if (null !== $view) {
  785. $this->setView($view);
  786. }
  787. $content = '';
  788. foreach ($this->getDecorators() as $decorator) {
  789. $decorator->setElement($this);
  790. $content = $decorator->render($content);
  791. }
  792. return $content;
  793. }
  794. /**
  795. * String representation of group
  796. *
  797. * @return string
  798. */
  799. public function __toString()
  800. {
  801. try {
  802. $return = $this->render();
  803. return $return;
  804. } catch (Exception $e) {
  805. trigger_error($e->getMessage(), E_USER_WARNING);
  806. return '';
  807. }
  808. }
  809. /**
  810. * Set translator object
  811. *
  812. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  813. * @return Zend_Form_DisplayGroup
  814. */
  815. public function setTranslator($translator = null)
  816. {
  817. if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
  818. $this->_translator = $translator;
  819. } elseif ($translator instanceof Zend_Translate) {
  820. $this->_translator = $translator->getAdapter();
  821. } else {
  822. require_once 'Zend/Form/Exception.php';
  823. throw new Zend_Form_Exception('Invalid translator specified');
  824. }
  825. return $this;
  826. }
  827. /**
  828. * Retrieve translator object
  829. *
  830. * @return Zend_Translate_Adapter|null
  831. */
  832. public function getTranslator()
  833. {
  834. if ($this->translatorIsDisabled()) {
  835. return null;
  836. }
  837. if (null === $this->_translator) {
  838. require_once 'Zend/Form.php';
  839. return Zend_Form::getDefaultTranslator();
  840. }
  841. return $this->_translator;
  842. }
  843. /**
  844. * Indicate whether or not translation should be disabled
  845. *
  846. * @param bool $flag
  847. * @return Zend_Form_DisplayGroup
  848. */
  849. public function setDisableTranslator($flag)
  850. {
  851. $this->_translatorDisabled = (bool) $flag;
  852. return $this;
  853. }
  854. /**
  855. * Is translation disabled?
  856. *
  857. * @return bool
  858. */
  859. public function translatorIsDisabled()
  860. {
  861. return $this->_translatorDisabled;
  862. }
  863. /**
  864. * Overloading: allow rendering specific decorators
  865. *
  866. * Call renderDecoratorName() to render a specific decorator.
  867. *
  868. * @param string $method
  869. * @param array $args
  870. * @return string
  871. * @throws Zend_Form_Exception for invalid decorator or invalid method call
  872. */
  873. public function __call($method, $args)
  874. {
  875. if ('render' == substr($method, 0, 6)) {
  876. $decoratorName = substr($method, 6);
  877. if (false !== ($decorator = $this->getDecorator($decoratorName))) {
  878. $decorator->setElement($this);
  879. $seed = '';
  880. if (0 < count($args)) {
  881. $seed = array_shift($args);
  882. }
  883. return $decorator->render($seed);
  884. }
  885. require_once 'Zend/Form/Exception.php';
  886. throw new Zend_Form_Exception(sprintf('Decorator by name %s does not exist', $decoratorName));
  887. }
  888. require_once 'Zend/Form/Exception.php';
  889. throw new Zend_Form_Exception(sprintf('Method %s does not exist', $method));
  890. }
  891. // Interfaces: Iterator, Countable
  892. /**
  893. * Current element
  894. *
  895. * @return Zend_Form_Element
  896. */
  897. public function current()
  898. {
  899. $this->_sort();
  900. current($this->_elementOrder);
  901. $key = key($this->_elementOrder);
  902. return $this->getElement($key);
  903. }
  904. /**
  905. * Current element
  906. *
  907. * @return string
  908. */
  909. public function key()
  910. {
  911. $this->_sort();
  912. return key($this->_elementOrder);
  913. }
  914. /**
  915. * Move pointer to next element
  916. *
  917. * @return void
  918. */
  919. public function next()
  920. {
  921. $this->_sort();
  922. next($this->_elementOrder);
  923. }
  924. /**
  925. * Move pointer to beginning of element loop
  926. *
  927. * @return void
  928. */
  929. public function rewind()
  930. {
  931. $this->_sort();
  932. reset($this->_elementOrder);
  933. }
  934. /**
  935. * Determine if current element/subform/display group is valid
  936. *
  937. * @return bool
  938. */
  939. public function valid()
  940. {
  941. $this->_sort();
  942. return (current($this->_elementOrder) !== false);
  943. }
  944. /**
  945. * Count of elements/subforms that are iterable
  946. *
  947. * @return int
  948. */
  949. public function count()
  950. {
  951. return count($this->_elements);
  952. }
  953. /**
  954. * Sort items according to their order
  955. *
  956. * @return void
  957. */
  958. protected function _sort()
  959. {
  960. if ($this->_groupUpdated || !is_array($this->_elementOrder)) {
  961. $elementOrder = array();
  962. foreach ($this->getElements() as $key => $element) {
  963. $elementOrder[$key] = $element->getOrder();
  964. }
  965. $items = array();
  966. $index = 0;
  967. foreach ($elementOrder as $key => $order) {
  968. if (null === $order) {
  969. while (array_search($index, $elementOrder, true)) {
  970. ++$index;
  971. }
  972. $items[$index] = $key;
  973. ++$index;
  974. } else {
  975. $items[$order] = $key;
  976. }
  977. }
  978. $items = array_flip($items);
  979. asort($items);
  980. $this->_elementOrder = $items;
  981. $this->_groupUpdated = false;
  982. }
  983. }
  984. /**
  985. * Lazy-load a decorator
  986. *
  987. * @param array $decorator Decorator type and options
  988. * @param mixed $name Decorator name or alias
  989. * @return Zend_Form_Decorator_Interface
  990. */
  991. protected function _loadDecorator(array $decorator, $name)
  992. {
  993. $sameName = false;
  994. if ($name == $decorator['decorator']) {
  995. $sameName = true;
  996. }
  997. $instance = $this->_getDecorator($decorator['decorator'], $decorator['options']);
  998. if ($sameName) {
  999. $newName = get_class($instance);
  1000. $decoratorNames = array_keys($this->_decorators);
  1001. $order = array_flip($decoratorNames);
  1002. $order[$newName] = $order[$name];
  1003. $decoratorsExchange = array();
  1004. unset($order[$name]);
  1005. asort($order);
  1006. foreach ($order as $key => $index) {
  1007. if ($key == $newName) {
  1008. $decoratorsExchange[$key] = $instance;
  1009. continue;
  1010. }
  1011. $decoratorsExchange[$key] = $this->_decorators[$key];
  1012. }
  1013. $this->_decorators = $decoratorsExchange;
  1014. } else {
  1015. $this->_decorators[$name] = $instance;
  1016. }
  1017. return $instance;
  1018. }
  1019. }