1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
31: class PifaExporter {
32:
33: 34: 35: 36: 37:
38: private $_form;
39:
40: 41: 42: 43: 44:
45: private $_writer;
46:
47: 48: 49: 50: 51: 52:
53: public function __construct(PifaForm $pifaForm) {
54:
55:
56: $this->_form = $pifaForm;
57:
58:
59: $this->_writer = new cXmlWriter();
60: }
61:
62: 63: 64: 65: 66: 67: 68:
69: public function export($addData) {
70:
71:
72: $pifa = $this->_writer->addElement('pifa');
73:
74:
75: $this->_addForm($pifa, $this->_form);
76:
77:
78: if ($addData) {
79: $this->_addData($pifa, $this->_form);
80: }
81:
82:
83: $xml = $this->_writer->saveToString();
84:
85:
86: return $xml;
87: }
88:
89: 90: 91: 92: 93: 94: 95:
96: private function _addForm(DOMElement $parent, PifaForm $pifaForm) {
97:
98:
99: $attr = array();
100: $attr['name'] = $pifaForm->get('name');
101: $attr['table'] = $pifaForm->get('data_table');
102: $attr['method'] = strtolower($pifaForm->get('method'));
103: if ($pifaForm->get('with_timestamp')) {
104: $attr['timestamp'] = 'true';
105: } else {
106: $attr['timestamp'] = 'false';
107: }
108: $attr = array_filter($attr);
109:
110:
111: $formElem = $this->_writer->addElement('form', '', $parent, $attr);
112:
113:
114: foreach ($this->_form->getFields() as $pifaField) {
115: $this->_addField($formElem, $pifaField);
116: }
117: }
118:
119: 120: 121: 122: 123: 124: 125:
126: private function _addField(DOMElement $parent, PifaField $pifaField) {
127:
128:
129: $attr = array();
130: $attr['rank'] = $pifaField->get('field_rank');
131: $attr['type'] = $this->_getFieldTypeName($pifaField->get('field_type'));
132: $attr['column'] = $pifaField->get('column_name');
133: $attr['default'] = $pifaField->get('default_value');
134: if ($pifaField->get('obligatory')) {
135: $attr['obligatory'] = 'true';
136: } else {
137: $attr['obligatory'] = 'false';
138: }
139: $attr = array_filter($attr);
140:
141:
142: $fieldElem = $this->_writer->addElement('field', '', $parent, $attr);
143:
144:
145: $this->_addLabel($fieldElem, $pifaField);
146: $this->_addHelp($fieldElem, $pifaField);
147: $this->_addError($fieldElem, $pifaField);
148: $this->_addRule($fieldElem, $pifaField);
149: $this->_addClasses($fieldElem, $pifaField);
150: $this->_addOptions($fieldElem, $pifaField);
151: }
152:
153: 154: 155: 156: 157: 158:
159: private function _addLabel(DOMElement $parent, PifaField $pifaField) {
160:
161:
162: $value = strip_tags($pifaField->get('label'));
163: if (0 === strlen(trim($value))) {
164: return;
165: }
166:
167:
168: $attr = array();
169: if ($pifaField->get('display_label')) {
170: $attr['display'] = 'true';
171: } else {
172: $attr['display'] = 'false';
173: }
174: $attr = array_filter($attr);
175:
176:
177: $this->_writer->addElement('label', $value, $parent, $attr);
178: }
179:
180: 181: 182: 183: 184: 185: 186:
187: private function _addHelp(DOMElement $parent, PifaField $pifaField) {
188:
189:
190: $value = $pifaField->get('help_text');
191: if (0 === strlen(trim($value))) {
192: return;
193: }
194:
195:
196: $this->_writer->addElement('help', $value, $parent, array(), true);
197: }
198:
199: 200: 201: 202: 203: 204: 205:
206: private function _addError(DOMElement $parent, PifaField $pifaField) {
207:
208:
209: $value = $pifaField->get('error_message');
210: if (0 === strlen(trim($value))) {
211: return;
212: }
213:
214:
215: $this->_writer->addElement('error', $value, $parent, array(), true);
216: }
217:
218: 219: 220: 221: 222: 223: 224:
225: private function _addRule(DOMElement $parent, PifaField $pifaField) {
226:
227:
228: $value = $pifaField->get('rule');
229: if (0 === strlen(trim($value))) {
230: return;
231: }
232:
233:
234: $this->_writer->addElement('rule', $value, $parent, array(), true);
235: }
236:
237: 238: 239: 240: 241: 242: 243:
244: private function _addClasses(DOMElement $parent, PifaField $pifaField) {
245: $cssClasses = $pifaField->get('css_class');
246: $cssClasses = trim($cssClasses);
247: $cssClasses = explode(',', $cssClasses);
248: $cssClasses = array_filter($cssClasses);
249:
250:
251: if (empty($cssClasses)) {
252: return;
253: }
254:
255:
256: $classesElem = $this->_writer->addElement('classes', '', $parent);
257:
258:
259: foreach ($cssClasses as $value) {
260: $this->_writer->addElement('class', $value, $classesElem);
261: }
262: }
263:
264: 265: 266: 267: 268: 269: 270:
271: private function _addOptions(DOMElement $parent, PifaField $pifaField) {
272:
273:
274: $optionLabels = $pifaField->get('option_labels');
275: $optionLabels = trim($optionLabels);
276: $optionLabels = explode(',', $optionLabels);
277: $optionLabels = array_filter($optionLabels);
278:
279: $optionValues = $pifaField->get('option_values');
280: $optionValues = trim($optionValues);
281: $optionValues = explode(',', $optionValues);
282: $optionValues = array_filter($optionValues);
283:
284: $count = min(array(
285: count($optionLabels),
286: count($optionValues)
287: ));
288:
289:
290: $attr = array();
291: $attr['source'] = $pifaField->get('option_class');
292: $attr = array_filter($attr);
293:
294: if (0 === $count + count($attr)) {
295: return;
296: }
297:
298:
299: $optionsElem = $this->_writer->addElement('options', $pifaField->get('rule'), $parent, $attr);
300:
301: for ($i = 0; $i < $count; $i++) {
302:
303:
304: $attr = array();
305: $attr['value'] = $optionValues[$i];
306: $attr = array_filter($attr);
307:
308:
309: $this->_writer->addElement('option', $optionLabels[$i], $optionsElem, $attr);
310: }
311: }
312:
313: 314: 315: 316: 317: 318: 319: 320: 321:
322: private function _addData(DOMElement $parent, PifaForm $pifaForm) {
323:
324:
325: $fields = $pifaForm->getFields();
326: if (empty($fields)) {
327: return;
328: }
329:
330:
331: $columns = array();
332: foreach ($fields as $pifaField) {
333: $columns[] = $pifaField->get('column_name');
334: }
335: $columns = array_filter($columns);
336:
337:
338: $data = $pifaForm->getData();
339: if (empty($data)) {
340: return;
341: }
342:
343:
344: $dataElem = $this->_writer->addElement('data', '', $parent);
345:
346:
347: foreach ($data as $row) {
348:
349:
350: $attr = array();
351: if (true === (bool) $pifaForm->get('with_timestamp')) {
352: $attr['timestamp'] = $row['pifa_timestamp'];
353: }
354: $attr = array_filter($attr);
355:
356:
357: $rowElem = $this->_writer->addElement('row', '', $dataElem, $attr);
358:
359:
360: foreach ($columns as $index => $columnName) {
361:
362:
363: $attr = array();
364: $attr['name'] = $columnName;
365: $attr = array_filter($attr);
366:
367:
368: $this->_writer->addElement('col', $row[$columnName], $rowElem, $attr, true);
369: }
370: }
371: }
372:
373: 374: 375: 376: 377: 378:
379: private function _getFieldTypeName($fieldTypeId) {
380: $fieldTypeNames = array(
381: PifaField::INPUTTEXT => 'INPUTTEXT',
382: PifaField::TEXTAREA => 'TEXTAREA',
383: PifaField::INPUTPASSWORD => 'INPUTPASSWORD',
384: PifaField::INPUTRADIO => 'INPUTRADIO',
385: PifaField::INPUTCHECKBOX => 'INPUTCHECKBOX',
386: PifaField::SELECT => 'SELECT',
387: PifaField::SELECTMULTI => 'SELECTMULTI',
388: PifaField::DATEPICKER => 'DATEPICKER',
389: PifaField::INPUTFILE => 'INPUTFILE',
390: PifaField::PROCESSBAR => 'PROCESSBAR',
391: PifaField::SLIDER => 'SLIDER',
392:
393: PifaField::BUTTONSUBMIT => 'BUTTONSUBMIT',
394: PifaField::BUTTONRESET => 'BUTTONRESET',
395: PifaField::BUTTON => 'BUTTON',
396: PifaField::MATRIX => 'MATRIX',
397: PifaField::PARA => 'PARAGRAPH',
398: PifaField::INPUTHIDDEN => 'INPUTHIDDEN',
399: PifaField::FIELDSET_BEGIN => 'FIELDSET_BEGIN',
400: PifaField::FIELDSET_END => 'FIELDSET_END',
401: PifaField::BUTTONIMAGE => 'BUTTONIMAGE'
402: );
403: $fieldTypeName = $fieldTypeNames[$fieldTypeId];
404: $fieldTypeName = strtolower($fieldTypeName);
405: return $fieldTypeName;
406: }
407: }
408: