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->nodeValue;
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($formElem->getAttribute('table'), $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'] = $label;
173:         $labelElem = $this->_reader->getXpathNode($fieldPath . '/label');
174:         $display = (int) ('true' === $labelElem->getAttribute('display'));
175:         $data['display_label'] = $display;
176: 
177:         
178:         if (0 < $this->_reader->countXpathNodes($fieldPath . '/help')) {
179:             $help = $this->_reader->getXpathValue($fieldPath . '/help');
180:             $help = $this->_unCdata($help);
181:             $data['help_text'] = $help;
182:         }
183: 
184:         
185:         if (0 < $this->_reader->countXpathNodes($fieldPath . '/error')) {
186:             $error = $this->_reader->getXpathValue($fieldPath . '/error');
187:             $error = $this->_unCdata($error);
188:             $data['error_message'] = $error;
189:         }
190: 
191:         
192:         if (0 < $this->_reader->countXpathNodes($fieldPath . '/rule')) {
193:             $rule = $this->_reader->getXpathValue($fieldPath . '/rule');
194:             $rule = $this->_unCdata($rule);
195:             $data['rule'] = $rule;
196:         }
197: 
198:         
199:         $classElems = $this->_reader->getXpathNodeList($fieldPath . '/classes/class');
200:         $cssClass = array();
201:         foreach ($classElems as $classElem) {
202:             array_push($cssClass, $classElem->nodeValue);
203:         }
204:         $data['css_class'] = implode(',', $cssClass);
205: 
206:         
207:         $optionsElem = $this->_reader->getXpathNode($fieldPath . '/options');
208:         if ($optionsElem) {
209:             if ($optionsElem->hasAttribute('source')) {
210:                 $data['option_class'] = $optionsElem->getAttribute('source');
211:             }
212:             $optionElems = $this->_reader->getXpathNodeList($fieldPath . '/options/option');
213:             $optionLabels = $optionValues = array();
214:             foreach ($optionElems as $optionElem) {
215:                 array_push($optionLabels, $optionElem->nodeValue);
216:                 array_push($optionValues, $optionElem->getAttribute('value'));
217:             }
218:             $data['option_labels'] = implode(',', $optionLabels);
219:             $data['option_values'] = implode(',', $optionValues);
220:         }
221: 
222:         return $this->_pifaFieldColl->createNewItem($data);
223:     }
224: 
225:     226: 
227:     private function _checkTableName() {
228:         $db = cRegistry::getDb();
229:         $sql = '-- _checkTableName()
230:             show tables
231:                 like "' . $db->escape($this->_tableName) . '"
232:             ;';
233:         $db->query($sql);
234:         if (0 < $db->numRows()) {
235:             throw new PifaDatabaseException("table $this->_tableName already exists");
236:         }
237:     }
238: 
239:     240: 241: 242: 243: 244: 
245:     private function _getPifaFieldTypeId($fieldTypeName) {
246:         $fieldTypeName = strtoupper($fieldTypeName);
247:         $fieldTypeIds = array(
248:             'INPUTTEXT' => PifaField::INPUTTEXT,
249:             'TEXTAREA' => PifaField::TEXTAREA,
250:             'INPUTPASSWORD' => PifaField::INPUTPASSWORD,
251:             'INPUTRADIO' => PifaField::INPUTRADIO,
252:             'INPUTCHECKBOX' => PifaField::INPUTCHECKBOX,
253:             'SELECT' => PifaField::SELECT,
254:             'SELECTMULTI' => PifaField::SELECTMULTI,
255:             'DATEPICKER' => PifaField::DATEPICKER,
256:             'INPUTFILE' => PifaField::INPUTFILE,
257:             'PROCESSBAR' => PifaField::PROCESSBAR,
258:             'SLIDER' => PifaField::SLIDER,
259:             
260:             'BUTTONSUBMIT' => PifaField::BUTTONSUBMIT,
261:             'BUTTONRESET' => PifaField::BUTTONRESET,
262:             
263:             'BUTTONBACK' => PifaField::BUTTONBACK,
264:             'BUTTON' => PifaField::BUTTON,
265:             'MATRIX' => PifaField::MATRIX,
266:             'PARAGRAPH' => PifaField::PARA,
267:             'INPUTHIDDEN' => PifaField::INPUTHIDDEN,
268:             'FIELDSET_BEGIN' => PifaField::FIELDSET_BEGIN,
269:             'FIELDSET_END' => PifaField::FIELDSET_END,
270:             'BUTTONIMAGE' => PifaField::BUTTONIMAGE
271:         );
272:         $fieldTypeId = $fieldTypeIds[$fieldTypeName];
273:         return $fieldTypeId;
274:     }
275: 
276:     277: 278: 279: 280: 281: 282: 
283:     private function _unCdata($str) {
284:         $regex = '/<\!\[CDATA\[(.*)\]\]>/is';
285:         $match = preg_replace($regex, '$1', $str);
286:         if (is_null($match)) {
287:             throw new PifaException("could not _unCdata() '$str'");
288:         }
289:         return (string) $match;
290:     }
291: }
292: