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:  *
   5:  * @package Plugin
   6:  * @subpackage FormAssistant
   7:  * @version SVN Revision $Rev:$
   8:  * @author marcus.gnass
   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:  * Page to be displayed in the left bottom frame.
  18:  * It provides a navigation for all forms defined for the current client and the
  19:  * current language.
  20:  *
  21:  * @author marcus.gnass
  22:  */
  23: class PifaLeftBottomPage extends cGuiPage {
  24: 
  25:     /**
  26:      * Create an instance.
  27:      */
  28:     public function __construct() {
  29: 
  30:         /**
  31:          *
  32:          * @param string $action to be performed
  33:          */
  34:         global $action;
  35: 
  36:         /**
  37:          *
  38:          * @param int $idform id of form to be edited
  39:          */
  40:         global $idform;
  41: 
  42:         parent::__construct('left_bottom', Pifa::getName());
  43: 
  44:         $this->addScript('form_assistant.js');
  45:         $this->addScript('left_bottom.js');
  46: 
  47:         $this->set('s', 'menu', $this->_getMenu());
  48: 
  49:         // add translations to template
  50:         $this->set('s', 'I18N', json_encode(array(
  51:             'confirm_delete_form' => Pifa::i18n('CONFIRM_DELETE_FORM')
  52:         )));
  53:     }
  54: 
  55:     /**
  56:      * Get menu with all forms of current client in current language.
  57:      */
  58:     private function _getMenu() {
  59:         global $area;
  60: 
  61:         $cfg = cRegistry::getConfig();
  62:         $client = cRegistry::getClientId();
  63:         $lang = cRegistry::getLanguageId();
  64: 
  65:         // get all forms of current client in current language
  66:         $forms = PifaFormCollection::getByClientAndLang($client, $lang);
  67:         if (false === $forms) {
  68:             return '<!-- no forms for current client/language -->';
  69:         }
  70: 
  71:         // $formIcon = Pifa::getUrl() . 'images/icon_form.png';
  72: 
  73:         // create menu
  74:         $menu = new cGuiMenu();
  75:         while (false !== $form = $forms->next()) {
  76: 
  77:             $idform = $form->get('idform');
  78:             $formName = $form->get('name');
  79: 
  80:             $menu->setTitle($idform, $formName);
  81: 
  82:             // create link to show/edit the form
  83:             $link = new cHTMLLink();
  84:             $link->setMultiLink($area, '', $area, PifaRightBottomFormPage::SHOW_FORM);
  85:             $link->setCustom('idform', $idform);
  86:             $link->setAttribute('title', 'idform: ' . $idform);
  87:             $menu->setLink($idform, $link);
  88: 
  89:             // create link to delete the form
  90:             if (cRegistry::getPerm()->have_perm_area_action('form', PifaRightBottomFormPage::DELETE_FORM)) {
  91:                 $link = new cHTMLLink();
  92:                 $link->setMultiLink($area, PifaRightBottomFormPage::DELETE_FORM, $area, PifaRightBottomFormPage::DELETE_FORM);
  93:                 $link->setCustom('idform', $idform);
  94:                 $link->setClass('pifa-icon-delete-form');
  95:                 $deleteForm = Pifa::i18n('DELETE_FORM');
  96:                 $link->setAlt($deleteForm);
  97:                 $link->setContent('<img src="' . $cfg['path']['images'] . 'delete.gif" title="' . $deleteForm . '" alt="' . $deleteForm . '">');
  98:                 // $menu->setLink($idform, $link);
  99:                 $menu->setActions($idform, 'delete', $link);
 100:             } else {
 101:                 $menu->setActions($idform, 'delete', '<img src="' . $cfg['path']['images'] . 'delete_inact.gif" title="' . $deleteForm . '" alt="' . $deleteForm . '">');
 102:             }
 103:         }
 104: 
 105:         return $menu->render(false);
 106:     }
 107: 
 108: }
 109: 
 110: /**
 111:  * Page for area "form" to be displayed in the right bottom frame.
 112:  *
 113:  * @author marcus.gnass
 114:  */
 115: class PifaRightBottomFormPage extends cGuiPage {
 116: 
 117:     /**
 118:      * Action constant.
 119:      *
 120:      * @var string
 121:      */
 122:     const SHOW_FORM = 'pifa_show_form';
 123: 
 124:     /**
 125:      * Action constant.
 126:      *
 127:      * @var string
 128:      */
 129:     const STORE_FORM = 'pifa_store_form';
 130: 
 131:     /**
 132:      * Action constant.
 133:      *
 134:      * @var string
 135:      */
 136:     const DELETE_FORM = 'pifa_delete_form';
 137: 
 138:     /**
 139:      * model for a single PIFA form
 140:      *
 141:      * @var PifaForm
 142:      */
 143:     private $_pifaForm = NULL;
 144: 
 145:     /**
 146:      * Creates and aggregates a model for a collection of PIFA forms
 147:      * and another for a single PIFA form.
 148:      *
 149:      * If an ID for an item is given this is loaded from database
 150:      * and its values are stored in the appropriate model.
 151:      *
 152:      * @throws PifaException if form could not be loaded
 153:      */
 154:     public function __construct() {
 155: 
 156:         /**
 157:          *
 158:          * @param string $action to be performed
 159:          */
 160:         global $action;
 161: 
 162:         /**
 163:          *
 164:          * @param int $idform id of form to be edited
 165:          */
 166:         global $idform;
 167: 
 168:         /**
 169:          *
 170:          * @param int $idfield id of field to be edited
 171:          */
 172:         global $idfield;
 173: 
 174:         parent::__construct('right_bottom', Pifa::getName());
 175: 
 176:         $this->addStyle('smoothness/jquery-ui-1.8.20.custom.css');
 177:         $this->addStyle('right_bottom.css');
 178:         $this->addScript('form_assistant.js');
 179:         $this->addScript('right_bottom.js');
 180: 
 181:         // create models
 182:         $this->_pifaForm = new PifaForm();
 183: 
 184:         // load models
 185:         $idform = cSecurity::toInteger($idform);
 186:         if (0 < $idform) {
 187:             if (false === $this->_pifaForm->loadByPrimaryKey($idform)) {
 188:                 $msg = Pifa::i18n('FORM_LOAD_ERROR');
 189:                 throw new PifaException($msg);
 190:             }
 191:         }
 192: 
 193:         // add translations to template
 194:         $this->set('s', 'I18N', json_encode(array(
 195:             'cancel' => Pifa::i18n('CANCEL'),
 196:             'save' => Pifa::i18n('SAVE')
 197:         )));
 198: 
 199:         // dispatch action
 200:         try {
 201:             $this->_dispatch($action);
 202:         } catch (PifaException $e) {
 203:             $cGuiNotification = new cGuiNotification();
 204:             $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $e->getMessage());
 205:             $this->set('s', 'notification', $notification);
 206:             $this->set('s', 'content', '');
 207:         }
 208:     }
 209: 
 210:     /**
 211:      * Dispatches the given action.
 212:      *
 213:      * @param string $action to be executed
 214:      * @param string $notification
 215:      * @throws PifaException if the given action is unknown
 216:      * @throws PifaIllegalStateException if permissions are missing
 217:      */
 218:     protected function _dispatch($action, $notification = '') {
 219:         global $area;
 220: 
 221:         // check for permission
 222:         if (!cRegistry::getPerm()->have_perm_area_action($area, $action)) {
 223:             $msg = Pifa::i18n('NO_PERMISSIONS');
 224:             throw new PifaIllegalStateException($msg);
 225:         }
 226: 
 227:         if (NULL === $action) {
 228:             $cGuiNotification = new cGuiNotification();
 229:             $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_INFO, Pifa::i18n('please select a form'));
 230:             $this->set('s', 'notification', $notification);
 231:             $this->set('s', 'content', '');
 232:             return;
 233:         }
 234: 
 235:         // dispatch actions
 236:         switch ($action) {
 237:             case self::SHOW_FORM:
 238:                 $this->set('s', 'notification', $notification);
 239:                 try {
 240:                     $this->set('s', 'content', $this->_showForm());
 241:                 } catch (SmartyCompilerException $e) {
 242:                     $this->set('s', 'content', Pifa::notifyException($e));
 243:                 }
 244:                 break;
 245: 
 246:             case self::STORE_FORM:
 247:                 $notification = '';
 248:                 try {
 249:                     $this->_storeForm();
 250:                     $this->setReload();
 251:                     // reload right_top after saving of form
 252:                     $idform = $this->_pifaForm->get('idform');
 253:                     $url = "main.php?area=form&frame=3&idform=$idform&action=" . PifaRightBottomFormPage::SHOW_FORM;
 254:                     $url = cRegistry::getSession()->url($url);
 255:                     $this->addScript("<script type=\"text/javascript\">
 256:                         Con.getFrame('right_top').location.href = '$url';
 257:                         </script>");
 258:                 } catch (Exception $e) {
 259:                     $notification = Pifa::notifyException($e);
 260:                 }
 261:                 $this->_dispatch(self::SHOW_FORM, $notification);
 262:                 break;
 263: 
 264:             case self::DELETE_FORM:
 265:                 $notification = '';
 266:                 try {
 267:                     $this->_deleteForm();
 268:                     $cGuiNotification = new cGuiNotification();
 269:                     $this->set('s', 'notification', $cGuiNotification->returnNotification(cGuiNotification::LEVEL_INFO, Pifa::i18n('FORM_DELETED')));
 270:                     $this->set('s', 'content', '');
 271:                     $this->setReload();
 272:                 } catch (Exception $e) {
 273:                     $notification = Pifa::notifyException($e);
 274:                 }
 275:                 break;
 276: 
 277:             default:
 278:                 $msg = Pifa::i18n('UNKNOWN_ACTION');
 279:                 throw new PifaException($msg);
 280:         }
 281:     }
 282: 
 283:     /**
 284:      * Build and return form for PIFA forms.
 285:      *
 286:      * @return string
 287:      */
 288:     private function _showForm() {
 289:         global $area;
 290: 
 291:         $cfg = cRegistry::getConfig();
 292: 
 293:         // get form action
 294:         $formAction = '';
 295:         if (cRegistry::getPerm()->have_perm_area_action('form', self::STORE_FORM)) {
 296:             $formAction = new cHTMLLink();
 297:             $formAction->setCLink($area, 4, self::STORE_FORM);
 298:             $formAction = $formAction->getHref();
 299:         }
 300: 
 301:         // get current or default values for form
 302:         if (!is_null($this->_pifaForm) && $this->_pifaForm->isLoaded()) {
 303:             $idform = $this->_pifaForm->get('idform');
 304:             $nameValue = $this->_pifaForm->get('name');
 305:             $dataTableValue = $this->_pifaForm->get('data_table');
 306:             $methodValue = $this->_pifaForm->get('method');
 307:             $withTimestampValue = (bool) $this->_pifaForm->get('with_timestamp');
 308:         } else {
 309:             $idform = NULL;
 310: 
 311:             // read item data from form
 312:             $nameValue = empty($_POST['name'])? '' : $_POST['name'];
 313:             $nameValue = cSecurity::unescapeDB($nameValue);
 314:             $nameValue = cSecurity::toString($nameValue);
 315:             $nameValue = trim($nameValue);
 316: 
 317:             $dataTableValue = empty($_POST['data_table'])? '' : $_POST['data_table'];
 318:             $dataTableValue = trim($dataTableValue);
 319:             $dataTableValue = strtolower($dataTableValue);
 320:             $dataTableValue = preg_replace('/[^a-z0-9_]/', '_', $dataTableValue);
 321: 
 322:             $methodValue = '';
 323:             $withTimestampValue = true;
 324:         }
 325: 
 326:         $tpl = cSmartyBackend::getInstance(true);
 327:         $tpl->assign('formAction', $formAction);
 328:         $tpl->assign('idform', $idform);
 329:         $tpl->assign('nameValue', $nameValue);
 330:         $tpl->assign('dataTableValue', $dataTableValue);
 331:         $tpl->assign('methodValue', strtoupper($methodValue));
 332:         $tpl->assign('withTimestampValue', $withTimestampValue);
 333:         $tpl->assign('hasWithTimestamp', Pifa::TIMESTAMP_BYFORM === Pifa::getTimestampSetting());
 334:         $tpl->assign('trans', array(
 335:             'legend' => Pifa::i18n('form'),
 336:             'name' => Pifa::i18n('form name'),
 337:             'dataTable' => Pifa::i18n('data table'),
 338:             'method' => Pifa::i18n('method'),
 339:             'withTimestamp' => Pifa::i18n('with timestamp'),
 340:             'pleaseChoose' => Pifa::i18n('please choose'),
 341:             'saveForm' => Pifa::i18n('save form')
 342:         ));
 343: 
 344:         $out = $tpl->fetch($cfg['templates']['pifa_right_bottom_form']);
 345: 
 346:         return $out;
 347:     }
 348: 
 349:     /**
 350:      * Handles a POST request of the first form, showing a forms details.
 351:      *
 352:      * @throws PifaException
 353:      */
 354:     private function _storeForm() {
 355: 
 356:         // determine if item is loaded
 357:         $isLoaded = $this->_pifaForm->isLoaded();
 358: 
 359:         // read item data from form
 360:         $name = $_POST['name'];
 361:         $name = cSecurity::unescapeDB($name);
 362:         $name = cSecurity::toString($name);
 363:         $name = trim($name);
 364: 
 365:         $dataTable = $_POST['data_table'];
 366:         $dataTable = trim($dataTable);
 367:         $dataTable = strtolower($dataTable);
 368:         $dataTable = preg_replace('/[^a-z0-9_]/', '_', $dataTable);
 369: 
 370:         $method = $_POST['method'];
 371:         $method = trim($method);
 372:         $method = strtoupper($method);
 373: 
 374:         switch (Pifa::getTimestampSetting()) {
 375:             case Pifa::TIMESTAMP_NEVER:
 376:                 $withTimestamp = false;
 377:                 break;
 378:             case Pifa::TIMESTAMP_BYFORM:
 379:                 $withTimestamp = 'on' === $_POST['with_timestamp'];
 380:                 break;
 381:             case Pifa::TIMESTAMP_ALWAYS:
 382:                 $withTimestamp = true;
 383:                 break;
 384:         }
 385: 
 386:         // validate item data
 387:         if (0 === strlen($name)) {
 388:             $msg = Pifa::i18n('EMPTY_FORMNAME_ERROR');
 389:             throw new PifaException($msg);
 390:         }
 391:         if (0 === strlen($dataTable)) {
 392:             $msg = Pifa::i18n('EMPTY_DATETABLENAME_ERROR');
 393:             throw new PifaException($msg);
 394:         }
 395:         if (!in_array($method, array(
 396:             'GET',
 397:             'POST'
 398:         ))) {
 399:             $msg = Pifa::i18n('FORM_METHOD_ERROR');
 400:             throw new PifaException($msg);
 401:         }
 402: 
 403:         if ($isLoaded) {
 404:             // remember old table values
 405:             $oldDataTable = $this->_pifaForm->get('data_table');
 406:             $oldWithTimestamp = (bool) $this->_pifaForm->get('with_timestamp');
 407:         } else {
 408:             // create new item for given client & language
 409:             $pifaFormCollection = new PifaFormCollection();
 410:             $this->_pifaForm = $pifaFormCollection->createNewItem(array(
 411:                 'idclient' => cRegistry::getClientId(),
 412:                 'idlang' => cRegistry::getLanguageId()
 413:             ));
 414:         }
 415: 
 416:         // set item data
 417:         // Never, really never, call Item->set() if the value doesn't differ
 418:         // from the previous one. Otherwise the genericDb thinks that the item
 419:         // is modified and tries to store it resulting in a return value of
 420:         // false!
 421:         if ($name !== $this->_pifaForm->get('name')) {
 422:             $this->_pifaForm->set('name', $name);
 423:         }
 424:         if ($dataTable !== $this->_pifaForm->get('data_table')) {
 425:             $this->_pifaForm->set('data_table', $dataTable);
 426:         }
 427:         if (0 !== strcasecmp($method, $this->_pifaForm->get('method'))) {
 428:             $this->_pifaForm->set('method', $method);
 429:         }
 430:         if ($withTimestamp !== (bool) $this->_pifaForm->get('with_timestamp')) {
 431:             $this->_pifaForm->set('with_timestamp', $withTimestamp);
 432:         }
 433: 
 434:         if ($isLoaded) {
 435:             // optionally alter data table
 436:             // HINT: passing the old values is correct!
 437:             // The new values have already been stored inside the pifaForm
 438:             // object!
 439:             $this->_pifaForm->alterTable($oldDataTable, $oldWithTimestamp);
 440:         } else {
 441:             // create table
 442:             $this->_pifaForm->createTable($withTimestamp);
 443:         }
 444: 
 445:         // store item
 446:         if (false === $this->_pifaForm->store()) {
 447:             $msg = Pifa::i18n('FORM_STORE_ERROR');
 448:             $msg = sprintf($msg, $this->_pifaForm->getLastError());
 449:             throw new PifaException($msg);
 450:         }
 451:     }
 452: 
 453:     /**
 454:      */
 455:     private function _deleteForm() {
 456:         $this->_pifaForm->delete();
 457:         $this->_pifaForm = NULL;
 458:     }
 459: 
 460: }
 461: 
 462: /**
 463:  * Page for area "form_fields" to be displayed in the right bottom frame.
 464:  *
 465:  * @author marcus.gnass
 466:  */
 467: class PifaRightBottomFormFieldsPage extends cGuiPage {
 468: 
 469:     /**
 470:      * Action constant.
 471:      *
 472:      * @var string
 473:      */
 474:     const SHOW_FIELDS = 'pifa_show_fields';
 475: 
 476:     /**
 477:      * model for a single PIFA form
 478:      *
 479:      * @var PifaForm
 480:      */
 481:     private $_pifaForm = NULL;
 482: 
 483:     /**
 484:      * Creates and aggregates a model for a collection of PIFA forms
 485:      * and another for a single PIFA form.
 486:      *
 487:      * If an ID for an item is given this is loaded from database
 488:      * and its values are stored in the appropriate model.
 489:      *
 490:      * @throws PifaException if form could not be loaded
 491:      */
 492:     public function __construct() {
 493: 
 494:         /**
 495:          *
 496:          * @param string $action to be performed
 497:          */
 498:         global $action;
 499: 
 500:         /**
 501:          *
 502:          * @param int $idform id of form to be edited
 503:          */
 504:         global $idform;
 505: 
 506:         /**
 507:          *
 508:          * @param int $idfield id of field to be edited
 509:          */
 510:         global $idfield;
 511: 
 512:         parent::__construct('right_bottom', Pifa::getName());
 513: 
 514:         $this->addStyle('smoothness/jquery-ui-1.8.20.custom.css');
 515:         $this->addStyle('right_bottom.css');
 516:         $this->addScript('form_assistant.js');
 517:         $this->addScript('right_bottom.js');
 518: 
 519:         // create models
 520:         $this->_pifaForm = new PifaForm();
 521: 
 522:         // load models
 523:         $idform = cSecurity::toInteger($idform);
 524:         if (0 < $idform) {
 525:             $ret = $this->_pifaForm->loadByPrimaryKey($idform);
 526:             if (false === $ret) {
 527:                 $msg = Pifa::i18n('FORM_LOAD_ERROR');
 528:                 throw new PifaException($msg);
 529:             }
 530:         }
 531: 
 532:         // add translations to template
 533:         $this->set('s', 'I18N', json_encode(array(
 534:             'cancel' => Pifa::i18n('CANCEL'),
 535:             'save' => Pifa::i18n('SAVE'),
 536:             'confirm_delete_field' => Pifa::i18n('CONFIRM_DELETE_FIELD')
 537:         )));
 538: 
 539:         // dispatch action
 540:         try {
 541:             $this->_dispatch($action);
 542:         } catch (PifaException $e) {
 543:             $cGuiNotification = new cGuiNotification();
 544:             $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $e->getMessage());
 545:             $this->set('s', 'notification', $notification);
 546:             $this->set('s', 'content', '');
 547:         }
 548:     }
 549: 
 550:     /**
 551:      * Dispatches the given action.
 552:      *
 553:      * @param string $action to be executed
 554:      * @param string $notification
 555:      * @throws PifaException if the given action is unknown
 556:      * @throws PifaIllegalStateException if permissions are missing
 557:      */
 558:     protected function _dispatch($action, $notification = '') {
 559:         global $area;
 560: 
 561:         // check for permission
 562:         if (!cRegistry::getPerm()->have_perm_area_action($area, $action)) {
 563:             $msg = Pifa::i18n('NO_PERMISSIONS');
 564:             throw new PifaIllegalStateException($msg);
 565:         }
 566: 
 567:         if (NULL === $action) {
 568:             $this->set('s', 'notification', Pifa::i18n('please select a form'));
 569:             $this->set('s', 'content', '');
 570:             return;
 571:         }
 572: 
 573:         // dispatch actions
 574:         switch ($action) {
 575: 
 576:             case self::SHOW_FIELDS:
 577:                 $this->set('s', 'notification', $notification);
 578:                 try {
 579:                     $this->set('s', 'content', $this->_showFields());
 580:                 } catch (SmartyCompilerException $e) {
 581:                     $this->set('s', 'content', Pifa::notifyException($e));
 582:                 }
 583:                 break;
 584: 
 585:             default:
 586:                 $msg = Pifa::i18n('UNKNOWN_ACTION');
 587:                 throw new PifaException($msg);
 588:         }
 589:     }
 590: 
 591:     /**
 592:      */
 593:     private function _showFields() {
 594:         global $area;
 595: 
 596:         $cfg = cRegistry::getConfig();
 597: 
 598:         $idform = $idfield = NULL;
 599:         $fields = NULL;
 600:         $editField = $deleteField = NULL;
 601:         if ($this->_pifaForm->isLoaded()) {
 602: 
 603:             $idform = $this->_pifaForm->get('idform');
 604:             $idfield = $_GET['idfield'];
 605: 
 606:             $fields = $this->_pifaForm->getFields();
 607: 
 608:             if (cRegistry::getPerm()->have_perm_area_action('form_ajax', PifaAjaxHandler::GET_FIELD_FORM)) {
 609:                 $editField = new cHTMLLink();
 610:                 $editField->setCLink('form_ajax', 4, PifaAjaxHandler::GET_FIELD_FORM);
 611:                 $editField->setCustom('idform', $idform);
 612:                 $editField = $editField->getHref();
 613:             }
 614: 
 615:             if (cRegistry::getPerm()->have_perm_area_action('form_ajax', PifaAjaxHandler::DELETE_FIELD)) {
 616:                 $deleteField = new cHTMLLink();
 617:                 $deleteField->setCLink('form_ajax', 4, PifaAjaxHandler::DELETE_FIELD);
 618:                 $deleteField->setCustom('idform', $idform);
 619:                 $deleteField = $deleteField->getHref();
 620:             }
 621:         }
 622: 
 623:         // get and fill template
 624:         $tpl = cSmartyBackend::getInstance(true);
 625: 
 626:         // translations
 627:         $tpl->assign('trans', array(
 628:             'legend' => Pifa::i18n('fields'),
 629:             'pleaseSaveFirst' => Pifa::i18n('please save first'),
 630:             'dialogTitle' => Pifa::i18n('edit field'),
 631:             'edit' => Pifa::i18n('EDIT'),
 632:             'delete' => Pifa::i18n('DELETE'),
 633:             'obligatory' => Pifa::i18n('OBLIGATORY')
 634:         ));
 635: 
 636:         // params
 637:         $tpl->assign('ajaxParams', implode('&', array(
 638:             'area=form_ajax',
 639:             'frame=4',
 640:             'contenido=' . cRegistry::getBackendSessionId()
 641:         )));
 642:         if (cRegistry::getPerm()->have_perm_area_action('form_ajax', PifaAjaxHandler::GET_FIELD_FORM)) {
 643:             $tpl->assign('dragParams', implode('&', array(
 644:                 'area=form_ajax',
 645:                 'frame=4',
 646:                 'contenido=' . cRegistry::getBackendSessionId(),
 647:                 'action=' . PifaAjaxHandler::GET_FIELD_FORM,
 648:                 'idform=' . $idform
 649:             )));
 650:         }
 651:         if (cRegistry::getPerm()->have_perm_area_action('form_ajax', PifaAjaxHandler::REORDER_FIELDS)) {
 652:             $tpl->assign('sortParams', implode('&', array(
 653:                 'area=form_ajax',
 654:                 'frame=4',
 655:                 'contenido=' . cRegistry::getBackendSessionId(),
 656:                 'action=' . PifaAjaxHandler::REORDER_FIELDS,
 657:                 'idform=' . $this->_pifaForm->get('idform')
 658:             )));
 659:         }
 660: 
 661:         // data
 662:         $tpl->assign('idform', $idform);
 663:         $tpl->assign('idfield', $idfield);
 664: 
 665:         $tpl->assign('fields', $fields);
 666:         $tpl->assign('fieldTypes', PifaField::getFieldTypeNames());
 667: 
 668:         // for partial
 669:         $tpl->assign('editField', $editField);
 670:         $tpl->assign('deleteField', $deleteField);
 671:         // define path to partial template for displaying a single field row
 672:         $tpl->assign('partialFieldRow', $cfg['templates']['pifa_ajax_field_row']);
 673: 
 674:         $out = $tpl->fetch($cfg['templates']['pifa_right_bottom_fields']);
 675: 
 676:         return $out;
 677:     }
 678: 
 679: }
 680: 
 681: /**
 682:  * Page for area "form_data" to be displayed in the right bottom frame.
 683:  *
 684:  * @author marcus.gnass
 685:  */
 686: class PifaRightBottomFormDataPage extends cGuiPage {
 687: 
 688:     /**
 689:      * Action constant.
 690:      *
 691:      * @var string
 692:      */
 693:     const SHOW_DATA = 'pifa_show_data';
 694: 
 695:     /**
 696:      * model for a single PIFA form
 697:      *
 698:      * @var PifaForm
 699:      */
 700:     private $_pifaForm = NULL;
 701: 
 702:     /**
 703:      * Creates and aggregates a model for a collection of PIFA forms
 704:      * and another for a single PIFA form.
 705:      *
 706:      * If an ID for an item is given this is loaded from database
 707:      * and its values are stored in the appropriate model.
 708:      *
 709:      * @throws PifaException if form could not be loaded
 710:      */
 711:     public function __construct() {
 712: 
 713:         /**
 714:          *
 715:          * @param string $action to be performed
 716:          */
 717:         global $action;
 718: 
 719:         /**
 720:          *
 721:          * @param int $idform id of form to be edited
 722:          */
 723:         global $idform;
 724: 
 725:         /**
 726:          *
 727:          * @param int $idfield id of field to be edited
 728:          */
 729:         global $idfield;
 730: 
 731:         parent::__construct('right_bottom', Pifa::getName());
 732: 
 733:         $this->addStyle('smoothness/jquery-ui-1.8.20.custom.css');
 734:         $this->addStyle('right_bottom.css');
 735:         $this->addScript('form_assistant.js');
 736:         $this->addScript('right_bottom.js');
 737: 
 738:         // create models
 739:         $this->_pifaForm = new PifaForm();
 740: 
 741:         // load models
 742:         $idform = cSecurity::toInteger($idform);
 743:         if (0 < $idform) {
 744:             if (false === $this->_pifaForm->loadByPrimaryKey($idform)) {
 745:                 $msg = Pifa::i18n('FORM_LOAD_ERROR');
 746:                 throw new PifaException($msg);
 747:             }
 748:         }
 749: 
 750:         // add translations to template
 751:         $this->set('s', 'I18N', json_encode(array(
 752:             'cancel' => Pifa::i18n('CANCEL'),
 753:             'save' => Pifa::i18n('SAVE')
 754:         )));
 755: 
 756:         // dispatch action
 757:         try {
 758:             $this->_dispatch($action);
 759:         } catch (PifaException $e) {
 760:             $cGuiNotification = new cGuiNotification();
 761:             $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $e->getMessage());
 762:             $this->set('s', 'notification', $notification);
 763:             $this->set('s', 'content', '');
 764:         }
 765:     }
 766: 
 767:     /**
 768:      * Dispatches the given action.
 769:      *
 770:      * @param string $action to be executed
 771:      * @param string $notification
 772:      * @throws PifaException if the given action is unknown
 773:      * @throws PifaIllegalStateException if permissions are missing
 774:      */
 775:     protected function _dispatch($action, $notification = '') {
 776:         global $area;
 777: 
 778:         // check for permission
 779:         if (!cRegistry::getPerm()->have_perm_area_action($area, $action)) {
 780:             $msg = Pifa::i18n('NO_PERMISSIONS');
 781:             throw new PifaIllegalStateException($msg);
 782:         }
 783: 
 784:         if (NULL === $action) {
 785:             $this->set('s', 'notification', Pifa::i18n('please select a form'));
 786:             $this->set('s', 'content', '');
 787:             return;
 788:         }
 789: 
 790:         // dispatch actions
 791:         switch ($action) {
 792: 
 793:             case self::SHOW_DATA:
 794:                 $this->set('s', 'notification', $notification);
 795:                 try {
 796:                     $this->set('s', 'content', $this->_showData());
 797:                 } catch (SmartyCompilerException $e) {
 798:                     $this->set('s', 'content', Pifa::notifyException($e));
 799:                 }
 800:                 break;
 801: 
 802:             default:
 803:                 $msg = Pifa::i18n('UNKNOWN_ACTION');
 804:                 throw new PifaException($msg);
 805:         }
 806:     }
 807: 
 808:     /**
 809:      */
 810:     private function _showData() {
 811:         $cfg = cRegistry::getConfig();
 812: 
 813:         $tpl = cSmartyBackend::getInstance(true);
 814: 
 815:         // translations
 816:         $tpl->assign('trans', array(
 817:             'legend' => Pifa::i18n('data'),
 818:             'pleaseSaveFirst' => Pifa::i18n('please save first'),
 819:             'export' => Pifa::i18n('download data as CSV')
 820:         ));
 821: 
 822:         $tpl->assign('form', $this->_pifaForm);
 823:         $tpl->assign('getFileUrl', 'main.php?' . implode('&', array(
 824:             'area=form_ajax',
 825:             'frame=4',
 826:             'contenido=' . cRegistry::getBackendSessionId(),
 827:             'action=' . PifaAjaxHandler::GET_FILE
 828:         )));
 829: 
 830:         try {
 831:             $tpl->assign('fields', $this->_pifaForm->getFields());
 832:         } catch (Exception $e) {
 833:             $tpl->assign('fields', Pifa::notifyException($e));
 834:         }
 835: 
 836:         $tpl->assign('withTimestamp', (bool) $this->_pifaForm->get('with_timestamp'));
 837: 
 838:         try {
 839:             $data = $this->_pifaForm->getData();
 840:             $tpl->assign('data', $data);
 841:             if (!empty($data) && cRegistry::getPerm()->have_perm_area_action('form_ajax', PifaAjaxHandler::EXPORT_DATA)) {
 842:                 $tpl->assign('exportUrl', 'main.php?' . implode('&', array(
 843:                     'area=form_ajax',
 844:                     'frame=4',
 845:                     'contenido=' . cRegistry::getBackendSessionId(),
 846:                     'action=' . PifaAjaxHandler::EXPORT_DATA,
 847:                     'idform=' . $this->_pifaForm->get('idform')
 848:                 )));
 849:             }
 850:         } catch (Exception $e) {
 851:             $tpl->assign('data', Pifa::notifyException($e));
 852:         }
 853: 
 854:         $out = $tpl->fetch($cfg['templates']['pifa_right_bottom_data']);
 855: 
 856:         return $out;
 857:     }
 858: 
 859: }
 860: 
 861: /**
 862:  * Page for area "form_export" to be displayed in the right bottom frame.
 863:  * This page allows for exporting a form as XML.
 864:  *
 865:  * @author marcus.gnass
 866:  */
 867: class PifaRightBottomFormExportPage extends cGuiPage {
 868: 
 869:     /**
 870:      * Action constant.
 871:      *
 872:      * @var string
 873:      */
 874:     const EXPORT_FORM = 'pifa_export_form';
 875: 
 876:     /**
 877:      * model for a single PIFA form
 878:      *
 879:      * @var PifaForm
 880:      */
 881:     private $_pifaForm = NULL;
 882: 
 883:     /**
 884:      * Creates and aggregates a model for a single PIFA form.
 885:      *
 886:      * If an ID for an item is given this is loaded from database
 887:      * and its values are stored in the appropriate model.
 888:      *
 889:      * @throws PifaException if form could not be loaded
 890:      */
 891:     public function __construct() {
 892: 
 893:         /**
 894:          *
 895:          * @param string $action to be performed
 896:          */
 897:         global $action;
 898: 
 899:         /**
 900:          *
 901:          * @param int $idform id of form to be edited
 902:          */
 903:         global $idform;
 904: 
 905:         parent::__construct('right_bottom', Pifa::getName());
 906: 
 907:         $this->addStyle('smoothness/jquery-ui-1.8.20.custom.css');
 908:         $this->addStyle('right_bottom.css');
 909:         $this->addScript('form_assistant.js');
 910:         $this->addScript('right_bottom.js');
 911: 
 912:         // create models
 913:         $this->_pifaForm = new PifaForm();
 914: 
 915:         // load models
 916:         $idform = cSecurity::toInteger($idform);
 917:         if (0 < $idform) {
 918:             if (false === $this->_pifaForm->loadByPrimaryKey($idform)) {
 919:                 $msg = Pifa::i18n('FORM_LOAD_ERROR');
 920:                 throw new PifaException($msg);
 921:             }
 922:         }
 923: 
 924:         // dispatch action
 925:         try {
 926:             $this->_dispatch($action);
 927:         } catch (PifaException $e) {
 928:             $cGuiNotification = new cGuiNotification();
 929:             $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $e->getMessage());
 930:             $this->set('s', 'notification', $notification);
 931:             $this->set('s', 'content', '');
 932:         }
 933:     }
 934: 
 935:     /**
 936:      * Dispatches the given action.
 937:      *
 938:      * @param string $action to be executed
 939:      * @param string $notification
 940:      * @throws PifaException if the given action is unknown
 941:      * @throws PifaIllegalStateException if permissions are missing
 942:      */
 943:     protected function _dispatch($action, $notification = '') {
 944: 
 945:         // dispatch actions
 946:         switch ($action) {
 947: 
 948:             case self::EXPORT_FORM:
 949: 
 950:                 // check for permission
 951:                 if (!cRegistry::getPerm()->have_perm_area_action('form_ajax', $action)) {
 952:                     $msg = Pifa::i18n('NO_PERMISSIONS');
 953:                     throw new PifaIllegalStateException($msg);
 954:                 }
 955: 
 956:                 $this->set('s', 'notification', $notification);
 957:                 try {
 958:                     $this->set('s', 'content', $this->_exportForm());
 959:                 } catch (SmartyCompilerException $e) {
 960:                     $this->set('s', 'content', Pifa::notifyException($e));
 961:                 }
 962:                 break;
 963: 
 964:             default:
 965:                 $msg = Pifa::i18n('UNKNOWN_ACTION');
 966:                 throw new PifaException($msg);
 967:         }
 968:     }
 969: 
 970:     /**
 971:      */
 972:     private function _exportForm() {
 973:         $cfg = cRegistry::getConfig();
 974: 
 975:         $tpl = cSmartyBackend::getInstance(true);
 976: 
 977:         // translations
 978:         $tpl->assign('trans', array(
 979:             'legend' => Pifa::i18n('pifa_export_form'),
 980:             'withData' => Pifa::i18n('WITH_DATA'),
 981:             'export' => Pifa::i18n('EXPORT')
 982:         ));
 983: 
 984:         $tpl->assign('formAction', 'main.php?' . implode('&', array(
 985:             'area=form_ajax',
 986:             'frame=4',
 987:             'contenido=' . cRegistry::getBackendSessionId(),
 988:             'action=' . PifaAjaxHandler::EXPORT_FORM,
 989:             'idform=' . $this->_pifaForm->get('idform')
 990:         )));
 991: 
 992:         $tpl->assign('idform', $this->_pifaForm->get('idform'));
 993: 
 994:         $out = $tpl->fetch($cfg['templates']['pifa_right_bottom_export']);
 995: 
 996:         return $out;
 997:     }
 998: 
 999: }
