Overview

Packages

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