2
0

DisplayGroup.php 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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-2009 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-2009 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;
  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. }
  566. /**
  567. * Instantiate a decorator based on class name or class name fragment
  568. *
  569. * @param string $name
  570. * @param null|array $options
  571. * @return Zend_Form_Decorator_Interface
  572. */
  573. protected function _getDecorator($name, $options = null)
  574. {
  575. $class = $this->getPluginLoader()->load($name);
  576. if (null === $options) {
  577. $decorator = new $class;
  578. } else {
  579. $decorator = new $class($options);
  580. }
  581. return $decorator;
  582. }
  583. /**
  584. * Add a decorator for rendering the group
  585. *
  586. * @param string|Zend_Form_Decorator_Interface $decorator
  587. * @param array|Zend_Config $options Options with which to initialize decorator
  588. * @return Zend_Form_DisplayGroup
  589. */
  590. public function addDecorator($decorator, $options = null)
  591. {
  592. if ($decorator instanceof Zend_Form_Decorator_Interface) {
  593. $name = get_class($decorator);
  594. } elseif (is_string($decorator)) {
  595. $name = $decorator;
  596. $decorator = array(
  597. 'decorator' => $name,
  598. 'options' => $options,
  599. );
  600. } elseif (is_array($decorator)) {
  601. foreach ($decorator as $name => $spec) {
  602. break;
  603. }
  604. if (is_numeric($name)) {
  605. require_once 'Zend/Form/Exception.php';
  606. throw new Zend_Form_Exception('Invalid alias provided to addDecorator; must be alphanumeric string');
  607. }
  608. if (is_string($spec)) {
  609. $decorator = array(
  610. 'decorator' => $spec,
  611. 'options' => $options,
  612. );
  613. } elseif ($spec instanceof Zend_Form_Decorator_Interface) {
  614. $decorator = $spec;
  615. }
  616. } else {
  617. require_once 'Zend/Form/Exception.php';
  618. throw new Zend_Form_Exception('Invalid decorator provided to addDecorator; must be string or Zend_Form_Decorator_Interface');
  619. }
  620. $this->_decorators[$name] = $decorator;
  621. return $this;
  622. }
  623. /**
  624. * Add many decorators at once
  625. *
  626. * @param array $decorators
  627. * @return Zend_Form_DisplayGroup
  628. */
  629. public function addDecorators(array $decorators)
  630. {
  631. foreach ($decorators as $decoratorInfo) {
  632. if (is_string($decoratorInfo)) {
  633. $this->addDecorator($decoratorInfo);
  634. } elseif ($decoratorInfo instanceof Zend_Form_Decorator_Interface) {
  635. $this->addDecorator($decoratorInfo);
  636. } elseif (is_array($decoratorInfo)) {
  637. $argc = count($decoratorInfo);
  638. $options = array();
  639. if (isset($decoratorInfo['decorator'])) {
  640. $decorator = $decoratorInfo['decorator'];
  641. if (isset($decoratorInfo['options'])) {
  642. $options = $decoratorInfo['options'];
  643. }
  644. $this->addDecorator($decorator, $options);
  645. } else {
  646. switch (true) {
  647. case (0 == $argc):
  648. break;
  649. case (1 <= $argc):
  650. $decorator = array_shift($decoratorInfo);
  651. case (2 <= $argc):
  652. $options = array_shift($decoratorInfo);
  653. default:
  654. $this->addDecorator($decorator, $options);
  655. break;
  656. }
  657. }
  658. } else {
  659. require_once 'Zend/Form/Exception.php';
  660. throw new Zend_Form_Exception('Invalid decorator passed to addDecorators()');
  661. }
  662. }
  663. return $this;
  664. }
  665. /**
  666. * Overwrite all decorators
  667. *
  668. * @param array $decorators
  669. * @return Zend_Form_DisplayGroup
  670. */
  671. public function setDecorators(array $decorators)
  672. {
  673. $this->clearDecorators();
  674. return $this->addDecorators($decorators);
  675. }
  676. /**
  677. * Retrieve a registered decorator
  678. *
  679. * @param string $name
  680. * @return false|Zend_Form_Decorator_Abstract
  681. */
  682. public function getDecorator($name)
  683. {
  684. if (!isset($this->_decorators[$name])) {
  685. $len = strlen($name);
  686. foreach ($this->_decorators as $localName => $decorator) {
  687. if ($len > strlen($localName)) {
  688. continue;
  689. }
  690. if (0 === substr_compare($localName, $name, -$len, $len, true)) {
  691. if (is_array($decorator)) {
  692. return $this->_loadDecorator($decorator, $localName);
  693. }
  694. return $decorator;
  695. }
  696. }
  697. return false;
  698. }
  699. if (is_array($this->_decorators[$name])) {
  700. return $this->_loadDecorator($this->_decorators[$name], $name);
  701. }
  702. return $this->_decorators[$name];
  703. }
  704. /**
  705. * Retrieve all decorators
  706. *
  707. * @return array
  708. */
  709. public function getDecorators()
  710. {
  711. foreach ($this->_decorators as $key => $value) {
  712. if (is_array($value)) {
  713. $this->_loadDecorator($value, $key);
  714. }
  715. }
  716. return $this->_decorators;
  717. }
  718. /**
  719. * Remove a single decorator
  720. *
  721. * @param string $name
  722. * @return bool
  723. */
  724. public function removeDecorator($name)
  725. {
  726. $decorator = $this->getDecorator($name);
  727. if ($decorator) {
  728. if (array_key_exists($name, $this->_decorators)) {
  729. unset($this->_decorators[$name]);
  730. } else {
  731. $class = get_class($decorator);
  732. unset($this->_decorators[$class]);
  733. }
  734. return true;
  735. }
  736. return false;
  737. }
  738. /**
  739. * Clear all decorators
  740. *
  741. * @return Zend_Form_DisplayGroup
  742. */
  743. public function clearDecorators()
  744. {
  745. $this->_decorators = array();
  746. return $this;
  747. }
  748. /**
  749. * Set view
  750. *
  751. * @param Zend_View_Interface $view
  752. * @return Zend_Form_DisplayGroup
  753. */
  754. public function setView(Zend_View_Interface $view = null)
  755. {
  756. $this->_view = $view;
  757. return $this;
  758. }
  759. /**
  760. * Retrieve view
  761. *
  762. * @return Zend_View_Interface
  763. */
  764. public function getView()
  765. {
  766. if (null === $this->_view) {
  767. require_once 'Zend/Controller/Action/HelperBroker.php';
  768. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  769. $this->setView($viewRenderer->view);
  770. }
  771. return $this->_view;
  772. }
  773. /**
  774. * Render display group
  775. *
  776. * @return string
  777. */
  778. public function render(Zend_View_Interface $view = null)
  779. {
  780. if (null !== $view) {
  781. $this->setView($view);
  782. }
  783. $content = '';
  784. foreach ($this->getDecorators() as $decorator) {
  785. $decorator->setElement($this);
  786. $content = $decorator->render($content);
  787. }
  788. return $content;
  789. }
  790. /**
  791. * String representation of group
  792. *
  793. * @return string
  794. */
  795. public function __toString()
  796. {
  797. try {
  798. $return = $this->render();
  799. return $return;
  800. } catch (Exception $e) {
  801. trigger_error($e->getMessage(), E_USER_WARNING);
  802. return '';
  803. }
  804. }
  805. /**
  806. * Set translator object
  807. *
  808. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  809. * @return Zend_Form_DisplayGroup
  810. */
  811. public function setTranslator($translator = null)
  812. {
  813. if ((null === $translator) || ($translator instanceof Zend_Translate_Adapter)) {
  814. $this->_translator = $translator;
  815. } elseif ($translator instanceof Zend_Translate) {
  816. $this->_translator = $translator->getAdapter();
  817. } else {
  818. require_once 'Zend/Form/Exception.php';
  819. throw new Zend_Form_Exception('Invalid translator specified');
  820. }
  821. return $this;
  822. }
  823. /**
  824. * Retrieve translator object
  825. *
  826. * @return Zend_Translate_Adapter|null
  827. */
  828. public function getTranslator()
  829. {
  830. if ($this->translatorIsDisabled()) {
  831. return null;
  832. }
  833. if (null === $this->_translator) {
  834. require_once 'Zend/Form.php';
  835. return Zend_Form::getDefaultTranslator();
  836. }
  837. return $this->_translator;
  838. }
  839. /**
  840. * Indicate whether or not translation should be disabled
  841. *
  842. * @param bool $flag
  843. * @return Zend_Form_DisplayGroup
  844. */
  845. public function setDisableTranslator($flag)
  846. {
  847. $this->_translatorDisabled = (bool) $flag;
  848. return $this;
  849. }
  850. /**
  851. * Is translation disabled?
  852. *
  853. * @return bool
  854. */
  855. public function translatorIsDisabled()
  856. {
  857. return $this->_translatorDisabled;
  858. }
  859. /**
  860. * Overloading: allow rendering specific decorators
  861. *
  862. * Call renderDecoratorName() to render a specific decorator.
  863. *
  864. * @param string $method
  865. * @param array $args
  866. * @return string
  867. * @throws Zend_Form_Exception for invalid decorator or invalid method call
  868. */
  869. public function __call($method, $args)
  870. {
  871. if ('render' == substr($method, 0, 6)) {
  872. $decoratorName = substr($method, 6);
  873. if (false !== ($decorator = $this->getDecorator($decoratorName))) {
  874. $decorator->setElement($this);
  875. $seed = '';
  876. if (0 < count($args)) {
  877. $seed = array_shift($args);
  878. }
  879. return $decorator->render($seed);
  880. }
  881. require_once 'Zend/Form/Exception.php';
  882. throw new Zend_Form_Exception(sprintf('Decorator by name %s does not exist', $decoratorName));
  883. }
  884. require_once 'Zend/Form/Exception.php';
  885. throw new Zend_Form_Exception(sprintf('Method %s does not exist', $method));
  886. }
  887. // Interfaces: Iterator, Countable
  888. /**
  889. * Current element
  890. *
  891. * @return Zend_Form_Element
  892. */
  893. public function current()
  894. {
  895. $this->_sort();
  896. current($this->_elementOrder);
  897. $key = key($this->_elementOrder);
  898. return $this->getElement($key);
  899. }
  900. /**
  901. * Current element
  902. *
  903. * @return string
  904. */
  905. public function key()
  906. {
  907. $this->_sort();
  908. return key($this->_elementOrder);
  909. }
  910. /**
  911. * Move pointer to next element
  912. *
  913. * @return void
  914. */
  915. public function next()
  916. {
  917. $this->_sort();
  918. next($this->_elementOrder);
  919. }
  920. /**
  921. * Move pointer to beginning of element loop
  922. *
  923. * @return void
  924. */
  925. public function rewind()
  926. {
  927. $this->_sort();
  928. reset($this->_elementOrder);
  929. }
  930. /**
  931. * Determine if current element/subform/display group is valid
  932. *
  933. * @return bool
  934. */
  935. public function valid()
  936. {
  937. $this->_sort();
  938. return (current($this->_elementOrder) !== false);
  939. }
  940. /**
  941. * Count of elements/subforms that are iterable
  942. *
  943. * @return int
  944. */
  945. public function count()
  946. {
  947. return count($this->_elements);
  948. }
  949. /**
  950. * Sort items according to their order
  951. *
  952. * @return void
  953. */
  954. protected function _sort()
  955. {
  956. if ($this->_groupUpdated || !is_array($this->_elementOrder)) {
  957. $elementOrder = array();
  958. foreach ($this->getElements() as $key => $element) {
  959. $elementOrder[$key] = $element->getOrder();
  960. }
  961. $items = array();
  962. $index = 0;
  963. foreach ($elementOrder as $key => $order) {
  964. if (null === $order) {
  965. while (array_search($index, $elementOrder, true)) {
  966. ++$index;
  967. }
  968. $items[$index] = $key;
  969. ++$index;
  970. } else {
  971. $items[$order] = $key;
  972. }
  973. }
  974. $items = array_flip($items);
  975. asort($items);
  976. $this->_elementOrder = $items;
  977. $this->_groupUpdated = false;
  978. }
  979. }
  980. /**
  981. * Lazy-load a decorator
  982. *
  983. * @param array $decorator Decorator type and options
  984. * @param mixed $name Decorator name or alias
  985. * @return Zend_Form_Decorator_Interface
  986. */
  987. protected function _loadDecorator(array $decorator, $name)
  988. {
  989. $sameName = false;
  990. if ($name == $decorator['decorator']) {
  991. $sameName = true;
  992. }
  993. $instance = $this->_getDecorator($decorator['decorator'], $decorator['options']);
  994. if ($sameName) {
  995. $newName = get_class($instance);
  996. $decoratorNames = array_keys($this->_decorators);
  997. $order = array_flip($decoratorNames);
  998. $order[$newName] = $order[$name];
  999. $decoratorsExchange = array();
  1000. unset($order[$name]);
  1001. asort($order);
  1002. foreach ($order as $key => $index) {
  1003. if ($key == $newName) {
  1004. $decoratorsExchange[$key] = $instance;
  1005. continue;
  1006. }
  1007. $decoratorsExchange[$key] = $this->_decorators[$key];
  1008. }
  1009. $this->_decorators = $decoratorsExchange;
  1010. } else {
  1011. $this->_decorators[$name] = $instance;
  1012. }
  1013. return $instance;
  1014. }
  1015. }