1000: 
1001: /**
1002:  * Page for area "form_import" to be displayed in the right bottom frame.
1003:  * This page allows for importing a form that is available as XML.
1004:  *
1005:  * @author marcus.gnass
1006:  */
1007: class PifaRightBottomFormImportPage extends cGuiPage {
1008: 
1009:     /**
1010:      * Action constant.
1011:      *
1012:      * @var string
1013:      */
1014:     const IMPORT_FORM = 'pifa_import_form';
1015: 
1016:     /**
1017:      * Create an instance.
1018:      * Dispatches the current action and displays a notification.
1019:      */
1020:     public function __construct() {
1021: 
1022:         /**
1023:          *
1024:          * @param string $action to be performed
1025:          */
1026:         global $action;
1027: 
1028:         parent::__construct('right_bottom', Pifa::getName());
1029: 
1030:         $this->addStyle('smoothness/jquery-ui-1.8.20.custom.css');
1031:         $this->addStyle('right_bottom.css');
1032:         $this->addScript('form_assistant.js');
1033:         $this->addScript('right_bottom.js');
1034: 
1035:         // dispatch action
1036:         try {
1037:             $this->_dispatch($action);
1038:         } catch (PifaException $e) {
1039:             $cGuiNotification = new cGuiNotification();
1040:             $notification = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $e->getMessage());
1041:             $this->set('s', 'notification', $notification);
1042:             $this->set('s', 'content', '');
1043:         }
1044:     }
1045: 
1046:     /**
1047:      * Dispatches the given action.
1048:      *
1049:      * @param string $action to be executed
1050:      * @param string $notification
1051:      * @throws PifaException if the given action is unknown
1052:      * @throws PifaIllegalStateException if permissions are missing
1053:      */
1054:     protected function _dispatch($action, $notification = '') {
1055:         global $area;
1056: 
1057:         // check for permission
1058:         if (!cRegistry::getPerm()->have_perm_area_action($area, $action)) {
1059:             $msg = Pifa::i18n('NO_PERMISSIONS');
1060:             throw new PifaIllegalStateException($msg);
1061:         }
1062: 
1063:         // dispatch actions
1064:         switch ($action) {
1065: 
1066:             case self::IMPORT_FORM:
1067:                 $this->set('s', 'notification', $notification);
1068:                 try {
1069: 
1070:                     switch (strtoupper($_SERVER['REQUEST_METHOD'])) {
1071:                         case 'GET':
1072:                             $this->set('s', 'content', $this->_importFormGet());
1073:                             break;
1074: 
1075:                         case 'POST':
1076:                             $this->set('s', 'content', $this->_importFormPost());
1077:                             break;
1078:                     }
1079: 
1080:                     $this->setReload();
1081:                 } catch (SmartyCompilerException $e) {
1082:                     $this->set('s', 'content', Pifa::notifyException($e));
1083:                 }
1084:                 break;
1085: 
1086:             default:
1087:                 $msg = Pifa::i18n('UNKNOWN_ACTION');
1088:                 throw new PifaException($msg);
1089:         }
1090:     }
1091: 
1092:     /**
1093:      * Handles the GET & POST request for the form_import area.
1094:      * On a GET request a form is displayed that allows for uploading an XML
1095:      * export file and an additional checkbox to select if gathered data should
1096:      * be imported too.
1097:      * On a POST request the import of the uploaded file is performed via
1098:      * PifaImporter. Eventually a notification is displayed.
1099:      *
1100:      * @return string
1101:      */
1102:     private function _importFormGet($showTableNameField = false) {
1103:         $cfg = cRegistry::getConfig();
1104: 
1105:         $tpl = cSmartyBackend::getInstance(true);
1106: 
1107:         // translations
1108:         $tpl->assign('trans', array(
1109:             'legend' => Pifa::i18n('pifa_import_form'),
1110:             'xml' => Pifa::i18n('XML'),
1111:             'used_table_name_error' => Pifa::i18n('USED_TABLE_NAME_ERROR'),
1112:             'table_name' => Pifa::i18n('data table'),
1113:             'export' => Pifa::i18n('IMPORT')
1114:         ));
1115: 
1116:         $tpl->assign('formAction', 'main.php?' . implode('&', array(
1117:             'area=form_import',
1118:             'frame=4',
1119:             'contenido=' . cRegistry::getBackendSessionId(),
1120:             'action=' . self::IMPORT_FORM
1121:         )));
1122: 
1123:         $tpl->assign('showTableNameField', $showTableNameField);
1124: 
1125:         $out = $tpl->fetch($cfg['templates']['pifa_right_bottom_import']);
1126: 
1127:         return $out;
1128:     }
1129: 
1130:     /**
1131:      * Handles the GET & POST request for the form_import area.
1132:      * On a GET request a form is displayed that allows for uploading an XML
1133:      * export file and an additional checkbox to select if gathered data should
1134:      * be imported too.
1135:      * On a POST request the import of the uploaded file is performed via
1136:      * PifaImporter. Eventually a notification is displayed.
1137:      *
1138:      * @return string
1139:      */
1140:     private function _importFormPost() {
1141:         $cGuiNotification = new cGuiNotification();
1142: 
1143:         // read XML from file
1144:         $xml = file_get_contents($_FILES['xml']['tmp_name']);
1145:         $tableName = isset($_POST['table_name'])? $_POST['table_name'] : NULL;
1146: 
1147:         // check read operation
1148:         if (false === $xml) {
1149:             $note = Pifa::i18n('READ_XML_ERROR');
1150:             return $cGuiNotification->returnNotification(cGuiNotification::LEVEL_ERROR, $note);
1151:         }
1152: 
1153:         try {
1154: 
1155:             // perform import process
1156:             $pifaImporter = new PifaImporter();
1157:             $pifaImporter->setTableName($tableName);
1158:             $pifaImporter->import($xml);
1159: 
1160:             // display success message
1161:             $note = Pifa::i18n('IMPORT_SUCCESS');
1162:             $out = $cGuiNotification->returnNotification(cGuiNotification::LEVEL_INFO, $note);
1163:         } catch (PifaDatabaseException $e) {
1164:             $out = $this->_importFormGet(true);
1165:         }
1166: 
1167:         return $out;
1168:     }
1169: 
1170: }
1171: 
CMS CONTENIDO 4.9.3 API documentation generated by ApiGen 2.8.0