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