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 exporter.
  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 from its data
 20:  * table.
 21:  *
 22:  * Example usage:
 23:  * <code>plugin_include('form_assistant', 'classes/class.pifa.exporter.php');
 24:  * $exp = new PifaExporter(new PifaForm($idform));
 25:  * $xml = $exp->export(false);
 26:  * Util::logDump($xml);</code>
 27:  *
 28:  * @author Marcus Gnaß <marcus.gnass@4fb.de>
 29:  */
 30: class PifaExporter {
 31: 
 32:     /**
 33:      * The PIFA form to export.
 34:      *
 35:      * @var PifaForm
 36:      */
 37:     private $_form;
 38: 
 39:     /**
 40:      * The XML writer that is used to create the export XML file.
 41:      *
 42:      * @var cXmlWriter
 43:      */
 44:     private $_writer;
 45: 
 46:     /**
 47:      * Create an instance.
 48:      * Creates PIFA form and XML writer member instances.
 49:      *
 50:      * @param PifaForm $pifaForm to export
 51:      */
 52:     public function __construct(PifaForm $pifaForm) {
 53: 
 54:         // aggregate PIFA form
 55:         $this->_form = $pifaForm;
 56: 
 57:         // build XML writer
 58:         $this->_writer = new cXmlWriter();
 59:     }
 60: 
 61:     /**
 62:      * Create and return export XML of PIFA form and its fields.
 63:      * Optionally includes gathered form data.
 64:      *
 65:      * @param bool $addData if form data should be included in export
 66:      * @return string created XML
 67:      */
 68:     public function export($addData) {
 69: 
 70:         // add pifa (root) element
 71:         $pifa = $this->_writer->addElement('pifa');
 72: 
 73:         // add form & fields structure
 74:         $this->_addForm($pifa, $this->_form);
 75: 
 76:         // optionally add gathered data
 77:         if ($addData) {
 78:             $this->_addData($pifa, $this->_form);
 79:         }
 80: 
 81:         // create XML
 82:         $xml = $this->_writer->saveToString();
 83: 
 84:         // return XML
 85:         return $xml;
 86:     }
 87: 
 88:     /**
 89:      * Adds a "form" element containing one "field" elements for each defined
 90:      * PIFA field.
 91:      *
 92:      * @param DOMElement $parent to add element to
 93:      * @param PifaForm $pifaForm to create XML for
 94:      */
 95:     private function _addForm(DOMElement $parent, PifaForm $pifaForm) {
 96: 
 97:         // build attributes
 98:         $attr = array();
 99:         $attr['name'] = $pifaForm->get('name');
100:         $attr['table'] = $pifaForm->get('data_table');
101:         $attr['method'] = cString::toLowerCase($pifaForm->get('method'));
102:         if ($pifaForm->get('with_timestamp')) {
103:             $attr['timestamp'] = 'true';
104:         } else {
105:             $attr['timestamp'] = 'false';
106:         }
107:         $attr = array_filter($attr);
108: 
109:         // add element
110:         $formElem = $this->_writer->addElement('form', '', $parent, $attr);
111: 
112:         // add child elements
113:         foreach ($this->_form->getFields() as $pifaField) {
114:             $this->_addField($formElem, $pifaField);
115:         }
116:     }
117: 
118:     /**
119:      * Adds a "field" element optionally containing "label", "help", "error",
120:      * "rule", "classes" and "options" elements.
121:      *
122:      * @param DOMElement $parent to add element to
123:      * @param PifaField $pifaField to create XML for
124:      */
125:     private function _addField(DOMElement $parent, PifaField $pifaField) {
126: 
127:         // build attributes
128:         $attr = array();
129:         $attr['rank'] = $pifaField->get('field_rank');
130:         $attr['type'] = $this->_getFieldTypeName($pifaField->get('field_type'));
131:         $attr['column'] = $pifaField->get('column_name');
132:         $attr['default'] = $pifaField->get('default_value');
133:         if ($pifaField->get('obligatory')) {
134:             $attr['obligatory'] = 'true';
135:         } else {
136:             $attr['obligatory'] = 'false';
137:         }
138:         $attr = array_filter($attr);
139: 
140:         // add element
141:         $fieldElem = $this->_writer->addElement('field', '', $parent, $attr);
142: 
143:         // add child elements
144:         $this->_addLabel($fieldElem, $pifaField);
145:         $this->_addHelp($fieldElem, $pifaField);
146:         $this->_addError($fieldElem, $pifaField);
147:         $this->_addRule($fieldElem, $pifaField);
148:         $this->_addClasses($fieldElem, $pifaField);
149:         $this->_addOptions($fieldElem, $pifaField);
150:     }
151: 
152:     /**
153:      * Adds an optional "label" element.
154:      *
155:      * @param DOMElement $parent to add element to
156:      * @param PifaField $pifaField to create XML for
157:      */
158:     private function _addLabel(DOMElement $parent, PifaField $pifaField) {
159: 
160:         // get value
161:         $value = strip_tags($pifaField->get('label'));
162:         if (0 === cString::getStringLength(trim($value))) {
163:             return;
164:         }
165: 
166:         // build attributes
167:         $attr = array();
168:         if ($pifaField->get('display_label')) {
169:             $attr['display'] = 'true';
170:         } else {
171:             $attr['display'] = 'false';
172:         }
173:         $attr = array_filter($attr);
174: 
175:         // add element
176:         $this->_writer->addElement('label', $value, $parent, $attr);
177:     }
178: 
179:     /**
180:      * Adds an optional "help" element.
181:      * As the help text is free text it will be stored as CDATA.
182:      *
183:      * @param DOMElement $parent to add element to
184:      * @param PifaField $pifaField to create XML for
185:      */
186:     private function _addHelp(DOMElement $parent, PifaField $pifaField) {
187: 
188:         // get value
189:         $value = $pifaField->get('help_text');
190:         if (0 === cString::getStringLength(trim($value))) {
191:             return;
192:         }
193: 
194:         // add element
195:         $this->_writer->addElement('help', $value, $parent, array(), true);
196:     }
197: 
198:     /**
199:      * Adds an optional "error" element.
200:      * As the error message is free text it will be stored as CDATA.
201:      *
202:      * @param DOMElement $parent to add element to
203:      * @param PifaField $pifaField to create XML for
204:      */
205:     private function _addError(DOMElement $parent, PifaField $pifaField) {
206: 
207:         // get value
208:         $value = $pifaField->get('error_message');
209:         if (0 === cString::getStringLength(trim($value))) {
210:             return;
211:         }
212: 
213:         // add element
214:         $this->_writer->addElement('error', $value, $parent, array(), true);
215:     }
216: 
217:     /**
218:      * Adds an optional "rule" element.
219:      * As the rule is a regular expression it will be stored as CDATA.
220:      *
221:      * @param DOMElement $parent to add element to
222:      * @param PifaField $pifaField to create XML for
223:      */
224:     private function _addRule(DOMElement $parent, PifaField $pifaField) {
225: 
226:         // get value
227:         $value = $pifaField->get('rule');
228:         if (0 === cString::getStringLength(trim($value))) {
229:             return;
230:         }
231: 
232:         // add element
233:         $this->_writer->addElement('rule', $value, $parent, array(), true);
234:     }
235: 
236:     /**
237:      * Adds an optional "classes" element containing one "class" element for
238:      * each defined class.
239:      *
240:      * @param DOMElement $parent to add element to
241:      * @param PifaField $pifaField to create XML for
242:      */
243:     private function _addClasses(DOMElement $parent, PifaField $pifaField) {
244:         $cssClasses = $pifaField->get('css_class');
245:         $cssClasses = trim($cssClasses);
246:         $cssClasses = explode(',', $cssClasses);
247:         $cssClasses = array_filter($cssClasses);
248: 
249:         // skip classes element if no classes were defined
250:         if (empty($cssClasses)) {
251:             return;
252:         }
253: 
254:         // add element
255:         $classesElem = $this->_writer->addElement('classes', '', $parent);
256: 
257:         // add child elements
258:         foreach ($cssClasses as $value) {
259:             $this->_writer->addElement('class', $value, $classesElem);
260:         }
261:     }
262: 
263:     /**
264:      * Adds an optional "options" element containing one "option" element for
265:      * each defined option.
266:      *
267:      * @param DOMElement $parent to add element to
268:      * @param PifaField $pifaField to create XML for
269:      */
270:     private function _addOptions(DOMElement $parent, PifaField $pifaField) {
271: 
272:         // add child elements
273:         $optionLabels = $pifaField->get('option_labels');
274:         $optionLabels = trim($optionLabels);
275:         $optionLabels = explode(',', $optionLabels);
276:         $optionLabels = array_filter($optionLabels);
277: 
278:         $optionValues = $pifaField->get('option_values');
279:         $optionValues = trim($optionValues);
280:         $optionValues = explode(',', $optionValues);
281:         $optionValues = array_filter($optionValues);
282: 
283:         $count = min(array(
284:             count($optionLabels),
285:             count($optionValues)
286:         ));
287: 
288:         // build attributes
289:         $attr = array();
290:         $attr['source'] = $pifaField->get('option_class');
291:         $attr = array_filter($attr);
292: 
293:         if (0 === $count + count($attr)) {
294:             return;
295:         }
296: 
297:         // add element
298:         $optionsElem = $this->_writer->addElement('options', $pifaField->get('rule'), $parent, $attr);
299: 
300:         for ($i = 0; $i < $count; $i++) {
301: 
302:             // build attributes
303:             $attr = array();
304:             $attr['value'] = $optionValues[$i];
305:             $attr = array_filter($attr);
306: 
307:             // add element
308:             $this->_writer->addElement('option', $optionLabels[$i], $optionsElem, $attr);
309:         }
310:     }
311: 
312:     /**
313:      * Adds an optional "data" element containing one "row" element for each
314:      * record in the forms data table (gathered data).
315:      * If the for has either no fields or its data table has no records, no
316:      * "data" element will be added.
317:      *
318:      * @param DOMElement $parent to add element to
319:      * @param PifaForm $pifaForm to create XML for
320:      */
321:     private function _addData(DOMElement $parent, PifaForm $pifaForm) {
322: 
323:         // get fields from form
324:         $fields = $pifaForm->getFields();
325:         if (empty($fields)) {
326:             return;
327:         }
328: 
329:         // get all column names as array
330:         $columns = array();
331:         foreach ($fields as $pifaField) {
332:             $columns[] = $pifaField->get('column_name');
333:         }
334:         $columns = array_filter($columns);
335: 
336:         // get data from form
337:         $data = $pifaForm->getData();
338:         if (empty($data)) {
339:             return;
340:         }
341: 
342:         // add element (if form has fields and data)
343:         $dataElem = $this->_writer->addElement('data', '', $parent);
344: 
345:         // add data rows
346:         foreach ($data as $row) {
347: 
348:             // build attributes
349:             $attr = array();
350:             if (true === (bool) $pifaForm->get('with_timestamp')) {
351:                 $attr['timestamp'] = $row['pifa_timestamp'];
352:             }
353:             $attr = array_filter($attr);
354: 
355:             // add element
356:             $rowElem = $this->_writer->addElement('row', '', $dataElem, $attr);
357: 
358:             // append value
359:             foreach ($columns as $index => $columnName) {
360: 
361:                 // build attributes
362:                 $attr = array();
363:                 $attr['name'] = $columnName;
364:                 $attr = array_filter($attr);
365: 
366:                 // add element
367:                 $this->_writer->addElement('col', $row[$columnName], $rowElem, $attr, true);
368:             }
369:         }
370:     }
371: 
372:     /**
373:      * Map a numeric PIFA field ID to a name that may be used as translatable
374:      * token (i18n).
375:      *
376:      * @param int $fieldTypeId to map
377:      */
378:     private function _getFieldTypeName($fieldTypeId) {
379:         $fieldTypeNames = array(
380:             PifaField::INPUTTEXT => 'INPUTTEXT',
381:             PifaField::TEXTAREA => 'TEXTAREA',
382:             PifaField::INPUTPASSWORD => 'INPUTPASSWORD',
383:             PifaField::INPUTRADIO => 'INPUTRADIO',
384:             PifaField::INPUTCHECKBOX => 'INPUTCHECKBOX',
385:             PifaField::SELECT => 'SELECT',
386:             PifaField::SELECTMULTI => 'SELECTMULTI',
387:             PifaField::DATEPICKER => 'DATEPICKER',
388:             PifaField::INPUTFILE => 'INPUTFILE',
389:             PifaField::PROCESSBAR => 'PROCESSBAR',
390:             PifaField::SLIDER => 'SLIDER',
391:             // PifaField::CAPTCHA => 'CAPTCHA',
392:             PifaField::BUTTONSUBMIT => 'BUTTONSUBMIT',
393:             PifaField::BUTTONRESET => 'BUTTONRESET',
394:             PifaField::BUTTON => 'BUTTON',
395:             PifaField::MATRIX => 'MATRIX',
396:             PifaField::PARA => 'PARAGRAPH',
397:             PifaField::INPUTHIDDEN => 'INPUTHIDDEN',
398:             PifaField::FIELDSET_BEGIN => 'FIELDSET_BEGIN',
399:             PifaField::FIELDSET_END => 'FIELDSET_END',
400:             PifaField::BUTTONIMAGE => 'BUTTONIMAGE'
401:         );
402:         $fieldTypeName = $fieldTypeNames[$fieldTypeId];
403:         $fieldTypeName = cString::toLowerCase($fieldTypeName);
404:         return $fieldTypeName;
405:     }
406: }
407: 
CMS CONTENIDO 4.10.0 API documentation generated by ApiGen 2.8.0