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