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 PifaAjaxHandler 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:  * Class for area "form_ajax" handling all Ajax requests for the PIFA backend.
 18:  *
 19:  * @author Marcus Gnaß <marcus.gnass@4fb.de>
 20:  */
 21: class PifaAjaxHandler {
 22: 
 23:     /**
 24:      * Action constant to display a form for editing a PIFA form field.
 25:      *
 26:      * @var string
 27:      */
 28:     const GET_FIELD_FORM = 'pifa_get_field_form';
 29: 
 30:     /**
 31:      * Action constant to process a form for editing a PIFA form field.
 32:      *
 33:      * @var string
 34:      */
 35:     const POST_FIELD_FORM = 'pifa_post_field_form';
 36: 
 37:     /**
 38:      * Action constant.
 39:      *
 40:      * @var string
 41:      */
 42:     const REORDER_FIELDS = 'pifa_reorder_fields';
 43: 
 44:     /**
 45:      * Action constant.
 46:      *
 47:      * @var string
 48:      */
 49:     const EXPORT_DATA = 'pifa_export_data';
 50: 
 51:     /**
 52:      * Action constant.
 53:      *
 54:      * @var string
 55:      */
 56:     const EXPORT_FORM = 'pifa_export_form';
 57: 
 58:     /**
 59:      * Action constant.
 60:      *
 61:      * @var string
 62:      */
 63:     const IMPORT_FORM = 'pifa_import_form';
 64: 
 65:     /**
 66:      * Action constant.
 67:      *
 68:      * @var string
 69:      */
 70:     const GET_FILE = 'pifa_get_file';
 71: 
 72:     /**
 73:      * Action constant.
 74:      *
 75:      * @var string
 76:      */
 77:     const DELETE_FIELD = 'pifa_delete_field';
 78: 
 79:     /**
 80:      * Action constant.
 81:      *
 82:      * @var string
 83:      */
 84:     const DELETE_DATA = 'pifa_delete_data';
 85: 
 86:     /**
 87:      * Action constant.
 88:      *
 89:      * @var string
 90:      */
 91:     const GET_OPTION_ROW = 'pifa_get_option_row';
 92: 
 93:     /**
 94:      * @param $action
 95:      *
 96:      * @throws PifaException if action is unknown
 97:      * @throws PifaIllegalStateException
 98:      * @throws cDbException
 99:      * @throws cException
100:      */
101:     function dispatch($action) {
102:         global $area;
103: 
104:         // check for permission
105:         if (!cRegistry::getPerm()->have_perm_area_action($area, $action)) {
106:             $msg = Pifa::i18n('NO_PERMISSIONS');
107:             throw new PifaIllegalStateException($msg);
108:         }
109: 
110:         switch ($action) {
111: 
112:             case self::GET_FIELD_FORM:
113:                 // display a form for editing a PIFA form field
114:                 $idform = cSecurity::toInteger($_GET['idform']);
115:                 $idfield = cSecurity::toInteger($_GET['idfield']);
116:                 $fieldType = cSecurity::toInteger($_GET['field_type']);
117:                 $this->_getFieldForm($idform, $idfield, $fieldType);
118:                 break;
119: 
120:             case self::POST_FIELD_FORM:
121:                 // process a form for editing a PIFA form field
122:                 $idform = cSecurity::toInteger($_POST['idform']);
123:                 $idfield = cSecurity::toInteger($_POST['idfield']);
124:                 // $this->_editFieldForm($idform, $idfield);
125:                 $this->_postFieldForm($idform, $idfield);
126:                 break;
127: 
128:             case self::DELETE_FIELD:
129:                 $idfield = cSecurity::toInteger($_GET['idfield']);
130:                 $this->_deleteField($idfield);
131:                 break;
132: 
133:             case self::DELETE_DATA:
134:                 $idform  = cSecurity::toInteger($_GET['idform']);
135:                 $iddatas = explode(',', $_POST['iddatas']);
136:                 $iddatas = array_map('cSecurity::toInteger', $iddatas);
137:                 $iddatas = array_filter($iddatas);
138:                 $this->_deleteData($idform, $iddatas);
139:                 break;
140: 
141:             case self::REORDER_FIELDS:
142:                 $idform = cSecurity::toInteger($_POST['idform']);
143:                 $idfields = implode(',', array_map('cSecurity::toInteger', explode(',', $_POST['idfields'])));
144:                 $this->_reorderFields($idform, $idfields);
145:                 break;
146: 
147:             case self::EXPORT_DATA:
148:                 $idform = cSecurity::toInteger($_GET['idform']);
149:                 $this->_exportData($idform);
150:                 break;
151: 
152:             case self::EXPORT_FORM:
153:                 $idform = cSecurity::toInteger($_POST['idform']);
154:                 $withData = 'on' === $_POST['with_data'];
155:                 $this->_exportForm($idform, $withData);
156:                 break;
157: 
158:             case self::IMPORT_FORM:
159:                 $xml = $_FILES['xml'];
160:                 $this->_importForm($xml);
161:                 break;
162: 
163:             case self::GET_FILE:
164:                 $name = cSecurity::toString($_GET['name']);
165:                 $file = cSecurity::toString($_GET['file']);
166:                 $this->_getFile($name, $file);
167:                 break;
168: 
169:             case self::GET_OPTION_ROW:
170:                 $index = cSecurity::toInteger($_GET['index']);
171:                 $this->_getOptionRow($index);
172:                 break;
173: 
174:             default:
175:                 $msg = Pifa::i18n('UNKNOWN_ACTION');
176:                 // Util::log(get_class($this) . ': ' . $msg . ': ' . $action);
177:                 throw new PifaException($msg);
178:         }
179:     }
180: 
181:     /**
182:      * Displays a form for editing a PIFA form field.
183:      *
184:      * @param int $idform
185:      * @param int $idfield
186:      * @param int $fieldType
187:      *
188:      * @throws PifaException
189:      * @throws cDbException
190:      * @throws cException
191:      */
192:     private function _getFieldForm($idform, $idfield, $fieldType) {
193:         $cfg = cRegistry::getConfig();
194: 
195:         // get field
196:         if (0 < $idfield) {
197:             // edit existing field
198:             $field = new PifaField();
199:             $field->loadByPrimaryKey($idfield);
200:         } elseif (0 < $fieldType) {
201:             // create new field by type
202:             $field = new PifaField();
203:             $field->loadByRecordSet(array(
204:                 'field_type' => $fieldType
205:             ));
206:         } else {
207:             // bugger off
208:             // TODO check error message
209:             $msg = Pifa::i18n('FORM_CREATE_ERROR');
210:             throw new PifaException($msg);
211:         }
212: 
213:         // get option classes
214:         $optionClasses = Pifa::getExtensionClasses('PifaExternalOptionsDatasourceInterface');
215:         array_unshift($optionClasses, array(
216:             'value' => '',
217:             'label' => Pifa::i18n('none')
218:         ));
219: 
220:         // create form
221:         $tpl = cSmartyBackend::getInstance(true);
222: 
223:         // translations
224:         $tpl->assign('trans', array(
225:             'idfield' => Pifa::i18n('ID'),
226:             'fieldRank' => Pifa::i18n('RANK'),
227:             'fieldType' => Pifa::i18n('FIELD_TYPE'),
228:             'columnName' => Pifa::i18n('COLUMN_NAME'),
229:             'label' => Pifa::i18n('LABEL'),
230:             'displayLabel' => Pifa::i18n('DISPLAY_LABEL'),
231:             'defaultValue' => Pifa::i18n('DEFAULT_VALUE'),
232:             'helpText' => Pifa::i18n('HELP_TEXT'),
233:             'rule' => Pifa::i18n('VALIDATION_RULE'),
234:             'errorMessage' => Pifa::i18n('ERROR_MESSAGE'),
235:             'database' => Pifa::i18n('DATABASE'),
236:             'options' => Pifa::i18n('OPTIONS'),
237:             'general' => Pifa::i18n('GENERAL'),
238:             'obligatory' => Pifa::i18n('OBLIGATORY'),
239:             'value' => Pifa::i18n('VALUE'),
240:             'addOption' => Pifa::i18n('ADD_OPTION'),
241:             'submitValue' => Pifa::i18n('SAVE'),
242:             'styling' => Pifa::i18n('STYLING'),
243:             'cssClass' => Pifa::i18n('CSS_CLASS'),
244:             'uri' => Pifa::i18n('URI'),
245:             'externalOptionsDatasource' => Pifa::i18n('EXTERNAL_OPTIONS_DATASOURCE'),
246:             'deleteAll' => Pifa::i18n('DELETE_CSS_CLASSES')
247:         ));
248: 
249:         // hidden form values (requires right to store form field)
250:         if (cRegistry::getPerm()->have_perm_area_action('form_ajax', self::POST_FIELD_FORM)) {
251:             $tpl->assign('contenido', cRegistry::getBackendSessionId());
252:             $tpl->assign('action', self::POST_FIELD_FORM);
253:             $tpl->assign('idform', $idform);
254:         }
255: 
256:         // field
257:         $tpl->assign('field', $field);
258: 
259:         // CSS classes
260:         $tpl->assign('cssClasses', explode(',', getEffectiveSetting('pifa', 'field-css-classes', 'half-row,full-row,line-bottom,line-top')));
261: 
262:         // option classes (external options datasources)
263:         $tpl->assign('optionClasses', $optionClasses);
264: 
265:         // build href to add new option row (requires right to add option)
266:         if (cRegistry::getPerm()->have_perm_area_action('form_ajax', self::POST_FIELD_FORM) && cRegistry::getPerm()->have_perm_area_action('form_ajax', self::GET_OPTION_ROW)) {
267:             $tpl->assign('hrefAddOption', 'main.php?' . implode('&', array(
268:                 'area=form_ajax',
269:                 'frame=4',
270:                 'contenido=' . cRegistry::getBackendSessionId(),
271:                 'action=' . PifaAjaxHandler::GET_OPTION_ROW
272:             )));
273:         }
274: 
275:         // path to partial template for displaying a single option row
276:         $tpl->assign('partialOptionRow', $cfg['templates']['pifa_ajax_option_row']);
277: 
278:         $tpl->display($cfg['templates']['pifa_ajax_field_form']);
279:     }
280: 
281:     /**
282:      * Processes a form for editing a PIFA form field.
283:      *
284:      * @param int $idform
285:      * @param int $idfield
286:      *
287:      * @throws PifaException
288:      * @throws cDbException
289:      * @throws cException
290:      */
291:     private function _postFieldForm($idform, $idfield) {
292:         global $area;
293: 
294:         $string_cast_deep = function($value) {
295:             $value = cSecurity::unescapeDB($value);
296:             $value = cSecurity::toString($value);
297:             $value = trim($value);
298:             // replace comma by comma entity
299:             $value = str_replace(',', '&#44;', $value);
300:             return $value;
301:         };
302: 
303:         $cfg = cRegistry::getConfig();
304: 
305:         // load or create field
306:         if (0 < $idfield) {
307:             // load field
308:             $pifaField = new PifaField($idfield);
309:             if (!$pifaField->isLoaded()) {
310:                 $msg = Pifa::i18n('FIELD_LOAD_ERROR');
311:                 throw new PifaException($msg);
312:             }
313:             $isFieldCreated = false;
314:         } else {
315:             // get field type for new form field
316:             $fieldType = $_POST['field_type'];
317:             $fieldType = cSecurity::toInteger($fieldType);
318:             // create field
319:             $collection = new PifaFieldCollection();
320:             $pifaField = $collection->createNewItem(array(
321:                 'idform' => $idform,
322:                 'field_type' => $fieldType
323:             ));
324:             $isFieldCreated = true;
325:         }
326: 
327:         // remember old column name
328:         // will be an empty string for new fields
329:         $oldColumnName = $pifaField->get('column_name');
330: 
331:         // set the new rank of the item
332:         $fieldRank = $_POST['field_rank'];
333:         $fieldRank = cSecurity::toInteger($fieldRank);
334:         if ($fieldRank !== $pifaField->get('field_rank')) {
335:             $pifaField->set('field_rank', $fieldRank);
336:         }
337: 
338:         /*
339:          * Read item data from form, validate item data and finally set item
340:          * data. Which data is editable depends upon the field type. So certain
341:          * data will only be stored if its field is shown in the form. Never,
342:          * really never, call Item->set() if the value doesn't differ from the
343:          * previous one. Otherwise the genericDb thinks that the item is
344:          * modified and tries to store it, resulting in a return value of false!
345:          */
346: 
347:         // According to the MySQL documentation table and column names
348:         // must not be longer than 64 charcters.
349:         if ($pifaField->showField('column_name')) {
350:             $columnName = $_POST['column_name'];
351:             $columnName = cSecurity::unescapeDB($columnName);
352:             $columnName = cSecurity::toString($columnName);
353:             $columnName = trim($columnName);
354:             $columnName = cString::toLowerCase($columnName);
355:             // does not seem to work
356:             // $columnName = cString::replaceDiacritics($columnName);
357:             $columnName = preg_replace('/[^a-z0-9_]/', '_', $columnName);
358:             $columnName = cString::getPartOfString($columnName, 0, 64);
359:             if ($columnName !== $pifaField->get('column_name')) {
360:                 $pifaField->set('column_name', $columnName);
361:             }
362:         }
363: 
364:         if ($pifaField->showField('label')) {
365:             $label = $_POST['label'];
366:             $label = cSecurity::unescapeDB($label);
367:             $label = cSecurity::toString($label);
368:             $label = strip_tags($label);
369:             $label = trim($label);
370:             $label = cString::getPartOfString($label, 0, 1023);
371:             if ($label !== $pifaField->get('label')) {
372:                 $pifaField->set('label', $label);
373:             }
374:         }
375: 
376:         if ($pifaField->showField('display_label')) {
377:             $displayLabel = $_POST['display_label'];
378:             $displayLabel = cSecurity::unescapeDB($displayLabel);
379:             $displayLabel = cSecurity::toString($displayLabel);
380:             $displayLabel = trim($displayLabel);
381:             $displayLabel = 'on' === $displayLabel? 1 : 0;
382:             if ($displayLabel !== $pifaField->get('display_label')) {
383:                 $pifaField->set('display_label', $displayLabel);
384:             }
385:         }
386: 
387:         if ($pifaField->showField('uri')) {
388:             $uri = $_POST['uri'];
389:             $uri = cSecurity::unescapeDB($uri);
390:             $uri = cSecurity::toString($uri);
391:             $uri = trim($uri);
392:             $uri = cString::getPartOfString($uri, 0, 1023);
393:             if ($uri !== $pifaField->get('uri')) {
394:                 $pifaField->set('uri', $uri);
395:             }
396:         }
397: 
398:         if ($pifaField->showField('default_value')) {
399:             $defaultValue = $_POST['default_value'];
400:             $defaultValue = cSecurity::unescapeDB($defaultValue);
401:             $defaultValue = cSecurity::toString($defaultValue);
402:             $defaultValue = trim($defaultValue);
403:             $defaultValue = cString::getPartOfString($defaultValue, 0, 1023);
404:             if ($defaultValue !== $pifaField->get('default_value')) {
405:                 $pifaField->set('default_value', $defaultValue);
406:             }
407:         }
408: 
409:         if ($pifaField->showField('option_labels')) {
410:             if (array_key_exists('option_labels', $_POST) && is_array($_POST['option_labels'])) {
411:                 $optionLabels = implode(',', array_map($string_cast_deep, $_POST['option_labels']));
412:                 $optionLabels = cString::getPartOfString($optionLabels, 0, 1023);
413:             }
414:             if ($optionLabels !== $pifaField->get('option_labels')) {
415:                 $pifaField->set('option_labels', $optionLabels);
416:             }
417:         }
418: 
419:         if ($pifaField->showField('option_values')) {
420:             if (array_key_exists('option_values', $_POST) && is_array($_POST['option_values'])) {
421:                 $optionValues = implode(',', array_map($string_cast_deep, $_POST['option_values']));
422:                 $optionValues = cString::getPartOfString($optionValues, 0, 1023);
423:             }
424:             if ($optionValues !== $pifaField->get('option_values')) {
425:                 $pifaField->set('option_values', $optionValues);
426:             }
427:         }
428: 
429:         if ($pifaField->showField('help_text')) {
430:             $helpText = $_POST['help_text'];
431:             $helpText = cSecurity::unescapeDB($helpText);
432:             $helpText = cSecurity::toString($helpText);
433:             $helpText = trim($helpText);
434:             if ($helpText !== $pifaField->get('help_text')) {
435:                 $pifaField->set('help_text', $helpText);
436:             }
437:         }
438: 
439:         if ($pifaField->showField('obligatory')) {
440:             $obligatory = $_POST['obligatory'];
441:             $obligatory = cSecurity::unescapeDB($obligatory);
442:             $obligatory = cSecurity::toString($obligatory);
443:             $obligatory = trim($obligatory);
444:             $obligatory = 'on' === $obligatory? 1 : 0;
445:             if ($obligatory !== $pifaField->get('obligatory')) {
446:                 $pifaField->set('obligatory', $obligatory);
447:             }
448:         }
449: 
450:         if ($pifaField->showField('rule')) {
451:             $rule = $_POST['rule'];
452:             $rule = cSecurity::toString($rule, false);
453:             $rule = trim($rule);
454:             $rule = cString::getPartOfString($rule, 0, 1023);
455:             // check if rule is valid
456:             if (0 === cString::getStringLength($rule)) {
457:                 $pifaField->set('rule', $rule);
458:             } elseif (false === @preg_match($rule, 'And always remember: the world is an orange!')) {
459:                 // PASS
460:             } elseif ($rule === $pifaField->get('rule')) {
461:                 // PASS
462:             } else {
463:                 $pifaField->set('rule', $rule);
464:             }
465:         }
466: 
467:         if ($pifaField->showField('error_message')) {
468:             $errorMessage = $_POST['error_message'];
469:             $errorMessage = cSecurity::unescapeDB($errorMessage);
470:             $errorMessage = cSecurity::toString($errorMessage);
471:             $errorMessage = trim($errorMessage);
472:             $errorMessage = cString::getPartOfString($errorMessage, 0, 1023);
473:             if ($errorMessage !== $pifaField->get('error_message')) {
474:                 $pifaField->set('error_message', $errorMessage);
475:             }
476:         }
477: 
478:         if ($pifaField->showField('css_class') && array_key_exists('css_class', $_POST) && is_array($_POST['css_class'])) {
479:             $cssClass = implode(',', array_map($string_cast_deep, $_POST['css_class']));
480:             $cssClass = cString::getPartOfString($cssClass, 0, 1023);
481:         }
482:         if ($cssClass !== $pifaField->get('css_class')) {
483:             $pifaField->set('css_class', $cssClass);
484:         }
485: 
486:         if ($pifaField->showField('option_class')) {
487:             $optionClass = $_POST['option_class'];
488:             $optionClass = cSecurity::unescapeDB($optionClass);
489:             $optionClass = cSecurity::toString($optionClass);
490:             $optionClass = trim($optionClass);
491:             $optionClass = cString::getPartOfString($optionClass, 0, 1023);
492:             if ($optionClass !== $pifaField->get('option_class')) {
493:                 $pifaField->set('option_class', $optionClass);
494:             }
495:         }
496: 
497:         // store (add, drop or change) column in data table
498:         $pifaForm = new PifaForm($idform);
499:         try {
500:             $pifaForm->storeColumn($pifaField, $oldColumnName);
501:         } catch (PifaException $e) {
502:             // if column could not be created
503:             if ($isFieldCreated) {
504:                 // the field has to be deleted if its newly created
505:                 $pifaField->delete();
506:             } else {
507:                 // the field has to keep its old column name
508:                 $pifaField->set('column_name', $oldColumnName);
509:             }
510:             throw $e;
511:         }
512: 
513:         // store item
514:         if (false === $pifaField->store()) {
515:             $msg = Pifa::i18n('FIELD_STORE_ERROR');
516:             $msg = sprintf($msg, $pifaField->getLastError());
517:             throw new PifaException($msg);
518:         }
519: 
520:         // if a new field was created
521:         // younger siblings have to be moved
522:         if (true === $isFieldCreated) {
523: 
524:             // update ranks of younger siblings
525:             $sql = "-- PifaAjaxHandler->_postFieldForm()
526:                 UPDATE
527:                     " . cRegistry::getDbTableName('pifa_field') . "
528:                 SET
529:                     field_rank = field_rank + 1
530:                 WHERE
531:                     idform = " . cSecurity::toInteger($idform) . "
532:                     AND field_rank >= " . cSecurity::toInteger($fieldRank) . "
533:                     AND idfield <> " . cSecurity::toInteger($pifaField->get('idfield')) . "
534:                 ;";
535: 
536:             $db = cRegistry::getDb();
537:             $succ = $db->query($sql);
538:             // if (false === $succ) {
539:             //     // false is returned if no fields were updated
540:             //     // but that doesn't matter cause new field might
541:             //     // have no younger siblings
542:             // }
543:         }
544: 
545:         // return new row to be displayed in list
546:         $editField = new cHTMLLink();
547:         $editField->setCLink($area, 4, self::GET_FIELD_FORM);
548:         $editField->setCustom('idform', $idform);
549:         $editField = $editField->getHref();
550: 
551:         $deleteField = new cHTMLLink();
552:         $deleteField->setCLink($area, 4, self::DELETE_FIELD);
553:         $deleteField->setCustom('idform', $idform);
554:         $deleteField = $deleteField->getHref();
555: 
556:         $tpl = cSmartyBackend::getInstance(true);
557: 
558:         // translations
559:         $tpl->assign('trans', array(
560:             'edit' => Pifa::i18n('EDIT'),
561:             'delete' => Pifa::i18n('DELETE'),
562:             'obligatory' => Pifa::i18n('OBLIGATORY')
563:         ));
564: 
565:         // the field
566:         $tpl->assign('field', $pifaField);
567: 
568:         $tpl->assign('editField', $editField);
569:         $tpl->assign('deleteField', $deleteField);
570: 
571:         $tpl->display($cfg['templates']['pifa_ajax_field_row']);
572:     }
573: 
574:     /**
575:      * @param int $idfield
576:      * @throws PifaException
577:      */
578:     private function _deleteField($idfield) {
579:         if (0 === $idfield) {
580:             $msg = Pifa::i18n('MISSING_IDFIELD');
581:             throw new PifaException($msg);
582:         }
583: 
584:         $pifaField = new PifaField($idfield);
585:         $pifaField->delete();
586:     }
587: 
588:     /**
589:      *
590:      * @param int   $idform
591:      * @param array $iddatas
592:      *
593:      * @throws PifaException
594:      */
595:     private function _deleteData($idform, array $iddatas) {
596:         if (empty($iddatas)) {
597:             $msg = Pifa::i18n('MISSING_IDDATA');
598:             throw new PifaException($msg);
599:         }
600: 
601:         $pifaForm = new PifaForm($idform);
602:         $pifaForm->deleteData($iddatas);
603:     }
604: 
605:     /**
606:      * reorder fields
607:      *
608:      * @param int $idform
609:      * @param string $idfields CSV of integers
610:      */
611:     private function _reorderFields($idform, $idfields) {
612:         PifaFieldCollection::reorder($idform, $idfields);
613:     }
614: 
615:     /**
616:      * @param int $idform
617:      */
618:     private function _exportData($idform) {
619: 
620:         // read and echo data
621:         $pifaForm = new PifaForm($idform);
622:         $filename = $pifaForm->get('data_table') . date('_Y_m_d_H_i_s') . '.csv';
623:         $data = $pifaForm->getDataAsCsv();
624: 
625:         // prevent caching
626:         session_cache_limiter('private');
627:         session_cache_limiter('must-revalidate');
628: 
629:         // set header
630:         header('Pragma: cache');
631:         header('Expires: 0');
632:         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
633:         header('Cache-Control: private');
634:         header('Content-Type: text/csv');
635:         header('Content-Length: ' . cString::getStringLength($data));
636:         header('Content-Disposition: attachment; filename="' . $filename . '"');
637:         header('Content-Transfer-Encoding: binary');
638: 
639:         // echo payload
640:         echo $data;
641:     }
642: 
643:     /**
644:      * Create an PIFA form export file as XML and displays this as attachment
645:      * for download.
646:      *
647:      * @param int $idform of form to be exported
648:      * @param bool $withData if form data should be included
649:      */
650:     private function _exportForm($idform, $withData) {
651: 
652:         // read and echo data
653:         $pifaForm = new PifaForm($idform);
654:         $filename = $pifaForm->get('data_table') . date('_Y_m_d_H_i_s') . '.xml';
655: 
656:         $pifaExporter = new PifaExporter($pifaForm);
657:         $xml = $pifaExporter->export($withData);
658: 
659:         // prevent caching
660:         session_cache_limiter('private');
661:         session_cache_limiter('must-revalidate');
662: 
663:         // set header
664:         header('Pragma: cache');
665:         header('Expires: 0');
666:         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
667:         header('Cache-Control: private');
668:         header('Content-Type: text/xml');
669:         // header('Content-Type: application/xml');
670:         header('Content-Length: ' . cString::getStringLength($xml, '8bit'));
671:         header('Content-Disposition: attachment; filename="' . $filename . '"');
672:         header('Content-Transfer-Encoding: binary');
673: 
674:         // echo payload
675:         echo $xml;
676:     }
677: 
678:     /**
679:      * @param string $name
680:      * @param string $file
681:      */
682:     private function _getFile($name, $file) {
683:         $cfg = cRegistry::getConfig();
684: 
685:         $path = $cfg['path']['contenido_cache'] . 'form_assistant/';
686: 
687:         $file = basename($file);
688: 
689:         header('Pragma: cache');
690:         header('Expires: 0');
691:         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
692:         header('Cache-Control: private');
693: 
694:         /*
695:          * TODO find solution application/zip works on Ubuntu 12.04 but has
696:          * problems on XP/IE7/IE8 application/octet-stream works on XP/IE7/IE8
697:          * but has problems on Ubuntu 12.04
698:          */
699:         header('Content-Type: application/octet-stream');
700: 
701:         header('Content-Length: ' . filesize($path . $file));
702:         header('Content-Disposition: attachment; filename="' . $name . '"');
703:         header('Content-Transfer-Encoding: binary');
704: 
705:         $buffer = '';
706:         $handle = fopen($path . $file, 'rb');
707:         if (false === $handle) {
708:             return;
709:         }
710:         while (!feof($handle)) {
711:             print fread($handle, 1 * (1024 * 1024));
712:             ob_flush();
713:             flush();
714:         }
715:         fclose($handle);
716:     }
717: 
718:     /**
719:      * @param int $index
720:      *
721:      * @throws cException
722:      */
723:     private function _getOptionRow($index) {
724:         $cfg = cRegistry::getConfig();
725: 
726:         $tpl = cSmartyBackend::getInstance(true);
727: 
728:         // translations
729:         $tpl->assign('trans', array(
730:             'label' => Pifa::i18n('LABEL'),
731:             'value' => Pifa::i18n('VALUE')
732:         ));
733: 
734:         $tpl->assign('i', $index);
735: 
736:         // option
737:         $tpl->assign('option', array(
738:             'label' => '',
739:             'value' => ''
740:         ));
741: 
742:         $tpl->display($cfg['templates']['pifa_ajax_option_row']);
743:     }
744: }
745: 
746: ?>
CMS CONTENIDO 4.10.1 API documentation generated by ApiGen 2.8.0