1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
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@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_XmlConnect
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Xmlconnect form validators container element
29: *
30: * @category Mage
31: * @package Mage_XmlConnect
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_XmlConnect_Model_Simplexml_Form_Element_Validator
35: extends Mage_XmlConnect_Model_Simplexml_Form_Element_Fieldset
36: {
37: /**
38: * Main element node
39: *
40: * @var string
41: */
42: protected $_mainNode = 'validators';
43:
44: /**
45: * Validator id prefix
46: *
47: * @var string
48: */
49: protected $_validatorIdPrefix = 'validator_';
50:
51: /**
52: * Rule type block renderer
53: *
54: * @var string
55: */
56: protected $_ruleTypeBlock = 'validator_rule';
57:
58: /**
59: * Init validator container
60: *
61: * @param array $attributes
62: */
63: public function __construct($attributes = array())
64: {
65: parent::__construct($attributes);
66: $this->_renderer = Mage_XmlConnect_Model_Simplexml_Form::getValidatorRenderer();
67: $this->setType('validator');
68: }
69:
70: /**
71: * Skip name attribute for validator
72: *
73: * @todo re-factor required attributes logic to make it easy to replace them
74: * @param Mage_XmlConnect_Model_Simplexml_Element $xmlObj
75: * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
76: */
77: protected function _addName(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
78: {
79: return $this;
80: }
81:
82: /**
83: * Default element attribute array
84: *
85: * @return array
86: */
87: public function getXmlAttributes()
88: {
89: return array();
90: }
91:
92: /**
93: * Required element attribute array
94: *
95: * @return array
96: */
97: public function getRequiredXmlAttributes()
98: {
99: return array();
100: }
101:
102: /**
103: * Set element id
104: *
105: * @param $id
106: * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
107: */
108: public function setId($id)
109: {
110: parent::setId($this->getValidatorIdPrefix() . $id);
111: return $this;
112: }
113:
114: /**
115: * Get validator prefix
116: *
117: * @return string
118: */
119: public function getValidatorIdPrefix()
120: {
121: return $this->_validatorIdPrefix;
122: }
123:
124: /**
125: * Set validator prefix
126: *
127: * @param string $validatorIdPrefix
128: * @return Mage_XmlConnect_Model_Simplexml_Form_Element_Validator
129: */
130: public function setValidatorIdPrefix($validatorIdPrefix)
131: {
132: $this->_validatorIdPrefix = $validatorIdPrefix;
133: return $this;
134: }
135:
136: /**
137: * Get object attributes array
138: *
139: * @return array
140: */
141: public function getAttributes()
142: {
143: $attributes = array_merge($this->getXmlAttributes(), $this->getCustomAttributes());
144: if (!empty($attributes)) {
145: return $this->getXmlObjAttributes($attributes);
146: } else {
147: return $attributes;
148: }
149: }
150:
151: /**
152: * Add rule element to validator container
153: *
154: * @param array $config
155: * @param boolean $after
156: * @return Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract
157: */
158: public function addRule(array $config, $after = false)
159: {
160: if (isset($config['type'])) {
161: $ruleType = $config['type'];
162: }
163:
164: $elementId = $this->getXmlId() . '_' . $ruleType;
165: $element = parent::addField($elementId, $this->getRuleTypeBlock(), $config, $after);
166: if ($renderer = Mage_XmlConnect_Model_Simplexml_Form::getValidatorRuleRenderer()) {
167: $element->setRenderer($renderer);
168: }
169: return $element;
170: }
171:
172: /**
173: * Get rule type block renderer
174: *
175: * @return string
176: */
177: public function getRuleTypeBlock()
178: {
179: return $this->_ruleTypeBlock;
180: }
181:
182: /**
183: * Set rule type block renderer
184: *
185: * @param string $ruleTypeBlock
186: * @return Mage_XmlConnect_Model_Simplexml_Form_Element_Validator
187: */
188: public function setRuleTypeBlock($ruleTypeBlock)
189: {
190: $this->_ruleTypeBlock = $ruleTypeBlock;
191: return $this;
192: }
193: }
194: