/** * Get fields for the form's SObject type * @param object * @return array */ public function getSObjectFields($dc) { $arrOptions = array(); $objFormField = \FormFieldModel::findByPk($dc->id); if ($objFormField === null) { return $arrOptions; } $objForm = \FormModel::findByPk($objFormField->pid); if ($objForm === null) { return $arrOptions; } if ($objForm->useSalesforce && $objForm->salesforceAPIConfig && $objForm->salesforceSObject) { try { $objClient = Salesforce::getClient($objForm->salesforceAPIConfig); $arrConfig = $GLOBALS['TL_SOBJECTS'][$objForm->salesforceSObject]; $strClass = $arrConfig['class']; if (class_exists($strClass)) { $objResults = $objClient->describeSObjects(array($strClass::getType())); $arrFields = $objResults[0]->getFields(); foreach ($arrFields as $field) { if (!$field->isCreateable()) { continue; } $arrOptions[$field->getName()] = $field->getName(); } } } catch (\Exception $e) { } } return $arrOptions; }
public function getFormFields(DataContainer $dc) { $arrOptions = array('column' => array("type='stores'")); $objFields = FormFieldModel::findPublishedByPid($dc->id, $arrOptions); if ($objFields) { while ($objFields->next()) { $arrFields[$objFields->name] = $objFields->label; } return $arrFields; } }
/** * Show a hint if a JavaScript library needs to be included in the page layout */ public function showJsLibraryHint($dc) { //if ($_POST || Input::get('act') != 'edit') //return; $objFfm = FormFieldModel::findByPk($dc->id); if ($objFfm === null) { return; } if ($objFfm->type == 'textarea' && $objFfm->fag_enabled) { Message::addInfo(sprintf($GLOBALS['TL_LANG']['tl_content']['includeTemplates'], 'moo_autogrow', 'j_autogrow')); } }
public function getCheckBoxFormFields() { $objFormField = \FormFieldModel::findBy(array('pid=?', 'type=?'), array(\Input::get('id'), 'checkbox')); $arrOptions = array(); if ($objFormField !== null) { while ($objFormField->next()) { if ($objFormField->name) { $arrOptions[] = $objFormField->name; } } } return $arrOptions; }
/** * Get sub form fields. * * @param \DataContainer $dataContainer The data container. * * @return array */ public function getSubformFields($dataContainer) { $options = array(); if ($dataContainer->activeRecord) { $fields = \FormFieldModel::findBy(['pid=?'], $dataContainer->activeRecord->subform, ['order' => 'label']); if ($fields) { foreach ($fields as $field) { $options[$field->id] = $field->label ?: $fields->name; } } } return $options; }
public static function doCreateOptions($formFieldId, $formFieldEventId) { $objEvents = HeimrichHannot\CalendarPlus\CalendarPlusEventsModel::findPublishedSubEvents($formFieldEventId); if ($objEvents !== null) { $options = array(); while ($objEvents->next()) { $options[] = array('label' => $objEvents->shortTitle ? $objEvents->shortTitle : $objEvents->title, 'value' => specialchars($objEvents->id)); } $objFormField = \FormFieldModel::findByPk($formFieldId); $objFormField->event = $formFieldEventId; $objFormField->options = serialize($options); $objFormField->save(); } }
/** * Prepare a set of fields. * * @param \FormFieldModel[] $fields The given set of fields being prepared. * @param \FormFieldModel[] $combinedFields The prepared list of form fields. * @param array $formStack A stack of form ids to protect against recursion. * * @return void */ private function prepareFields($fields, &$combinedFields, $formStack) { foreach ($fields as $field) { if ($field->type === 'subform') { $subformFields = \FormFieldModel::findPublishedByPid($field->subform); if ($subformFields && !in_array($field->subform, $formStack)) { $newStack = $formStack; $newStack[] = $field->subform; $this->prepareFields($subformFields, $combinedFields, $newStack); } continue; } $combinedFields[] = $field; } }
public function processFormData($arrSubmitted, $arrData, $arrFiles, $arrLabels, \Form $objForm) { // check if there is a form field which we use as the code if (($objField = \FormFieldModel::findBy(array("pid = ?", "useCode = '1'"), array($objForm->id))) === null) { return; } // check if there is submitted data for the form field if (!isset($arrSubmitted[$objField->name])) { return; } // get the code $code = $arrSubmitted[$objField->name]; // check if there is a page present for this alias if (($objPage = \PageModel::findPublishedByIdOrAlias($code)) === null) { return; } // redirect to page \Controller::redirect(\Controller::generateFrontendUrl($objPage->row(), null, null, true)); }
/** * Add column set field to the colsetStart content element. * * We need to do it dynamically because subcolumns creates its palette dynamically. * * @param \DataContainer $dataContainer The data container driver. * * @return void * * @SuppressWarnings(PHPMD.Superglobals) */ public function appendColumnsetIdToPalette($dataContainer) { if ($dataContainer->table == 'tl_content') { $model = \ContentModel::findByPK($dataContainer->id); if ($model->sc_type > 0) { \MetaPalettes::appendFields($dataContainer->table, 'colsetStart', 'colset', array('bootstrap_grid')); } } elseif ($dataContainer->table == 'tl_form_field') { $model = \FormFieldModel::findByPk($dataContainer->id); if ($model->fsc_type > 0) { $GLOBALS['TL_DCA']['tl_form_field']['palettes']['formcolstart'] = str_replace('fsc_color,', 'fsc_color,bootstrap_grid,', $GLOBALS['TL_DCA']['tl_form_field']['palettes']['formcolstart']); } } else { $model = \ModuleModel::findByPk($dataContainer->id); if ($model->sc_type > 0) { $GLOBALS['TL_DCA']['tl_module']['palettes']['subcolumns'] = str_replace('sc_type,', 'sc_type,columnset_id,', $GLOBALS['TL_DCA']['tl_module']['palettes']['subcolumns']); } } }
/** * Replace {{option_label::*}} insert tag * * use case: * * {{option_label::ID::value}} * * @param array $arrTag */ private function replaceOptionsLabel($arrTag) { $id = $arrTag[1]; $value = $arrTag[2]; $field = \FormFieldModel::findByPk($id); if (null === $field) { return $value; } $options = deserialize($field->options); if (empty($options) || !is_array($options)) { return $value; } foreach ($options as $option) { if ($value == $option['value']) { return $option['label']; } } return $value; }
/** * Loads the form field models (calling the compileFormFields hook and ignoring itself). */ private function loadFormFieldModels() { $formFieldModels = \FormFieldModel::findPublishedByPid($this->formModel->id); if (null === $formFieldModels) { $formFieldModels = []; } else { $formFieldModels = $formFieldModels->getModels(); } // Needed for the hook $form = $this->createDummyForm(); if (isset($GLOBALS['TL_HOOKS']['compileFormFields']) && is_array($GLOBALS['TL_HOOKS']['compileFormFields'])) { foreach ($GLOBALS['TL_HOOKS']['compileFormFields'] as $k => $callback) { // Do not call ourselves recursively if ('MPForms' === $callback[0]) { continue; } $objCallback = \System::importStatic($callback[0]); $formFieldModels = $objCallback->{$callback[1]}($formFieldModels, $this->getFormId(), $form); } } $this->formFieldModels = $formFieldModels; }
/** * Get the data ready for Salesforce * * @access protected * @param array * @param integer * @return array */ protected static function getSalesforceFields($arrSubmitted, $intFormId) { $arrReturn = array(); $objFormFields = \FormFieldModel::findPublishedByPid($intFormId); if ($objFormFields !== null) { while ($objFormFields->next()) { if ($objFormFields->current()->salesforceField) { $arrReturn[$objFormFields->current()->salesforceField] = $arrSubmitted[$objFormFields->current()->name]; } } } return $arrReturn; }
public function processFormDataHook($arrSubmitted, $arrData, $arrFiles, $arrLabels, $objForm) { $formId = $objForm->formID != '' ? 'auto_' . $objForm->formID : 'auto_form_' . $objForm->id; // Get all form fields $arrFields = array(); $objFields = \FormFieldModel::findPublishedByPid($objForm->id); // default order by sorting $strReturn = null; if ($objFields !== null) { $start = false; while ($objFields->next()) { if ($objFields->successType == 'successStart') { $start = true; } if ($start || !$objForm->hideFormOnSuccess) { $arrFields[] = $objFields->current(); } if ($objFields->successType == 'successStop') { $start = false; // hideFormOnSuccess: do not render other fields than successStart, fields inside and successStop if ($objForm->hideFormOnSuccess) { break; } } } } if (!empty($arrFields) && is_array($arrFields)) { $row = 0; $max_row = count($arrFields); foreach ($arrFields as $objField) { $strClass = $GLOBALS['TL_FFL'][$objField->type]; // Continue if the class is not defined if (!class_exists($strClass)) { continue; } $arrData = $objField->row(); $arrData['decodeEntities'] = true; $arrData['allowHtml'] = $objForm->allowTags; $arrData['rowClass'] = 'row_' . $row . ($row == 0 ? ' row_first' : ($row == $max_row - 1 ? ' row_last' : '')) . ($row % 2 == 0 ? ' even' : ' odd'); $arrData['tableless'] = $objForm->tableless; // Increase the row count if its a password field if ($objField->type == 'password') { ++$row; ++$max_row; $arrData['rowClassConfirm'] = 'row_' . $row . ($row == $max_row - 1 ? ' row_last' : '') . ($row % 2 == 0 ? ' even' : ' odd'); } // Submit buttons do not use the name attribute if ($objField->type == 'submit') { $arrData['name'] = ''; } // Unset the default value depending on the field type (see #4722) if (!empty($arrData['value'])) { if (!in_array('value', trimsplit('[,;]', $GLOBALS['TL_DCA']['tl_form_field']['palettes'][$objField->type]))) { $arrData['value'] = ''; } } $objWidget = new $strClass($arrData); $objWidget->required = $objField->mandatory ? true : false; // HOOK: load form field callback if (isset($GLOBALS['TL_HOOKS']['loadFormField']) && is_array($GLOBALS['TL_HOOKS']['loadFormField'])) { foreach ($GLOBALS['TL_HOOKS']['loadFormField'] as $callback) { $this->import($callback[0]); $objWidget = $this->{$callback}[0]->{$callback}[1]($objWidget, $formId, $arrData, $objForm); } } $strReturn .= $objWidget->parse(); ++$row; } } if ($objForm->isAjaxForm && !is_null($strReturn)) { $strReturn .= '<input type="hidden" name="FORM_SUBMIT" value="' . $formId . '">'; $strReturn .= '<input type="hidden" name="REQUEST_TOKEN" value="' . \RequestToken::get() . '">'; die(\Controller::replaceInsertTags($strReturn)); } }
/** * Store the data of a sub form field. * * @param int $fieldId The field id. * @param int|bool $leadStore The lead store field or setting. * * @return void */ private function storeSubformFieldData($fieldId, $leadStore) { $field = \FormFieldModel::findByPK($fieldId); $data = array(); if ($this->hasLeadMaster()) { $masterField = \FormFieldModel::findByPk($leadStore); if (!$masterField) { return; } $fieldName = $masterField->name; $masterId = $masterField->id; } else { $fieldName = $field->name; $masterId = $field->id; } // Regular data if (isset($this->postData[$field->name])) { $value = \Leads::prepareValue($this->postData[$field->name], $field); $label = \Leads::prepareLabel($value, $field); $data = array('pid' => $this->leadId, 'sorting' => $field->sorting, 'tstamp' => time(), 'master_id' => $masterId, 'field_id' => $field->id, 'name' => $fieldName, 'value' => $value, 'label' => $label); } // Files if (isset($this->files[$field->name]) && $this->files[$field->name]['uploaded']) { $value = \Leads::prepareValue($this->files[$field->name], $field); $label = \Leads::prepareLabel($value, $field); $data = array('pid' => $this->leadId, 'sorting' => $field->sorting, 'tstamp' => time(), 'master_id' => $field->master_id, 'field_id' => $field->id, 'name' => $field->name, 'value' => $value, 'label' => $label); } $this->insertIntoDatabase($field, $data); }
/** * Generate the form * @return string */ protected function compile() { $hasUpload = false; $doNotSubmit = false; $arrSubmitted = array(); $blnAddDateJS = true; $this->loadDataContainer('tl_form_field'); $formId = $this->formID != '' ? 'auto_' . $this->formID : 'auto_form_' . $this->id; $arrUnset = array('FORM_NEXT', 'FORM_BACK'); foreach ($arrUnset as $strKey) { unset($_SESSION['FORM_DATA'][$strKey]); } // Form is used to edit existing formdata record if ($this->objEditRecord instanceof \Database\Result) { $arrEditRecord = $this->objEditRecord->row(); $this->blnEditform = true; $this->import('Formdata'); } // Check if the form is a multipage form $objPaginators = \Database::getInstance()->prepare("SELECT id,pid,invisible,`sorting`,`type`,`name`,`label`,`value`,`imageSubmit`,`singleSRC`,`sLabel`,`efgAddBackButton`,`efgBackStoreSessionValues`,`efgBackSlabel`,`efgBackImageSubmit`,`efgBackSingleSRC` FROM tl_form_field WHERE pid=? AND `type`=?" . (!BE_USER_LOGGED_IN ? " AND invisible=''" : "") . " ORDER BY `sorting`")->execute($this->id, 'efgFormPaginator'); if ($objPaginators->numRows) { $this->blnMultipage = true; $this->intTotalPages = (int) $objPaginators->numRows; while ($objPaginators->next()) { $this->arrPaginators[] = $objPaginators->row(); } } // Use the core class Form if this is not a multi page form and not frontend edit form if (!$this->blnMultipage && !$this->blnEditform || $this->method == 'GET' || TL_MODE == 'BE') { // unset files in session to avoid wrong validation and submission of file uploads // .. files may be stored in session after frontend editing or submission of multi page form $_SESSION['FILES'] = array(); $this->strTemplate = 'form'; return parent::compile(); } global $objPage; if ($objPage->outputFormat == 'html5') { $blnIsHtml5 = true; } $this->blnAllowSkipRequired = false; // allow form submission, if ALL required fields are empty $this->arrWidgetsFailedValidation = array(); // validation result of required widgets $doNotValidate = false; $strMode = ''; $intActivePage = 1; $this->Template = new \FrontendTemplate($this->strTemplate); \System::loadLanguageFile('tl_form'); // render a previous completed page if (strlen($_SESSION['EFP'][$formId]['render_page'])) { $intActivePage = (int) $_SESSION['EFP'][$formId]['render_page']; $this->intActivePage = (int) $_SESSION['EFP'][$formId]['render_page']; $strMode = 'reload'; unset($_SESSION['EFP'][$formId]['render_page']); } elseif (!strlen($_POST['FORM_SUBMIT'])) { unset($_SESSION['EFP'][$formId]['render_page']); unset($_SESSION['EFP'][$formId]['completed']); } if (\Input::post('FORM_SUBMIT') == $formId && (strlen($_POST['FORM_PAGE']) || is_array($_POST['FORM_STEP']))) { $intActivePage = (int) $_POST['FORM_PAGE']; $intGoto = 0; if (strlen($_POST['FORM_BACK']) || strlen($_POST['FORM_BACK_x'])) { $intActivePage = (int) $_POST['FORM_PAGE']; $doNotValidate = true; $strMode = 'back'; } elseif (is_array($_POST['FORM_STEP']) || is_array($_POST['FORM_STEP_x'])) { $intGoto = is_array($_POST['FORM_STEP']) ? key($_POST['FORM_STEP']) : key($_POST['FORM_STEP_x']); if ($intGoto < $intActivePage) { $_SESSION['EFP'][$formId]['render_page'] = $intGoto < 1 ? 1 : $intGoto; \Controller::reload(); } elseif ($intGoto > $intActivePage && $_SESSION['EFP'][$formId]['completed']['page_' . $intGoto]) { $_SESSION['EFP'][$formId]['render_page'] = $intGoto; \Controller::reload(); } } if ($intActivePage < 1) { $intActivePage = 1; } if ($intActivePage > $this->intTotalPages) { $intActivePage = $this->intTotalPages; } $this->intActivePage = $intActivePage; } $this->Template->fields = ''; $this->Template->hidden = ''; $this->Template->formSubmit = $formId; $this->Template->tableless = $this->tableless ? true : false; $this->Template->method = $this->method == 'GET' ? 'get' : 'post'; if ($this->blnMultipage || $this->blnEditform) { $objPageWidget = new \FormHidden(array('name' => 'FORM_PAGE', 'value' => $this->intActivePage)); $this->Template->hidden .= $objPageWidget->parse(); } $this->initializeSession($formId); $arrLabels = array(); // Get all form fields $arrFields = array(); $objFields = \FormFieldModel::findPublishedByPid($this->id); if ($objFields !== null) { while ($objFields->next()) { $arrFields[] = $objFields->current(); } } // HOOK: compile form fields if (isset($GLOBALS['TL_HOOKS']['compileFormFields']) && is_array($GLOBALS['TL_HOOKS']['compileFormFields'])) { foreach ($GLOBALS['TL_HOOKS']['compileFormFields'] as $callback) { $this->import($callback[0]); $arrFields = $this->{$callback}[0]->{$callback}[1]($arrFields, $formId, $this); } } // Process the fields if (!empty($arrFields) && is_array($arrFields)) { $row = 0; $max_row = count($arrFields); foreach ($arrFields as $objField) { // Skip fields outside range of active page if ($this->intTotalPages > 1 && ($this->blnMultipage || $this->blnEditform)) { $intFieldSorting = (int) $objField->sorting; if ($this->intActivePage <= 1 && $intFieldSorting > (int) $this->arrPaginators[$this->intActivePage - 1]['sorting']) { continue; } elseif ($this->intActivePage > 1 && $this->intActivePage < $this->intTotalPages && ($intFieldSorting <= (int) $this->arrPaginators[$this->intActivePage - 2]['sorting'] || $intFieldSorting > (int) $this->arrPaginators[$this->intActivePage - 1]['sorting'])) { continue; } elseif ($this->intActivePage == $this->intTotalPages && $intFieldSorting <= (int) $this->arrPaginators[$this->intActivePage - 2]['sorting']) { continue; } } $strClass = $GLOBALS['TL_FFL'][$objField->type]; // Continue if the class is not defined if (!class_exists($strClass)) { continue; } $arrData = $objField->row(); $arrData['decodeEntities'] = true; $arrData['allowHtml'] = $this->allowTags; $arrData['rowClass'] = 'row_' . $row . ($row == 0 ? ' row_first' : ($row == $max_row - 1 ? ' row_last' : '')) . ($row % 2 == 0 ? ' even' : ' odd'); $arrData['tableless'] = $this->tableless; if ($this->blnMultipage || $this->blnEditform) { $arrData['formMultipage'] = $this->blnMultipage; $arrData['formActivePage'] = $this->intActivePage; $arrData['formTotalPages'] = $this->intTotalPages; } // Increase the row count if it is a password field if ($objField->type == 'password') { ++$row; ++$max_row; $arrData['rowClassConfirm'] = 'row_' . $row . ($row == $max_row - 1 ? ' row_last' : '') . ($row % 2 == 0 ? ' even' : ' odd'); } // Submit buttons do not use the name attribute if ($objField->type == 'submit') { $arrData['name'] = ''; } $objWidget = new $strClass($arrData); $objWidget->required = $objField->mandatory ? true : false; if ($objWidget->required) { $this->arrWidgetsFailedValidation[$objField->name] = 0; } // Unset session values if no FORM_SUBMIT or form page has not been completed // (to avoid wrong validation against session values and to avoid usage of values of other forms): // This behaviour can be deactivated by setting: $GLOBALS['EFP'][$formId]['doNotCleanStoredSessionData'] = true; if ($strMode != 'reload' && strlen($objField->name)) { if (!strlen($_POST['FORM_SUBMIT']) || !$_SESSION['EFP'][$formId]['completed']['page_' . $this->intActivePage]) { if (!$GLOBALS['EFP'][$formId]['doNotCleanStoredSessionData']) { unset($_SESSION['FORM_DATA'][$objField->name]); } if ($objWidget instanceof \uploadable) { unset($_SESSION['FILES'][$objField->name]); } } } // Always populate from existing session data if configured if ($GLOBALS['EFP'][$formId]['doNotCleanStoredSessionData'] == true && isset($_SESSION['FORM_DATA'][$objField->name])) { $objWidget->value = $_SESSION['FORM_DATA'][$objField->name]; } if ($strMode == 'reload' || $this->blnEditform && !strlen($_POST['FORM_BACK']) && !strlen($_POST['FORM_BACK_x'])) { // Frontend editing if ($this->blnEditform && !$_SESSION['EFP'][$formId]['completed']['page_' . $this->intActivePage]) { if (is_array($objWidget->options)) { $arrData['options'] = $objWidget->options; } // Prepare options array $arrData['options'] = $this->Formdata->prepareWidgetOptions($arrData); // Set rgxp 'date' for field type 'calendar' if not set if ($arrData['type'] == 'calendar') { if (!isset($arrData['rgxp'])) { $arrData['rgxp'] = 'date'; } } elseif ($arrData['type'] == 'xdependentcalendarfields') { $arrData['rgxp'] = 'date'; $arrData['dateFormat'] = $arrData['xdateformat']; } if ($objWidget instanceof \uploadable) { unset($_SESSION['FILES'][$objField->name]); } // Prepare value $varFieldValue = $this->Formdata->prepareDatabaseValueForWidget($arrEditRecord[$objField->name], $arrData); $objWidget->value = $varFieldValue; } else { // Populate field if page has been completed if ($_SESSION['EFP'][$formId]['completed']['page_' . $this->intActivePage] == true) { $objWidget->value = $_SESSION['FORM_DATA'][$objField->name]; } else { if ($objWidget instanceof \uploadable) { unset($_SESSION['FILES'][$objField->name]); } } } } // HOOK: Load form field callback if (!strlen($_POST['FORM_BACK']) && !strlen($_POST['FORM_BACK_x'])) { if (isset($GLOBALS['TL_HOOKS']['loadFormField']) && is_array($GLOBALS['TL_HOOKS']['loadFormField'])) { foreach ($GLOBALS['TL_HOOKS']['loadFormField'] as $callback) { $this->import($callback[0]); $objWidget = $this->{$callback}[0]->{$callback}[1]($objWidget, $formId, $this->arrData); } } } // Validate the input if (\Input::post('FORM_SUBMIT') == $formId) { // Populate field if (strlen($_POST['FORM_BACK']) || strlen($_POST['FORM_BACK_x'])) { if ($strMode == 'back' && strlen($this->arrPaginators[$this->intActivePage - 1]['efgBackStoreSessionValues'])) { unset($_SESSION['FORM_DATA'][$objField->name]); $objWidget->value = \Input::post($objField->name); } elseif ($_SESSION['EFP'][$formId]['completed']['page_' . $this->intActivePage] == true) { $objWidget->value = $_SESSION['FORM_DATA'][$objField->name]; unset($_SESSION['FORM_DATA'][$objField->name]); } } if (!$doNotValidate) { if ($objWidget instanceof \uploadable) { // If the widget does not store the file, store it in tmp folder and session to make it available for mails etc. if (!$objWidget->storeFile) { if ($this->intActivePage <= $this->intTotalPages) { // Unset the file in session, if this page has not been completed if (!$_SESSION['EFP'][$formId]['completed']['page_' . $this->intActivePage]) { unset($_SESSION['FILES'][$objField->name]); } $objWidget->validate(); // File has been uploaded, store it in temp folder if (is_uploaded_file($_SESSION['FILES'][$objField->name]['tmp_name'])) { $this->import('Files'); $strDstFile = TL_ROOT . '/system/tmp/' . md5_file($_SESSION['FILES'][$objField->name]['tmp_name']); if (@copy($_SESSION['FILES'][$objField->name]['tmp_name'], $strDstFile)) { $_SESSION['FILES'][$objField->name]['tmp_name'] = $strDstFile; $_SESSION['FILES'][$objField->name]['uploaded'] = true; $this->Files->chmod($strDstFile, 0644); } } } } else { $objWidget->validate(); } } else { $objWidget->validate(); } // HOOK: validate form field callback if (isset($GLOBALS['TL_HOOKS']['validateFormField']) && is_array($GLOBALS['TL_HOOKS']['validateFormField'])) { foreach ($GLOBALS['TL_HOOKS']['validateFormField'] as $callback) { $this->import($callback[0]); $objWidget = $this->{$callback}[0]->{$callback}[1]($objWidget, $formId, $this->arrData); } } } if ($objWidget->hasErrors()) { if ($objWidget->required) { $this->arrWidgetsFailedValidation[$objField->name] = 1; } $doNotSubmit = true; } elseif ($objWidget->submitInput() || $strMode == 'back') { if ($objWidget->submitInput()) { $arrSubmitted[$objField->name] = $objWidget->value; $_SESSION['FORM_DATA'][$objField->name] = $objWidget->value; unset($_POST[$objField->name]); // see #5474 } } } if ($objWidget instanceof \FormHidden) { $this->Template->hidden .= $objWidget->parse(); continue; } if ($objWidget instanceof \uploadable) { $hasUpload = true; if ($this->blnMultipage || $this->blnEditform) { // Save file info in the session in frontend edit mode if ($this->blnEditform && strlen($arrEditRecord[$objField->name]) && (!isset($_SESSION['FILES'][$objField->name]) || empty($_SESSION['FILES'][$objField->name]))) { $objFile = new \File($arrEditRecord[$objField->name]); if ($objFile->size) { $_SESSION['FILES'][$objField->name] = array('name' => $objFile->basename, 'type' => $objFile->mime, 'tmp_name' => TL_ROOT . '/' . $objFile->value, 'size' => $objFile->size, 'uploaded' => true); } } // Add info about uploaded file to upload input if (isset($_SESSION['FILES'][$objField->name]) && $_SESSION['FILES'][$objField->name]['uploaded']) { $this->Template->fields .= preg_replace('/(.*?)(<input.*?>)(.*?)/sim', '$1<p class="upload_info">' . sprintf($GLOBALS['TL_LANG']['MSC']['fileUploaded'], $_SESSION['FILES'][$objField->name]['name']) . '</p>$2$3', $objWidget->parse()); ++$row; continue; } } } if ($objWidget->name != '' && $objWidget->label != '') { $arrLabels[$objWidget->name] = $this->replaceInsertTags($objWidget->label); } $this->Template->fields .= $objWidget->parse(); ++$row; } } if ($doNotSubmit && $this->blnAllowSkipRequired) { if (!empty($this->arrWidgetsFailedValidation) && count(array_count_values($this->arrWidgetsFailedValidation)) == 1) { $doNotSubmit = false; } } // Process form data if (\Input::post('FORM_SUBMIT') == $formId && !$doNotSubmit) { if ($this->intTotalPages == 1 || !$this->blnMultipage && !$this->blnEditform) { $this->processFormData($arrSubmitted, $arrLabels); } else { // If not last page but page is completed, render next page if ($this->intActivePage < $this->intTotalPages && (strlen($_POST['FORM_NEXT']) || strlen($_POST['FORM_NEXT_x']))) { $_SESSION['EFP'][$formId]['render_page'] = (int) $_POST['FORM_PAGE'] + 1; $_SESSION['EFP'][$formId]['completed']['page_' . $_POST['FORM_PAGE']] = true; \Controller::reload(); } elseif ($strMode == 'back' && $this->intActivePage <= $this->intTotalPages && (strlen($_POST['FORM_BACK']) || strlen($_POST['FORM_BACK_x']))) { $_SESSION['EFP'][$formId]['render_page'] = (int) $_POST['FORM_PAGE'] - 1; $_SESSION['EFP'][$formId]['completed']['page_' . $_POST['FORM_PAGE']] = true; \Controller::reload(); } else { if ((int) $_POST['FORM_PAGE'] == $this->intTotalPages && (strlen($_POST['FORM_NEXT']) || strlen($_POST['FORM_NEXT_x']))) { unset($_SESSION['EFP'][$formId]['render_page']); unset($_SESSION['EFP'][$formId]['completed']); unset($_SESSION['FORM_DATA']['FORM_PAGE']); unset($_SESSION['FORM_DATA']['FORM_NEXT']); unset($_SESSION['FORM_DATA']['FORM_BACK']); // Populate arrSubmitted from session $arrSubmitted = $_SESSION['FORM_DATA']; $this->processFormData($arrSubmitted, $arrLabels); } } } } $strAttributes = ''; $arrAttributes = deserialize($this->attributes, true); // Add a css class if ($this->blnMultipage) { $arrAttributes[1] = trim($arrAttributes[1] . ' multipage form_page_' . $this->intActivePage); } if (strlen($arrAttributes[1])) { $strAttributes .= ' class="' . $arrAttributes[1] . '"'; } $this->Template->hasError = $doNotSubmit; $this->Template->attributes = $strAttributes; $this->Template->enctype = $hasUpload ? 'multipart/form-data' : 'application/x-www-form-urlencoded'; $this->Template->formId = strlen($arrAttributes[0]) ? $arrAttributes[0] : 'f' . $this->id; $this->Template->action = \Environment::get('indexFreeRequest'); $this->Template->maxFileSize = $hasUpload ? $this->objModel->getMaxUploadFileSize() : false; $this->Template->novalidate = $this->novalidate ? ' novalidate' : ''; // Get the target URL if ($this->method == 'GET' && $this->jumpTo && ($objTarget = $this->objModel->getRelated('jumpTo')) !== null) { $this->Template->action = $this->generateFrontendUrl($objTarget->row()); } // Add Javascript to handle html5 input attribute 'required' on back button if ($blnIsHtml5) { $this->Template->fields .= ' <script>' . $this->getBackButtonJavascriptString() . ' </script>'; } if ($blnAddDateJS) { $this->Template->fields .= ' <script' . (!$blnIsHtml5 ? ' type="text/javascript"' : '') . '>' . $this->getDateString() . ' </script>'; } return $this->Template->parse(); }
/** * Add form fields from a back end form generator form ID * * @param int $intId The form generator form ID * @param callable $varCallback Called for each field, return true if you want to include the field in the form * * @return $this * @throws \InvalidArgumentException */ public function addFieldsFromFormGenerator($intId, $varCallback = null) { if (($objFields = \FormFieldModel::findPublishedByPid($intId)) === null) { throw new \InvalidArgumentException('Form ID "' . $intId . '" does not exist or has no published fields.'); } $arrFields = array(); while ($objFields->next()) { // make sure "name" is set because not all form fields do need it and it would thus overwrite the array indexes $strName = $objFields->name ?: 'field_' . $objFields->id; $this->checkFormFieldNameIsValid($strName); $arrDca = $objFields->row(); // Make sure it has a "name" attribute because it is mandatory if (!isset($arrDca['name'])) { $arrDca['name'] = $strName; } if (is_callable($varCallback) && !call_user_func_array($varCallback, array(&$strName, &$arrDca))) { continue; } $this->arrFormFields[$strName] = $arrDca; } $this->intState = self::STATE_DIRTY; return $this; }
/** * HTML- Code für die Auswahl des Jahres erzeugen * * @return string HTML- String des select- Feldes */ protected function createFormSelectYear() { $selectJahre = $this->arrYears; $intSelectedYear = $this->intYear; // Array für Option erzeugen // wenn ausgewähltes Jahr, dann 'default' Key erzeugen // damit vom Contao- Widget ein 'selected' erzeugt wird foreach ($selectJahre as $value) { if ($value == $intSelectedYear) { $arrOptions[] = array('value' => $value, 'label' => $value, 'default' => 1); } else { $arrOptions[] = array('value' => $value, 'label' => $value); } } // tl_form abfragen $formModel = \FormModel::findOneByNlsh_ident('formSelectYear'); // wenn nicht vorhanden, dann neu erzeugen und in DB eintragen if ($formModel === NULL) { // tl_form erzeugen $formModel = new \FormModel(); $formModel->nlsh_ident = 'formSelectYear'; $formModel->save(); } // tl_form_field abfragen $formFieldModel = \FormFieldModel::findOneByPid($formModel->id); if ($formFieldModel === NULL) { // tl_formfield erzeugen $formFieldModel = new \FormFieldModel(); $formFieldModel->save(); } // tl_form- Eintrag vervollständigen $formModel->title = $GLOBALS['TL_LANG']['MSC']['nlsh_htmlSelect']['formTitle']; $formModel->alias = $GLOBALS['TL_LANG']['MSC']['nlsh_htmlSelect']['formAlias']; $formModel->jumpTo = $GLOBALS['objPage']->id; $formModel->format = 'raw'; $formModel->method = 'GET'; $formModel->attributes = serialize(array('ausgabejahr', '')); $formModel->tableless = 1; $formModel->save(); $formFieldModel->pid = $formModel->id; $formFieldModel->type = 'select'; $formFieldModel->name = 'Ausgabejahr'; $formFieldModel->label = $GLOBALS['TL_LANG']['MSC']['nlsh_gesamtausgabe']['auswahljahr']; $formFieldModel->options = $arrOptions; $formFieldModel->class = ''; $formFieldModel->onchange = 'this.form.submit()'; $formFieldModel->save(); // HTML für Formulat erzeugen $return = $this->getform($formModel); return $return; }
/** * Get the form postdata * * @param int $formId * * @return array */ protected function getFormPostData($formId) { static $data; if (!is_array($data)) { $data = array(); $fieldModels = \FormFieldModel::findPublishedByPid($formId); if ($fieldModels !== null) { foreach ($fieldModels as $fieldModel) { $data[$fieldModel->name] = \Input::post($fieldModel->name); } } } return $data; }
/** * Generate the module * * @return string */ public function run() { $objTemplate = new \BackendTemplate('be_rsce_convert'); $objTemplate->isActive = $this->isActive(); $objTemplate->action = ampersand(\Environment::get('request')); $objTemplate->indexHeadline = $GLOBALS['TL_LANG']['tl_maintenance']['searchIndex']; // Rebuild the index if (\Input::get('act') === 'rsce_convert') { // Check the request token if (!isset($_GET['rt']) || !\RequestToken::validate(\Input::get('rt'))) { $this->Session->set('INVALID_TOKEN_URL', \Environment::get('request')); $this->redirect('contao/confirm.php'); } $this->import('Database'); $failedElements = array(); $elementsCount = 0; $contentElements = \ContentModel::findBy(array(\ContentModel::getTable() . '.type LIKE ?'), 'rsce_%'); while ($contentElements && $contentElements->next()) { $html = $this->getHtmlFromElement($contentElements); if (!$html) { $failedElements[] = array('content', $contentElements->id, $contentElements->type); } else { $this->createInitialVersion(\ContentModel::getTable(), $contentElements->id); $this->Database->prepare('UPDATE ' . \ContentModel::getTable() . ' SET tstamp = ?, type = \'html\', html = ? WHERE id = ?')->executeUncached(time(), $html, $contentElements->id); $elementsCount++; $this->createNewVersion(\ContentModel::getTable(), $contentElements->id); $this->log('A new version of record "' . \ContentModel::getTable() . '.id=' . $contentElements->id . '" has been created', __METHOD__, TL_GENERAL); } } $moduleElements = \ModuleModel::findBy(array(\ModuleModel::getTable() . '.type LIKE ?'), 'rsce_%'); while ($moduleElements && $moduleElements->next()) { $html = $this->getHtmlFromElement($moduleElements); if (!$html) { $failedElements[] = array('module', $moduleElements->id, $moduleElements->type); } else { $this->createInitialVersion(\ModuleModel::getTable(), $moduleElements->id); $this->Database->prepare('UPDATE ' . \ModuleModel::getTable() . ' SET tstamp = ?, type = \'html\', html = ? WHERE id = ?')->executeUncached(time(), $html, $moduleElements->id); $elementsCount++; $this->createNewVersion(\ModuleModel::getTable(), $moduleElements->id); $this->log('A new version of record "' . \ModuleModel::getTable() . '.id=' . $moduleElements->id . '" has been created', __METHOD__, TL_GENERAL); } } $formElements = \FormFieldModel::findBy(array(\FormFieldModel::getTable() . '.type LIKE ?'), 'rsce_%'); while ($formElements && $formElements->next()) { $html = $this->getHtmlFromElement($formElements); if (!$html) { $failedElements[] = array('form', $formElements->id, $formElements->type); } else { $this->createInitialVersion(\FormFieldModel::getTable(), $formElements->id); $this->Database->prepare('UPDATE ' . \FormFieldModel::getTable() . ' SET tstamp = ?, type = \'html\', html = ? WHERE id = ?')->executeUncached(time(), $html, $formElements->id); $elementsCount++; $this->createNewVersion(\FormFieldModel::getTable(), $formElements->id); $this->log('A new version of record "' . \FormFieldModel::getTable() . '.id=' . $formElements->id . '" has been created', __METHOD__, TL_GENERAL); } } foreach ($failedElements as $element) { $this->log('Failed to convert ' . $element[0] . ' element ID ' . $element[1] . ' (' . $element[2] . ') to a standard HTML element', __METHOD__, TL_ERROR); } $this->log('Converted ' . $elementsCount . ' RockSolid Custom Elements to standard HTML elements', __METHOD__, TL_GENERAL); $objTemplate->elementsCount = $elementsCount; $objTemplate->failedElements = $failedElements; } $this->loadLanguageFile('rocksolid_custom_elements'); return $objTemplate->parse(); }
/** * Generate the form * * @return string */ protected function compile() { $hasUpload = false; $doNotSubmit = false; $arrSubmitted = array(); $this->loadDataContainer('tl_form_field'); $formId = $this->formID != '' ? 'auto_' . $this->formID : 'auto_form_' . $this->id; $this->Template->fields = ''; $this->Template->hidden = ''; $this->Template->formSubmit = $formId; $this->Template->method = $this->method == 'GET' ? 'get' : 'post'; $this->initializeSession($formId); $arrLabels = array(); // Get all form fields $arrFields = array(); $objFields = \FormFieldModel::findPublishedByPid($this->id); if ($objFields !== null) { while ($objFields->next()) { if ($objFields->name != '') { $arrFields[$objFields->name] = $objFields->current(); } else { $arrFields[] = $objFields->current(); } } } // HOOK: compile form fields if (isset($GLOBALS['TL_HOOKS']['compileFormFields']) && is_array($GLOBALS['TL_HOOKS']['compileFormFields'])) { foreach ($GLOBALS['TL_HOOKS']['compileFormFields'] as $callback) { $this->import($callback[0]); $arrFields = $this->{$callback}[0]->{$callback}[1]($arrFields, $formId, $this); } } // Process the fields if (!empty($arrFields) && is_array($arrFields)) { $row = 0; $max_row = count($arrFields); foreach ($arrFields as $objField) { /** @var \FormFieldModel $objField */ $strClass = $GLOBALS['TL_FFL'][$objField->type]; // Continue if the class is not defined if (!class_exists($strClass)) { continue; } $arrData = $objField->row(); $arrData['decodeEntities'] = true; $arrData['allowHtml'] = $this->allowTags; $arrData['rowClass'] = 'row_' . $row . ($row == 0 ? ' row_first' : ($row == $max_row - 1 ? ' row_last' : '')) . ($row % 2 == 0 ? ' even' : ' odd'); // Increase the row count if its a password field if ($objField->type == 'password') { ++$row; ++$max_row; $arrData['rowClassConfirm'] = 'row_' . $row . ($row == $max_row - 1 ? ' row_last' : '') . ($row % 2 == 0 ? ' even' : ' odd'); } // Submit buttons do not use the name attribute if ($objField->type == 'submit') { $arrData['name'] = ''; } // Unset the default value depending on the field type (see #4722) if (!empty($arrData['value'])) { if (!in_array('value', trimsplit('[,;]', $GLOBALS['TL_DCA']['tl_form_field']['palettes'][$objField->type]))) { $arrData['value'] = ''; } } /** @var \Widget $objWidget */ $objWidget = new $strClass($arrData); $objWidget->required = $objField->mandatory ? true : false; // HOOK: load form field callback if (isset($GLOBALS['TL_HOOKS']['loadFormField']) && is_array($GLOBALS['TL_HOOKS']['loadFormField'])) { foreach ($GLOBALS['TL_HOOKS']['loadFormField'] as $callback) { $this->import($callback[0]); $objWidget = $this->{$callback}[0]->{$callback}[1]($objWidget, $formId, $this->arrData, $this); } } // Validate the input if (\Input::post('FORM_SUBMIT') == $formId) { $objWidget->validate(); // HOOK: validate form field callback if (isset($GLOBALS['TL_HOOKS']['validateFormField']) && is_array($GLOBALS['TL_HOOKS']['validateFormField'])) { foreach ($GLOBALS['TL_HOOKS']['validateFormField'] as $callback) { $this->import($callback[0]); $objWidget = $this->{$callback}[0]->{$callback}[1]($objWidget, $formId, $this->arrData, $this); } } if ($objWidget->hasErrors()) { $doNotSubmit = true; } elseif ($objWidget->submitInput()) { $arrSubmitted[$objField->name] = $objWidget->value; $_SESSION['FORM_DATA'][$objField->name] = $objWidget->value; unset($_POST[$objField->name]); // see #5474 } } if ($objWidget instanceof \uploadable) { $hasUpload = true; } if ($objWidget instanceof \FormHidden) { $this->Template->hidden .= $objWidget->parse(); --$max_row; continue; } if ($objWidget->name != '' && $objWidget->label != '') { $arrLabels[$objWidget->name] = $this->replaceInsertTags($objWidget->label); // see #4268 } $this->Template->fields .= $objWidget->parse(); ++$row; } } // Process the form data if (\Input::post('FORM_SUBMIT') == $formId && !$doNotSubmit) { $this->processFormData($arrSubmitted, $arrLabels, $arrFields); } // Add a warning to the page title if ($doNotSubmit && !\Environment::get('isAjaxRequest')) { /** @var \PageModel $objPage */ global $objPage; $title = $objPage->pageTitle ?: $objPage->title; $objPage->pageTitle = $GLOBALS['TL_LANG']['ERR']['form'] . ' - ' . $title; $_SESSION['FILES'] = array(); // see #3007 } $strAttributes = ''; $arrAttributes = deserialize($this->attributes, true); if ($arrAttributes[1] != '') { $strAttributes .= ' class="' . $arrAttributes[1] . '"'; } $this->Template->hasError = $doNotSubmit; $this->Template->attributes = $strAttributes; $this->Template->enctype = $hasUpload ? 'multipart/form-data' : 'application/x-www-form-urlencoded'; $this->Template->formId = $arrAttributes[0] ?: 'f' . $this->id; $this->Template->action = \Environment::get('indexFreeRequest'); $this->Template->maxFileSize = $hasUpload ? $this->objModel->getMaxUploadFileSize() : false; $this->Template->novalidate = $this->novalidate ? ' novalidate' : ''; // Get the target URL if ($this->method == 'GET' && $this->jumpTo && ($objTarget = $this->objModel->getRelated('jumpTo')) !== null) { $this->Template->action = $this->generateFrontendUrl($objTarget->row()); } return $this->Template->parse(); }
private function updateFormFieldEfgLookupOptions() { // Update form data 'table name' in form fields of type EfgFormLookupCheckbox, EfgFormLookupRadio, EfgFormLookupSelectMenu // .. as of version 2.0.0 EFG creates dca file names and form keys based on form alias, // .. like fd_term-paper-submission (former fd_term_paper_submission) $this->import('Formdata'); // Get all form data storing forms $arrStoringForms = $this->Formdata->arrStoringForms; // Get all form fields having efgLookupOptions $arrFormFields = $this->Database->prepare("SELECT id, efgLookupOptions FROM tl_form_field WHERE efgLookupOptions != ''")->execute()->fetchAllAssoc(); if (count($arrFormFields) >= 1 && count($arrStoringForms) >= 1) { $arrMapFormDcaKey = array(); // Build mapping, old form key => new form key foreach ($arrStoringForms as $strFormKey => $arrFormConfig) { $strOldKey = 'fd_' . (!empty($arrFormConfig['formID']) ? $arrFormConfig['formID'] : str_replace('-', '_', standardize($arrFormConfig['title']))); $strNewKey = 'fd_' . (!empty($arrFormConfig['alias']) ? $arrFormConfig['alias'] : str_replace('-', '_', standardize($arrFormConfig['title']))); $arrMapFormDcaKey[$strOldKey] = $strNewKey; } foreach ($arrFormFields as $arrFormField) { $arrLookupOptions = deserialize($arrFormField['efgLookupOptions']); if (!empty($arrLookupOptions['lookup_field'])) { // Replace old form data 'table name' by new form 'table name' if lookup table is form data 'table' if (substr($arrLookupOptions['lookup_field'], 0, 3) == 'fd_') { $arrLookupFieldParams = explode('.', $arrLookupOptions['lookup_field']); if (in_array($arrLookupFieldParams[0], array_keys($arrMapFormDcaKey))) { $arrLookupOptions['lookup_field'] = $arrMapFormDcaKey[$arrLookupFieldParams[0]] . '.' . $arrLookupFieldParams[1]; $objFormFieldModel = \FormFieldModel::findBy('id', $arrFormField['id']); $objFormFieldModel->efgLookupOptions = serialize($arrLookupOptions); $objFormFieldModel->save(); } } } } } }