00001 <?php
00022 require_once 'Zend/Validate/Interface.php';
00023
00033 class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
00034 {
00038 const DECORATOR = 'DECORATOR';
00039 const ELEMENT = 'ELEMENT';
00045 const METHOD_DELETE = 'delete';
00046 const METHOD_GET = 'get';
00047 const METHOD_POST = 'post';
00048 const METHOD_PUT = 'put';
00054 const ENCTYPE_URLENCODED = 'application/x-www-form-urlencoded';
00055 const ENCTYPE_MULTIPART = 'multipart/form-data';
00062 protected $_attribs = array();
00063
00068 protected $_decorators = array();
00069
00074 protected $_defaultDisplayGroupClass = 'Zend_Form_DisplayGroup';
00075
00080 protected $_description;
00081
00086 protected $_disableLoadDefaultDecorators = false;
00087
00092 protected $_displayGroupPrefixPaths = array();
00093
00098 protected $_displayGroups = array();
00099
00104 protected $_elementDecorators;
00105
00110 protected $_elementPrefixPaths = array();
00111
00116 protected $_elements = array();
00117
00122 protected $_elementsBelongTo;
00123
00128 protected $_errorMessages = array();
00129
00134 protected $_errorsExist = false;
00135
00140 protected $_errorsForced = false;
00141
00146 protected $_formOrder;
00147
00152 protected $_isArray = false;
00153
00158 protected $_legend;
00159
00164 protected $_loaders = array();
00165
00170 protected $_methods = array('delete', 'get', 'post', 'put');
00171
00176 protected $_order = array();
00177
00182 protected $_orderUpdated = false;
00183
00188 protected $_subFormPrefixPaths = array();
00189
00194 protected $_subForms = array();
00195
00199 protected $_translator;
00200
00205 protected static $_translatorDefault;
00206
00211 protected $_translatorDisabled = false;
00212
00216 protected $_view;
00217
00226 public function __construct($options = null)
00227 {
00228 if (is_array($options)) {
00229 $this->setOptions($options);
00230 } elseif ($options instanceof Zend_Config) {
00231 $this->setConfig($options);
00232 }
00233
00234
00235 $this->init();
00236
00237 $this->loadDefaultDecorators();
00238 }
00239
00245 public function __clone()
00246 {
00247 $elements = array();
00248 foreach ($this->getElements() as $name => $element) {
00249 $elements[] = clone $element;
00250 }
00251 $this->setElements($elements);
00252
00253 $subForms = array();
00254 foreach ($this->getSubForms() as $name => $subForm) {
00255 $subForms[$name] = clone $subForm;
00256 }
00257 $this->setSubForms($subForms);
00258
00259 $displayGroups = array();
00260 foreach ($this->_displayGroups as $group) {
00261 $clone = clone $group;
00262 $elements = array();
00263 foreach ($clone->getElements() as $name => $e) {
00264 $elements[] = $this->getElement($name);
00265 }
00266 $clone->setElements($elements);
00267 $displayGroups[] = $clone;
00268 }
00269 $this->setDisplayGroups($displayGroups);
00270 }
00271
00277 public function reset()
00278 {
00279 foreach ($this->getElements() as $element) {
00280 $element->setValue(null);
00281 }
00282 foreach ($this->getSubForms() as $subForm) {
00283 $subForm->reset();
00284 }
00285
00286 return $this;
00287 }
00288
00294 public function init()
00295 {
00296 }
00297
00304 public function setOptions(array $options)
00305 {
00306 if (isset($options['prefixPath'])) {
00307 $this->addPrefixPaths($options['prefixPath']);
00308 unset($options['prefixPath']);
00309 }
00310
00311 if (isset($options['elementPrefixPath'])) {
00312 $this->addElementPrefixPaths($options['elementPrefixPath']);
00313 unset($options['elementPrefixPath']);
00314 }
00315
00316 if (isset($options['displayGroupPrefixPath'])) {
00317 $this->addDisplayGroupPrefixPaths($options['displayGroupPrefixPath']);
00318 unset($options['displayGroupPrefixPath']);
00319 }
00320
00321 if (isset($options['elementDecorators'])) {
00322 $this->_elementDecorators = $options['elementDecorators'];
00323 unset($options['elementDecorators']);
00324 }
00325
00326 if (isset($options['elements'])) {
00327 $this->setElements($options['elements']);
00328 unset($options['elements']);
00329 }
00330
00331 if (isset($options['defaultDisplayGroupClass'])) {
00332 $this->setDefaultDisplayGroupClass($options['defaultDisplayGroupClass']);
00333 unset($options['defaultDisplayGroupClass']);
00334 }
00335
00336 if (isset($options['displayGroupDecorators'])) {
00337 $displayGroupDecorators = $options['displayGroupDecorators'];
00338 unset($options['displayGroupDecorators']);
00339 }
00340
00341 if (isset($options['elementsBelongTo'])) {
00342 $elementsBelongTo = $options['elementsBelongTo'];
00343 unset($options['elementsBelongTo']);
00344 }
00345
00346 if (isset($options['attribs'])) {
00347 $this->addAttribs($options['attribs']);
00348 unset($options['attribs']);
00349 }
00350
00351 $forbidden = array(
00352 'Options', 'Config', 'PluginLoader', 'SubForms', 'View', 'Translator',
00353 'Attrib', 'Default',
00354 );
00355
00356 foreach ($options as $key => $value) {
00357 $normalized = ucfirst($key);
00358 if (in_array($normalized, $forbidden)) {
00359 continue;
00360 }
00361
00362 $method = 'set' . $normalized;
00363 if (method_exists($this, $method)) {
00364 $this->$method($value);
00365 } else {
00366 $this->setAttrib($key, $value);
00367 }
00368 }
00369
00370 if (isset($displayGroupDecorators)) {
00371 $this->setDisplayGroupDecorators($displayGroupDecorators);
00372 }
00373
00374 if (isset($elementsBelongTo)) {
00375 $this->setElementsBelongTo($elementsBelongTo);
00376 }
00377
00378 return $this;
00379 }
00380
00387 public function setConfig(Zend_Config $config)
00388 {
00389 return $this->setOptions($config->toArray());
00390 }
00391
00392
00393
00394
00403 public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type = null)
00404 {
00405 $type = strtoupper($type);
00406 switch ($type) {
00407 case self::DECORATOR:
00408 case self::ELEMENT:
00409 $this->_loaders[$type] = $loader;
00410 return $this;
00411 default:
00412 require_once 'Zend/Form/Exception.php';
00413 throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
00414 }
00415 }
00416
00430 public function getPluginLoader($type = null)
00431 {
00432 $type = strtoupper($type);
00433 if (!isset($this->_loaders[$type])) {
00434 switch ($type) {
00435 case self::DECORATOR:
00436 $prefixSegment = 'Form_Decorator';
00437 $pathSegment = 'Form/Decorator';
00438 break;
00439 case self::ELEMENT:
00440 $prefixSegment = 'Form_Element';
00441 $pathSegment = 'Form/Element';
00442 break;
00443 default:
00444 require_once 'Zend/Form/Exception.php';
00445 throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
00446 }
00447
00448 require_once 'Zend/Loader/PluginLoader.php';
00449 $this->_loaders[$type] = new Zend_Loader_PluginLoader(
00450 array('Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/')
00451 );
00452 }
00453
00454 return $this->_loaders[$type];
00455 }
00456
00477 public function addPrefixPath($prefix, $path, $type = null)
00478 {
00479 $type = strtoupper($type);
00480 switch ($type) {
00481 case self::DECORATOR:
00482 case self::ELEMENT:
00483 $loader = $this->getPluginLoader($type);
00484 $loader->addPrefixPath($prefix, $path);
00485 return $this;
00486 case null:
00487 $prefix = rtrim($prefix, '_');
00488 $path = rtrim($path, DIRECTORY_SEPARATOR);
00489 foreach (array(self::DECORATOR, self::ELEMENT) as $type) {
00490 $cType = ucfirst(strtolower($type));
00491 $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR;
00492 $pluginPrefix = $prefix . '_' . $cType;
00493 $loader = $this->getPluginLoader($type);
00494 $loader->addPrefixPath($pluginPrefix, $pluginPath);
00495 }
00496 return $this;
00497 default:
00498 require_once 'Zend/Form/Exception.php';
00499 throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
00500 }
00501 }
00502
00509 public function addPrefixPaths(array $spec)
00510 {
00511 if (isset($spec['prefix']) && isset($spec['path'])) {
00512 return $this->addPrefixPath($spec['prefix'], $spec['path']);
00513 }
00514 foreach ($spec as $type => $paths) {
00515 if (is_numeric($type) && is_array($paths)) {
00516 $type = null;
00517 if (isset($paths['prefix']) && isset($paths['path'])) {
00518 if (isset($paths['type'])) {
00519 $type = $paths['type'];
00520 }
00521 $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
00522 }
00523 } elseif (!is_numeric($type)) {
00524 if (!isset($paths['prefix']) || !isset($paths['path'])) {
00525 continue;
00526 }
00527 $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
00528 }
00529 }
00530 return $this;
00531 }
00532
00541 public function addElementPrefixPath($prefix, $path, $type = null)
00542 {
00543 $this->_elementPrefixPaths[] = array(
00544 'prefix' => $prefix,
00545 'path' => $path,
00546 'type' => $type,
00547 );
00548
00549 foreach ($this->getElements() as $element) {
00550 $element->addPrefixPath($prefix, $path, $type);
00551 }
00552
00553 foreach ($this->getSubForms() as $subForm) {
00554 $subForm->addElementPrefixPath($prefix, $path, $type);
00555 }
00556
00557 return $this;
00558 }
00559
00566 public function addElementPrefixPaths(array $spec)
00567 {
00568 $this->_elementPrefixPaths = $this->_elementPrefixPaths + $spec;
00569
00570 foreach ($this->getElements() as $element) {
00571 $element->addPrefixPaths($spec);
00572 }
00573
00574 return $this;
00575 }
00576
00584 public function addDisplayGroupPrefixPath($prefix, $path)
00585 {
00586 $this->_displayGroupPrefixPaths[] = array(
00587 'prefix' => $prefix,
00588 'path' => $path,
00589 );
00590
00591 foreach ($this->getDisplayGroups() as $group) {
00592 $group->addPrefixPath($prefix, $path);
00593 }
00594
00595 return $this;
00596 }
00597
00604 public function addDisplayGroupPrefixPaths(array $spec)
00605 {
00606 foreach ($spec as $key => $value) {
00607 if (is_string($value) && !is_numeric($key)) {
00608 $this->addDisplayGroupPrefixPath($key, $value);
00609 continue;
00610 }
00611
00612 if (is_string($value) && is_numeric($key)) {
00613 continue;
00614 }
00615
00616 if (is_array($value)) {
00617 $count = count($value);
00618 if (array_keys($value) === range(0, $count - 1)) {
00619 if ($count < 2) {
00620 continue;
00621 }
00622 $prefix = array_shift($value);
00623 $path = array_shift($value);
00624 $this->addDisplayGroupPrefixPath($prefix, $path);
00625 continue;
00626 }
00627 if (array_key_exists('prefix', $value) && array_key_exists('path', $value)) {
00628 $this->addDisplayGroupPrefixPath($value['prefix'], $value['path']);
00629 }
00630 }
00631 }
00632 return $this;
00633 }
00634
00635
00636
00644 public function setAttrib($key, $value)
00645 {
00646 $key = (string) $key;
00647 $this->_attribs[$key] = $value;
00648 return $this;
00649 }
00650
00657 public function addAttribs(array $attribs)
00658 {
00659 foreach ($attribs as $key => $value) {
00660 $this->setAttrib($key, $value);
00661 }
00662 return $this;
00663 }
00664
00673 public function setAttribs(array $attribs)
00674 {
00675 $this->clearAttribs();
00676 return $this->addAttribs($attribs);
00677 }
00678
00685 public function getAttrib($key)
00686 {
00687 $key = (string) $key;
00688 if (!isset($this->_attribs[$key])) {
00689 return null;
00690 }
00691
00692 return $this->_attribs[$key];
00693 }
00694
00700 public function getAttribs()
00701 {
00702 return $this->_attribs;
00703 }
00704
00711 public function removeAttrib($key)
00712 {
00713 if (isset($this->_attribs[$key])) {
00714 unset($this->_attribs[$key]);
00715 return true;
00716 }
00717
00718 return false;
00719 }
00720
00726 public function clearAttribs()
00727 {
00728 $this->_attribs = array();
00729 return $this;
00730 }
00731
00738 public function setAction($action)
00739 {
00740 return $this->setAttrib('action', (string) $action);
00741 }
00742
00750 public function getAction()
00751 {
00752 $action = $this->getAttrib('action');
00753 if (null === $action) {
00754 $action = '';
00755 $this->setAction($action);
00756 }
00757 return $action;
00758 }
00759
00769 public function setMethod($method)
00770 {
00771 $method = strtolower($method);
00772 if (!in_array($method, $this->_methods)) {
00773 require_once 'Zend/Form/Exception.php';
00774 throw new Zend_Form_Exception(sprintf('"%s" is an invalid form method', $method));
00775 }
00776 $this->setAttrib('method', $method);
00777 return $this;
00778 }
00779
00785 public function getMethod()
00786 {
00787 if (null === ($method = $this->getAttrib('method'))) {
00788 $method = self::METHOD_POST;
00789 $this->setAttrib('method', $method);
00790 }
00791 return strtolower($method);
00792 }
00793
00800 public function setEnctype($value)
00801 {
00802 $this->setAttrib('enctype', $value);
00803 return $this;
00804 }
00805
00811 public function getEnctype()
00812 {
00813 if (null === ($enctype = $this->getAttrib('enctype'))) {
00814 $enctype = self::ENCTYPE_URLENCODED;
00815 $this->setAttrib('enctype', $enctype);
00816 }
00817 return $this->getAttrib('enctype');
00818 }
00819
00827 public function filterName($value, $allowBrackets = false)
00828 {
00829 $charset = '^a-zA-Z0-9_\x7f-\xff';
00830 if ($allowBrackets) {
00831 $charset .= '\[\]';
00832 }
00833 return preg_replace('/[' . $charset . ']/', '', (string) $value);
00834 }
00835
00842 public function setName($name)
00843 {
00844 $name = $this->filterName($name);
00845 if (('0' !== $name) && empty($name)) {
00846 require_once 'Zend/Form/Exception.php';
00847 throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty');
00848 }
00849
00850 return $this->setAttrib('name', $name);
00851 }
00852
00858 public function getName()
00859 {
00860 return $this->getAttrib('name');
00861 }
00862
00870 public function getFullyQualifiedName()
00871 {
00872 return $this->getName();
00873 }
00874
00880 public function getId()
00881 {
00882 if (null !== ($id = $this->getAttrib('id'))) {
00883 return $id;
00884 }
00885
00886 $id = $this->getFullyQualifiedName();
00887
00888
00889 if (!strstr($id, '[')) {
00890 return $id;
00891 }
00892
00893
00894 if ('[]' == substr($id, -2)) {
00895 $id = substr($id, 0, strlen($id) - 2);
00896 }
00897 $id = str_replace('][', '-', $id);
00898 $id = str_replace(array(']', '['), '-', $id);
00899 $id = trim($id, '-');
00900
00901 return $id;
00902 }
00903
00910 public function setLegend($value)
00911 {
00912 $this->_legend = (string) $value;
00913 return $this;
00914 }
00915
00921 public function getLegend()
00922 {
00923 return $this->_legend;
00924 }
00925
00932 public function setDescription($value)
00933 {
00934 $this->_description = (string) $value;
00935 return $this;
00936 }
00937
00943 public function getDescription()
00944 {
00945 return $this->_description;
00946 }
00947
00954 public function setOrder($index)
00955 {
00956 $this->_formOrder = (int) $index;
00957 return $this;
00958 }
00959
00965 public function getOrder()
00966 {
00967 return $this->_formOrder;
00968 }
00969
00970
00971
00988 public function addElement($element, $name = null, $options = null)
00989 {
00990 if (is_string($element)) {
00991 if (null === $name) {
00992 require_once 'Zend/Form/Exception.php';
00993 throw new Zend_Form_Exception('Elements specified by string must have an accompanying name');
00994 }
00995
00996 if (is_array($this->_elementDecorators)) {
00997 if (null === $options) {
00998 $options = array('decorators' => $this->_elementDecorators);
00999 } elseif ($options instanceof Zend_Config) {
01000 $options = $options->toArray();
01001 }
01002 if (is_array($options)
01003 && !array_key_exists('decorators', $options)
01004 ) {
01005 $options['decorators'] = $this->_elementDecorators;
01006 }
01007 }
01008
01009 $this->_elements[$name] = $this->createElement($element, $name, $options);
01010 } elseif ($element instanceof Zend_Form_Element) {
01011 $prefixPaths = array();
01012 $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
01013 if (!empty($this->_elementPrefixPaths)) {
01014 $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
01015 }
01016
01017 if (null === $name) {
01018 $name = $element->getName();
01019 }
01020
01021 $this->_elements[$name] = $element;
01022 $this->_elements[$name]->addPrefixPaths($prefixPaths);
01023 }
01024
01025 $this->_order[$name] = $this->_elements[$name]->getOrder();
01026 $this->_orderUpdated = true;
01027 $this->_setElementsBelongTo($name);
01028
01029 return $this;
01030 }
01031
01045 public function createElement($type, $name, $options = null)
01046 {
01047 if (!is_string($type)) {
01048 require_once 'Zend/Form/Exception.php';
01049 throw new Zend_Form_Exception('Element type must be a string indicating type');
01050 }
01051
01052 if (!is_string($name)) {
01053 require_once 'Zend/Form/Exception.php';
01054 throw new Zend_Form_Exception('Element name must be a string');
01055 }
01056
01057 $prefixPaths = array();
01058 $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
01059 if (!empty($this->_elementPrefixPaths)) {
01060 $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
01061 }
01062
01063 if ($options instanceof Zend_Config) {
01064 $options = $options->toArray();
01065 }
01066
01067 if ((null === $options) || !is_array($options)) {
01068 $options = array('prefixPath' => $prefixPaths);
01069 } elseif (is_array($options)) {
01070 if (array_key_exists('prefixPath', $options)) {
01071 $options['prefixPath'] = array_merge($prefixPaths, $options['prefixPath']);
01072 } else {
01073 $options['prefixPath'] = $prefixPaths;
01074 }
01075 }
01076
01077 $class = $this->getPluginLoader(self::ELEMENT)->load($type);
01078 $element = new $class($name, $options);
01079
01080 return $element;
01081 }
01082
01089 public function addElements(array $elements)
01090 {
01091 foreach ($elements as $key => $spec) {
01092 $name = null;
01093 if (!is_numeric($key)) {
01094 $name = $key;
01095 }
01096
01097 if (is_string($spec) || ($spec instanceof Zend_Form_Element)) {
01098 $this->addElement($spec, $name);
01099 continue;
01100 }
01101
01102 if (is_array($spec)) {
01103 $argc = count($spec);
01104 $options = array();
01105 if (isset($spec['type'])) {
01106 $type = $spec['type'];
01107 if (isset($spec['name'])) {
01108 $name = $spec['name'];
01109 }
01110 if (isset($spec['options'])) {
01111 $options = $spec['options'];
01112 }
01113 $this->addElement($type, $name, $options);
01114 } else {
01115 switch ($argc) {
01116 case 0:
01117 continue;
01118 case (1 <= $argc):
01119 $type = array_shift($spec);
01120 case (2 <= $argc):
01121 if (null === $name) {
01122 $name = array_shift($spec);
01123 } else {
01124 $options = array_shift($spec);
01125 }
01126 case (3 <= $argc):
01127 if (empty($options)) {
01128 $options = array_shift($spec);
01129 }
01130 default:
01131 $this->addElement($type, $name, $options);
01132 }
01133 }
01134 }
01135 }
01136 return $this;
01137 }
01138
01145 public function setElements(array $elements)
01146 {
01147 $this->clearElements();
01148 return $this->addElements($elements);
01149 }
01150
01157 public function getElement($name)
01158 {
01159 if (array_key_exists($name, $this->_elements)) {
01160 return $this->_elements[$name];
01161 }
01162 return null;
01163 }
01164
01170 public function getElements()
01171 {
01172 return $this->_elements;
01173 }
01174
01181 public function removeElement($name)
01182 {
01183 $name = (string) $name;
01184 if (isset($this->_elements[$name])) {
01185 unset($this->_elements[$name]);
01186 if (array_key_exists($name, $this->_order)) {
01187 unset($this->_order[$name]);
01188 $this->_orderUpdated = true;
01189 } else {
01190 foreach ($this->_displayGroups as $group) {
01191 if (null !== $group->getElement($name)) {
01192 $group->removeElement($name);
01193 }
01194 }
01195 }
01196 return true;
01197 }
01198
01199 return false;
01200 }
01201
01207 public function clearElements()
01208 {
01209 foreach (array_keys($this->_elements) as $key) {
01210 if (array_key_exists($key, $this->_order)) {
01211 unset($this->_order[$key]);
01212 }
01213 }
01214 $this->_elements = array();
01215 $this->_orderUpdated = true;
01216 return $this;
01217 }
01218
01227 public function setDefaults(array $defaults)
01228 {
01229 foreach ($this->getElements() as $name => $element) {
01230 if (array_key_exists($name, $defaults)) {
01231 $this->setDefault($name, $defaults[$name]);
01232 }
01233 }
01234 foreach ($this->getSubForms() as $name => $form) {
01235 if (array_key_exists($name, $defaults)) {
01236 $form->setDefaults($defaults[$name]);
01237 } else {
01238 $form->setDefaults($defaults);
01239 }
01240 }
01241 return $this;
01242 }
01243
01251 public function setDefault($name, $value)
01252 {
01253 $name = (string) $name;
01254 if ($element = $this->getElement($name)) {
01255 $element->setValue($value);
01256 } else {
01257 if (is_scalar($value)) {
01258 foreach ($this->getSubForms() as $subForm) {
01259 $subForm->setDefault($name, $value);
01260 }
01261 } elseif (is_array($value) && ($subForm = $this->getSubForm($name))) {
01262 $subForm->setDefaults($value);
01263 }
01264 }
01265 return $this;
01266 }
01267
01274 public function getValue($name)
01275 {
01276 if ($element = $this->getElement($name)) {
01277 return $element->getValue();
01278 }
01279
01280 if ($subForm = $this->getSubForm($name)) {
01281 return $subForm->getValues(true);
01282 }
01283
01284 foreach ($this->getSubForms() as $subForm) {
01285 if ($name == $subForm->getElementsBelongTo()) {
01286 return $subForm->getValues(true);
01287 }
01288 }
01289 return null;
01290 }
01291
01298 public function getValues($suppressArrayNotation = false)
01299 {
01300 $values = array();
01301 foreach ($this->getElements() as $key => $element) {
01302 if (!$element->getIgnore()) {
01303 $values[$key] = $element->getValue();
01304 }
01305 }
01306 foreach ($this->getSubForms() as $key => $subForm) {
01307 $fValues = $this->_attachToArray($subForm->getValues(true), $subForm->getElementsBelongTo());
01308 $values = array_merge($values, $fValues);
01309 }
01310
01311 if (!$suppressArrayNotation && $this->isArray()) {
01312 $values = $this->_attachToArray($values, $this->getElementsBelongTo());
01313 }
01314
01315 return $values;
01316 }
01317
01324 public function getUnfilteredValue($name)
01325 {
01326 if ($element = $this->getElement($name)) {
01327 return $element->getUnfilteredValue();
01328 }
01329 return null;
01330 }
01331
01337 public function getUnfilteredValues()
01338 {
01339 $values = array();
01340 foreach ($this->getElements() as $key => $element) {
01341 $values[$key] = $element->getUnfilteredValue();
01342 }
01343
01344 return $values;
01345 }
01346
01353 public function setElementFilters(array $filters)
01354 {
01355 foreach ($this->getElements() as $element) {
01356 $element->setFilters($filters);
01357 }
01358 return $this;
01359 }
01360
01367 public function setElementsBelongTo($array)
01368 {
01369 $origName = $this->getElementsBelongTo();
01370 $name = $this->filterName($array, true);
01371 if (empty($name)) {
01372 $name = null;
01373 }
01374 $this->_elementsBelongTo = $name;
01375
01376 if (null === $name) {
01377 $this->setIsArray(false);
01378 if (null !== $origName) {
01379 $this->_setElementsBelongTo();
01380 }
01381 } else {
01382 $this->setIsArray(true);
01383 $this->_setElementsBelongTo();
01384 }
01385
01386 return $this;
01387 }
01388
01395 protected function _setElementsBelongTo($name = null)
01396 {
01397 $array = $this->getElementsBelongTo();
01398
01399 if (null === $array) {
01400 return;
01401 }
01402
01403 if (null === $name) {
01404 foreach ($this->getElements() as $element) {
01405 $element->setBelongsTo($array);
01406 }
01407 } else {
01408 if (null !== ($element = $this->getElement($name))) {
01409 $element->setBelongsTo($array);
01410 }
01411 }
01412 }
01413
01419 public function getElementsBelongTo()
01420 {
01421 if ((null === $this->_elementsBelongTo) && $this->isArray()) {
01422 $name = $this->getName();
01423 if (!empty($name)) {
01424 return $name;
01425 }
01426 }
01427 return $this->_elementsBelongTo;
01428 }
01429
01436 public function setIsArray($flag)
01437 {
01438 $this->_isArray = (bool) $flag;
01439 return $this;
01440 }
01441
01447 public function isArray()
01448 {
01449 return $this->_isArray;
01450 }
01451
01452
01453
01462 public function addSubForm(Zend_Form $form, $name, $order = null)
01463 {
01464 $name = (string) $name;
01465 foreach ($this->_loaders as $type => $loader) {
01466 $loaderPaths = $loader->getPaths();
01467 foreach ($loaderPaths as $prefix => $paths) {
01468 foreach ($paths as $path) {
01469 $form->addPrefixPath($prefix, $path, $type);
01470 }
01471 }
01472 }
01473
01474 if (!empty($this->_elementPrefixPaths)) {
01475 foreach ($this->_elementPrefixPaths as $spec) {
01476 list($prefix, $path, $type) = array_values($spec);
01477 $form->addElementPrefixPath($prefix, $path, $type);
01478 }
01479 }
01480
01481 if (!empty($this->_displayGroupPrefixPaths)) {
01482 foreach ($this->_displayGroupPrefixPaths as $spec) {
01483 list($prefix, $path) = array_values($spec);
01484 $form->addDisplayGroupPrefixPath($prefix, $path);
01485 }
01486 }
01487
01488 if (null !== $order) {
01489 $form->setOrder($order);
01490 }
01491
01492 $form->setName($name);
01493 $this->_subForms[$name] = $form;
01494 $this->_order[$name] = $order;
01495 $this->_orderUpdated = true;
01496 return $this;
01497 }
01498
01505 public function addSubForms(array $subForms)
01506 {
01507 foreach ($subForms as $key => $spec) {
01508 $name = null;
01509 if (!is_numeric($key)) {
01510 $name = $key;
01511 }
01512
01513 if ($spec instanceof Zend_Form) {
01514 $this->addSubForm($spec, $name);
01515 continue;
01516 }
01517
01518 if (is_array($spec)) {
01519 $argc = count($spec);
01520 $order = null;
01521 switch ($argc) {
01522 case 0:
01523 continue;
01524 case (1 <= $argc):
01525 $subForm = array_shift($spec);
01526 case (2 <= $argc):
01527 $name = array_shift($spec);
01528 case (3 <= $argc):
01529 $order = array_shift($spec);
01530 default:
01531 $this->addSubForm($subForm, $name, $order);
01532 }
01533 }
01534 }
01535 return $this;
01536 }
01537
01544 public function setSubForms(array $subForms)
01545 {
01546 $this->clearSubForms();
01547 return $this->addSubForms($subForms);
01548 }
01549
01556 public function getSubForm($name)
01557 {
01558 $name = (string) $name;
01559 if (isset($this->_subForms[$name])) {
01560 return $this->_subForms[$name];
01561 }
01562 return null;
01563 }
01564
01570 public function getSubForms()
01571 {
01572 return $this->_subForms;
01573 }
01574
01581 public function removeSubForm($name)
01582 {
01583 $name = (string) $name;
01584 if (array_key_exists($name, $this->_subForms)) {
01585 unset($this->_subForms[$name]);
01586 if (array_key_exists($name, $this->_order)) {
01587 unset($this->_order[$name]);
01588 $this->_orderUpdated = true;
01589 }
01590 return true;
01591 }
01592
01593 return false;
01594 }
01595
01601 public function clearSubForms()
01602 {
01603 foreach (array_keys($this->_subForms) as $key) {
01604 if (array_key_exists($key, $this->_order)) {
01605 unset($this->_order[$key]);
01606 }
01607 }
01608 $this->_subForms = array();
01609 $this->_orderUpdated = true;
01610 return $this;
01611 }
01612
01613
01614
01615
01622 public function setDefaultDisplayGroupClass($class)
01623 {
01624 $this->_defaultDisplayGroupClass = (string) $class;
01625 return $this;
01626 }
01627
01633 public function getDefaultDisplayGroupClass()
01634 {
01635 return $this->_defaultDisplayGroupClass;
01636 }
01637
01651 public function addDisplayGroup(array $elements, $name, $options = null)
01652 {
01653 $group = array();
01654 foreach ($elements as $element) {
01655 if (isset($this->_elements[$element])) {
01656 $add = $this->getElement($element);
01657 if (null !== $add) {
01658 unset($this->_order[$element]);
01659 $group[] = $add;
01660 }
01661 }
01662 }
01663 if (empty($group)) {
01664 require_once 'Zend/Form/Exception.php';
01665 throw new Zend_Form_Exception('No valid elements specified for display group');
01666 }
01667
01668 $name = (string) $name;
01669
01670 if (is_array($options)) {
01671 $options['elements'] = $group;
01672 } elseif ($options instanceof Zend_Config) {
01673 $options = $options->toArray();
01674 $options['elements'] = $group;
01675 } else {
01676 $options = array('elements' => $group);
01677 }
01678
01679 if (isset($options['displayGroupClass'])) {
01680 $class = $options['displayGroupClass'];
01681 unset($options['displayGroupClass']);
01682 } else {
01683 $class = $this->getDefaultDisplayGroupClass();
01684 }
01685
01686 if (!class_exists($class)) {
01687 require_once 'Zend/Loader.php';
01688 Zend_Loader::loadClass($class);
01689 }
01690 $this->_displayGroups[$name] = new $class(
01691 $name,
01692 $this->getPluginLoader(self::DECORATOR),
01693 $options
01694 );
01695
01696 if (!empty($this->_displayGroupPrefixPaths)) {
01697 $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths);
01698 }
01699
01700 $this->_order[$name] = $this->_displayGroups[$name]->getOrder();
01701 $this->_orderUpdated = true;
01702 return $this;
01703 }
01704
01712 protected function _addDisplayGroupObject(Zend_Form_DisplayGroup $group, $name = null)
01713 {
01714 if (null === $name) {
01715 $name = $group->getName();
01716 if (empty($name)) {
01717 require_once 'Zend/Form/Exception.php';
01718 throw new Zend_Form_Exception('Invalid display group added; requires name');
01719 }
01720 }
01721
01722 $this->_displayGroups[$name] = $group;
01723
01724 if (!empty($this->_displayGroupPrefixPaths)) {
01725 $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths);
01726 }
01727
01728 $this->_order[$name] = $this->_displayGroups[$name]->getOrder();
01729 $this->_orderUpdated = true;
01730 return $this;
01731 }
01732
01739 public function addDisplayGroups(array $groups)
01740 {
01741 foreach ($groups as $key => $spec) {
01742 $name = null;
01743 if (!is_numeric($key)) {
01744 $name = $key;
01745 }
01746
01747 if ($spec instanceof Zend_Form_DisplayGroup) {
01748 $this->_addDisplayGroupObject($spec);
01749 }
01750
01751 if (!is_array($spec) || empty($spec)) {
01752 continue;
01753 }
01754
01755 $argc = count($spec);
01756 $options = array();
01757
01758 if (isset($spec['elements'])) {
01759 $elements = $spec['elements'];
01760 if (isset($spec['name'])) {
01761 $name = $spec['name'];
01762 }
01763 if (isset($spec['options'])) {
01764 $options = $spec['options'];
01765 }
01766 $this->addDisplayGroup($elements, $name, $options);
01767 } else {
01768 switch ($argc) {
01769 case (1 <= $argc):
01770 $elements = array_shift($spec);
01771 if (!is_array($elements) && (null !== $name)) {
01772 $elements = array_merge((array) $elements, $spec);
01773 $this->addDisplayGroup($elements, $name);
01774 break;
01775 }
01776 case (2 <= $argc):
01777 if (null !== $name) {
01778 $options = array_shift($spec);
01779 $this->addDisplayGroup($elements, $name, $options);
01780 break;
01781 }
01782 $name = array_shift($spec);
01783 case (3 <= $argc):
01784 $options = array_shift($spec);
01785 default:
01786 $this->addDisplayGroup($elements, $name, $options);
01787 }
01788 }
01789 }
01790 return $this;
01791 }
01792
01799 public function setDisplayGroups(array $groups)
01800 {
01801 return $this->clearDisplayGroups()
01802 ->addDisplayGroups($groups);
01803 }
01804
01811 public function getDisplayGroup($name)
01812 {
01813 $name = (string) $name;
01814 if (isset($this->_displayGroups[$name])) {
01815 return $this->_displayGroups[$name];
01816 }
01817
01818 return null;
01819 }
01820
01826 public function getDisplayGroups()
01827 {
01828 return $this->_displayGroups;
01829 }
01830
01837 public function removeDisplayGroup($name)
01838 {
01839 $name = (string) $name;
01840 if (array_key_exists($name, $this->_displayGroups)) {
01841 foreach ($this->_displayGroups[$name] as $key => $element) {
01842 if (array_key_exists($key, $this->_elements)) {
01843 $this->_order[$key] = $element->getOrder();
01844 $this->_orderUpdated = true;
01845 }
01846 }
01847 unset($this->_displayGroups[$name]);
01848
01849 if (array_key_exists($name, $this->_order)) {
01850 unset($this->_order[$name]);
01851 $this->_orderUpdated = true;
01852 }
01853 return true;
01854 }
01855
01856 return false;
01857 }
01858
01864 public function clearDisplayGroups()
01865 {
01866 foreach ($this->_displayGroups as $key => $group) {
01867 if (array_key_exists($key, $this->_order)) {
01868 unset($this->_order[$key]);
01869 }
01870 foreach ($group as $name => $element) {
01871 if (isset($this->_elements[$name])) {
01872 $this->_order[$name] = $element->getOrder();
01873 }
01874 $this->_order[$name] = $element->getOrder();
01875 }
01876 }
01877 $this->_displayGroups = array();
01878 $this->_orderUpdated = true;
01879 return $this;
01880 }
01881
01882
01883
01884
01893 public function populate(array $values)
01894 {
01895 return $this->setDefaults($values);
01896 }
01897
01906 protected function _getArrayName($value)
01907 {
01908 if (empty($value) || !is_string($value)) {
01909 return $value;
01910 }
01911
01912 if (!strstr($value, '[')) {
01913 return $value;
01914 }
01915
01916 $endPos = strlen($value) - 1;
01917 if (']' != $value[$endPos]) {
01918 return $value;
01919 }
01920
01921 $start = strrpos($value, '[') + 1;
01922 $name = substr($value, $start, $endPos - $start);
01923 return $name;
01924 }
01925
01936 protected function _dissolveArrayValue($value, $arrayPath)
01937 {
01938
01939 while ($arrayPos = strpos($arrayPath, '[')) {
01940
01941 $arrayKey = trim(substr($arrayPath, 0, $arrayPos), ']');
01942
01943
01944 if (isset($value[$arrayKey])) {
01945 $value = $value[$arrayKey];
01946 }
01947
01948
01949 $arrayPath = trim(substr($arrayPath, $arrayPos + 1), ']');
01950 }
01951
01952 if (isset($value[$arrayPath])) {
01953 $value = $value[$arrayPath];
01954 }
01955
01956 return $value;
01957 }
01958
01966 protected function _attachToArray($value, $arrayPath)
01967 {
01968
01969 while ($arrayPos = strrpos($arrayPath, '[')) {
01970
01971 $arrayKey = trim(substr($arrayPath, $arrayPos + 1), ']');
01972
01973
01974 $value = array($arrayKey => $value);
01975
01976
01977 $arrayPath = trim(substr($arrayPath, 0, $arrayPos), ']');
01978 }
01979
01980 $value = array($arrayPath => $value);
01981
01982 return $value;
01983 }
01984
01991 public function isValid($data)
01992 {
01993 if (!is_array($data)) {
01994 require_once 'Zend/Form/Exception.php';
01995 throw new Zend_Form_Exception(__CLASS__ . '::' . __METHOD__ . ' expects an array');
01996 }
01997 $translator = $this->getTranslator();
01998 $valid = true;
01999
02000 if ($this->isArray()) {
02001 $data = $this->_dissolveArrayValue($data, $this->getElementsBelongTo());
02002 }
02003
02004 foreach ($this->getElements() as $key => $element) {
02005 $element->setTranslator($translator);
02006 if (!isset($data[$key])) {
02007 $valid = $element->isValid(null, $data) && $valid;
02008 } else {
02009 $valid = $element->isValid($data[$key], $data) && $valid;
02010 }
02011 }
02012 foreach ($this->getSubForms() as $key => $form) {
02013 $form->setTranslator($translator);
02014 if (isset($data[$key])) {
02015 $valid = $form->isValid($data[$key]) && $valid;
02016 } else {
02017 $valid = $form->isValid($data) && $valid;
02018 }
02019 }
02020
02021 $this->_errorsExist = !$valid;
02022
02023
02024 if ($this->_errorsForced) {
02025 return false;
02026 }
02027
02028 return $valid;
02029 }
02030
02039 public function isValidPartial(array $data)
02040 {
02041 if ($this->isArray()) {
02042 $data = $this->_dissolveArrayValue($data, $this->getElementsBelongTo());
02043 }
02044
02045 $translator = $this->getTranslator();
02046 $valid = true;
02047 $validatedSubForms = array();
02048
02049 foreach ($data as $key => $value) {
02050 if (null !== ($element = $this->getElement($key))) {
02051 if (null !== $translator) {
02052 $element->setTranslator($translator);
02053 }
02054 $valid = $element->isValid($value, $data) && $valid;
02055 } elseif (null !== ($subForm = $this->getSubForm($key))) {
02056 if (null !== $translator) {
02057 $subForm->setTranslator($translator);
02058 }
02059 $valid = $subForm->isValidPartial($data[$key]) && $valid;
02060 $validatedSubForms[] = $key;
02061 }
02062 }
02063 foreach ($this->getSubForms() as $key => $subForm) {
02064 if (!in_array($key, $validatedSubForms)) {
02065 if (null !== $translator) {
02066 $subForm->setTranslator($translator);
02067 }
02068
02069 $valid = $subForm->isValidPartial($data) && $valid;
02070 }
02071 }
02072
02073 $this->_errorsExist = !$valid;
02074 return $valid;
02075 }
02076
02087 public function processAjax(array $data)
02088 {
02089 require_once 'Zend/Json.php';
02090 if ($this->isValidPartial($data)) {
02091 return Zend_Json::encode(true);
02092 }
02093 $messages = $this->getMessages();
02094 return Zend_Json::encode($messages);
02095 }
02096
02103 public function addErrorMessage($message)
02104 {
02105 $this->_errorMessages[] = (string) $message;
02106 return $this;
02107 }
02108
02115 public function addErrorMessages(array $messages)
02116 {
02117 foreach ($messages as $message) {
02118 $this->addErrorMessage($message);
02119 }
02120 return $this;
02121 }
02122
02129 public function setErrorMessages(array $messages)
02130 {
02131 $this->clearErrorMessages();
02132 return $this->addErrorMessages($messages);
02133 }
02134
02140 public function getErrorMessages()
02141 {
02142 return $this->_errorMessages;
02143 }
02144
02150 public function clearErrorMessages()
02151 {
02152 $this->_errorMessages = array();
02153 return $this;
02154 }
02155
02161 public function markAsError()
02162 {
02163 $this->_errorsExist = true;
02164 $this->_errorsForced = true;
02165 return $this;
02166 }
02167
02174 public function addError($message)
02175 {
02176 $this->addErrorMessage($message);
02177 $this->markAsError();
02178 return $this;
02179 }
02180
02187 public function addErrors(array $messages)
02188 {
02189 foreach ($messages as $message) {
02190 $this->addError($message);
02191 }
02192 return $this;
02193 }
02194
02201 public function setErrors(array $messages)
02202 {
02203 $this->clearErrorMessages();
02204 return $this->addErrors($messages);
02205 }
02206
02207
02208 public function persistData()
02209 {
02210 }
02211
02217 public function isErrors()
02218 {
02219 return $this->_errorsExist;
02220 }
02221
02228 public function getErrors($name = null)
02229 {
02230 $errors = array();
02231 if ((null !== $name) && isset($this->_elements[$name])) {
02232 $errors = $this->getElement($name)->getErrors();
02233 } elseif ((null !== $name) && isset($this->_subForms[$name])) {
02234 $errors = $this->getSubForm($name)->getErrors();
02235 } else {
02236 foreach ($this->_elements as $key => $element) {
02237 $errors[$key] = $element->getErrors();
02238 }
02239 foreach ($this->getSubForms() as $key => $subForm) {
02240 $fErrors = $this->_attachToArray($subForm->getErrors(), $subForm->getElementsBelongTo());
02241 $errors = array_merge($errors, $fErrors);
02242 }
02243 }
02244 return $errors;
02245 }
02246
02254 public function getMessages($name = null, $suppressArrayNotation = false)
02255 {
02256 if ((null !== $name) && isset($this->_elements[$name])) {
02257 return $this->getElement($name)->getMessages();
02258 }
02259
02260 if ((null !== $name) && isset($this->_subForms[$name])) {
02261 return $this->getSubForm($name)->getMessages(null, true);
02262 }
02263
02264 $arrayKeys = array();
02265 foreach ($this->getSubForms() as $key => $subForm) {
02266 $array = $this->_getArrayName($subForm->getElementsBelongTo());
02267 if (!empty($array)) {
02268 if ($name == $array) {
02269 return $subForm->getMessages(null, true);
02270 }
02271 $arrayKeys[$key] = $subForm->getElementsBelongTo();
02272 }
02273 }
02274
02275 $customMessages = $this->_getErrorMessages();
02276 if ($this->isErrors() && !empty($customMessages)) {
02277 return $customMessages;
02278 }
02279
02280 $messages = array();
02281
02282 foreach ($this->getElements() as $name => $element) {
02283 $eMessages = $element->getMessages();
02284 if (!empty($eMessages)) {
02285 $messages[$name] = $eMessages;
02286 }
02287 }
02288
02289 foreach ($this->getSubForms() as $key => $subForm) {
02290 $fMessages = $subForm->getMessages(null, true);
02291 if (!empty($fMessages)) {
02292 if (array_key_exists($key, $arrayKeys)) {
02293 $fMessages = $this->_attachToArray($fMessages, $arrayKeys[$key]);
02294 $messages = array_merge($messages, $fMessages);
02295 } else {
02296 $messages[$key] = $fMessages;
02297 }
02298 }
02299 }
02300
02301 if (!$suppressArrayNotation && $this->isArray()) {
02302 $messages = $this->_attachToArray($messages, $this->getElementsBelongTo());
02303 }
02304
02305 return $messages;
02306 }
02307
02308
02309
02310
02317 public function setView(Zend_View_Interface $view = null)
02318 {
02319 $this->_view = $view;
02320 return $this;
02321 }
02322
02330 public function getView()
02331 {
02332 if (null === $this->_view) {
02333 require_once 'Zend/Controller/Action/HelperBroker.php';
02334 $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
02335 $this->setView($viewRenderer->view);
02336 }
02337
02338 return $this->_view;
02339 }
02340
02348 protected function _getDecorator($name, $options)
02349 {
02350 $class = $this->getPluginLoader(self::DECORATOR)->load($name);
02351 if (null === $options) {
02352 $decorator = new $class;
02353 } else {
02354 $decorator = new $class($options);
02355 }
02356
02357 return $decorator;
02358 }
02359
02367 public function addDecorator($decorator, $options = null)
02368 {
02369 if ($decorator instanceof Zend_Form_Decorator_Interface) {
02370 $name = get_class($decorator);
02371 } elseif (is_string($decorator)) {
02372 $name = $decorator;
02373 $decorator = array(
02374 'decorator' => $name,
02375 'options' => $options,
02376 );
02377 } elseif (is_array($decorator)) {
02378 foreach ($decorator as $name => $spec) {
02379 break;
02380 }
02381 if (is_numeric($name)) {
02382 require_once 'Zend/Form/Exception.php';
02383 throw new Zend_Form_Exception('Invalid alias provided to addDecorator; must be alphanumeric string');
02384 }
02385 if (is_string($spec)) {
02386 $decorator = array(
02387 'decorator' => $spec,
02388 'options' => $options,
02389 );
02390 } elseif ($spec instanceof Zend_Form_Decorator_Interface) {
02391 $decorator = $spec;
02392 }
02393 } else {
02394 require_once 'Zend/Form/Exception.php';
02395 throw new Zend_Form_Exception('Invalid decorator provided to addDecorator; must be string or Zend_Form_Decorator_Interface');
02396 }
02397
02398 $this->_decorators[$name] = $decorator;
02399
02400 return $this;
02401 }
02402
02409 public function addDecorators(array $decorators)
02410 {
02411 foreach ($decorators as $decoratorInfo) {
02412 if (is_string($decoratorInfo)) {
02413 $this->addDecorator($decoratorInfo);
02414 } elseif ($decoratorInfo instanceof Zend_Form_Decorator_Interface) {
02415 $this->addDecorator($decoratorInfo);
02416 } elseif (is_array($decoratorInfo)) {
02417 $argc = count($decoratorInfo);
02418 $options = array();
02419 if (isset($decoratorInfo['decorator'])) {
02420 $decorator = $decoratorInfo['decorator'];
02421 if (isset($decoratorInfo['options'])) {
02422 $options = $decoratorInfo['options'];
02423 }
02424 $this->addDecorator($decorator, $options);
02425 } else {
02426 switch (true) {
02427 case (0 == $argc):
02428 break;
02429 case (1 <= $argc):
02430 $decorator = array_shift($decoratorInfo);
02431 case (2 <= $argc):
02432 $options = array_shift($decoratorInfo);
02433 default:
02434 $this->addDecorator($decorator, $options);
02435 break;
02436 }
02437 }
02438 } else {
02439 require_once 'Zend/Form/Exception.php';
02440 throw new Zend_Form_Exception('Invalid decorator passed to addDecorators()');
02441 }
02442 }
02443
02444 return $this;
02445 }
02446
02453 public function setDecorators(array $decorators)
02454 {
02455 $this->clearDecorators();
02456 return $this->addDecorators($decorators);
02457 }
02458
02465 public function getDecorator($name)
02466 {
02467 if (!isset($this->_decorators[$name])) {
02468 $len = strlen($name);
02469 foreach ($this->_decorators as $localName => $decorator) {
02470 if ($len > strlen($localName)) {
02471 continue;
02472 }
02473
02474 if (0 === substr_compare($localName, $name, -$len, $len, true)) {
02475 if (is_array($decorator)) {
02476 return $this->_loadDecorator($decorator, $localName);
02477 }
02478 return $decorator;
02479 }
02480 }
02481 return false;
02482 }
02483
02484 if (is_array($this->_decorators[$name])) {
02485 return $this->_loadDecorator($this->_decorators[$name], $name);
02486 }
02487
02488 return $this->_decorators[$name];
02489 }
02490
02496 public function getDecorators()
02497 {
02498 foreach ($this->_decorators as $key => $value) {
02499 if (is_array($value)) {
02500 $this->_loadDecorator($value, $key);
02501 }
02502 }
02503 return $this->_decorators;
02504 }
02505
02512 public function removeDecorator($name)
02513 {
02514 $decorator = $this->getDecorator($name);
02515 if ($decorator) {
02516 if (array_key_exists($name, $this->_decorators)) {
02517 unset($this->_decorators[$name]);
02518 } else {
02519 $class = get_class($decorator);
02520 if (!array_key_exists($class, $this->_decorators)) {
02521 return false;
02522 }
02523 unset($this->_decorators[$class]);
02524 }
02525 return true;
02526 }
02527
02528 return false;
02529 }
02530
02536 public function clearDecorators()
02537 {
02538 $this->_decorators = array();
02539 return $this;
02540 }
02541
02550 public function setElementDecorators(array $decorators, array $elements = null, $include = true)
02551 {
02552 if (is_array($elements)) {
02553 if ($include) {
02554 $elementObjs = array();
02555 foreach ($elements as $name) {
02556 if (null !== ($element = $this->getElement($name))) {
02557 $elementObjs[] = $element;
02558 }
02559 }
02560 } else {
02561 $elementObjs = $this->getElements();
02562 foreach ($elements as $name) {
02563 if (array_key_exists($name, $elementObjs)) {
02564 unset($elementObjs[$name]);
02565 }
02566 }
02567 }
02568 } else {
02569 $elementObjs = $this->getElements();
02570 }
02571
02572 foreach ($elementObjs as $element) {
02573 $element->setDecorators($decorators);
02574 }
02575
02576 $this->_elementDecorators = $decorators;
02577
02578 return $this;
02579 }
02580
02587 public function setDisplayGroupDecorators(array $decorators)
02588 {
02589 foreach ($this->getDisplayGroups() as $group) {
02590 $group->setDecorators($decorators);
02591 }
02592
02593 return $this;
02594 }
02595
02602 public function setSubFormDecorators(array $decorators)
02603 {
02604 foreach ($this->getSubForms() as $form) {
02605 $form->setDecorators($decorators);
02606 }
02607
02608 return $this;
02609 }
02610
02617 public function render(Zend_View_Interface $view = null)
02618 {
02619 if (null !== $view) {
02620 $this->setView($view);
02621 }
02622
02623 $content = '';
02624 foreach ($this->getDecorators() as $decorator) {
02625 $decorator->setElement($this);
02626 $content = $decorator->render($content);
02627 }
02628 return $content;
02629 }
02630
02638 public function __toString()
02639 {
02640 try {
02641 $return = $this->render();
02642 return $return;
02643 } catch (Exception $e) {
02644 $message = "Exception caught by form: " . $e->getMessage()
02645 . "\nStack Trace:\n" . $e->getTraceAsString();
02646 trigger_error($message, E_USER_WARNING);
02647 return '';
02648 }
02649 }
02650
02651
02652
02653
02660 public function setTranslator($translator = null)
02661 {
02662 if (null === $translator) {
02663 $this->_translator = null;
02664 } elseif ($translator instanceof Zend_Translate_Adapter) {
02665 $this->_translator = $translator;
02666 } elseif ($translator instanceof Zend_Translate) {
02667 $this->_translator = $translator->getAdapter();
02668 } else {
02669 require_once 'Zend/Form/Exception.php';
02670 throw new Zend_Form_Exception('Invalid translator specified');
02671 }
02672
02673 return $this;
02674 }
02675
02682 public static function setDefaultTranslator($translator = null)
02683 {
02684 if (null === $translator) {
02685 self::$_translatorDefault = null;
02686 } elseif ($translator instanceof Zend_Translate_Adapter) {
02687 self::$_translatorDefault = $translator;
02688 } elseif ($translator instanceof Zend_Translate) {
02689 self::$_translatorDefault = $translator->getAdapter();
02690 } else {
02691 require_once 'Zend/Form/Exception.php';
02692 throw new Zend_Form_Exception('Invalid translator specified');
02693 }
02694 }
02695
02701 public function getTranslator()
02702 {
02703 if ($this->translatorIsDisabled()) {
02704 return null;
02705 }
02706
02707 if (null === $this->_translator) {
02708 return self::getDefaultTranslator();
02709 }
02710
02711 return $this->_translator;
02712 }
02713
02719 public static function getDefaultTranslator()
02720 {
02721 if (null === self::$_translatorDefault) {
02722 require_once 'Zend/Registry.php';
02723 if (Zend_Registry::isRegistered('Zend_Translate')) {
02724 $translator = Zend_Registry::get('Zend_Translate');
02725 if ($translator instanceof Zend_Translate_Adapter) {
02726 return $translator;
02727 } elseif ($translator instanceof Zend_Translate) {
02728 return $translator->getAdapter();
02729 }
02730 }
02731 }
02732 return self::$_translatorDefault;
02733 }
02734
02741 public function setDisableTranslator($flag)
02742 {
02743 $this->_translatorDisabled = (bool) $flag;
02744 return $this;
02745 }
02746
02752 public function translatorIsDisabled()
02753 {
02754 return $this->_translatorDisabled;
02755 }
02756
02763 public function __get($name)
02764 {
02765 if (isset($this->_elements[$name])) {
02766 return $this->_elements[$name];
02767 } elseif (isset($this->_subForms[$name])) {
02768 return $this->_subForms[$name];
02769 } elseif (isset($this->_displayGroups[$name])) {
02770 return $this->_displayGroups[$name];
02771 }
02772
02773 return null;
02774 }
02775
02784 public function __set($name, $value)
02785 {
02786 if ($value instanceof Zend_Form_Element) {
02787 $this->addElement($value, $name);
02788 return;
02789 } elseif ($value instanceof Zend_Form) {
02790 $this->addSubForm($value, $name);
02791 return;
02792 } elseif (is_array($value)) {
02793 $this->addDisplayGroup($value, $name);
02794 return;
02795 }
02796
02797 require_once 'Zend/Form/Exception.php';
02798 if (is_object($value)) {
02799 $type = get_class($value);
02800 } else {
02801 $type = gettype($value);
02802 }
02803 throw new Zend_Form_Exception('Only form elements and groups may be overloaded; variable of type "' . $type . '" provided');
02804 }
02805
02812 public function __isset($name)
02813 {
02814 if (isset($this->_elements[$name])
02815 || isset($this->_subForms[$name])
02816 || isset($this->_displayGroups[$name]))
02817 {
02818 return true;
02819 }
02820
02821 return false;
02822 }
02823
02830 public function __unset($name)
02831 {
02832 if (isset($this->_elements[$name])) {
02833 unset($this->_elements[$name]);
02834 } elseif (isset($this->_subForms[$name])) {
02835 unset($this->_subForms[$name]);
02836 } elseif (isset($this->_displayGroups[$name])) {
02837 unset($this->_displayGroups[$name]);
02838 }
02839 }
02840
02851 public function __call($method, $args)
02852 {
02853 if ('render' == substr($method, 0, 6)) {
02854 $decoratorName = substr($method, 6);
02855 if (false !== ($decorator = $this->getDecorator($decoratorName))) {
02856 $decorator->setElement($this);
02857 $seed = '';
02858 if (0 < count($args)) {
02859 $seed = array_shift($args);
02860 }
02861 return $decorator->render($seed);
02862 }
02863
02864 require_once 'Zend/Form/Exception.php';
02865 throw new Zend_Form_Exception(sprintf('Decorator by name %s does not exist', $decoratorName));
02866 }
02867
02868 require_once 'Zend/Form/Exception.php';
02869 throw new Zend_Form_Exception(sprintf('Method %s does not exist', $method));
02870 }
02871
02872
02873
02879 public function current()
02880 {
02881 $this->_sort();
02882 current($this->_order);
02883 $key = key($this->_order);
02884
02885 if (isset($this->_elements[$key])) {
02886 return $this->getElement($key);
02887 } elseif (isset($this->_subForms[$key])) {
02888 return $this->getSubForm($key);
02889 } elseif (isset($this->_displayGroups[$key])) {
02890 return $this->getDisplayGroup($key);
02891 } else {
02892 require_once 'Zend/Form/Exception.php';
02893 throw new Zend_Form_Exception(sprintf('Corruption detected in form; invalid key ("%s") found in internal iterator', (string) $key));
02894 }
02895 }
02896
02902 public function key()
02903 {
02904 $this->_sort();
02905 return key($this->_order);
02906 }
02907
02913 public function next()
02914 {
02915 $this->_sort();
02916 next($this->_order);
02917 }
02918
02924 public function rewind()
02925 {
02926 $this->_sort();
02927 reset($this->_order);
02928 }
02929
02935 public function valid()
02936 {
02937 $this->_sort();
02938 return (current($this->_order) !== false);
02939 }
02940
02946 public function count()
02947 {
02948 return count($this->_order);
02949 }
02950
02957 public function setDisableLoadDefaultDecorators($flag)
02958 {
02959 $this->_disableLoadDefaultDecorators = (bool) $flag;
02960 return $this;
02961 }
02962
02968 public function loadDefaultDecoratorsIsDisabled()
02969 {
02970 return $this->_disableLoadDefaultDecorators;
02971 }
02972
02978 public function loadDefaultDecorators()
02979 {
02980 if ($this->loadDefaultDecoratorsIsDisabled()) {
02981 return;
02982 }
02983
02984 $decorators = $this->getDecorators();
02985 if (empty($decorators)) {
02986 $this->addDecorator('FormElements')
02987 ->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
02988 ->addDecorator('Form');
02989 }
02990 }
02991
02997 protected function _sort()
02998 {
02999 if ($this->_orderUpdated) {
03000 $items = array();
03001 $index = 0;
03002 foreach ($this->_order as $key => $order) {
03003 if (null === $order) {
03004 if (null === ($order = $this->{$key}->getOrder())) {
03005 while (array_search($index, $this->_order, true)) {
03006 ++$index;
03007 }
03008 $items[$index] = $key;
03009 ++$index;
03010 } else {
03011 $items[$order] = $key;
03012 }
03013 } else {
03014 $items[$order] = $key;
03015 }
03016 }
03017
03018 $items = array_flip($items);
03019 asort($items);
03020 $this->_order = $items;
03021 $this->_orderUpdated = false;
03022 }
03023 }
03024
03032 protected function _loadDecorator(array $decorator, $name)
03033 {
03034 $sameName = false;
03035 if ($name == $decorator['decorator']) {
03036 $sameName = true;
03037 }
03038
03039 $instance = $this->_getDecorator($decorator['decorator'], $decorator['options']);
03040 if ($sameName) {
03041 $newName = get_class($instance);
03042 $decoratorNames = array_keys($this->_decorators);
03043 $order = array_flip($decoratorNames);
03044 $order[$newName] = $order[$name];
03045 $decoratorsExchange = array();
03046 unset($order[$name]);
03047 asort($order);
03048 foreach ($order as $key => $index) {
03049 if ($key == $newName) {
03050 $decoratorsExchange[$key] = $instance;
03051 continue;
03052 }
03053 $decoratorsExchange[$key] = $this->_decorators[$key];
03054 }
03055 $this->_decorators = $decoratorsExchange;
03056 } else {
03057 $this->_decorators[$name] = $instance;
03058 }
03059
03060 return $instance;
03061 }
03062
03068 protected function _getErrorMessages()
03069 {
03070 $messages = $this->getErrorMessages();
03071 $translator = $this->getTranslator();
03072 if (null !== $translator) {
03073 foreach ($messages as $key => $message) {
03074 $messages[$key] = $translator->translate($message);
03075 }
03076 }
03077 return $messages;
03078 }
03079 }