Esempio n. 1
0
 /**
  * Edit a record
  */
 protected function editSingleRecord()
 {
     /**
      * Prepare URL
      */
     $page_get = 'page_fd' . $this->id;
     $strUrl = preg_replace('/\\?.*$/', '', urldecode(\Environment::get('request')));
     $strUrlParams = '';
     $blnQuery = false;
     foreach (preg_split('/&(amp;)?/', $_SERVER['QUERY_STRING']) as $fragment) {
         if (strlen($fragment)) {
             if (strncasecmp($fragment, $this->strDetailKey, strlen($this->strDetailKey)) !== 0 && strncasecmp($fragment, 'act', 3) !== 0 && strncasecmp($fragment, 'order_by', 8) !== 0 && strncasecmp($fragment, 'sort', 4) !== 0 && strncasecmp($fragment, $page_get, strlen($page_get)) !== 0) {
                 $strUrlParams .= (!$blnQuery ? '' : '&') . $fragment;
                 $blnQuery = true;
             }
         }
     }
     // Check record
     if ($this->intRecordId === null || intval($this->intRecordId) < 1) {
         unset($_GET[$this->strDetailKey]);
         unset($_GET['act']);
         $strRed = preg_replace(array('/\\/' . $this->strDetailKey . '\\/' . \Input::get($this->strDetailKey) . '/i', '/' . $this->strDetailKey . '=' . \Input::get($this->strDetailKey) . '/i'), array('', ''), $strUrl) . (strlen($strUrlParams) ? '?' . $strUrlParams : '');
         \Controller::redirect($strRed);
     }
     // Check Owner and Alias
     $objOwner = \Database::getInstance()->prepare("SELECT fd_member,alias FROM tl_formdata WHERE id=?")->execute($this->intRecordId);
     $varOwner = $objOwner->fetchAssoc();
     // Check access
     if (!empty($this->efg_list_access) && $this->efg_list_access != 'public') {
         if (!in_array(intval($varOwner['fd_member']), $this->arrAllowedOwnerIds)) {
             $strRed = preg_replace(array('/\\/' . $this->strDetailKey . '\\/' . \Input::get($this->strDetailKey) . '/i', '/' . $this->strDetailKey . '=' . \Input::get($this->strDetailKey) . '/i', '/act=edit/i'), array('', '', ''), $strUrl) . (strlen($strUrlParams) ? '?' . $strUrlParams : '');
             \Controller::redirect($strRed);
         }
     }
     // Check edit access
     $blnEditAllowed = false;
     if ($this->efg_fe_edit_access == 'none') {
         $blnEditAllowed = false;
     } elseif ($this->efg_fe_edit_access == 'public') {
         $blnEditAllowed = true;
     } elseif (!empty($this->efg_fe_edit_access)) {
         if (in_array(intval($varOwner['fd_member']), $this->arrAllowedEditOwnerIds)) {
             $blnEditAllowed = true;
         }
     }
     if ($blnEditAllowed == false) {
         $strRed = preg_replace(array('/\\/' . $this->strDetailKey . '\\/' . \Input::get($this->strDetailKey) . '/i', '/' . $this->strDetailKey . '=' . \Input::get($this->strDetailKey) . '/i', '/act=edit/i'), array('', '', ''), $strUrl) . (strlen($strUrlParams) ? '?' . $strUrlParams : '');
         \Controller::redirect($strRed);
     }
     $intListingId = 0;
     if ($this->id) {
         $intListingId = intval($this->id);
     }
     $strForm = '';
     $intFormId = 0;
     // Fallback template
     if (!strlen($this->list_edit_layout)) {
         $this->list_edit_layout = 'edit_fd_default';
     }
     // Get the form
     $objCheckRecord = \Database::getInstance()->prepare("SELECT form FROM tl_formdata WHERE id=?")->limit(1)->execute($this->intRecordId);
     if ($objCheckRecord->numRows == 1) {
         $strForm = $objCheckRecord->form;
     }
     // Get the ContentElement holding the form
     if (strlen($strForm)) {
         $objForm = \FormModel::findOneBy('title', $strForm);
         if ($objForm !== null) {
             $objFormElement = \ContentModel::findOneBy('form', $objForm->id);
         }
     }
     if ($objFormElement === null) {
         $this->log("Could not find a ContentElement containing the form \"" . $strForm . "\"", __METHOD__, 'ERROR');
         $strRed = preg_replace(array('/\\/' . $this->strDetailKey . '\\/' . \Input::get($this->strDetailKey) . '/i', '/' . $this->strDetailKey . '=' . \Input::get($this->strDetailKey) . '/i', '/act=edit/i'), array('', '', ''), $strUrl) . (strlen($strUrlParams) ? '?' . $strUrlParams : '');
         \Controller::redirect($strRed);
     }
     $this->Template = new \FrontendTemplate($this->list_edit_layout);
     $arrRecordFields = array_merge($this->arrBaseFields, $this->arrDetailFields);
     $strQuery = "SELECT ";
     $strWhere = '';
     $strSep = '';
     foreach ($arrRecordFields as $field) {
         if (in_array($field, $this->arrBaseFields)) {
             $strQuery .= $strSep . $field;
             $strSep = ', ';
         }
         if (!empty($this->arrDetailFields) && in_array($field, $this->arrDetailFields)) {
             $strQuery .= $strSep . '(SELECT value FROM tl_formdata_details WHERE ff_name="' . $field . '" AND pid=f.id ) AS `' . $field . '`';
             $strSep = ', ';
         }
     }
     $strQuery .= " FROM " . $this->list_table . " f";
     $strWhere .= (strlen($strWhere) ? " AND " : " WHERE ") . "id=?";
     $strQuery .= $strWhere;
     $objRecord = \Database::getInstance()->prepare($strQuery)->limit(1)->execute($this->intRecordId);
     if ($objRecord->numRows < 1) {
         return;
     }
     $_SESSION['EFP']['LISTING_MOD']['id'] = $intListingId;
     if ($objFormElement !== null) {
         $this->Template->editform = $this->generateEditForm($objFormElement, $objRecord);
     }
 }
