*/ class tao_models_classes_import_CsvUploadForm extends tao_helpers_form_FormContainer { const IS_OPTION_FIRST_COLUMN_ENABLE = 'enable_option_first_columns'; // --- ASSOCIATIONS --- // --- ATTRIBUTES --- // --- OPERATIONS --- /** * Short description of method initForm * * @access public * @author Joel Bout, * @return mixed */ public function initForm() { $this->form = new tao_helpers_form_xhtml_Form('export'); $submitElt = tao_helpers_form_FormFactory::getElement('import', 'Free'); $submitElt->setValue(""); $this->form->setActions([$submitElt], 'bottom'); $this->form->setActions([], 'top'); } /** * overriden * * @access public * @author Joel Bout, * @return mixed */ public function initElements() { //create file upload form box $fileElt = tao_helpers_form_FormFactory::getElement('source', 'AsyncFile'); $fileElt->setDescription(__("Add a CSV file")); if (isset($_POST['import_sent_csv'])) { $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); } else { $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty', ['message' => ''])); } $fileElt->addValidators([ tao_helpers_form_FormFactory::getValidator( 'FileMimeType', [ 'mimetype' => [ 'text/plain', 'text/csv', 'text/comma-separated-values', 'text/anytext', 'application/csv', 'application/txt', 'application/csv-tab-delimited-table', 'application/excel', 'application/vnd.ms-excel', 'application/vnd.msexcel', ], 'extension' => ['csv', 'txt'] ] ), tao_helpers_form_FormFactory::getValidator( 'FileSize', ['max' => SystemHelper::getFileUploadLimit()] ) ]); $this->form->addElement($fileElt); $this->form->createGroup('file', __('Import Metadata from CSV file'), ['source']); $csvSentElt = tao_helpers_form_FormFactory::getElement('import_sent_csv', 'Hidden'); $csvSentElt->setValue(1); $this->form->addElement($csvSentElt); // options $optDelimiter = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::FIELD_DELIMITER, 'Textbox'); $optDelimiter->setDescription(__("Field delimiter")); $optDelimiter->setValue(';'); $optDelimiter->addAttribute("size", 6); $optDelimiter->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); $this->form->addElement($optDelimiter); $optEncloser = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::FIELD_ENCLOSER, 'Textbox'); $optEncloser->setDescription(__("Field encloser")); $optEncloser->setValue('"'); $optEncloser->addAttribute("size", 6); $optEncloser->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); $this->form->addElement($optEncloser); /* $optMulti = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::MULTI_VALUES_DELIMITER, 'Textbox'); $optMulti->setDescription(__("Multiple values delimiter")); $optMulti->setValue('|'); $optMulti->addAttribute("size", 6); $optMulti->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); $this->form->addElement($optMulti); */ if (isset($this->options[static::IS_OPTION_FIRST_COLUMN_ENABLE])) { if ($this->options[static::IS_OPTION_FIRST_COLUMN_ENABLE] === true) { $optFirstColumn = $this->addFirstColumnElement(); } } else { //backwards compatible $optFirstColumn = $this->addFirstColumnElement(); } $opts = [$optDelimiter, $optEncloser]; if (isset($optFirstColumn)) { $opts[] = $optFirstColumn; } $this->form->createGroup('options', __('CSV Options'), $opts); } /** * @return tao_helpers_form_FormElement * @throws Exception * @throws common_Exception */ protected function addFirstColumnElement() { $optFirstColumn = tao_helpers_form_FormFactory::getElement(tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES, 'Checkbox'); $optFirstColumn->setDescription(__("First row column names")); $optFirstColumn->setOptions([tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES => '']); $optFirstColumn->setValue(tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES); $this->form->addElement($optFirstColumn); return $optFirstColumn; } }