-
Notifications
You must be signed in to change notification settings - Fork 2
/
Abstract.php
524 lines (480 loc) · 13.8 KB
/
Abstract.php
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magento.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magento.com for more information.
*
* @category Mage
* @package Mage_XmlConnect
* @copyright Copyright (c) 2006-2014 X.commerce, Inc. (http://www.magento.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* XmlConnect simple xml abstract from class
*
* @category Mage
* @package Mage_XmlConnect
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_XmlConnect_Model_Simplexml_Form_Abstract extends Varien_Object
{
/**
* Element unique id
*
* @var string
*/
protected $_id;
/**
* Form level elements collection
*
* @var Mage_XmlConnect_Model_Simplexml_Form_Element_Collection
*/
protected $_elements;
/**
* Element type classes
*
* @var array
*/
protected $_types = array();
/**
* From Simplexml object
*
* @var Mage_XmlConnect_Model_Simplexml_Element
*/
protected $_xml;
/**
* Main element node
*
* @var string
*/
protected $_mainNode = 'form';
/**
* Is name attribute required
*
* @var bool
*/
protected $_nameRequired = true;
/**
* Custom attributes array
*
* @var array
*/
protected $_customAttributes = array();
/**
* Init form model
*
* @param array $attributes
*/
public function __construct($attributes = array())
{
parent::__construct($attributes);
$this->_prepareXmlObject();
}
/**
* Init form parent Simplexml object
*
* @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
*/
protected function _prepareXmlObject()
{
$this->setXmlObject(
Mage::getModel('xmlconnect/simplexml_element', '<' . $this->_mainNode . '></' . $this->_mainNode . '>')
);
return $this;
}
/**
* Get base simple xml object
*
* @return Mage_XmlConnect_Model_Simplexml_Element
*/
public function getXmlObject()
{
return $this->_xml;
}
/**
* Set simple xml object
*
* @param Mage_XmlConnect_Model_Simplexml_Element $xml
* @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
*/
public function setXmlObject(Mage_XmlConnect_Model_Simplexml_Element $xml)
{
$this->_xml = $xml;
return $this;
}
/**
* Get element id
*
* @return string
*/
public function getId()
{
return $this->_id;
}
/**
* Set element id
*
* @param $id
* @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
*/
public function setId($id)
{
$this->_id = $id;
$this->setData('xml_id', $id);
return $this;
}
/**
* Get element id
*
* @return string
*/
public function getXmlId()
{
return $this->getXmlIdPrefix() . $this->getData('xml_id') . $this->getXmlIdSuffix();
}
/**
* Add form element type
*
* @param string $type
* @param string $className
* @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
*/
public function addType($type, $className)
{
$this->_types[$type] = $className;
return $this;
}
/**
* Get elements collection
*
* @return Mage_XmlConnect_Model_Simplexml_Form_Element_Collection
*/
public function getElements()
{
if (empty($this->_elements)) {
$this->_elements = Mage::getModel('xmlconnect/simplexml_form_element_collection', $this);
}
return $this->_elements;
}
/**
* Add form element
*
* @param Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract $element
* @param bool|string $after
* @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
*/
public function addElement(Mage_XmlConnect_Model_Simplexml_Form_Element_Abstract $element, $after = false)
{
$element->setForm($this);
$this->getElements()->add($element, $after);
return $this;
}
/**
* Add child element
*
* if $after parameter is false - add element to the end of a collection
* if $after parameter is ^ - prepend element to the beginning of a collection
* if $after parameter is string - add element after the element with some id
*
* @param string $elementId
* @param string $type
* @param array $config
* @param mixed $after
* @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
*/
public function addField($elementId, $type, $config, $after = false)
{
if (isset($this->_types[$type])) {
$className = $this->_types[$type];
} else {
$className = 'Mage_XmlConnect_Model_Simplexml_Form_Element_' . uc_words($type);
}
$element = Mage::getModel($className, $config);
$element->setId($elementId);
$this->addElement($element, $after);
return $element;
}
/**
* Remove element from collection
*
* @param string $elementId
* @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
*/
public function removeField($elementId)
{
$this->getElements()->remove($elementId);
return $this;
}
/**
* Add fieldset element
*
* @param string $elementId
* @param array $config
* @param bool|string $after
* @return Mage_XmlConnect_Model_Simplexml_Form_Element_Fieldset
*/
public function addFieldset($elementId, $config = array(), $after = false)
{
$element = Mage::getModel('xmlconnect/simplexml_form_element_fieldset', $config);
$element->setId($elementId);
$this->addElement($element, $after);
return $element;
}
/**
* Add validator element
*
* @param array $config
* @param bool|string $after
* @return Mage_XmlConnect_Model_Simplexml_Form_Element_Validator
*/
public function addValidator($config = array(), $after = false)
{
$element = Mage::getModel('xmlconnect/simplexml_form_element_validator', $config);
$element->setId($this->getXmlId());
$this->addElement($element, $after);
return $element;
}
/**
* Get array of existing elements
*
* @param array $arrAttributes
* @return array
*/
public function __toArray(array $arrAttributes = array())
{
$res = array();
$res['config'] = $this->getData();
$res['formElements']= array();
foreach ($this->getElements() as $element) {
$res['formElements'][] = $element->toArray();
}
return $res;
}
/**
* Return allowed xml form attributes
*
* @return array
*/
public function getXmlAttributes()
{
return array('enctype');
}
/**
* Required form attribute array
*
* @return array
*/
public function getRequiredXmlAttributes()
{
return array('action' => null, 'method' => 'post');
}
/**
* Get after element xml
*
* @return array|Mage_XmlConnect_Model_Simplexml_Element
*/
public function getAfterElementXml()
{
return $this->getData('after_element_xml');
}
/**
* Get xml object attributes
*
* @param array $attributes
* @return array
*/
public function getXmlObjAttributes($attributes = array())
{
$data = array();
if (empty($attributes)) {
$attributes = array_keys($this->_data);
}
foreach ($this->_data as $key => $value) {
if (in_array($key, $attributes)) {
$data[$key] = $value;
}
}
ksort($data);
return $data;
}
/**
* Get object attributes array
*
* @return array
*/
public function getAttributes()
{
$attributes = array_merge($this->getXmlAttributes(), $this->getCustomAttributes());
return $this->getXmlObjAttributes($attributes);
}
/**
* Add after element xml to object
*
* @param Mage_XmlConnect_Model_Simplexml_Element $xmlObj
* @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
*/
public function addAfterXmlElementToObj(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
{
if ($this->_checkXmlInstance($this->getAfterElementXml())) {
$xmlObj->appendChild($this->getAfterElementXml());
} elseif (is_array($this->getAfterElementXml())) {
foreach ($this->getAfterElementXml() as $afterElement) {
if (!$this->_checkXmlInstance($afterElement)) {
continue;
}
$xmlObj->appendChild($afterElement);
}
}
return $this;
}
/**
* Add required attributes to element
*
* @throws Mage_Core_Exception
* @param Mage_XmlConnect_Model_Simplexml_Element $xmlObj
* @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
*/
protected function _addRequiredAttributes(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
{
$this->_addId($xmlObj);
$this->_addName($xmlObj);
foreach ($this->getRequiredXmlAttributes() as $attribute => $defValue) {
$data = $this->getData($this->_underscore($attribute));
if (null !== $data) {
$xmlObj->addAttribute($attribute, $xmlObj->xmlAttribute($data));
} elseif(null !== $defValue){
$xmlObj->addAttribute($attribute, $xmlObj->xmlAttribute($defValue));
} else {
Mage::throwException(Mage::helper('xmlconnect')->__('%s attribute is required.', $attribute));
}
}
return $this;
}
/**
* Add validator to element xml object
*
* @param Mage_XmlConnect_Model_Simplexml_Element $xmlObj
* @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
*/
protected function _addValidator(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
{
return $this;
}
/**
* Add form id to element
*
* @throws Mage_Core_Exception
* @param Mage_XmlConnect_Model_Simplexml_Element $xmlObj
* @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
*/
protected function _addId(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
{
if ($this->getXmlId()) {
$xmlObj->addAttribute('id', $xmlObj->xmlAttribute($this->getXmlId()));
} else {
Mage::throwException(
Mage::helper('xmlconnect')->__('"id" attribute is required for a "%s" field.', $this->getType())
);
}
return $this;
}
/**
* Add form name to element
*
* @param Mage_XmlConnect_Model_Simplexml_Element $xmlObj
* @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
*/
protected function _addName(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
{
if ($this->getName()) {
$name = $this->getName();
} elseif($this->getNameRequired()) {
$name = $this->getXmlId();
}
if (isset($name)) {
$xmlObj->addAttribute('name', $xmlObj->xmlAttribute($name));
}
return $this;
}
/**
* Is object instance of Simplexml object
*
* @param $object
* @return bool
*/
protected function _checkXmlInstance($object)
{
return $object instanceof Mage_XmlConnect_Model_Simplexml_Element;
}
/**
* Get is name required attribute
*
* @return boolean
*/
public function getNameRequired()
{
return $this->_nameRequired;
}
/**
* Set is name required attribute
*
* @param boolean $nameRequired
* @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
*/
public function setNameRequired($nameRequired)
{
$this->_nameRequired = $nameRequired;
return $this;
}
/**
* Get custom attributes
*
* @return array
*/
public function getCustomAttributes()
{
return $this->_customAttributes;
}
/**
* Set custom attributes
*
* @param array $customAttributes
* @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
*/
public function setCustomAttributes(array $customAttributes)
{
$this->_customAttributes = $customAttributes;
return $this;
}
/**
* Check value and return as array - attribute => value
*
* @param string $attribute
* @param mixed $value
* @return array
*/
public function checkAttribute($attribute, $value = null)
{
if (null === $value) {
$value = $this->getData($attribute);
}
if (null !== $value) {
return array($attribute => $value);
}
return array();
}
}