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
    • 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:  * PIFA form importer.
  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:      * @throws PifaException if XML could not be loaded
 80:      * @throws PifaException if existance of table could not be determined
 81:      * @throws PifaException if table already exists
 82:      * @throws PifaException if table could not be created
 83:      */
 84:     public function import($xml) {
 85: 
 86:         // load XML
 87:         if (!$this->_reader->loadXML($xml)) {
 88:             throw new PifaException('XML could not be loaded');
 89:         }
 90: 
 91:         // import form
 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:         // import fields
100:         $fieldElems = $this->_reader->getXpathNodeList('/pifa/form/field');
101:         foreach ($fieldElems as $fieldElem) {
102:             $pifaField = $this->_createPifaField($fieldElem, $pifaForm);
103:         }
104: 
105:         // create table
106:         $pifaForm->loadFields();
107:         $pifaForm->createTable('true' === $formElem->getAttribute('timestamp'));
108: 
109:         // import data
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:      * Create new PIFA form for current client and language.
129:      *
130:      * @param DOMElement $formElem to get data from
131:      * @return PifaForm
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:      * Create PIFA field for given PIFA form..
146:      *
147:      * @param DOMElement $formElem to create field for
148:      * @return PifaForm
149:      */
150:     private function _createPifaField(DOMElement $fieldElem, PifaForm $pifaForm) {
151: 
152:         // get XPATH of this element to access children
153:         $fieldPath = $fieldElem->getNodePath();
154: 
155:         // create PIFA field
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:         // import default (optional)
165:         if ($fieldElem->hasAttribute('default')) {
166:             $data['default_value'] = $fieldElem->getAttribute('default');
167:         }
168: 
169:         // import label
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:         // import help (optional)
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:         // import error (optional)
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:         // import rule (optional)
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:         // import classes
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:         // import options
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:      * Map a PIFA field name to a numeric ID that is used to identify the
242:      * appropriate database record.
243:      *
244:      * @param string $fieldTypeName to map
245:      */
246:     private function _getPifaFieldTypeId($fieldTypeName) {
247:         $fieldTypeName = cString::toUpperCase($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:             // 'CAPTCHA' => PifaField::CAPTCHA,
261:             'BUTTONSUBMIT' => PifaField::BUTTONSUBMIT,
262:             'BUTTONRESET' => PifaField::BUTTONRESET,
263:             // @deprecated use PifaField::BUTTON instead
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:      * Remove CDATA syntax from a string using a regular expression.
279:      *
280:      * @param string $str to handle
281:      * @throws PifaException
282:      * @return string
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: 
CMS CONTENIDO 4.10.0 API documentation generated by ApiGen 2.8.0