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
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • 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
  • SolrRightBottomPage

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:  * @version SVN Revision $Rev:$
  9:  * @author marcus.gnass
 10:  * @copyright four for business AG
 11:  * @link http://www.4fb.de
 12:  */
 13: 
 14: // assert CONTENIDO framework
 15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 16: 
 17: /**
 18:  * This class allows for exporting a PIFA form as XML.
 19:  * The exported file contains the structure of the form and its fields.
 20:  * Optionally the export file may contain the forms gathered data.
 21:  *
 22:  * Example usage:
 23:  * <code>plugin_include('form_assistant', 'classes/class.pifa.importer.php');
 24:  * $imp = new PifaImporter();
 25:  * $imp->import($xml);</code>
 26:  *
 27:  * @author marcus.gnass
 28:  */
 29: class PifaImporter {
 30: 
 31:     /**
 32:      * The XML reader that is used to import a XML file.
 33:      *
 34:      * @var cXmlReader
 35:      */
 36:     private $_reader;
 37: 
 38:     /**
 39:      * PIFA form collection used to create new item.
 40:      *
 41:      * @var PifaFormCollection
 42:      */
 43:     private $_pifaFormColl;
 44: 
 45:     /**
 46:      * PIFA field collection used to create new item.
 47:      *
 48:      * @var PifaFieldCollection
 49:      */
 50:     private $_pifaFieldColl;
 51: 
 52:     /**
 53:      * Name of data table to create
 54:      *
 55:      * @var string
 56:      */
 57:     private $_tableName;
 58: 
 59:     /**
 60:      * Create an instance.
 61:      * Creates XML reader member instances.
 62:      */
 63:     public function __construct() {
 64:         $this->_reader = new cXmlReader();
 65:         $this->_pifaFormColl = new PifaFormCollection();
 66:         $this->_pifaFieldColl = new PifaFieldCollection();
 67:     }
 68: 
 69:     /**
 70:      * @param string $_tableName
 71:      */
 72:     public function setTableName($_tableName) {
 73:         $this->_tableName = $_tableName;
 74:     }
 75: 
 76:     /**
 77:      * Import the given XML.
 78:      *
 79:      * @param string $xml to import
 80:      * @throws PifaException if XML could not be loaded
 81:      * @throws PifaException if existance of table could not be determined
 82:      * @throws PifaException if table already exists
 83:      * @throws PifaException if table could not be created
 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:         $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:         // import fields
101:         $fieldElems = $this->_reader->getXpathNodeList('/pifa/form/field');
102:         foreach ($fieldElems as $fieldElem) {
103:             $pifaField = $this->_createPifaField($fieldElem, $pifaForm);
104:         }
105: 
106:         // create table
107:         $pifaForm->loadFields();
108:         $pifaForm->createTable('true' === $formElem->getAttribute('timestamp'));
109: 
110:         // import data
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:      * Create new PIFA form for current client and language.
130:      *
131:      * @param DOMElement $formElem to get data from
132:      * @return PifaForm
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:      * Create PIFA field for given PIFA form..
147:      *
148:      * @param DOMElement $formElem to create field for
149:      * @return PifaForm
150:      */
151:     private function _createPifaField(DOMElement $fieldElem, PifaForm $pifaForm) {
152: 
153:         // get XPATH of this element to access children
154:         $fieldPath = $fieldElem->getNodePath();
155: 
156:         // create PIFA field
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:         // import default (optional)
166:         if ($fieldElem->hasAttribute('default')) {
167:             $data['default_value'] = $fieldElem->getAttribute('default');
168:         }
169: 
170:         // import label
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:         // import help (optional)
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:         // import error (optional)
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:         // import rule (optional)
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:         // import classes
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:         // import options
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:      * Map a PIFA field name to a numeric ID that is used to identify the
243:      * appropriate database record.
244:      *
245:      * @param string $fieldTypeName to map
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:             // 'CAPTCHA' => PifaField::CAPTCHA,
262:             'BUTTONSUBMIT' => PifaField::BUTTONSUBMIT,
263:             'BUTTONRESET' => PifaField::BUTTONRESET,
264:             // @deprecated use PifaField::BUTTON instead
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:      * Remove CDATA syntax from a string using a regular expression.
280:      *
281:      * @param string $str to handle
282:      * @throws PifaException
283:      * @return string
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: 
CMS CONTENIDO 4.9.8 API documentation generated by ApiGen 2.8.0