PageRenderTime 80ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Form.php

https://bitbucket.org/mercysam/zfs
PHP | 3048 lines | 1730 code | 314 blank | 1004 comment | 265 complexity | 94995385d2821a7db2d2df9444882af8 MD5 | raw file

Large files files are truncated, but you can click here to view the full 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 …

Large files files are truncated, but you can click here to view the full file