PageRenderTime 58ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Zend/Form.php

https://bitbucket.org/mercysam/zfs
PHP | 3048 lines | 1730 code | 314 blank | 1004 comment | 265 complexity | 94995385d2821a7db2d2df9444882af8 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Validate_Interface */
  21. require_once 'Zend/Validate/Interface.php';
  22. /**
  23. * Zend_Form
  24. *
  25. * @category Zend
  26. * @package Zend_Form
  27. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @version $Id: Form.php 12787 2008-11-23 14:17:44Z matthew $
  30. */
  31. class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
  32. {
  33. /**#@+
  34. * Plugin loader type constants
  35. */
  36. const DECORATOR = 'DECORATOR';
  37. const ELEMENT = 'ELEMENT';
  38. /**#@-*/
  39. /**#@+
  40. * Method type constants
  41. */
  42. const METHOD_DELETE = 'delete';
  43. const METHOD_GET = 'get';
  44. const METHOD_POST = 'post';
  45. const METHOD_PUT = 'put';
  46. /**#@-*/
  47. /**#@+
  48. * Encoding type constants
  49. */
  50. const ENCTYPE_URLENCODED = 'application/x-www-form-urlencoded';
  51. const ENCTYPE_MULTIPART = 'multipart/form-data';
  52. /**#@-*/
  53. /**
  54. * Form metadata and attributes
  55. * @var array
  56. */
  57. protected $_attribs = array();
  58. /**
  59. * Decorators for rendering
  60. * @var array
  61. */
  62. protected $_decorators = array();
  63. /**
  64. * Default display group class
  65. * @var string
  66. */
  67. protected $_defaultDisplayGroupClass = 'Zend_Form_DisplayGroup';
  68. /**
  69. * Form description
  70. * @var string
  71. */
  72. protected $_description;
  73. /**
  74. * Should we disable loading the default decorators?
  75. * @var bool
  76. */
  77. protected $_disableLoadDefaultDecorators = false;
  78. /**
  79. * Display group prefix paths
  80. * @var array
  81. */
  82. protected $_displayGroupPrefixPaths = array();
  83. /**
  84. * Groups of elements grouped for display purposes
  85. * @var array
  86. */
  87. protected $_displayGroups = array();
  88. /**
  89. * Prefix paths to use when creating elements
  90. * @var array
  91. */
  92. protected $_elementPrefixPaths = array();
  93. /**
  94. * Form elements
  95. * @var array
  96. */
  97. protected $_elements = array();
  98. /**
  99. * Array to which elements belong (if any)
  100. * @var string
  101. */
  102. protected $_elementsBelongTo;
  103. /**
  104. * Custom form-level error messages
  105. * @var array
  106. */
  107. protected $_errorMessages = array();
  108. /**
  109. * Are there errors in the form?
  110. * @var bool
  111. */
  112. protected $_errorsExist = false;
  113. /**
  114. * Form order
  115. * @var int|null
  116. */
  117. protected $_formOrder;
  118. /**
  119. * Whether or not form elements are members of an array
  120. * @var bool
  121. */
  122. protected $_isArray = false;
  123. /**
  124. * Form legend
  125. * @var string
  126. */
  127. protected $_legend;
  128. /**
  129. * Plugin loaders
  130. * @var array
  131. */
  132. protected $_loaders = array();
  133. /**
  134. * Allowed form methods
  135. * @var array
  136. */
  137. protected $_methods = array('delete', 'get', 'post', 'put');
  138. /**
  139. * Order in which to display and iterate elements
  140. * @var array
  141. */
  142. protected $_order = array();
  143. /**
  144. * Whether internal order has been updated or not
  145. * @var bool
  146. */
  147. protected $_orderUpdated = false;
  148. /**
  149. * Sub form prefix paths
  150. * @var array
  151. */
  152. protected $_subFormPrefixPaths = array();
  153. /**
  154. * Sub forms
  155. * @var array
  156. */
  157. protected $_subForms = array();
  158. /**
  159. * @var Zend_Translate
  160. */
  161. protected $_translator;
  162. /**
  163. * Global default translation adapter
  164. * @var Zend_Translate
  165. */
  166. protected static $_translatorDefault;
  167. /**
  168. * is the translator disabled?
  169. * @var bool
  170. */
  171. protected $_translatorDisabled = false;
  172. /**
  173. * @var Zend_View_Interface
  174. */
  175. protected $_view;
  176. /**
  177. * Constructor
  178. *
  179. * Registers form view helper as decorator
  180. *
  181. * @param mixed $options
  182. * @return void
  183. */
  184. public function __construct($options = null)
  185. {
  186. if (is_array($options)) {
  187. $this->setOptions($options);
  188. } elseif ($options instanceof Zend_Config) {
  189. $this->setConfig($options);
  190. }
  191. // Extensions...
  192. $this->init();
  193. $this->loadDefaultDecorators();
  194. }
  195. /**
  196. * Clone form object and all children
  197. *
  198. * @return void
  199. */
  200. public function __clone()
  201. {
  202. $elements = array();
  203. foreach ($this->getElements() as $name => $element) {
  204. $elements[] = clone $element;
  205. }
  206. $this->setElements($elements);
  207. $subForms = array();
  208. foreach ($this->getSubForms() as $name => $subForm) {
  209. $subForms[$name] = clone $subForm;
  210. }
  211. $this->setSubForms($subForms);
  212. $displayGroups = array();
  213. foreach ($this->_displayGroups as $group) {
  214. $clone = clone $group;
  215. $elements = array();
  216. foreach ($clone->getElements() as $name => $e) {
  217. $elements[] = $this->getElement($name);
  218. }
  219. $clone->setElements($elements);
  220. $displayGroups[] = $clone;
  221. }
  222. $this->setDisplayGroups($displayGroups);
  223. }
  224. /**
  225. * Reset values of form
  226. *
  227. * @return Zend_Form
  228. */
  229. public function reset()
  230. {
  231. foreach ($this->getElements() as $element) {
  232. $element->setValue(null);
  233. }
  234. foreach ($this->getSubForms() as $subForm) {
  235. $subForm->reset();
  236. }
  237. return $this;
  238. }
  239. /**
  240. * Initialize form (used by extending classes)
  241. *
  242. * @return void
  243. */
  244. public function init()
  245. {
  246. }
  247. /**
  248. * Set form state from options array
  249. *
  250. * @param array $options
  251. * @return Zend_Form
  252. */
  253. public function setOptions(array $options)
  254. {
  255. if (isset($options['prefixPath'])) {
  256. $this->addPrefixPaths($options['prefixPath']);
  257. unset($options['prefixPath']);
  258. }
  259. if (isset($options['elementPrefixPath'])) {
  260. $this->addElementPrefixPaths($options['elementPrefixPath']);
  261. unset($options['elementPrefixPath']);
  262. }
  263. if (isset($options['displayGroupPrefixPath'])) {
  264. $this->addDisplayGroupPrefixPaths($options['displayGroupPrefixPath']);
  265. unset($options['displayGroupPrefixPath']);
  266. }
  267. if (isset($options['elements'])) {
  268. $this->setElements($options['elements']);
  269. unset($options['elements']);
  270. }
  271. if (isset($options['elementDecorators'])) {
  272. $elementDecorators = $options['elementDecorators'];
  273. unset($options['elementDecorators']);
  274. }
  275. if (isset($options['defaultDisplayGroupClass'])) {
  276. $this->setDefaultDisplayGroupClass($options['defaultDisplayGroupClass']);
  277. unset($options['defaultDisplayGroupClass']);
  278. }
  279. if (isset($options['displayGroupDecorators'])) {
  280. $displayGroupDecorators = $options['displayGroupDecorators'];
  281. unset($options['displayGroupDecorators']);
  282. }
  283. if (isset($options['elementsBelongTo'])) {
  284. $elementsBelongTo = $options['elementsBelongTo'];
  285. unset($options['elementsBelongTo']);
  286. }
  287. if (isset($options['attribs'])) {
  288. $this->addAttribs($options['attribs']);
  289. unset($options['attribs']);
  290. }
  291. $forbidden = array(
  292. 'Options', 'Config', 'PluginLoader', 'SubForms', 'View', 'Translator',
  293. 'Attrib', 'Default',
  294. );
  295. foreach ($options as $key => $value) {
  296. $normalized = ucfirst($key);
  297. if (in_array($normalized, $forbidden)) {
  298. continue;
  299. }
  300. $method = 'set' . $normalized;
  301. if (method_exists($this, $method)) {
  302. $this->$method($value);
  303. } else {
  304. $this->setAttrib($key, $value);
  305. }
  306. }
  307. if (isset($elementDecorators)) {
  308. $this->setElementDecorators($elementDecorators);
  309. }
  310. if (isset($displayGroupDecorators)) {
  311. $this->setDisplayGroupDecorators($displayGroupDecorators);
  312. }
  313. if (isset($elementsBelongTo)) {
  314. $this->setElementsBelongTo($elementsBelongTo);
  315. }
  316. return $this;
  317. }
  318. /**
  319. * Set form state from config object
  320. *
  321. * @param Zend_Config $config
  322. * @return Zend_Form
  323. */
  324. public function setConfig(Zend_Config $config)
  325. {
  326. return $this->setOptions($config->toArray());
  327. }
  328. // Loaders
  329. /**
  330. * Set plugin loaders for use with decorators and elements
  331. *
  332. * @param Zend_Loader_PluginLoader_Interface $loader
  333. * @param string $type 'decorator' or 'element'
  334. * @return Zend_Form
  335. * @throws Zend_Form_Exception on invalid type
  336. */
  337. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type = null)
  338. {
  339. $type = strtoupper($type);
  340. switch ($type) {
  341. case self::DECORATOR:
  342. case self::ELEMENT:
  343. $this->_loaders[$type] = $loader;
  344. return $this;
  345. default:
  346. require_once 'Zend/Form/Exception.php';
  347. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
  348. }
  349. }
  350. /**
  351. * Retrieve plugin loader for given type
  352. *
  353. * $type may be one of:
  354. * - decorator
  355. * - element
  356. *
  357. * If a plugin loader does not exist for the given type, defaults are
  358. * created.
  359. *
  360. * @param string $type
  361. * @return Zend_Loader_PluginLoader_Interface
  362. */
  363. public function getPluginLoader($type = null)
  364. {
  365. $type = strtoupper($type);
  366. if (!isset($this->_loaders[$type])) {
  367. switch ($type) {
  368. case self::DECORATOR:
  369. $prefixSegment = 'Form_Decorator';
  370. $pathSegment = 'Form/Decorator';
  371. break;
  372. case self::ELEMENT:
  373. $prefixSegment = 'Form_Element';
  374. $pathSegment = 'Form/Element';
  375. break;
  376. default:
  377. require_once 'Zend/Form/Exception.php';
  378. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  379. }
  380. require_once 'Zend/Loader/PluginLoader.php';
  381. $this->_loaders[$type] = new Zend_Loader_PluginLoader(
  382. array('Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/')
  383. );
  384. }
  385. return $this->_loaders[$type];
  386. }
  387. /**
  388. * Add prefix path for plugin loader
  389. *
  390. * If no $type specified, assumes it is a base path for both filters and
  391. * validators, and sets each according to the following rules:
  392. * - decorators: $prefix = $prefix . '_Decorator'
  393. * - elements: $prefix = $prefix . '_Element'
  394. *
  395. * Otherwise, the path prefix is set on the appropriate plugin loader.
  396. *
  397. * If $type is 'decorators', sets the path in the decorator plugin loader
  398. * for all elements. Additionally, if no $type is provided,
  399. * {@link Zend_Form_Element::addPrefixPath()} is called on each element.
  400. *
  401. * @param string $path
  402. * @return Zend_Form
  403. * @throws Zend_Form_Exception for invalid type
  404. */
  405. public function addPrefixPath($prefix, $path, $type = null)
  406. {
  407. $type = strtoupper($type);
  408. switch ($type) {
  409. case self::DECORATOR:
  410. case self::ELEMENT:
  411. $loader = $this->getPluginLoader($type);
  412. $loader->addPrefixPath($prefix, $path);
  413. return $this;
  414. case null:
  415. $prefix = rtrim($prefix, '_');
  416. $path = rtrim($path, DIRECTORY_SEPARATOR);
  417. foreach (array(self::DECORATOR, self::ELEMENT) as $type) {
  418. $cType = ucfirst(strtolower($type));
  419. $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR;
  420. $pluginPrefix = $prefix . '_' . $cType;
  421. $loader = $this->getPluginLoader($type);
  422. $loader->addPrefixPath($pluginPrefix, $pluginPath);
  423. }
  424. return $this;
  425. default:
  426. require_once 'Zend/Form/Exception.php';
  427. throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
  428. }
  429. }
  430. /**
  431. * Add many prefix paths at once
  432. *
  433. * @param array $spec
  434. * @return Zend_Form
  435. */
  436. public function addPrefixPaths(array $spec)
  437. {
  438. if (isset($spec['prefix']) && isset($spec['path'])) {
  439. return $this->addPrefixPath($spec['prefix'], $spec['path']);
  440. }
  441. foreach ($spec as $type => $paths) {
  442. if (is_numeric($type) && is_array($paths)) {
  443. $type = null;
  444. if (isset($paths['prefix']) && isset($paths['path'])) {
  445. if (isset($paths['type'])) {
  446. $type = $paths['type'];
  447. }
  448. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  449. }
  450. } elseif (!is_numeric($type)) {
  451. if (!isset($paths['prefix']) || !isset($paths['path'])) {
  452. continue;
  453. }
  454. $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
  455. }
  456. }
  457. return $this;
  458. }
  459. /**
  460. * Add prefix path for all elements
  461. *
  462. * @param string $prefix
  463. * @param string $path
  464. * @param string $type
  465. * @return Zend_Form
  466. */
  467. public function addElementPrefixPath($prefix, $path, $type = null)
  468. {
  469. $this->_elementPrefixPaths[] = array(
  470. 'prefix' => $prefix,
  471. 'path' => $path,
  472. 'type' => $type,
  473. );
  474. foreach ($this->getElements() as $element) {
  475. $element->addPrefixPath($prefix, $path, $type);
  476. }
  477. foreach ($this->getSubForms() as $subForm) {
  478. $subForm->addElementPrefixPath($prefix, $path, $type);
  479. }
  480. return $this;
  481. }
  482. /**
  483. * Add prefix paths for all elements
  484. *
  485. * @param array $spec
  486. * @return Zend_Form
  487. */
  488. public function addElementPrefixPaths(array $spec)
  489. {
  490. $this->_elementPrefixPaths = $this->_elementPrefixPaths + $spec;
  491. foreach ($this->getElements() as $element) {
  492. $element->addPrefixPaths($spec);
  493. }
  494. return $this;
  495. }
  496. /**
  497. * Add prefix path for all display groups
  498. *
  499. * @param string $prefix
  500. * @param string $path
  501. * @return Zend_Form
  502. */
  503. public function addDisplayGroupPrefixPath($prefix, $path)
  504. {
  505. $this->_displayGroupPrefixPaths[] = array(
  506. 'prefix' => $prefix,
  507. 'path' => $path,
  508. );
  509. foreach ($this->getDisplayGroups() as $group) {
  510. $group->addPrefixPath($prefix, $path);
  511. }
  512. return $this;
  513. }
  514. /**
  515. * Add multiple display group prefix paths at once
  516. *
  517. * @param array $spec
  518. * @return Zend_Form
  519. */
  520. public function addDisplayGroupPrefixPaths(array $spec)
  521. {
  522. foreach ($spec as $key => $value) {
  523. if (is_string($value) && !is_numeric($key)) {
  524. $this->addDisplayGroupPrefixPath($key, $value);
  525. continue;
  526. }
  527. if (is_string($value) && is_numeric($key)) {
  528. continue;
  529. }
  530. if (is_array($value)) {
  531. $count = count($value);
  532. if (array_keys($value) === range(0, $count - 1)) {
  533. if ($count < 2) {
  534. continue;
  535. }
  536. $prefix = array_shift($value);
  537. $path = array_shift($value);
  538. $this->addDisplayGroupPrefixPath($prefix, $path);
  539. continue;
  540. }
  541. if (array_key_exists('prefix', $value) && array_key_exists('path', $value)) {
  542. $this->addDisplayGroupPrefixPath($value['prefix'], $value['path']);
  543. }
  544. }
  545. }
  546. return $this;
  547. }
  548. // Form metadata:
  549. /**
  550. * Set form attribute
  551. *
  552. * @param string $key
  553. * @param mixed $value
  554. * @return Zend_Form
  555. */
  556. public function setAttrib($key, $value)
  557. {
  558. $key = (string) $key;
  559. $this->_attribs[$key] = $value;
  560. return $this;
  561. }
  562. /**
  563. * Add multiple form attributes at once
  564. *
  565. * @param array $attribs
  566. * @return Zend_Form
  567. */
  568. public function addAttribs(array $attribs)
  569. {
  570. foreach ($attribs as $key => $value) {
  571. $this->setAttrib($key, $value);
  572. }
  573. return $this;
  574. }
  575. /**
  576. * Set multiple form attributes at once
  577. *
  578. * Overwrites any previously set attributes.
  579. *
  580. * @param array $attribs
  581. * @return Zend_Form
  582. */
  583. public function setAttribs(array $attribs)
  584. {
  585. $this->clearAttribs();
  586. return $this->addAttribs($attribs);
  587. }
  588. /**
  589. * Retrieve a single form attribute
  590. *
  591. * @param string $key
  592. * @return mixed
  593. */
  594. public function getAttrib($key)
  595. {
  596. $key = (string) $key;
  597. if (!isset($this->_attribs[$key])) {
  598. return null;
  599. }
  600. return $this->_attribs[$key];
  601. }
  602. /**
  603. * Retrieve all form attributes/metadata
  604. *
  605. * @return array
  606. */
  607. public function getAttribs()
  608. {
  609. return $this->_attribs;
  610. }
  611. /**
  612. * Remove attribute
  613. *
  614. * @param string $key
  615. * @return bool
  616. */
  617. public function removeAttrib($key)
  618. {
  619. if (isset($this->_attribs[$key])) {
  620. unset($this->_attribs[$key]);
  621. return true;
  622. }
  623. return false;
  624. }
  625. /**
  626. * Clear all form attributes
  627. *
  628. * @return Zend_Form
  629. */
  630. public function clearAttribs()
  631. {
  632. $this->_attribs = array();
  633. return $this;
  634. }
  635. /**
  636. * Set form action
  637. *
  638. * @param string $action
  639. * @return Zend_Form
  640. */
  641. public function setAction($action)
  642. {
  643. return $this->setAttrib('action', (string) $action);
  644. }
  645. /**
  646. * Get form action
  647. *
  648. * Sets default to '' if not set.
  649. *
  650. * @return string
  651. */
  652. public function getAction()
  653. {
  654. $action = $this->getAttrib('action');
  655. if (null === $action) {
  656. $action = '';
  657. $this->setAction($action);
  658. }
  659. return $action;
  660. }
  661. /**
  662. * Set form method
  663. *
  664. * Only values in {@link $_methods()} allowed
  665. *
  666. * @param string $method
  667. * @return Zend_Form
  668. * @throws Zend_Form_Exception
  669. */
  670. public function setMethod($method)
  671. {
  672. $method = strtolower($method);
  673. if (!in_array($method, $this->_methods)) {
  674. require_once 'Zend/Form/Exception.php';
  675. throw new Zend_Form_Exception(sprintf('"%s" is an invalid form method', $method));
  676. }
  677. $this->setAttrib('method', $method);
  678. return $this;
  679. }
  680. /**
  681. * Retrieve form method
  682. *
  683. * @return string
  684. */
  685. public function getMethod()
  686. {
  687. if (null === ($method = $this->getAttrib('method'))) {
  688. $method = self::METHOD_POST;
  689. $this->setAttrib('method', $method);
  690. }
  691. return strtolower($method);
  692. }
  693. /**
  694. * Set encoding type
  695. *
  696. * @param string $value
  697. * @return Zend_Form
  698. */
  699. public function setEnctype($value)
  700. {
  701. $this->setAttrib('enctype', $value);
  702. return $this;
  703. }
  704. /**
  705. * Get encoding type
  706. *
  707. * @return string
  708. */
  709. public function getEnctype()
  710. {
  711. if (null === ($enctype = $this->getAttrib('enctype'))) {
  712. $enctype = self::ENCTYPE_URLENCODED;
  713. $this->setAttrib('enctype', $enctype);
  714. }
  715. return $this->getAttrib('enctype');
  716. }
  717. /**
  718. * Filter a name to only allow valid variable characters
  719. *
  720. * @param string $value
  721. * @param bool $allowBrackets
  722. * @return string
  723. */
  724. public function filterName($value, $allowBrackets = false)
  725. {
  726. $charset = '^a-zA-Z0-9_\x7f-\xff';
  727. if ($allowBrackets) {
  728. $charset .= '\[\]';
  729. }
  730. return preg_replace('/[' . $charset . ']/', '', (string) $value);
  731. }
  732. /**
  733. * Set form name
  734. *
  735. * @param string $name
  736. * @return Zend_Form
  737. */
  738. public function setName($name)
  739. {
  740. $name = $this->filterName($name);
  741. if (('0' !== $name) && empty($name)) {
  742. require_once 'Zend/Form/Exception.php';
  743. throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty');
  744. }
  745. return $this->setAttrib('name', $name);
  746. }
  747. /**
  748. * Get name attribute
  749. *
  750. * @return null|string
  751. */
  752. public function getName()
  753. {
  754. return $this->getAttrib('name');
  755. }
  756. /**
  757. * Get fully qualified name
  758. *
  759. * Places name as subitem of array and/or appends brackets.
  760. *
  761. * @return string
  762. */
  763. public function getFullyQualifiedName()
  764. {
  765. return $this->getName();
  766. }
  767. /**
  768. * Get element id
  769. *
  770. * @return string
  771. */
  772. public function getId()
  773. {
  774. if (null !== ($id = $this->getAttrib('id'))) {
  775. return $id;
  776. }
  777. $id = $this->getFullyQualifiedName();
  778. // Bail early if no array notation detected
  779. if (!strstr($id, '[')) {
  780. return $id;
  781. }
  782. // Strip array notation
  783. if ('[]' == substr($id, -2)) {
  784. $id = substr($id, 0, strlen($id) - 2);
  785. }
  786. $id = str_replace('][', '-', $id);
  787. $id = str_replace(array(']', '['), '-', $id);
  788. $id = trim($id, '-');
  789. return $id;
  790. }
  791. /**
  792. * Set form legend
  793. *
  794. * @param string $value
  795. * @return Zend_Form
  796. */
  797. public function setLegend($value)
  798. {
  799. $this->_legend = (string) $value;
  800. return $this;
  801. }
  802. /**
  803. * Get form legend
  804. *
  805. * @return string
  806. */
  807. public function getLegend()
  808. {
  809. return $this->_legend;
  810. }
  811. /**
  812. * Set form description
  813. *
  814. * @param string $value
  815. * @return Zend_Form
  816. */
  817. public function setDescription($value)
  818. {
  819. $this->_description = (string) $value;
  820. return $this;
  821. }
  822. /**
  823. * Retrieve form description
  824. *
  825. * @return string
  826. */
  827. public function getDescription()
  828. {
  829. return $this->_description;
  830. }
  831. /**
  832. * Set form order
  833. *
  834. * @param int $index
  835. * @return Zend_Form
  836. */
  837. public function setOrder($index)
  838. {
  839. $this->_formOrder = (int) $index;
  840. return $this;
  841. }
  842. /**
  843. * Get form order
  844. *
  845. * @return int|null
  846. */
  847. public function getOrder()
  848. {
  849. return $this->_formOrder;
  850. }
  851. // Element interaction:
  852. /**
  853. * Add a new element
  854. *
  855. * $element may be either a string element type, or an object of type
  856. * Zend_Form_Element. If a string element type is provided, $name must be
  857. * provided, and $options may be optionally provided for configuring the
  858. * element.
  859. *
  860. * If a Zend_Form_Element is provided, $name may be optionally provided,
  861. * and any provided $options will be ignored.
  862. *
  863. * @param string|Zend_Form_Element $element
  864. * @param string $name
  865. * @param array|Zend_Config $options
  866. * @return Zend_Form
  867. */
  868. public function addElement($element, $name = null, $options = null)
  869. {
  870. if (is_string($element)) {
  871. if (null === $name) {
  872. require_once 'Zend/Form/Exception.php';
  873. throw new Zend_Form_Exception('Elements specified by string must have an accompanying name');
  874. }
  875. $this->_elements[$name] = $this->createElement($element, $name, $options);
  876. } elseif ($element instanceof Zend_Form_Element) {
  877. $prefixPaths = array();
  878. $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
  879. if (!empty($this->_elementPrefixPaths)) {
  880. $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
  881. }
  882. if (null === $name) {
  883. $name = $element->getName();
  884. }
  885. $this->_elements[$name] = $element;
  886. $this->_elements[$name]->addPrefixPaths($prefixPaths);
  887. }
  888. $this->_order[$name] = $this->_elements[$name]->getOrder();
  889. $this->_orderUpdated = true;
  890. $this->_setElementsBelongTo($name);
  891. return $this;
  892. }
  893. /**
  894. * Create an element
  895. *
  896. * Acts as a factory for creating elements. Elements created with this
  897. * method will not be attached to the form, but will contain element
  898. * settings as specified in the form object (including plugin loader
  899. * prefix paths, default decorators, etc.).
  900. *
  901. * @param string $type
  902. * @param string $name
  903. * @param array|Zend_Config $options
  904. * @return Zend_Form_Element
  905. */
  906. public function createElement($type, $name, $options = null)
  907. {
  908. if (!is_string($type)) {
  909. require_once 'Zend/Form/Exception.php';
  910. throw new Zend_Form_Exception('Element type must be a string indicating type');
  911. }
  912. if (!is_string($name)) {
  913. require_once 'Zend/Form/Exception.php';
  914. throw new Zend_Form_Exception('Element name must be a string');
  915. }
  916. $prefixPaths = array();
  917. $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
  918. if (!empty($this->_elementPrefixPaths)) {
  919. $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
  920. }
  921. if ($options instanceof Zend_Config) {
  922. $options = $options->toArray();
  923. }
  924. if ((null === $options) || !is_array($options)) {
  925. $options = array('prefixPath' => $prefixPaths);
  926. } elseif (is_array($options)) {
  927. if (array_key_exists('prefixPath', $options)) {
  928. $options['prefixPath'] = array_merge($prefixPaths, $options['prefixPath']);
  929. } else {
  930. $options['prefixPath'] = $prefixPaths;
  931. }
  932. }
  933. $class = $this->getPluginLoader(self::ELEMENT)->load($type);
  934. $element = new $class($name, $options);
  935. return $element;
  936. }
  937. /**
  938. * Add multiple elements at once
  939. *
  940. * @param array $elements
  941. * @return Zend_Form
  942. */
  943. public function addElements(array $elements)
  944. {
  945. foreach ($elements as $key => $spec) {
  946. $name = null;
  947. if (!is_numeric($key)) {
  948. $name = $key;
  949. }
  950. if (is_string($spec) || ($spec instanceof Zend_Form_Element)) {
  951. $this->addElement($spec, $name);
  952. continue;
  953. }
  954. if (is_array($spec)) {
  955. $argc = count($spec);
  956. $options = array();
  957. if (isset($spec['type'])) {
  958. $type = $spec['type'];
  959. if (isset($spec['name'])) {
  960. $name = $spec['name'];
  961. }
  962. if (isset($spec['options'])) {
  963. $options = $spec['options'];
  964. }
  965. $this->addElement($type, $name, $options);
  966. } else {
  967. switch ($argc) {
  968. case 0:
  969. continue;
  970. case (1 <= $argc):
  971. $type = array_shift($spec);
  972. case (2 <= $argc):
  973. if (null === $name) {
  974. $name = array_shift($spec);
  975. } else {
  976. $options = array_shift($spec);
  977. }
  978. case (3 <= $argc):
  979. if (empty($options)) {
  980. $options = array_shift($spec);
  981. }
  982. default:
  983. $this->addElement($type, $name, $options);
  984. }
  985. }
  986. }
  987. }
  988. return $this;
  989. }
  990. /**
  991. * Set form elements (overwrites existing elements)
  992. *
  993. * @param array $elements
  994. * @return Zend_Form
  995. */
  996. public function setElements(array $elements)
  997. {
  998. $this->clearElements();
  999. return $this->addElements($elements);
  1000. }
  1001. /**
  1002. * Retrieve a single element
  1003. *
  1004. * @param string $name
  1005. * @return Zend_Form_Element|null
  1006. */
  1007. public function getElement($name)
  1008. {
  1009. if (array_key_exists($name, $this->_elements)) {
  1010. return $this->_elements[$name];
  1011. }
  1012. return null;
  1013. }
  1014. /**
  1015. * Retrieve all elements
  1016. *
  1017. * @return array
  1018. */
  1019. public function getElements()
  1020. {
  1021. return $this->_elements;
  1022. }
  1023. /**
  1024. * Remove element
  1025. *
  1026. * @param string $name
  1027. * @return boolean
  1028. */
  1029. public function removeElement($name)
  1030. {
  1031. $name = (string) $name;
  1032. if (isset($this->_elements[$name])) {
  1033. unset($this->_elements[$name]);
  1034. if (array_key_exists($name, $this->_order)) {
  1035. unset($this->_order[$name]);
  1036. $this->_orderUpdated = true;
  1037. } else {
  1038. foreach ($this->_displayGroups as $group) {
  1039. if (null !== $group->getElement($name)) {
  1040. $group->removeElement($name);
  1041. }
  1042. }
  1043. }
  1044. return true;
  1045. }
  1046. return false;
  1047. }
  1048. /**
  1049. * Remove all form elements
  1050. *
  1051. * @return Zend_Form
  1052. */
  1053. public function clearElements()
  1054. {
  1055. foreach (array_keys($this->_elements) as $key) {
  1056. if (array_key_exists($key, $this->_order)) {
  1057. unset($this->_order[$key]);
  1058. }
  1059. }
  1060. $this->_elements = array();
  1061. $this->_orderUpdated = true;
  1062. return $this;
  1063. }
  1064. /**
  1065. * Set default values for elements
  1066. *
  1067. * If an element's name is not specified as a key in the array, its value
  1068. * is set to null.
  1069. *
  1070. * @param array $defaults
  1071. * @return Zend_Form
  1072. */
  1073. public function setDefaults(array $defaults)
  1074. {
  1075. foreach ($this->getElements() as $name => $element) {
  1076. if (array_key_exists($name, $defaults)) {
  1077. $this->setDefault($name, $defaults[$name]);
  1078. }
  1079. }
  1080. foreach ($this->getSubForms() as $name => $form) {
  1081. if (array_key_exists($name, $defaults)) {
  1082. $form->setDefaults($defaults[$name]);
  1083. } else {
  1084. $form->setDefaults($defaults);
  1085. }
  1086. }
  1087. return $this;
  1088. }
  1089. /**
  1090. * Set default value for an element
  1091. *
  1092. * @param string $name
  1093. * @param mixed $value
  1094. * @return Zend_Form
  1095. */
  1096. public function setDefault($name, $value)
  1097. {
  1098. $name = (string) $name;
  1099. if ($element = $this->getElement($name)) {
  1100. $element->setValue($value);
  1101. } else {
  1102. if (is_scalar($value)) {
  1103. foreach ($this->getSubForms() as $subForm) {
  1104. $subForm->setDefault($name, $value);
  1105. }
  1106. } elseif (is_array($value) && ($subForm = $this->getSubForm($name))) {
  1107. $subForm->setDefaults($value);
  1108. }
  1109. }
  1110. return $this;
  1111. }
  1112. /**
  1113. * Retrieve value for single element
  1114. *
  1115. * @param string $name
  1116. * @return mixed
  1117. */
  1118. public function getValue($name)
  1119. {
  1120. if ($element = $this->getElement($name)) {
  1121. return $element->getValue();
  1122. }
  1123. if ($subForm = $this->getSubForm($name)) {
  1124. return $subForm->getValues(true);
  1125. }
  1126. foreach ($this->getSubForms() as $subForm) {
  1127. if ($name == $subForm->getElementsBelongTo()) {
  1128. return $subForm->getValues(true);
  1129. }
  1130. }
  1131. return null;
  1132. }
  1133. /**
  1134. * Retrieve all form element values
  1135. *
  1136. * @param bool $suppressArrayNotation
  1137. * @return array
  1138. */
  1139. public function getValues($suppressArrayNotation = false)
  1140. {
  1141. $values = array();
  1142. foreach ($this->getElements() as $key => $element) {
  1143. if (!$element->getIgnore()) {
  1144. $values[$key] = $element->getValue();
  1145. }
  1146. }
  1147. foreach ($this->getSubForms() as $key => $subForm) {
  1148. $fValues = $this->_attachToArray($subForm->getValues(true), $subForm->getElementsBelongTo());
  1149. $values = array_merge($values, $fValues);
  1150. }
  1151. if (!$suppressArrayNotation && $this->isArray()) {
  1152. $values = $this->_attachToArray($values, $this->getElementsBelongTo());
  1153. }
  1154. return $values;
  1155. }
  1156. /**
  1157. * Get unfiltered element value
  1158. *
  1159. * @param string $name
  1160. * @return mixed
  1161. */
  1162. public function getUnfilteredValue($name)
  1163. {
  1164. if ($element = $this->getElement($name)) {
  1165. return $element->getUnfilteredValue();
  1166. }
  1167. return null;
  1168. }
  1169. /**
  1170. * Retrieve all unfiltered element values
  1171. *
  1172. * @return array
  1173. */
  1174. public function getUnfilteredValues()
  1175. {
  1176. $values = array();
  1177. foreach ($this->getElements() as $key => $element) {
  1178. $values[$key] = $element->getUnfilteredValue();
  1179. }
  1180. return $values;
  1181. }
  1182. /**
  1183. * Set all elements' filters
  1184. *
  1185. * @param array $filters
  1186. * @return Zend_Form
  1187. */
  1188. public function setElementFilters(array $filters)
  1189. {
  1190. foreach ($this->getElements() as $element) {
  1191. $element->setFilters($filters);
  1192. }
  1193. return $this;
  1194. }
  1195. /**
  1196. * Set name of array elements belong to
  1197. *
  1198. * @param string $array
  1199. * @return Zend_Form
  1200. */
  1201. public function setElementsBelongTo($array)
  1202. {
  1203. $origName = $this->getElementsBelongTo();
  1204. $name = $this->filterName($array, true);
  1205. if (empty($name)) {
  1206. $name = null;
  1207. }
  1208. $this->_elementsBelongTo = $name;
  1209. if (null === $name) {
  1210. $this->setIsArray(false);
  1211. if (null !== $origName) {
  1212. $this->_setElementsBelongTo();
  1213. }
  1214. } else {
  1215. $this->setIsArray(true);
  1216. $this->_setElementsBelongTo();
  1217. }
  1218. return $this;
  1219. }
  1220. /**
  1221. * Set array to which elements belong
  1222. *
  1223. * @param string $name Element name
  1224. * @return void
  1225. */
  1226. protected function _setElementsBelongTo($name = null)
  1227. {
  1228. $array = $this->getElementsBelongTo();
  1229. if (null === $array) {
  1230. return;
  1231. }
  1232. if (null === $name) {
  1233. foreach ($this->getElements() as $element) {
  1234. $element->setBelongsTo($array);
  1235. }
  1236. } else {
  1237. if (null !== ($element = $this->getElement($name))) {
  1238. $element->setBelongsTo($array);
  1239. }
  1240. }
  1241. }
  1242. /**
  1243. * Get name of array elements belong to
  1244. *
  1245. * @return string|null
  1246. */
  1247. public function getElementsBelongTo()
  1248. {
  1249. if ((null === $this->_elementsBelongTo) && $this->isArray()) {
  1250. $name = $this->getName();
  1251. if (!empty($name)) {
  1252. return $name;
  1253. }
  1254. }
  1255. return $this->_elementsBelongTo;
  1256. }
  1257. /**
  1258. * Set flag indicating elements belong to array
  1259. *
  1260. * @param bool $flag Value of flag
  1261. * @return Zend_Form
  1262. */
  1263. public function setIsArray($flag)
  1264. {
  1265. $this->_isArray = (bool) $flag;
  1266. return $this;
  1267. }
  1268. /**
  1269. * Get flag indicating if elements belong to an array
  1270. *
  1271. * @return bool
  1272. */
  1273. public function isArray()
  1274. {
  1275. return $this->_isArray;
  1276. }
  1277. // Element groups:
  1278. /**
  1279. * Add a form group/subform
  1280. *
  1281. * @param Zend_Form $form
  1282. * @param string $name
  1283. * @param int $order
  1284. * @return Zend_Form
  1285. */
  1286. public function addSubForm(Zend_Form $form, $name, $order = null)
  1287. {
  1288. $name = (string) $name;
  1289. foreach ($this->_loaders as $type => $loader) {
  1290. $loaderPaths = $loader->getPaths();
  1291. foreach ($loaderPaths as $prefix => $paths) {
  1292. foreach ($paths as $path) {
  1293. $form->addPrefixPath($prefix, $path, $type);
  1294. }
  1295. }
  1296. }
  1297. if (!empty($this->_elementPrefixPaths)) {
  1298. foreach ($this->_elementPrefixPaths as $spec) {
  1299. list($prefix, $path, $type) = array_values($spec);
  1300. $form->addElementPrefixPath($prefix, $path, $type);
  1301. }
  1302. }
  1303. if (!empty($this->_displayGroupPrefixPaths)) {
  1304. foreach ($this->_displayGroupPrefixPaths as $spec) {
  1305. list($prefix, $path) = array_values($spec);
  1306. $form->addDisplayGroupPrefixPath($prefix, $path);
  1307. }
  1308. }
  1309. if (null !== $order) {
  1310. $form->setOrder($order);
  1311. }
  1312. $form->setName($name);
  1313. $this->_subForms[$name] = $form;
  1314. $this->_order[$name] = $order;
  1315. $this->_orderUpdated = true;
  1316. return $this;
  1317. }
  1318. /**
  1319. * Add multiple form subForms/subforms at once
  1320. *
  1321. * @param array $subForms
  1322. * @return Zend_Form
  1323. */
  1324. public function addSubForms(array $subForms)
  1325. {
  1326. foreach ($subForms as $key => $spec) {
  1327. $name = null;
  1328. if (!is_numeric($key)) {
  1329. $name = $key;
  1330. }
  1331. if ($spec instanceof Zend_Form) {
  1332. $this->addSubForm($spec, $name);
  1333. continue;
  1334. }
  1335. if (is_array($spec)) {
  1336. $argc = count($spec);
  1337. $order = null;
  1338. switch ($argc) {
  1339. case 0:
  1340. continue;
  1341. case (1 <= $argc):
  1342. $subForm = array_shift($spec);
  1343. case (2 <= $argc):
  1344. $name = array_shift($spec);
  1345. case (3 <= $argc):
  1346. $order = array_shift($spec);
  1347. default:
  1348. $this->addSubForm($subForm, $name, $order);
  1349. }
  1350. }
  1351. }
  1352. return $this;
  1353. }
  1354. /**
  1355. * Set multiple form subForms/subforms (overwrites)
  1356. *
  1357. * @param array $subForms
  1358. * @return Zend_Form
  1359. */
  1360. public function setSubForms(array $subForms)
  1361. {
  1362. $this->clearSubForms();
  1363. return $this->addSubForms($subForms);
  1364. }
  1365. /**
  1366. * Retrieve a form subForm/subform
  1367. *
  1368. * @param string $name
  1369. * @return Zend_Form|null
  1370. */
  1371. public function getSubForm($name)
  1372. {
  1373. $name = (string) $name;
  1374. if (isset($this->_subForms[$name])) {
  1375. return $this->_subForms[$name];
  1376. }
  1377. return null;
  1378. }
  1379. /**
  1380. * Retrieve all form subForms/subforms
  1381. *
  1382. * @return array
  1383. */
  1384. public function getSubForms()
  1385. {
  1386. return $this->_subForms;
  1387. }
  1388. /**
  1389. * Remove form subForm/subform
  1390. *
  1391. * @param string $name
  1392. * @return boolean
  1393. */
  1394. public function removeSubForm($name)
  1395. {
  1396. $name = (string) $name;
  1397. if (array_key_exists($name, $this->_subForms)) {
  1398. unset($this->_subForms[$name]);
  1399. if (array_key_exists($name, $this->_order)) {
  1400. unset($this->_order[$name]);
  1401. $this->_orderUpdated = true;
  1402. }
  1403. return true;
  1404. }
  1405. return false;
  1406. }
  1407. /**
  1408. * Remove all form subForms/subforms
  1409. *
  1410. * @return Zend_Form
  1411. */
  1412. public function clearSubForms()
  1413. {
  1414. foreach (array_keys($this->_subForms) as $key) {
  1415. if (array_key_exists($key, $this->_order)) {
  1416. unset($this->_order[$key]);
  1417. }
  1418. }
  1419. $this->_subForms = array();
  1420. $this->_orderUpdated = true;
  1421. return $this;
  1422. }
  1423. // Display groups:
  1424. /**
  1425. * Set default display group class
  1426. *
  1427. * @param string $class
  1428. * @return Zend_Form
  1429. */
  1430. public function setDefaultDisplayGroupClass($class)
  1431. {
  1432. $this->_defaultDisplayGroupClass = (string) $class;
  1433. return $this;
  1434. }
  1435. /**
  1436. * Retrieve default display group class
  1437. *
  1438. * @return string
  1439. */
  1440. public function getDefaultDisplayGroupClass()
  1441. {
  1442. return $this->_defaultDisplayGroupClass;
  1443. }
  1444. /**
  1445. * Add a display group
  1446. *
  1447. * Groups named elements for display purposes.
  1448. *
  1449. * If a referenced element does not yet exist in the form, it is omitted.
  1450. *
  1451. * @param array $elements
  1452. * @param string $name
  1453. * @param array|Zend_Config $options
  1454. * @return Zend_Form
  1455. * @throws Zend_Form_Exception if no valid elements provided
  1456. */
  1457. public function addDisplayGroup(array $elements, $name, $options = null)
  1458. {
  1459. $group = array();
  1460. foreach ($elements as $element) {
  1461. if (isset($this->_elements[$element])) {
  1462. $add = $this->getElement($element);
  1463. if (null !== $add) {
  1464. unset($this->_order[$element]);
  1465. $group[] = $add;
  1466. }
  1467. }
  1468. }
  1469. if (empty($group)) {
  1470. require_once 'Zend/Form/Exception.php';
  1471. throw new Zend_Form_Exception('No valid elements specified for display group');
  1472. }
  1473. $name = (string) $name;
  1474. if (is_array($options)) {
  1475. $options['elements'] = $group;
  1476. } elseif ($options instanceof Zend_Config) {
  1477. $options = $options->toArray();
  1478. $options['elements'] = $group;
  1479. } else {
  1480. $options = array('elements' => $group);
  1481. }
  1482. if (isset($options['displayGroupClass'])) {
  1483. $class = $options['displayGroupClass'];
  1484. unset($options['displayGroupClass']);
  1485. } else {
  1486. $class = $this->getDefaultDisplayGroupClass();
  1487. }
  1488. if (!class_exists($class)) {
  1489. require_once 'Zend/Loader.php';
  1490. Zend_Loader::loadClass($class);
  1491. }
  1492. $this->_displayGroups[$name] = new $class(
  1493. $name,
  1494. $this->getPluginLoader(self::DECORATOR),
  1495. $options
  1496. );
  1497. if (!empty($this->_displayGroupPrefixPaths)) {
  1498. $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths);
  1499. }
  1500. $this->_order[$name] = $this->_displayGroups[$name]->getOrder();
  1501. $this->_orderUpdated = true;
  1502. return $this;
  1503. }
  1504. /**
  1505. * Add a display group object (used with cloning)
  1506. *
  1507. * @param Zend_Form_DisplayGroup $group
  1508. * @param string|null $name
  1509. * @return Zend_Form
  1510. */
  1511. protected function _addDisplayGroupObject(Zend_Form_DisplayGroup $group, $name = null)
  1512. {
  1513. if (null === $name) {
  1514. $name = $group->getName();
  1515. if (empty($name)) {
  1516. require_once 'Zend/Form/Exception.php';
  1517. throw new Zend_Form_Exception('Invalid display group added; requires name');
  1518. }
  1519. }
  1520. $this->_displayGroups[$name] = $group;
  1521. if (!empty($this->_displayGroupPrefixPaths)) {
  1522. $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths);
  1523. }
  1524. $this->_order[$name] = $this->_displayGroups[$name]->getOrder();
  1525. $this->_orderUpdated = true;
  1526. return $this;
  1527. }
  1528. /**
  1529. * Add multiple display groups at once
  1530. *
  1531. * @param array $groups
  1532. * @return Zend_Form
  1533. */
  1534. public function addDisplayGroups(array $groups)
  1535. {
  1536. foreach ($groups as $key => $spec) {
  1537. $name = null;
  1538. if (!is_numeric($key)) {
  1539. $name = $key;
  1540. }
  1541. if ($spec instanceof Zend_Form_DisplayGroup) {
  1542. $this->_addDisplayGroupObject($spec);
  1543. }
  1544. if (!is_array($spec) || empty($spec)) {
  1545. continue;
  1546. }
  1547. $argc = count($spec);
  1548. $options = array();
  1549. if (isset($spec['elements'])) {
  1550. $elements = $spec['elements'];
  1551. if (isset($spec['name'])) {
  1552. $name = $spec['name'];
  1553. }
  1554. if (isset($spec['options'])) {
  1555. $options = $spec['options'];
  1556. }
  1557. $this->addDisplayGroup($elements, $name, $options);
  1558. } else {
  1559. switch ($argc) {
  1560. case (1 <= $argc):
  1561. $elements = array_shift($spec);
  1562. if (!is_array($elements) && (null !== $name)) {
  1563. $elements = array_merge((array) $elements, $spec);
  1564. $this->addDisplayGroup($elements, $name);
  1565. break;
  1566. }
  1567. case (2 <= $argc):
  1568. if (null !== $name) {
  1569. $options = array_shift($spec);
  1570. $this->addDisplayGroup($elements, $name, $options);
  1571. break;
  1572. }
  1573. $name = array_shift($spec);
  1574. case (3 <= $argc):
  1575. $options = array_shift($spec);
  1576. default:
  1577. $this->addDisplayGroup($elements, $name, $options);
  1578. }
  1579. }
  1580. }
  1581. return $this;
  1582. }
  1583. /**
  1584. * Add multiple display groups (overwrites)
  1585. *
  1586. * @param array $groups
  1587. * @return Zend_Form
  1588. */
  1589. public function setDisplayGroups(array $groups)
  1590. {
  1591. return $this->clearDisplayGroups()
  1592. ->addDisplayGroups($groups);
  1593. }
  1594. /**
  1595. * Return a display group
  1596. *
  1597. * @param string $name
  1598. * @return array|null
  1599. */
  1600. public function getDisplayGroup($name)
  1601. {
  1602. $name = (string) $name;
  1603. if (isset($this->_displayGroups[$name])) {
  1604. return $this->_displayGroups[$name];
  1605. }
  1606. return null;
  1607. }
  1608. /**
  1609. * Return all display groups
  1610. *
  1611. * @return array
  1612. */
  1613. public function getDisplayGroups()
  1614. {
  1615. return $this->_displayGroups;
  1616. }
  1617. /**
  1618. * Remove a display group by name
  1619. *
  1620. * @param string $name
  1621. * @return boolean
  1622. */
  1623. public function removeDisplayGroup($name)
  1624. {
  1625. $name = (string) $name;
  1626. if (array_key_exists($name, $this->_displayGroups)) {
  1627. foreach ($this->_displayGroups[$name] as $key => $element) {
  1628. if (array_key_exists($key, $this->_elements)) {
  1629. $this->_order[$key] = $element->getOrder();
  1630. $this->_orderUpdated = true;
  1631. }
  1632. }
  1633. unset($this->_displayGroups[$name]);
  1634. if (array_key_exists($name, $this->_order)) {
  1635. unset($this->_order[$name]);
  1636. $this->_orderUpdated = true;
  1637. }
  1638. return true;
  1639. }
  1640. return false;
  1641. }
  1642. /**
  1643. * Remove all display groups
  1644. *
  1645. * @return Zend_Form
  1646. */
  1647. public function clearDisplayGroups()
  1648. {
  1649. foreach ($this->_displayGroups as $key => $group) {
  1650. if (array_key_exists($key, $this->_order)) {
  1651. unset($this->_order[$key]);
  1652. }
  1653. foreach ($group as $name => $element) {
  1654. if (isset($this->_elements[$name])) {
  1655. $this->_order[$name] = $element->getOrder();
  1656. }
  1657. $this->_order[$name] = $element->getOrder();
  1658. }
  1659. }
  1660. $this->_displayGroups = array();
  1661. $this->_orderUpdated = true;
  1662. return $this;
  1663. }
  1664. // Processing
  1665. /**
  1666. * Populate form
  1667. *
  1668. * Proxies to {@link setDefaults()}
  1669. *
  1670. * @param array $values
  1671. * @return Zend_Form
  1672. */
  1673. public function populate(array $values)
  1674. {
  1675. return $this->setDefaults($values);
  1676. }
  1677. /**
  1678. * Determine array key name from given value
  1679. *
  1680. * Given a value such as foo[bar][baz], returns the last element (in this case, 'baz').
  1681. *
  1682. * @param string $value
  1683. * @return string
  1684. */
  1685. protected function _getArrayName($value)
  1686. {
  1687. if (empty($value) || !is_string($value)) {
  1688. return $value;
  1689. }
  1690. if (!strstr($value, '[')) {
  1691. return $value;
  1692. }
  1693. $endPos = strlen($value) - 1;
  1694. if (']' != $value[$endPos]) {
  1695. return $value;
  1696. }
  1697. $start = strrpos($value, '[') + 1;
  1698. $name = substr($value, $start, $endPos - $start);
  1699. return $name;
  1700. }
  1701. /**
  1702. * Extract the value by walking the array using given array path.
  1703. *
  1704. * Given an array path such as foo[bar][baz], returns the value of the last
  1705. * element (in this case, 'baz').
  1706. *
  1707. * @param array $value Array to walk
  1708. * @param string $arrayPath Array notation path of the part to extract
  1709. * @return string
  1710. */
  1711. protected function _dissolveArrayValue($value, $arrayPath)
  1712. {
  1713. // As long as we have more levels
  1714. while ($arrayPos = strpos($arrayPath, '[')) {
  1715. // Get the next key in the path
  1716. $arrayKey = trim(substr($arrayPath, 0, $arrayPos), ']');
  1717. // Set the potentially final value or the next search point in the array
  1718. if (isset($value[$arrayKey])) {
  1719. $value = $value[$arrayKey];
  1720. }
  1721. // Set the next search point in the path
  1722. $arrayPath = trim(substr($arrayPath, $arrayPos + 1), ']');
  1723. }
  1724. if (isset($value[$arrayPath])) {
  1725. $value = $value[$arrayPath];
  1726. }
  1727. return $value;
  1728. }
  1729. /**
  1730. * Converts given arrayPath to an array and attaches given value at the end of it.
  1731. *
  1732. * @param mixed $value The value to attach
  1733. * @param string $arrayPath Given array path to convert and attach to.
  1734. * @return array
  1735. */
  1736. protected function _attachToArray($value, $arrayPath)
  1737. {
  1738. // As long as we have more levels
  1739. while ($arrayPos = strrpos($arrayPath, '[')) {
  1740. // Get the next key in the path
  1741. $arrayKey = trim(substr($arrayPath, $arrayPos + 1), ']');
  1742. // Attach
  1743. $value = array($arrayKey => $value);
  1744. // Set the next search point in the path
  1745. $arrayPath = trim(substr($arrayPath, 0, $arrayPos), ']');
  1746. }
  1747. $value = array($arrayPath => $value);
  1748. return $value;
  1749. }
  1750. /**
  1751. * Validate the form
  1752. *
  1753. * @param array $data
  1754. * @return boolean
  1755. */
  1756. public function isValid($data)
  1757. {
  1758. if (!is_array($data)) {
  1759. require_once 'Zend/Form/Exception.php';
  1760. throw new Zend_Form_Exception(__CLASS__ . '::' . __METHOD__ . ' expects an array');
  1761. }
  1762. $translator = $this->getTranslator();
  1763. $valid = true;
  1764. if ($this->isArray()) {
  1765. $data = $this->_dissolveArrayValue($data, $this->getElementsBelongTo());
  1766. }
  1767. foreach ($this->getElements() as $key => $element) {
  1768. $element->setTranslator($translator);
  1769. if (!isset($data[$key])) {
  1770. $valid = $element->isValid(null, $data) && $valid;
  1771. } else {
  1772. $valid = $element->isValid($data[$key], $data) && $valid;
  1773. }
  1774. }
  1775. foreach ($this->getSubForms() as $key => $form) {
  1776. $form->setTranslator($translator);
  1777. if (isset($data[$key])) {
  1778. $valid = $form->isValid($data[$key]) && $valid;
  1779. } else {
  1780. $valid = $form->isValid($data) && $valid;
  1781. }
  1782. }
  1783. $this->_errorsExist = !$valid;
  1784. return $valid;
  1785. }
  1786. /**
  1787. * Validate a partial form
  1788. *
  1789. * Does not check for required flags.
  1790. *
  1791. * @param array $data
  1792. * @return boolean
  1793. */
  1794. public function isValidPartial(array $data)
  1795. {
  1796. if ($this->isArray()) {
  1797. $data = $this->_dissolveArrayValue($data, $this->getElementsBelongTo());
  1798. }
  1799. $translator = $this->getTranslator();
  1800. $valid = true;
  1801. $validatedSubForms = array();
  1802. foreach ($data as $key => $value) {
  1803. if (null !== ($element = $this->getElement($key))) {
  1804. if (null !== $translator) {
  1805. $element->setTranslator($translator);
  1806. }
  1807. $valid = $element->isValid($value, $data) && $valid;
  1808. } elseif (null !== ($subForm = $this->getSubForm($key))) {
  1809. if (null !== $translator) {
  1810. $subForm->setTranslator($translator);
  1811. }
  1812. $valid = $subForm->isValidPartial($data[$key]) && $valid;
  1813. $validatedSubForms[] = $key;
  1814. }
  1815. }
  1816. foreach ($this->getSubForms() as $key => $subForm) {
  1817. if (!in_array($key, $validatedSubForms)) {
  1818. if (null !== $translator) {
  1819. $subForm->setTranslator($translator);
  1820. }
  1821. $valid = $subForm->isValidPartial($data) && $valid;
  1822. }
  1823. }
  1824. $this->_errorsExist = !$valid;
  1825. return $valid;
  1826. }
  1827. /**
  1828. * Process submitted AJAX data
  1829. *
  1830. * Checks if provided $data is valid, via {@link isValidPartial()}. If so,
  1831. * it returns JSON-encoded boolean true. If not, it returns JSON-encoded
  1832. * error messages (as returned by {@link getMessages()}).
  1833. *
  1834. * @param array $data
  1835. * @return string JSON-encoded boolean true or error messages
  1836. */
  1837. public function processAjax(array $data)
  1838. {
  1839. require_once 'Zend/Json.php';
  1840. if ($this->isValidPartial($data)) {
  1841. return Zend_Json::encode(true);
  1842. }
  1843. $messages = $this->getMessages();
  1844. return Zend_Json::encode($messages);
  1845. }
  1846. /**
  1847. * Add a custom error message to return in the event of failed validation
  1848. *
  1849. * @param string $message
  1850. * @return Zend_Form
  1851. */
  1852. public function addErrorMessage($message)
  1853. {
  1854. $this->_errorMessages[] = (string) $message;
  1855. return $this;
  1856. }
  1857. /**
  1858. * Add multiple custom error messages to return in the event of failed validation
  1859. *
  1860. * @param array $messages
  1861. * @return Zend_Form
  1862. */
  1863. public function addErrorMessages(array $messages)
  1864. {
  1865. foreach ($messages as $message) {
  1866. $this->addErrorMessage($message);
  1867. }
  1868. return $this;
  1869. }
  1870. /**
  1871. * Same as addErrorMessages(), but clears custom error message stack first
  1872. *
  1873. * @param array $messages
  1874. * @return Zend_Form
  1875. */
  1876. public function setErrorMessages(array $messages)
  1877. {
  1878. $this->clearErrorMessages();
  1879. return $this->addErrorMessages($messages);
  1880. }
  1881. /**
  1882. * Retrieve custom error messages
  1883. *
  1884. * @return array
  1885. */
  1886. public function getErrorMessages()
  1887. {
  1888. return $this->_errorMessages;
  1889. }
  1890. /**
  1891. * Clear custom error messages stack
  1892. *
  1893. * @return Zend_Form
  1894. */
  1895. public function clearErrorMessages()
  1896. {
  1897. $this->_errorMessages = array();
  1898. return $this;
  1899. }
  1900. /**
  1901. * Mark the element as being in a failed validation state
  1902. *
  1903. * @return Zend_Form
  1904. */
  1905. public function markAsError()
  1906. {
  1907. $this->_errorsExist = true;
  1908. return $this;
  1909. }
  1910. /**
  1911. * Add an error message and mark element as failed validation
  1912. *
  1913. * @param string $message
  1914. * @return Zend_Form
  1915. */
  1916. public function addError($message)
  1917. {
  1918. $this->addErrorMessage($message);
  1919. $this->markAsError();
  1920. return $this;
  1921. }
  1922. /**
  1923. * Add multiple error messages and flag element as failed validation
  1924. *
  1925. * @param array $messages
  1926. * @return Zend_Form
  1927. */
  1928. public function addErrors(array $messages)
  1929. {
  1930. foreach ($messages as $message) {
  1931. $this->addError($message);
  1932. }
  1933. return $this;
  1934. }
  1935. /**
  1936. * Overwrite any previously set error messages and flag as failed validation
  1937. *
  1938. * @param array $messages
  1939. * @return Zend_Form
  1940. */
  1941. public function setErrors(array $messages)
  1942. {
  1943. $this->clearErrorMessages();
  1944. return $this->addErrors($messages);
  1945. }
  1946. public function persistData()
  1947. {
  1948. }
  1949. /**
  1950. * Are there errors in the form?
  1951. *
  1952. * @return bool
  1953. */
  1954. public function isErrors()
  1955. {
  1956. return $this->_errorsExist;
  1957. }
  1958. /**
  1959. * Get error codes for all elements failing validation
  1960. *
  1961. * @param string $name
  1962. * @return array
  1963. */
  1964. public function getErrors($name = null)
  1965. {
  1966. $errors = array();
  1967. if ((null !== $name) && isset($this->_elements[$name])) {
  1968. $errors = $this->getElement($name)->getErrors();
  1969. } elseif ((null !== $name) && isset($this->_subForms[$name])) {
  1970. $errors = $this->getSubForm($name)->getErrors();
  1971. } else {
  1972. foreach ($this->_elements as $key => $element) {
  1973. $errors[$key] = $element->getErrors();
  1974. }
  1975. foreach ($this->getSubForms() as $key => $subForm) {
  1976. $fErrors = $this->_attachToArray($subForm->getErrors(), $subForm->getElementsBelongTo());
  1977. $errors = array_merge($errors, $fErrors);
  1978. }
  1979. }
  1980. return $errors;
  1981. }
  1982. /**
  1983. * Retrieve error messages from elements failing validations
  1984. *
  1985. * @param string $name
  1986. * @param bool $suppressArrayNotation
  1987. * @return array
  1988. */
  1989. public function getMessages($name = null, $suppressArrayNotation = false)
  1990. {
  1991. if ((null !== $name) && isset($this->_elements[$name])) {
  1992. return $this->getElement($name)->getMessages();
  1993. }
  1994. if ((null !== $name) && isset($this->_subForms[$name])) {
  1995. return $this->getSubForm($name)->getMessages(null, true);
  1996. }
  1997. $arrayKeys = array();
  1998. foreach ($this->getSubForms() as $key => $subForm) {
  1999. $array = $this->_getArrayName($subForm->getElementsBelongTo());
  2000. if (!empty($array)) {
  2001. if ($name == $array) {
  2002. return $subForm->getMessages(null, true);
  2003. }
  2004. $arrayKeys[$key] = $subForm->getElementsBelongTo();
  2005. }
  2006. }
  2007. $customMessages = $this->_getErrorMessages();
  2008. if ($this->isErrors() && !empty($customMessages)) {
  2009. return $customMessages;
  2010. }
  2011. $messages = array();
  2012. foreach ($this->getElements() as $name => $element) {
  2013. $eMessages = $element->getMessages();
  2014. if (!empty($eMessages)) {
  2015. $messages[$name] = $eMessages;
  2016. }
  2017. }
  2018. foreach ($this->getSubForms() as $key => $subForm) {
  2019. $fMessages = $subForm->getMessages(null, true);
  2020. if (!empty($fMessages)) {
  2021. if (array_key_exists($key, $arrayKeys)) {
  2022. $fMessages = $this->_attachToArray($fMessages, $arrayKeys[$key]);
  2023. $messages = array_merge($messages, $fMessages);
  2024. } else {
  2025. $messages[$key] = $fMessages;
  2026. }
  2027. }
  2028. }
  2029. if (!$suppressArrayNotation && $this->isArray()) {
  2030. $messages = $this->_attachToArray($messages, $this->getElementsBelongTo());
  2031. }
  2032. return $messages;
  2033. }
  2034. // Rendering
  2035. /**
  2036. * Set view object
  2037. *
  2038. * @param Zend_View_Interface $view
  2039. * @return Zend_Form
  2040. */
  2041. public function setView(Zend_View_Interface $view = null)
  2042. {
  2043. $this->_view = $view;
  2044. return $this;
  2045. }
  2046. /**
  2047. * Retrieve view object
  2048. *
  2049. * If none registered, attempts to pull from ViewRenderer.
  2050. *
  2051. * @return Zend_View_Interface|null
  2052. */
  2053. public function getView()
  2054. {
  2055. if (null === $this->_view) {
  2056. require_once 'Zend/Controller/Action/HelperBroker.php';
  2057. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  2058. $this->setView($viewRenderer->view);
  2059. }
  2060. return $this->_view;
  2061. }
  2062. /**
  2063. * Instantiate a decorator based on class name or class name fragment
  2064. *
  2065. * @param string $name
  2066. * @param null|array $options
  2067. * @return Zend_Form_Decorator_Interface
  2068. */
  2069. protected function _getDecorator($name, $options)
  2070. {
  2071. $class = $this->getPluginLoader(self::DECORATOR)->load($name);
  2072. if (null === $options) {
  2073. $decorator = new $class;
  2074. } else {
  2075. $decorator = new $class($options);
  2076. }
  2077. return $decorator;
  2078. }
  2079. /**
  2080. * Add a decorator for rendering the element
  2081. *
  2082. * @param string|Zend_Form_Decorator_Interface $decorator
  2083. * @param array|Zend_Config $options Options with which to initialize decorator
  2084. * @return Zend_Form
  2085. */
  2086. public function addDecorator($decorator, $options = null)
  2087. {
  2088. if ($decorator instanceof Zend_Form_Decorator_Interface) {
  2089. $name = get_class($decorator);
  2090. } elseif (is_string($decorator)) {
  2091. $name = $decorator;
  2092. $decorator = array(
  2093. 'decorator' => $name,
  2094. 'options' => $options,
  2095. );
  2096. } elseif (is_array($decorator)) {
  2097. foreach ($decorator as $name => $spec) {
  2098. break;
  2099. }
  2100. if (is_numeric($name)) {
  2101. require_once 'Zend/Form/Exception.php';
  2102. throw new Zend_Form_Exception('Invalid alias provided to addDecorator; must be alphanumeric string');
  2103. }
  2104. if (is_string($spec)) {
  2105. $decorator = array(
  2106. 'decorator' => $spec,
  2107. 'options' => $options,
  2108. );
  2109. } elseif ($spec instanceof Zend_Form_Decorator_Interface) {
  2110. $decorator = $spec;
  2111. }
  2112. } else {
  2113. require_once 'Zend/Form/Exception.php';
  2114. throw new Zend_Form_Exception('Invalid decorator provided to addDecorator; must be string or Zend_Form_Decorator_Interface');
  2115. }
  2116. $this->_decorators[$name] = $decorator;
  2117. return $this;
  2118. }
  2119. /**
  2120. * Add many decorators at once
  2121. *
  2122. * @param array $decorators
  2123. * @return Zend_Form
  2124. */
  2125. public function addDecorators(array $decorators)
  2126. {
  2127. foreach ($decorators as $decoratorInfo) {
  2128. if (is_string($decoratorInfo)) {
  2129. $this->addDecorator($decoratorInfo);
  2130. } elseif ($decoratorInfo instanceof Zend_Form_Decorator_Interface) {
  2131. $this->addDecorator($decoratorInfo);
  2132. } elseif (is_array($decoratorInfo)) {
  2133. $argc = count($decoratorInfo);
  2134. $options = array();
  2135. if (isset($decoratorInfo['decorator'])) {
  2136. $decorator = $decoratorInfo['decorator'];
  2137. if (isset($decoratorInfo['options'])) {
  2138. $options = $decoratorInfo['options'];
  2139. }
  2140. $this->addDecorator($decorator, $options);
  2141. } else {
  2142. switch (true) {
  2143. case (0 == $argc):
  2144. break;
  2145. case (1 <= $argc):
  2146. $decorator = array_shift($decoratorInfo);
  2147. case (2 <= $argc):
  2148. $options = array_shift($decoratorInfo);
  2149. default:
  2150. $this->addDecorator($decorator, $options);
  2151. break;
  2152. }
  2153. }
  2154. } else {
  2155. require_once 'Zend/Form/Exception.php';
  2156. throw new Zend_Form_Exception('Invalid decorator passed to addDecorators()');
  2157. }
  2158. }
  2159. return $this;
  2160. }
  2161. /**
  2162. * Overwrite all decorators
  2163. *
  2164. * @param array $decorators
  2165. * @return Zend_Form
  2166. */
  2167. public function setDecorators(array $decorators)
  2168. {
  2169. $this->clearDecorators();
  2170. return $this->addDecorators($decorators);
  2171. }
  2172. /**
  2173. * Retrieve a registered decorator
  2174. *
  2175. * @param string $name
  2176. * @return false|Zend_Form_Decorator_Abstract
  2177. */
  2178. public function getDecorator($name)
  2179. {
  2180. if (!isset($this->_decorators[$name])) {
  2181. $len = strlen($name);
  2182. foreach ($this->_decorators as $localName => $decorator) {
  2183. if ($len > strlen($localName)) {
  2184. continue;
  2185. }
  2186. if (0 === substr_compare($localName, $name, -$len, $len, true)) {
  2187. if (is_array($decorator)) {
  2188. return $this->_loadDecorator($decorator, $localName);
  2189. }
  2190. return $decorator;
  2191. }
  2192. }
  2193. return false;
  2194. }
  2195. if (is_array($this->_decorators[$name])) {
  2196. return $this->_loadDecorator($this->_decorators[$name], $name);
  2197. }
  2198. return $this->_decorators[$name];
  2199. }
  2200. /**
  2201. * Retrieve all decorators
  2202. *
  2203. * @return array
  2204. */
  2205. public function getDecorators()
  2206. {
  2207. foreach ($this->_decorators as $key => $value) {
  2208. if (is_array($value)) {
  2209. $this->_loadDecorator($value, $key);
  2210. }
  2211. }
  2212. return $this->_decorators;
  2213. }
  2214. /**
  2215. * Remove a single decorator
  2216. *
  2217. * @param string $name
  2218. * @return bool
  2219. */
  2220. public function removeDecorator($name)
  2221. {
  2222. $decorator = $this->getDecorator($name);
  2223. if ($decorator) {
  2224. if (array_key_exists($name, $this->_decorators)) {
  2225. unset($this->_decorators[$name]);
  2226. } else {
  2227. $class = get_class($decorator);
  2228. if (!array_key_exists($class, $this->_decorators)) {
  2229. return false;
  2230. }
  2231. unset($this->_decorators[$class]);
  2232. }
  2233. return true;
  2234. }
  2235. return false;
  2236. }
  2237. /**
  2238. * Clear all decorators
  2239. *
  2240. * @return Zend_Form
  2241. */
  2242. public function clearDecorators()
  2243. {
  2244. $this->_decorators = array();
  2245. return $this;
  2246. }
  2247. /**
  2248. * Set all element decorators as specified
  2249. *
  2250. * @param array $decorators
  2251. * @param array|null $elements Specific elements to decorate or exclude from decoration
  2252. * @param bool $include Whether $elements is an inclusion or exclusion list
  2253. * @return Zend_Form
  2254. */
  2255. public function setElementDecorators(array $decorators, array $elements = null, $include = true)
  2256. {
  2257. if (is_array($elements)) {
  2258. if ($include) {
  2259. $elementObjs = array();
  2260. foreach ($elements as $name) {
  2261. if (null !== ($element = $this->getElement($name))) {
  2262. $elementObjs[] = $element;
  2263. }
  2264. }
  2265. } else {
  2266. $elementObjs = $this->getElements();
  2267. foreach ($elements as $name) {
  2268. if (array_key_exists($name, $elementObjs)) {
  2269. unset($elementObjs[$name]);
  2270. }
  2271. }
  2272. }
  2273. } else {
  2274. $elementObjs = $this->getElements();
  2275. }
  2276. foreach ($elementObjs as $element) {
  2277. $element->setDecorators($decorators);
  2278. }
  2279. return $this;
  2280. }
  2281. /**
  2282. * Set all display group decorators as specified
  2283. *
  2284. * @param array $decorators
  2285. * @return Zend_Form
  2286. */
  2287. public function setDisplayGroupDecorators(array $decorators)
  2288. {
  2289. foreach ($this->getDisplayGroups() as $group) {
  2290. $group->setDecorators($decorators);
  2291. }
  2292. return $this;
  2293. }
  2294. /**
  2295. * Set all subform decorators as specified
  2296. *
  2297. * @param array $decorators
  2298. * @return Zend_Form
  2299. */
  2300. public function setSubFormDecorators(array $decorators)
  2301. {
  2302. foreach ($this->getSubForms() as $form) {
  2303. $form->setDecorators($decorators);
  2304. }
  2305. return $this;
  2306. }
  2307. /**
  2308. * Render form
  2309. *
  2310. * @param Zend_View_Interface $view
  2311. * @return string
  2312. */
  2313. public function render(Zend_View_Interface $view = null)
  2314. {
  2315. if (null !== $view) {
  2316. $this->setView($view);
  2317. }
  2318. $content = '';
  2319. foreach ($this->getDecorators() as $decorator) {
  2320. $decorator->setElement($this);
  2321. $content = $decorator->render($content);
  2322. }
  2323. return $content;
  2324. }
  2325. /**
  2326. * Serialize as string
  2327. *
  2328. * Proxies to {@link render()}.
  2329. *
  2330. * @return string
  2331. */
  2332. public function __toString()
  2333. {
  2334. try {
  2335. $return = $this->render();
  2336. return $return;
  2337. } catch (Exception $e) {
  2338. $message = "Exception caught by form: " . $e->getMessage()
  2339. . "\nStack Trace:\n" . $e->getTraceAsString();
  2340. trigger_error($message, E_USER_WARNING);
  2341. return '';
  2342. }
  2343. }
  2344. // Localization:
  2345. /**
  2346. * Set translator object
  2347. *
  2348. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  2349. * @return Zend_Form
  2350. */
  2351. public function setTranslator($translator = null)
  2352. {
  2353. if (null === $translator) {
  2354. $this->_translator = null;
  2355. } elseif ($translator instanceof Zend_Translate_Adapter) {
  2356. $this->_translator = $translator;
  2357. } elseif ($translator instanceof Zend_Translate) {
  2358. $this->_translator = $translator->getAdapter();
  2359. } else {
  2360. require_once 'Zend/Form/Exception.php';
  2361. throw new Zend_Form_Exception('Invalid translator specified');
  2362. }
  2363. return $this;
  2364. }
  2365. /**
  2366. * Set global default translator object
  2367. *
  2368. * @param Zend_Translate|Zend_Translate_Adapter|null $translator
  2369. * @return void
  2370. */
  2371. public static function setDefaultTranslator($translator = null)
  2372. {
  2373. if (null === $translator) {
  2374. self::$_translatorDefault = null;
  2375. } elseif ($translator instanceof Zend_Translate_Adapter) {
  2376. self::$_translatorDefault = $translator;
  2377. } elseif ($translator instanceof Zend_Translate) {
  2378. self::$_translatorDefault = $translator->getAdapter();
  2379. } else {
  2380. require_once 'Zend/Form/Exception.php';
  2381. throw new Zend_Form_Exception('Invalid translator specified');
  2382. }
  2383. }
  2384. /**
  2385. * Retrieve translator object
  2386. *
  2387. * @return Zend_Translate|null
  2388. */
  2389. public function getTranslator()
  2390. {
  2391. if ($this->translatorIsDisabled()) {
  2392. return null;
  2393. }
  2394. if (null === $this->_translator) {
  2395. return self::getDefaultTranslator();
  2396. }
  2397. return $this->_translator;
  2398. }
  2399. /**
  2400. * Get global default translator object
  2401. *
  2402. * @return null|Zend_Translate
  2403. */
  2404. public static function getDefaultTranslator()
  2405. {
  2406. if (null === self::$_translatorDefault) {
  2407. require_once 'Zend/Registry.php';
  2408. if (Zend_Registry::isRegistered('Zend_Translate')) {
  2409. $translator = Zend_Registry::get('Zend_Translate');
  2410. if ($translator instanceof Zend_Translate_Adapter) {
  2411. return $translator;
  2412. } elseif ($translator instanceof Zend_Translate) {
  2413. return $translator->getAdapter();
  2414. }
  2415. }
  2416. }
  2417. return self::$_translatorDefault;
  2418. }
  2419. /**
  2420. * Indicate whether or not translation should be disabled
  2421. *
  2422. * @param bool $flag
  2423. * @return Zend_Form
  2424. */
  2425. public function setDisableTranslator($flag)
  2426. {
  2427. $this->_translatorDisabled = (bool) $flag;
  2428. return $this;
  2429. }
  2430. /**
  2431. * Is translation disabled?
  2432. *
  2433. * @return bool
  2434. */
  2435. public function translatorIsDisabled()
  2436. {
  2437. return $this->_translatorDisabled;
  2438. }
  2439. /**
  2440. * Overloading: access to elements, form groups, and display groups
  2441. *
  2442. * @param string $name
  2443. * @return Zend_Form_Element|Zend_Form|null
  2444. */
  2445. public function __get($name)
  2446. {
  2447. if (isset($this->_elements[$name])) {
  2448. return $this->_elements[$name];
  2449. } elseif (isset($this->_subForms[$name])) {
  2450. return $this->_subForms[$name];
  2451. } elseif (isset($this->_displayGroups[$name])) {
  2452. return $this->_displayGroups[$name];
  2453. }
  2454. return null;
  2455. }
  2456. /**
  2457. * Overloading: access to elements, form groups, and display groups
  2458. *
  2459. * @param string $name
  2460. * @param Zend_Form_Element|Zend_Form $value
  2461. * @return void
  2462. * @throws Zend_Form_Exception for invalid $value
  2463. */
  2464. public function __set($name, $value)
  2465. {
  2466. if ($value instanceof Zend_Form_Element) {
  2467. $this->addElement($value, $name);
  2468. return;
  2469. } elseif ($value instanceof Zend_Form) {
  2470. $this->addSubForm($value, $name);
  2471. return;
  2472. } elseif (is_array($value)) {
  2473. $this->addDisplayGroup($value, $name);
  2474. return;
  2475. }
  2476. require_once 'Zend/Form/Exception.php';
  2477. if (is_object($value)) {
  2478. $type = get_class($value);
  2479. } else {
  2480. $type = gettype($value);
  2481. }
  2482. throw new Zend_Form_Exception('Only form elements and groups may be overloaded; variable of type "' . $type . '" provided');
  2483. }
  2484. /**
  2485. * Overloading: access to elements, form groups, and display groups
  2486. *
  2487. * @param string $name
  2488. * @return boolean
  2489. */
  2490. public function __isset($name)
  2491. {
  2492. if (isset($this->_elements[$name])
  2493. || isset($this->_subForms[$name])
  2494. || isset($this->_displayGroups[$name]))
  2495. {
  2496. return true;
  2497. }
  2498. return false;
  2499. }
  2500. /**
  2501. * Overloading: access to elements, form groups, and display groups
  2502. *
  2503. * @param string $name
  2504. * @return void
  2505. */
  2506. public function __unset($name)
  2507. {
  2508. if (isset($this->_elements[$name])) {
  2509. unset($this->_elements[$name]);
  2510. } elseif (isset($this->_subForms[$name])) {
  2511. unset($this->_subForms[$name]);
  2512. } elseif (isset($this->_displayGroups[$name])) {
  2513. unset($this->_displayGroups[$name]);
  2514. }
  2515. }
  2516. /**
  2517. * Overloading: allow rendering specific decorators
  2518. *
  2519. * Call renderDecoratorName() to render a specific decorator.
  2520. *
  2521. * @param string $method
  2522. * @param array $args
  2523. * @return string
  2524. * @throws Zend_Form_Exception for invalid decorator or invalid method call
  2525. */
  2526. public function __call($method, $args)
  2527. {
  2528. if ('render' == substr($method, 0, 6)) {
  2529. $decoratorName = substr($method, 6);
  2530. if (false !== ($decorator = $this->getDecorator($decoratorName))) {
  2531. $decorator->setElement($this);
  2532. $seed = '';
  2533. if (0 < count($args)) {
  2534. $seed = array_shift($args);
  2535. }
  2536. return $decorator->render($seed);
  2537. }
  2538. require_once 'Zend/Form/Exception.php';
  2539. throw new Zend_Form_Exception(sprintf('Decorator by name %s does not exist', $decoratorName));
  2540. }
  2541. require_once 'Zend/Form/Exception.php';
  2542. throw new Zend_Form_Exception(sprintf('Method %s does not exist', $method));
  2543. }
  2544. // Interfaces: Iterator, Countable
  2545. /**
  2546. * Current element/subform/display group
  2547. *
  2548. * @return Zend_Form_Element|Zend_Form_DisplayGroup|Zend_Form
  2549. */
  2550. public function current()
  2551. {
  2552. $this->_sort();
  2553. current($this->_order);
  2554. $key = key($this->_order);
  2555. if (isset($this->_elements[$key])) {
  2556. return $this->getElement($key);
  2557. } elseif (isset($this->_subForms[$key])) {
  2558. return $this->getSubForm($key);
  2559. } elseif (isset($this->_displayGroups[$key])) {
  2560. return $this->getDisplayGroup($key);
  2561. } else {
  2562. require_once 'Zend/Form/Exception.php';
  2563. throw new Zend_Form_Exception(sprintf('Corruption detected in form; invalid key ("%s") found in internal iterator', (string) $key));
  2564. }
  2565. }
  2566. /**
  2567. * Current element/subform/display group name
  2568. *
  2569. * @return string
  2570. */
  2571. public function key()
  2572. {
  2573. $this->_sort();
  2574. return key($this->_order);
  2575. }
  2576. /**
  2577. * Move pointer to next element/subform/display group
  2578. *
  2579. * @return void
  2580. */
  2581. public function next()
  2582. {
  2583. $this->_sort();
  2584. next($this->_order);
  2585. }
  2586. /**
  2587. * Move pointer to beginning of element/subform/display group loop
  2588. *
  2589. * @return void
  2590. */
  2591. public function rewind()
  2592. {
  2593. $this->_sort();
  2594. reset($this->_order);
  2595. }
  2596. /**
  2597. * Determine if current element/subform/display group is valid
  2598. *
  2599. * @return bool
  2600. */
  2601. public function valid()
  2602. {
  2603. $this->_sort();
  2604. return (current($this->_order) !== false);
  2605. }
  2606. /**
  2607. * Count of elements/subforms that are iterable
  2608. *
  2609. * @return int
  2610. */
  2611. public function count()
  2612. {
  2613. return count($this->_order);
  2614. }
  2615. /**
  2616. * Set flag to disable loading default decorators
  2617. *
  2618. * @param bool $flag
  2619. * @return Zend_Form
  2620. */
  2621. public function setDisableLoadDefaultDecorators($flag)
  2622. {
  2623. $this->_disableLoadDefaultDecorators = (bool) $flag;
  2624. return $this;
  2625. }
  2626. /**
  2627. * Should we load the default decorators?
  2628. *
  2629. * @return bool
  2630. */
  2631. public function loadDefaultDecoratorsIsDisabled()
  2632. {
  2633. return $this->_disableLoadDefaultDecorators;
  2634. }
  2635. /**
  2636. * Load the default decorators
  2637. *
  2638. * @return void
  2639. */
  2640. public function loadDefaultDecorators()
  2641. {
  2642. if ($this->loadDefaultDecoratorsIsDisabled()) {
  2643. return;
  2644. }
  2645. $decorators = $this->getDecorators();
  2646. if (empty($decorators)) {
  2647. $this->addDecorator('FormElements')
  2648. ->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
  2649. ->addDecorator('Form');
  2650. }
  2651. }
  2652. /**
  2653. * Sort items according to their order
  2654. *
  2655. * @return void
  2656. */
  2657. protected function _sort()
  2658. {
  2659. if ($this->_orderUpdated) {
  2660. $items = array();
  2661. $index = 0;
  2662. foreach ($this->_order as $key => $order) {
  2663. if (null === $order) {
  2664. if (null === ($order = $this->{$key}->getOrder())) {
  2665. while (array_search($index, $this->_order, true)) {
  2666. ++$index;
  2667. }
  2668. $items[$index] = $key;
  2669. ++$index;
  2670. } else {
  2671. $items[$order] = $key;
  2672. }
  2673. } else {
  2674. $items[$order] = $key;
  2675. }
  2676. }
  2677. $items = array_flip($items);
  2678. asort($items);
  2679. $this->_order = $items;
  2680. $this->_orderUpdated = false;
  2681. }
  2682. }
  2683. /**
  2684. * Lazy-load a decorator
  2685. *
  2686. * @param array $decorator Decorator type and options
  2687. * @param mixed $name Decorator name or alias
  2688. * @return Zend_Form_Decorator_Interface
  2689. */
  2690. protected function _loadDecorator(array $decorator, $name)
  2691. {
  2692. $sameName = false;
  2693. if ($name == $decorator['decorator']) {
  2694. $sameName = true;
  2695. }
  2696. $instance = $this->_getDecorator($decorator['decorator'], $decorator['options']);
  2697. if ($sameName) {
  2698. $newName = get_class($instance);
  2699. $decoratorNames = array_keys($this->_decorators);
  2700. $order = array_flip($decoratorNames);
  2701. $order[$newName] = $order[$name];
  2702. $decoratorsExchange = array();
  2703. unset($order[$name]);
  2704. asort($order);
  2705. foreach ($order as $key => $index) {
  2706. if ($key == $newName) {
  2707. $decoratorsExchange[$key] = $instance;
  2708. continue;
  2709. }
  2710. $decoratorsExchange[$key] = $this->_decorators[$key];
  2711. }
  2712. $this->_decorators = $decoratorsExchange;
  2713. } else {
  2714. $this->_decorators[$name] = $instance;
  2715. }
  2716. return $instance;
  2717. }
  2718. /**
  2719. * Retrieve optionally translated custom error messages
  2720. *
  2721. * @return array
  2722. */
  2723. protected function _getErrorMessages()
  2724. {
  2725. $messages = $this->getErrorMessages();
  2726. $translator = $this->getTranslator();
  2727. if (null !== $translator) {
  2728. foreach ($messages as $key => $message) {
  2729. $messages[$key] = $translator->translate($message);
  2730. }
  2731. }
  2732. return $messages;
  2733. }
  2734. }