public function runOnValidationError($arrInvalidFields)
 {
     $arrDca = $GLOBALS['TL_DCA'][$this->strTable];
     \System::loadLanguageFile($this->strTable);
     StatusMessage::addError(sprintf($GLOBALS['TL_LANG']['frontendedit']['validationFailed'], '<ul>' . implode('', array_map(function ($val) use($arrDca) {
         return '<li>' . ($arrDca['fields'][$val]['label'][0] ?: $val) . '</li>';
     }, $arrInvalidFields))) . '</ul>', $this->objModule->id, 'validation-failed');
     $this->Template->message = StatusMessage::generate($this->objModule->id);
 }
 /**
  * Generate the module
  */
 protected function compile()
 {
     $strParent = parent::compile();
     if ($this->Template->error == $GLOBALS['TL_LANG']['MSC']['accountNotFound']) {
         StatusMessage::addError($GLOBALS['TL_LANG']['MSC']['accountNotFound'], $this->objModel->id);
     }
     if (!StatusMessage::isEmpty($this->objModel->id)) {
         $this->Template->error = StatusMessage::generate($this->objModel->id);
     }
     return $strParent;
 }
 /**
  * Auto-generate a form to edit the current database record
  *
  * @param integer
  * @param integer
  *
  * @return string
  */
 public function edit($intID = null, $ajaxId = null)
 {
     // render
     if ($this->renderStart) {
         $this->async = false;
         // async form submission for start/elements/stop not available yet
         $this->Template = new \FrontendTemplate($this->strTemplateStart);
         $this->generateStart();
         return \Controller::replaceInsertTags($this->Template->parse(), false);
     }
     $this->Template = new \FrontendTemplate($this->strTemplate);
     if ($this->renderStop) {
         $this->Template = new \FrontendTemplate($this->strTemplateStop);
     }
     if (empty($this->dca)) {
         return false;
     }
     // run callbacks on ajax reload
     if (is_array($this->dca['config']['onreload_callback'])) {
         foreach ($this->dca['config']['onreload_callback'] as $callback) {
             $this->import($callback[0]);
             $this->{$callback}[0]->{$callback}[1]($this);
             // reload model from database, maybe something has changed in callback
             if (!$this->saveToBlob) {
                 $this->objActiveRecord->refresh();
             }
         }
     }
     if (!$this->getFields()) {
         return false;
     }
     $blnAjax = $this->generateFields($ajaxId);
     if ($blnAjax) {
         $objTemplate = $this->generateSubpalette($ajaxId);
         return \Controller::replaceInsertTags($objTemplate->parse(), false);
     }
     if ($this->isSubmitted && $this->isDoNotSubmit()) {
         $this->runOnValidationError($this->arrInvalidFields);
     }
     $blnSubmittedBeforeReset = false;
     if ($this->isSubmitted && !$this->isDoNotSubmit()) {
         // run field callbacks, must be before save(), same as contao
         $this->runCallbacks();
         // save for save_callbacks
         $this->save();
         // process form
         $this->processForm();
         // generate messages and remove them from the session automatically
         $this->Template->message = StatusMessage::generate($this->objModule->id);
         // single submission will not render form again after submission
         if ($this->singleSubmission) {
             $this->invalid = true;
         } else {
             if ($this->getReset()) {
                 $this->reset();
                 $blnSubmittedBeforeReset = true;
             }
         }
         if (in_array('exporter', \ModuleLoader::getActive()) && $this->exportAfterSubmission) {
             $this->exportAfterSubmission();
         }
         $this->redirectAfterSubmission();
     }
     $this->generateStart();
     $this->Template->fields = $this->arrFields;
     $this->Template->isSubmitted = $this->isSubmitted();
     $this->Template->doNotSubmit = $this->isDoNotSubmit();
     $this->Template->submission = $this->objActiveRecord;
     $this->Template->hidden = $this->generateHiddenFields();
     $this->Template->invalid = $this->invalid;
     $this->Template->config = $this->objConfig;
     $this->compile();
     $strBuffer = \Controller::replaceInsertTags($this->Template->parse(), false);
     Ajax::runActiveAction(Form::FORMHYBRID_NAME, 'asyncFormSubmit', new FormAjax($this, $strBuffer, $blnSubmittedBeforeReset));
     Ajax::runActiveAction(Form::FORMHYBRID_NAME, 'reload', new FormAjax($this, $strBuffer));
     return $strBuffer;
 }
 protected function compile()
 {
     $this->Template->headline = $this->headline;
     $this->Template->hl = $this->hl;
     $this->Template->wrapperClass = $this->strWrapperClass;
     $this->Template->wrapperId = $this->strWrapperId;
     $this->strFormId = $this->formHybridDataContainer . '_' . $this->id;
     $strAction = $this->defaultAction ?: \Input::get('act');
     $this->arrEditable = deserialize($this->formHybridEditable, true);
     $this->strToken = $this->strToken ?: \Input::get('token');
     // Do not change this order (see #6191)
     $this->Template->style = !empty($this->arrStyle) ? implode(' ', $this->arrStyle) : '';
     $this->Template->class = trim('mod_' . $this->type . ' ' . $this->cssID[1]);
     $this->Template->cssID = $this->cssID[0] != '' ? ' id="' . $this->cssID[0] . '"' : '';
     $this->Template->inColumn = $this->strColumn;
     if ($this->Template->headline == '') {
         $this->Template->headline = $this->headline;
     }
     if ($this->Template->hl == '') {
         $this->Template->hl = $this->hl;
     }
     if (!empty($this->classes) && is_array($this->classes)) {
         $this->Template->class .= ' ' . implode(' ', $this->classes);
     }
     $this->addDefaultArchive();
     // at first check for the correct request token to be set
     if (!$this->deactivateTokens && !\RequestToken::validate($this->strToken)) {
         if (!$this->blnSilentMode) {
             StatusMessage::addError(sprintf($GLOBALS['TL_LANG']['frontendedit']['requestTokenExpired'], Url::replaceParameterInUri(Url::getUrl(), 'token', \RequestToken::get())), $this->id, 'requestTokenExpired');
         }
         return;
     }
     if ($this->formHybridAllowIdAsGetParameter) {
         $intId = \Input::get($this->formHybridIdGetParameter);
         if (is_numeric($intId)) {
             $this->intId = $intId;
         }
     }
     $strItemClass = \Model::getClassFromTable($this->formHybridDataContainer);
     // get id from share
     if ($strShare = \Input::get('share')) {
         if (($objItem = $strItemClass::findByShareToken($strShare)) !== null && !FormHybridList::shareTokenExpiredOrEmpty($objItem, time())) {
             $this->intId = $objItem->id;
         }
     }
     if (!$this->intId) {
         if (isset($GLOBALS['TL_HOOKS']['frontendEditAddNoIdBehavior']) && is_array($GLOBALS['TL_HOOKS']['frontendEditAddNoIdBehavior'])) {
             foreach ($GLOBALS['TL_HOOKS']['frontendEditAddNoIdBehavior'] as $arrCallback) {
                 $this->import($arrCallback[0]);
                 if ($this->{$arrCallback}[0]->{$arrCallback}[1]($this) === false) {
                     return;
                 }
             }
         }
         if ($this->noIdBehavior == 'error') {
             if (!$this->blnSilentMode) {
                 StatusMessage::addError($GLOBALS['TL_LANG']['frontendedit']['noIdFound'], $this->id, 'noidfound');
             }
             return;
         } elseif ($this->noIdBehavior == 'redirect' || $this->noIdBehavior == 'create_until') {
             $arrConditions = deserialize($this->existanceConditions, true);
             if ($this->existanceConditions && !empty($arrConditions)) {
                 $arrColumns = array();
                 $arrValues = array();
                 foreach ($arrConditions as $arrCondition) {
                     if (!$arrCondition['field']) {
                         continue;
                     }
                     $arrColumns[] = $arrCondition['field'] . '=?';
                     $arrValues[] = $this->replaceInsertTags($arrCondition['value']);
                 }
                 if (!empty($arrColumns) && ($objItem = $strItemClass::findOneBy($arrColumns, $arrValues)) !== null) {
                     $this->intId = $objItem->id;
                 }
             }
         }
         if (!$this->intId) {
             if ($this->noIdBehavior == 'redirect') {
                 if (!$this->blnSilentMode) {
                     StatusMessage::addError($GLOBALS['TL_LANG']['frontendedit']['noIdFound'], $this->id, 'noidfound');
                 }
                 return;
             } else {
                 $strFormId = FormHelper::getFormId($this->formHybridDataContainer, $this->id);
                 // get id from FormSession
                 if ($_POST) {
                     if ($intId = FormSession::getSubmissionId($strFormId)) {
                         $this->intId = $intId;
                     }
                 }
                 if (!$this->intId) {
                     // if no id is given a new instance is initiated
                     $objConfiguration = new FormConfiguration($this->arrData);
                     // ajax handling, required in this manor, as we have no real ajax controller in contao and ajax request not related to this module
                     // might trigger this module beforhand and new submission will be created after the submission was transfered to the user and id wont match any more
                     if (Ajax::isRelated(Form::FORMHYBRID_NAME) !== null) {
                         if ($intId = FormSession::getSubmissionId($strFormId)) {
                             $this->intId = $intId;
                         } else {
                             $objConfiguration->forceCreate = true;
                         }
                     }
                     $this->objForm = new $this->strFormClass($objConfiguration, $this->arrSubmitCallbacks, $this->intId ?: 0, $this);
                     if ($intId = $this->objForm->getId()) {
                         $this->intId = $intId;
                     }
                 }
             }
         }
     }
     // intId is set at this point!
     if (!$this->checkEntityExists($this->intId)) {
         if (!$this->blnSilentMode) {
             StatusMessage::addError($GLOBALS['TL_LANG']['formhybrid_list']['noPermission'], $this->id, 'nopermission');
         }
         if (Ajax::isRelated(Form::FORMHYBRID_NAME)) {
             $objResponse = new ResponseError();
             $objResponse->setResult(StatusMessage::generate($this->id));
             $objResponse->output();
         }
         return;
     }
     // page title
     if ($this->setPageTitle) {
         global $objPage;
         if (($objItem = General::getModelInstance($this->formHybridDataContainer, $this->intId)) !== null) {
             $objPage->pageTitle = $objItem->{$this->pageTitleField};
         }
     }
     if ($strAction == FRONTENDEDIT_ACT_DELETE) {
         if ($this->checkDeletePermission($this->intId)) {
             $blnResult = $this->deleteItem($this->intId);
             if (\Environment::get('isAjaxRequest')) {
                 die($blnResult);
             }
             // return to the list
             \Controller::redirect(Url::removeQueryString(array('act', 'id', 'token'), Url::getUrl()));
         } else {
             if (!$this->blnSilentMode) {
                 StatusMessage::addError($GLOBALS['TL_LANG']['formhybrid_list']['noPermission'], $this->id, 'nopermission');
             }
             return;
         }
     } else {
         if ($this->checkUpdatePermission($this->intId)) {
             // create a new lock if necessary
             if (in_array('entity_lock', \ModuleLoader::getActive()) && $this->addEntityLock) {
                 if (\HeimrichHannot\EntityLock\EntityLockModel::isLocked($this->formHybridDataContainer, $this->intId, $this)) {
                     $objLock = \HeimrichHannot\EntityLock\EntityLockModel::findActiveLock($this->formHybridDataContainer, $this->intId, $this);
                     $objItem = General::getModelInstance($this->formHybridDataContainer, $this->intId);
                     if (!$this->blnSilentMode) {
                         $strMessage = \HeimrichHannot\EntityLock\EntityLock::generateErrorMessage($this->formHybridDataContainer, $this->intId, $this);
                         if ($this->allowLockDeletion) {
                             $strUnlockForm = $this->generateUnlockForm($objItem, $objLock);
                             $strMessage .= $strUnlockForm;
                         }
                         StatusMessage::addError($strMessage, $this->id, 'locked');
                     }
                     if ($this->readOnlyOnLocked) {
                         $this->formHybridViewMode = FORMHYBRID_VIEW_MODE_READONLY;
                         $this->formHybridReadonlyTemplate = 'formhybridreadonly_default';
                     } else {
                         return;
                     }
                 } else {
                     \HeimrichHannot\EntityLock\EntityLockModel::create($this->formHybridDataContainer, $this->intId, $this);
                 }
             }
             if ($this->objForm === null) {
                 $this->objForm = new $this->strFormClass(new FormConfiguration($this->arrData), $this->arrSubmitCallbacks, $this->intId, $this);
             }
             $this->Template->form = $this->objForm->generate();
             $this->Template->item = $this->objForm->activeRecord;
             if (\Environment::get('isAjaxRequest') && \Input::get('scope') == 'modal') {
                 $objItem = General::getModelInstance($this->formHybridDataContainer, $this->intId);
                 $objModalWrapper = new \FrontendTemplate($this->modalTpl ?: 'formhybrid_reader_modal_bootstrap');
                 if ($objItem !== null) {
                     $objModalWrapper->setData($objItem->row());
                 }
                 $objModalWrapper->module = Arrays::arrayToObject($this->arrData);
                 $objModalWrapper->item = $this->replaceInsertTags($this->Template->parse());
                 die($objModalWrapper->parse());
             }
         } else {
             if (!$this->blnSilentMode) {
                 StatusMessage::addError($GLOBALS['TL_LANG']['formhybrid_list']['noPermission'], $this->id, 'nopermission');
             }
             return;
         }
     }
 }