PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/src/application/libraries/Zend/Form/DisplayGroup.php

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