Esempio n. 2
0
 public function importFile()
 {
     if (\Input::get('key') != 'import') {
         return '';
     }
     if (null === $this->arrImportIgnoreFields) {
         $this->arrImportIgnoreFields = array('id', 'pid', 'tstamp', 'form', 'ip', 'date', 'confirmationSent', 'confirmationDate', 'import_source');
     }
     if (null === $this->arrImportableFields) {
         $arrFdFields = array_merge($this->arrBaseFields, $this->arrDetailFields);
         $arrFdFields = array_diff($arrFdFields, $this->arrImportIgnoreFields);
         foreach ($arrFdFields as $strFdField) {
             $this->arrImportableFields[$strFdField] = $GLOBALS['TL_DCA']['tl_formdata']['fields'][$strFdField]['label'][0];
         }
     }
     $arrSessionData = $this->Session->get('EFG');
     if (null == $arrSessionData) {
         $arrSessionData = array();
     }
     $this->Session->set('EFG', $arrSessionData);
     // Import CSV
     if ($_POST['FORM_SUBMIT'] == 'tl_formdata_import') {
         $this->loadDataContainer('tl_files');
         $strMode = 'preview';
         $arrSessionData['import'][$this->strFormKey]['separator'] = $_POST['separator'];
         $arrSessionData['import'][$this->strFormKey]['csv_has_header'] = $_POST['csv_has_header'] == '1' ? '1' : '';
         $this->Session->set('EFG', $arrSessionData);
         if (intval(\Input::post('import_source')) == 0) {
             \Message::addError($GLOBALS['TL_LANG']['tl_formdata']['error_select_source']);
             \Controller::reload();
         }
         $objFileModel = \FilesModel::findById(\Input::post('import_source'));
         $objFile = new \File($objFileModel->path, true);
         if ($objFile->extension != 'csv') {
             \Message::addError(sprintf($GLOBALS['TL_LANG']['ERR']['filetype'], $objFile->extension));
             setcookie('BE_PAGE_OFFSET', 0, 0, '/');
             \Controller::reload();
         }
         // Get separator
         switch (\Input::post('separator')) {
             case 'semicolon':
                 $strSeparator = ';';
                 break;
             case 'tabulator':
                 $strSeparator = '\\t';
                 break;
             case 'comma':
             default:
                 $strSeparator = ',';
                 break;
         }
         if ($_POST['FORM_MODE'] == 'import') {
             $strMode = 'import';
             $time = time();
             $intTotal = null;
             $intInvalid = 0;
             $intValid = 0;
             $arrImportCols = \Input::post('import_cols');
             $arrSessionData['import'][$this->strFormKey]['import_cols'] = $arrImportCols;
             $this->Session->set('EFG', $arrSessionData);
             $arrMapFields = array_flip($arrImportCols);
             if (isset($arrMapFields['__IGNORE__'])) {
                 unset($arrMapFields['__IGNORE__']);
             }
             $blnUseCsvHeader = $arrSessionData['import'][$this->strFormKey]['csv_has_header'] == '1' ? true : false;
             $arrEntries = array();
             $resFile = $objFile->handle;
             $timeNow = time();
             $strFormTitle = $this->Formdata->arrFormsDcaKey[substr($this->strFormKey, 3)];
             $strAliasField = strlen($this->Formdata->arrStoringForms[substr($this->strFormKey, 3)]['efgAliasField']) ? $this->Formdata->arrStoringForms[substr($this->strFormKey, 3)]['efgAliasField'] : '';
             $objForm = \FormModel::findOneBy('title', $strFormTitle);
             if ($objForm !== null) {
                 $arrFormFields = $this->Formdata->getFormfieldsAsArray($objForm->id);
             }
             while (($arrRow = @fgetcsv($resFile, null, $strSeparator)) !== false) {
                 if (null === $intTotal) {
                     $intTotal = 0;
                     if ($blnUseCsvHeader) {
                         continue;
                     }
                 }
                 $strAlias = '';
                 if (isset($arrRow[$arrMapFields['alias']]) && strlen($arrRow[$arrMapFields['alias']])) {
                     $strAlias = $arrRow[$arrMapFields['alias']];
                 } elseif (isset($arrRow[$arrMapFields[$strAliasField]]) && strlen($arrRow[$arrMapFields[$strAliasField]])) {
                     \Input::setPost($strAliasField, $arrRow[$arrMapFields[$strAliasField]]);
                 }
                 $arrDetailSets = array();
                 // prepare base data
                 $arrSet = array('tstamp' => $timeNow, 'fd_member' => 0, 'fd_user' => intval($this->User->id), 'form' => $strFormTitle, 'ip' => \Environment::get('ip'), 'date' => $timeNow, 'published' => $GLOBALS['TL_DCA']['tl_formdata']['fields']['published']['default'] == '1' ? '1' : '');
                 foreach ($arrMapFields as $strField => $intCol) {
                     if (in_array($strField, $this->arrImportIgnoreFields)) {
                         continue;
                     }
                     if (in_array($strField, $this->arrBaseFields)) {
                         $arrField = $GLOBALS['TL_DCA']['tl_formdata']['fields'][$strField];
                         if (in_array($strField, $this->arrOwnerFields)) {
                             switch ($strField) {
                                 case 'fd_user':
                                     $array = 'arrUsers';
                                     break;
                                 case 'fd_member':
                                     $array = 'arrMembers';
                                     break;
                                 case 'fd_user_group':
                                     $array = 'arrUserGroups';
                                     break;
                                 case 'fd_member_group':
                                     $array = 'arrMemberGroups';
                                     break;
                             }
                             if (is_numeric($arrRow[$intCol]) && array_key_exists($arrRow[$intCol], $this->{$array})) {
                                 $varValue = $arrRow[$intCol];
                             } elseif (is_string($arrRow[$intCol])) {
                                 $varValue = intval(array_search($arrRow[$intCol], $this->{$array}));
                             }
                         } elseif ($strField == 'published') {
                             if ($arrRow[$intCol] == $arrField['label'][0] || intval($arrRow[$intCol]) == 1) {
                                 $varValue = '1';
                             } else {
                                 $varValue = '';
                             }
                         } elseif ($strField == 'alias') {
                             continue;
                         } else {
                             $varValue = $arrRow[$intCol];
                         }
                         $arrSet[$strField] = $varValue;
                     }
                 }
                 // prepare details data
                 foreach ($arrMapFields as $strField => $intCol) {
                     if (in_array($strField, $this->arrImportIgnoreFields)) {
                         continue;
                     }
                     if (in_array($strField, $this->arrDetailFields)) {
                         // $arrField = array_merge($arrFormFields[$strField], $GLOBALS['TL_DCA']['tl_formdata']['fields'][$strField]);
                         $arrField = $GLOBALS['TL_DCA']['tl_formdata']['fields'][$strField];
                         $varValue = $this->Formdata->prepareImportValueForDatabase($arrRow[$intCol], $arrField);
                         // prepare details data
                         $arrDetailSet = array('sorting' => $arrFormFields[$strField]['sorting'], 'tstamp' => $timeNow, 'ff_id' => $arrField['ff_id'], 'ff_name' => $strField, 'value' => $varValue);
                         $arrDetailSets[] = $arrDetailSet;
                     }
                 }
                 $intNewId = 0;
                 $blnSaved = true;
                 if (!empty($arrDetailSets)) {
                     $objNewFormdata = \Database::getInstance()->prepare("INSERT INTO tl_formdata %s")->set($arrSet)->execute();
                     $intNewId = $objNewFormdata->insertId;
                     $strAlias = $this->Formdata->generateAlias($strAlias, $this->strFormFilterValue, $intNewId);
                     if (strlen($strAlias)) {
                         \Database::getInstance()->prepare("UPDATE tl_formdata %s WHERE id=?")->set(array('alias' => $strAlias))->execute($intNewId);
                     }
                     foreach ($arrDetailSets as $kD => $arrDetailSet) {
                         $arrDetailSet['pid'] = $intNewId;
                         try {
                             $objNewFormdataDetails = \Database::getInstance()->prepare("INSERT INTO tl_formdata_details %s")->set($arrDetailSet)->execute();
                         } catch (\Exception $ee) {
                             $blnSaved = false;
                         }
                     }
                     if ($blnSaved === false && $intNewId > 0) {
                         \Database::getInstance()->prepare("DELETE FROM tl_formdata WHERE id=?")->execute($intNewId);
                     }
                 } else {
                     $blnSaved = false;
                 }
                 if ($blnSaved) {
                     $intValid++;
                 } else {
                     $intInvalid++;
                 }
                 $intTotal++;
             }
             \Message::addConfirmation(sprintf($GLOBALS['TL_LANG']['tl_formdata']['import_confirm'], $intValid));
             if ($intInvalid > 0) {
                 \Message::addInfo(sprintf($GLOBALS['TL_LANG']['tl_formdata']['import_invalid'], $intInvalid));
             }
             // Add a log entry
             $this->log('Imported file "' . $objFile->filename . '" into form data "' . $strFormTitle . '", created ' . $intValid . ' new records', __METHOD__, TL_GENERAL);
             setcookie('BE_PAGE_OFFSET', 0, 0, '/');
             \Controller::reload();
         }
         // Generate preview and form to select import fields
         if ($strMode == 'preview') {
             return $this->formImportPreview($objFile, $strSeparator);
         }
     }
     return $this->formImportSource();
 }