1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: abstract class Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract
35: extends Mage_XmlConnect_Model_Simplexml_Form_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_type;
43:
44: 45: 46: 47: 48:
49: protected $_form;
50:
51: 52: 53: 54: 55:
56: protected $_mainNode = 'field';
57:
58: 59: 60: 61: 62:
63: protected $_renderer;
64:
65: 66: 67: 68: 69:
70: public function __construct($attributes = array())
71: {
72: parent::__construct($attributes);
73: $this->_renderer = Mage_XmlConnect_Model_Simplexml_Form::getElementRenderer();
74: }
75:
76: 77: 78: 79: 80: 81: 82:
83: public function addElement(Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract $element, $after = false)
84: {
85: if ($this->getForm()) {
86: $this->getForm()->checkElementId($element->getId());
87: $this->getForm()->addElementToCollection($element);
88: }
89:
90: parent::addElement($element, $after);
91: return $this;
92: }
93:
94: 95: 96: 97: 98:
99: public function getType()
100: {
101: return $this->_type;
102: }
103:
104: 105: 106: 107: 108:
109: public function getForm()
110: {
111: return $this->_form;
112: }
113:
114: 115: 116: 117: 118:
119: public function getXmlId()
120: {
121: return $this->getForm()->getXmlIdPrefix() . $this->getData('xml_id') . $this->getForm()->getXmlIdSuffix();
122: }
123:
124: 125: 126: 127: 128:
129: public function getName()
130: {
131: $name = $this->getData('name');
132: if ($suffix = $this->getForm()->getFieldNameSuffix()) {
133: $name = $this->getForm()->addSuffixToName($name, $suffix);
134: }
135: return $name;
136: }
137:
138: 139: 140: 141: 142: 143:
144: public function setType($type)
145: {
146: $this->_type = $type;
147: $this->setData('type', $type);
148: return $this;
149: }
150:
151: 152: 153: 154: 155: 156:
157: public function setForm($form)
158: {
159: $this->_form = $form;
160: return $this;
161: }
162:
163: 164: 165: 166: 167: 168:
169: public function removeField($elementId)
170: {
171: $this->getForm()->removeField($elementId);
172: return parent::removeField($elementId);
173: }
174:
175: 176: 177: 178: 179:
180: public function getXmlAttributes()
181: {
182: return array('title', 'required', 'disabled', 'visible', 'relation');
183: }
184:
185: 186: 187: 188: 189:
190: public function getRequiredXmlAttributes()
191: {
192: return array('label' => null, 'type' => null);
193: }
194:
195: 196: 197: 198: 199:
200: public function getElementXml()
201: {
202: $xmlObj = $this->getXmlObject();
203: $this->_addRequiredAttributes($xmlObj);
204: foreach ($this->getAttributes() as $key => $val) {
205: $xmlObj->addAttribute($key, $xmlObj->xmlAttribute($val));
206: }
207: $this->_addValue($xmlObj);
208:
209: foreach ($this->getElements() as $element) {
210: if ($element->getType() == 'validator') {
211: $xmlObj->appendChild($element->toXmlObject());
212: }
213: }
214:
215: $this->addAfterXmlElementToObj($xmlObj);
216:
217: return $xmlObj;
218: }
219:
220: 221: 222: 223: 224: 225:
226: public function getEscapedValue($index = null)
227: {
228: $value = $this->getValue($index);
229:
230: if ($filter = $this->getValueFilter()) {
231: $value = $filter->filter($value);
232: }
233:
234: return $value;
235: }
236:
237: 238: 239: 240: 241: 242:
243: public function setRenderer(Mage_XmlConnect_Model_Simplexml_Form_Element_Renderer_Interface $renderer)
244: {
245: $this->_renderer = $renderer;
246: return $this;
247: }
248:
249: 250: 251: 252: 253:
254: public function getRenderer()
255: {
256: return $this->_renderer;
257: }
258:
259: 260: 261: 262: 263: 264:
265: protected function _addValue(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
266: {
267: if ($this->getEscapedValue()) {
268: $xmlObj->addAttribute('value', $xmlObj->xmlAttribute($this->getEscapedValue()));
269: }
270: return $this;
271: }
272:
273: 274: 275: 276: 277:
278: public function getDefaultXml()
279: {
280: $xml = $this->getData('default_xml');
281: if (null === $xml) {
282: $xml = $this->getElementXml();
283: }
284: return $xml;
285: }
286:
287: 288: 289: 290: 291:
292: public function getXml()
293: {
294: return $this->toXmlObject->asNiceXml();
295: }
296:
297: 298: 299: 300: 301:
302: public function toXmlObject()
303: {
304: if ($this->_renderer) {
305: return $this->_renderer->render($this);
306: } else {
307: return $this->getDefaultXml();
308: }
309: }
310:
311: 312: 313: 314: 315:
316: public function getXmlContainerId()
317: {
318: if ($this->hasData('container_id')) {
319: return $this->getData('container_id');
320: } elseif ($idPrefix = $this->getForm()->getFieldContainerIdPrefix()) {
321: return $idPrefix . $this->getId();
322: }
323: return '';
324: }
325: }
326: