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: class PifaImporter {
29:
30: 31: 32: 33: 34:
35: private $_reader;
36:
37: 38: 39: 40: 41:
42: private $_pifaFormColl;
43:
44: 45: 46: 47: 48:
49: private $_pifaFieldColl;
50:
51: 52: 53: 54: 55:
56: private $_tableName;
57:
58: 59: 60: 61:
62: public function __construct() {
63: $this->_reader = new cXmlReader();
64: $this->_pifaFormColl = new PifaFormCollection();
65: $this->_pifaFieldColl = new PifaFieldCollection();
66: }
67:
68: 69: 70:
71: public function setTableName($_tableName) {
72: $this->_tableName = $_tableName;
73: }
74:
75: 76: 77: 78: 79: 80: 81: 82: 83:
84: public function import($xml) {
85:
86:
87: if (!$this->_reader->loadXML($xml)) {
88: throw new PifaException('XML could not be loaded');
89: }
90:
91:
92: $formElem = $this->_reader->getXpathNode('/pifa/form');
93: if (is_null($this->_tableName)) {
94: $this->_tableName = $formElem->getAttribute('table');
95: }
96: $this->_checkTableName();
97: $pifaForm = $this->_createPifaForm($formElem);
98:
99:
100: $fieldElems = $this->_reader->getXpathNodeList('/pifa/form/field');
101: foreach ($fieldElems as $fieldElem) {
102: $pifaField = $this->_createPifaField($fieldElem, $pifaForm);
103: }
104:
105:
106: $pifaForm->loadFields();
107: $pifaForm->createTable('true' === $formElem->getAttribute('timestamp'));
108:
109:
110: $rowElems = $this->_reader->getXpathNodeList('/pifa/data/row');
111: $db = cRegistry::getDb();
112: foreach ($rowElems as $rowElem) {
113: $rowPath = $rowElem->getNodePath();
114: $fields = array();
115: if ('true' == $formElem->getAttribute('timestamp')) {
116: $fields['pifa_timestamp'] = $rowElem->getAttribute('timestamp');
117: }
118: $colElems = $this->_reader->getXpathNodeList($rowPath . '/col');
119: foreach ($colElems as $colElem) {
120: $fields[$colElem->getAttribute('name')] = $colElem->nodeValue;
121: }
122: $sql = $db->buildInsert($this->_tableName, $fields);
123: $db->query($sql);
124: }
125: }
126:
127: 128: 129: 130: 131: 132:
133: private function _createPifaForm(DOMElement $formElem) {
134: return $this->_pifaFormColl->createNewItem(array(
135: 'idclient' => cRegistry::getClientId(),
136: 'idlang' => cRegistry::getLanguageId(),
137: 'name' => $formElem->getAttribute('name'),
138: 'data_table' => $this->_tableName,
139: 'method' => $formElem->getAttribute('method'),
140: 'with_timestamp' => (int) ('true' === $formElem->getAttribute('timestamp'))
141: ));
142: }
143:
144: 145: 146: 147: 148: 149:
150: private function _createPifaField(DOMElement $fieldElem, PifaForm $pifaForm) {
151:
152:
153: $fieldPath = $fieldElem->getNodePath();
154:
155:
156: $data = array(
157: 'idform' => $pifaForm->get('idform'),
158: 'field_rank' => $fieldElem->getAttribute('rank'),
159: 'field_type' => $this->_getPifaFieldTypeId($fieldElem->getAttribute('type')),
160: 'column_name' => $fieldElem->getAttribute('column'),
161: 'obligatory' => (int) ('true' === $fieldElem->getAttribute('obligatory'))
162: );
163:
164:
165: if ($fieldElem->hasAttribute('default')) {
166: $data['default_value'] = $fieldElem->getAttribute('default');
167: }
168:
169:
170: $label = $this->_reader->getXpathValue($fieldPath . '/label');
171: $data['label'] = strip_tags($label);
172: $labelElem = $this->_reader->getXpathNode($fieldPath . '/label');
173: if ($labelElem) {
174: $display = (int) ('true' === $labelElem->getAttribute('display'));
175: $data['display_label'] = $display;
176: }
177:
178:
179: if (0 < $this->_reader->countXpathNodes($fieldPath . '/help')) {
180: $help = $this->_reader->getXpathValue($fieldPath . '/help');
181: $help = $this->_unCdata($help);
182: $data['help_text'] = $help;
183: }
184:
185:
186: if (0 < $this->_reader->countXpathNodes($fieldPath . '/error')) {
187: $error = $this->_reader->getXpathValue($fieldPath . '/error');
188: $error = $this->_unCdata($error);
189: $data['error_message'] = $error;
190: }
191:
192:
193: if (0 < $this->_reader->countXpathNodes($fieldPath . '/rule')) {
194: $rule = $this->_reader->getXpathValue($fieldPath . '/rule');
195: $rule = $this->_unCdata($rule);
196: $data['rule'] = $rule;
197: }
198:
199:
200: $classElems = $this->_reader->getXpathNodeList($fieldPath . '/classes/class');
201: $cssClass = array();
202: foreach ($classElems as $classElem) {
203: array_push($cssClass, $classElem->nodeValue);
204: }
205: $data['css_class'] = implode(',', $cssClass);
206:
207:
208: $optionsElem = $this->_reader->getXpathNode($fieldPath . '/options');
209: if ($optionsElem) {
210: if ($optionsElem->hasAttribute('source')) {
211: $data['option_class'] = $optionsElem->getAttribute('source');
212: }
213: $optionElems = $this->_reader->getXpathNodeList($fieldPath . '/options/option');
214: $optionLabels = $optionValues = array();
215: foreach ($optionElems as $optionElem) {
216: array_push($optionLabels, $optionElem->nodeValue);
217: array_push($optionValues, $optionElem->getAttribute('value'));
218: }
219: $data['option_labels'] = implode(',', $optionLabels);
220: $data['option_values'] = implode(',', $optionValues);
221: }
222:
223: return $this->_pifaFieldColl->createNewItem($data);
224: }
225:
226: 227:
228: private function _checkTableName() {
229: $db = cRegistry::getDb();
230: $sql = '-- _checkTableName()
231: show tables
232: like "' . $db->escape($this->_tableName) . '"
233: ;';
234: $db->query($sql);
235: if (0 < $db->numRows()) {
236: throw new PifaDatabaseException("table $this->_tableName already exists");
237: }
238: }
239:
240: 241: 242: 243: 244: 245:
246: private function _getPifaFieldTypeId($fieldTypeName) {
247: $fieldTypeName = strtoupper($fieldTypeName);
248: $fieldTypeIds = array(
249: 'INPUTTEXT' => PifaField::INPUTTEXT,
250: 'TEXTAREA' => PifaField::TEXTAREA,
251: 'INPUTPASSWORD' => PifaField::INPUTPASSWORD,
252: 'INPUTRADIO' => PifaField::INPUTRADIO,
253: 'INPUTCHECKBOX' => PifaField::INPUTCHECKBOX,
254: 'SELECT' => PifaField::SELECT,
255: 'SELECTMULTI' => PifaField::SELECTMULTI,
256: 'DATEPICKER' => PifaField::DATEPICKER,
257: 'INPUTFILE' => PifaField::INPUTFILE,
258: 'PROCESSBAR' => PifaField::PROCESSBAR,
259: 'SLIDER' => PifaField::SLIDER,
260:
261: 'BUTTONSUBMIT' => PifaField::BUTTONSUBMIT,
262: 'BUTTONRESET' => PifaField::BUTTONRESET,
263:
264: 'BUTTONBACK' => PifaField::BUTTONBACK,
265: 'BUTTON' => PifaField::BUTTON,
266: 'MATRIX' => PifaField::MATRIX,
267: 'PARAGRAPH' => PifaField::PARA,
268: 'INPUTHIDDEN' => PifaField::INPUTHIDDEN,
269: 'FIELDSET_BEGIN' => PifaField::FIELDSET_BEGIN,
270: 'FIELDSET_END' => PifaField::FIELDSET_END,
271: 'BUTTONIMAGE' => PifaField::BUTTONIMAGE
272: );
273: $fieldTypeId = $fieldTypeIds[$fieldTypeName];
274: return $fieldTypeId;
275: }
276:
277: 278: 279: 280: 281: 282: 283:
284: private function _unCdata($str) {
285: $regex = '/<\!\[CDATA\[(.*)\]\]>/is';
286: $match = preg_replace($regex, '$1', $str);
287: if (is_null($match)) {
288: throw new PifaException("could not _unCdata() '$str'");
289: }
290: return (string) $match;
291: }
292: }
293: