Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • PHP
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SIWECOS
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cContentTypePifaForm
  • DefaultFormModule
  • DefaultFormProcessor
  • ExampleOptionsDatasource
  • MailedFormProcessor
  • Pifa
  • PifaAbstractFormModule
  • PifaAbstractFormProcessor
  • PifaAjaxHandler
  • PifaExporter
  • PifaExternalOptionsDatasourceInterface
  • PifaField
  • PifaFieldCollection
  • PifaForm
  • PifaFormCollection
  • PifaImporter
  • PifaLeftBottomPage
  • PifaRightBottomFormDataPage
  • PifaRightBottomFormExportPage
  • PifaRightBottomFormFieldsPage
  • PifaRightBottomFormImportPage
  • PifaRightBottomFormPage

Exceptions

  • PifaDatabaseException
  • PifaException
  • PifaIllegalStateException
  • PifaMailException
  • PifaNotImplementedException
  • PifaNotYetStoredException
  • PifaValidationException
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: /**
  4:  * This file contains the PifaImporter class.
  5:  *
  6:  * @package Plugin
  7:  * @subpackage FormAssistant
  8:  * @author Marcus Gnaß <marcus.gnass@4fb.de>
  9:  * @copyright four for business AG
 10:  * @link http://www.4fb.de
 11:  */
 12: 
 13: // assert CONTENIDO framework
 14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 15: 
 16: /**
 17:  * This class allows for exporting a PIFA form as XML.
 18:  * The exported file contains the structure of the form and its fields.
 19:  * Optionally the export file may contain the forms gathered data.
 20:  *
 21:  * Example usage:
 22:  * <code>plugin_include('form_assistant', 'classes/class.pifa.importer.php');
 23:  * $imp = new PifaImporter();
 24:  * $imp->import($xml);</code>
 25:  *
 26:  * @author Marcus Gnaß <marcus.gnass@4fb.de>
 27:  */
 28: class PifaImporter {
 29: 
 30:     /**
 31:      * The XML reader that is used to import a XML file.
 32:      *
 33:      * @var cXmlReader
 34:      */
 35:     private $_reader;
 36: 
 37:     /**
 38:      * PIFA form collection used to create new item.
 39:      *
 40:      * @var PifaFormCollection
 41:      */
 42:     private $_pifaFormColl;
 43: 
 44:     /**
 45:      * PIFA field collection used to create new item.
 46:      *
 47:      * @var PifaFieldCollection
 48:      */
 49:     private $_pifaFieldColl;
 50: 
 51:     /**
 52:      * Name of data table to create
 53:      *
 54:      * @var string
 55:      */
 56:     private $_tableName;
 57: 
 58:     /**
 59:      * Create an instance.
 60:      * Creates XML reader member instances.
 61:      */
 62:     public function __construct() {
 63:         $this->_reader        = new cXmlReader();
 64:         $this->_pifaFormColl  = new PifaFormCollection();
 65:         $this->_pifaFieldColl = new PifaFieldCollection();
 66:     }
 67: 
 68:     /**
 69:      * @param string $_tableName
 70:      */
 71:     public function setTableName($_tableName) {
 72:         $this->_tableName = $_tableName;
 73:     }
 74: 
 75:     /**
 76:      * Import the given XML.
 77:      *
 78:      * @param string $xml to import
 79:      *
 80:      * @throws PifaDatabaseException
 81:      * @throws PifaException if table could not be created
 82:      * @throws cDbException
 83:      * @throws cException
 84:      */
 85:     public function import($xml) {
 86: 
 87:         // load XML
 88:         if (!$this->_reader->loadXML($xml)) {
 89:             throw new PifaException('XML could not be loaded');
 90:         }
 91: 
 92:         // import form
 93:         /** @var DOMElement $formElem */
 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:         // import fields
102:         $fieldElems = $this->_reader->getXpathNodeList('/pifa/form/field');
103:         foreach ($fieldElems as $fieldElem) {
104:             $this->_createPifaField($fieldElem, $pifaForm);
105:         }
106: 
107:         // create table
108:         $pifaForm->loadFields();
109:         $pifaForm->createTable('true' === $formElem->getAttribute('timestamp'));
110: 
111:         // import data
112:         $rowElems = $this->_reader->getXpathNodeList('/pifa/data/row');
113:         $db = cRegistry::getDb();
114:         /** @var DOMElement $rowElem */
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:             /** @var DOMElement $colElem */
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:      * Create new PIFA form for current client and language.
133:      *
134:      * @param DOMElement $formElem to get data from
135:      * @return PifaForm
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:      * Create PIFA field for given PIFA form..
150:      *
151:      * @param DOMElement $fieldElem
152:      * @param PifaForm   $pifaForm
153:      *
154:      * @return PifaField
155:      * @throws PifaException
156:      * @throws cException
157:      */
158:     private function _createPifaField(DOMElement $fieldElem, PifaForm $pifaForm) {
159: 
160:         // get XPATH of this element to access children
161:         $fieldPath = $fieldElem->getNodePath();
162: 
163:         // create PIFA field
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:         // import default (optional)
173:         if ($fieldElem->hasAttribute('default')) {
174:             $data['default_value'] = $fieldElem->getAttribute('default');
175:         }
176: 
177:         // import label
178:         $label = $this->_reader->getXpathValue($fieldPath . '/label');
179:         $data['label'] = strip_tags($label);
180:         /** @var DOMElement $labelElem */
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:         // import help (optional)
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:         // import error (optional)
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:         // import rule (optional)
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:         // import classes
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:         // import options
217:         /** @var DOMElement $optionsElem */
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:             /** @var DOMElement $optionElem */
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:      * @throws PifaDatabaseException
239:      * @throws cDbException
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:      * Map a PIFA field name to a numeric ID that is used to identify the
255:      * appropriate database record.
256:      *
257:      * @param string $fieldTypeName to map
258:      *
259:      * @return mixed
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:             // 'CAPTCHA' => PifaField::CAPTCHA,
276:             'BUTTONSUBMIT' => PifaField::BUTTONSUBMIT,
277:             'BUTTONRESET' => PifaField::BUTTONRESET,
278:             // @deprecated use PifaField::BUTTON instead
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:      * Remove CDATA syntax from a string using a regular expression.
294:      *
295:      * @param string $str to handle
296:      * @throws PifaException
297:      * @return string
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: 
CMS CONTENIDO 4.10.1 API documentation generated by ApiGen 2.8.0