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