Example #1
0
 /**
  * Short description of method initCSVElements
  *
  * @access protected
  * @author Bertrand Chevrier, <*****@*****.**>
  * @return mixed
  */
 protected function initCSVElements()
 {
     $adapter = new tao_helpers_data_GenerisAdapterCsv();
     $options = $adapter->getOptions();
     //create import options form
     foreach ($options as $optName => $optValue) {
         is_bool($optValue) ? $eltType = 'Checkbox' : ($eltType = 'Textbox');
         $optElt = tao_helpers_form_FormFactory::getElement($optName, $eltType);
         $optElt->setDescription(tao_helpers_Display::textCleaner($optName, ' '));
         $optElt->setValue(addslashes($optValue));
         $optElt->addAttribute("size", $optName == 'column_order' ? 40 : 6);
         if (is_null($optValue) || $optName == 'line_break') {
             $optElt->addAttribute("disabled", "true");
         }
         $optElt->setValue($optValue);
         if ($eltType == 'Checkbox') {
             $optElt->setOptions(array($optName => ''));
             $optElt->setValue($optName);
         }
         if (!preg_match("/column/", strtolower($optName))) {
             $optElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty'));
         }
         $this->form->addElement($optElt);
     }
     $this->form->createGroup('options', __('CSV Options'), array_keys($options));
     $descElt = tao_helpers_form_FormFactory::getElement('csv_desc', 'Label');
     $descElt->setValue(__("Please upload a CSV file formated as \"defined\" %min by %max the options above."));
     $this->form->addElement($descElt);
     //create file upload form box
     $fileElt = tao_helpers_form_FormFactory::getElement('source', 'AsyncFile');
     $fileElt->setDescription(__("Add the source file"));
     if (isset($_POST['import_sent_csv'])) {
         $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty'));
     } else {
         $fileElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty', array('message' => '')));
     }
     $fileElt->addValidators(array(tao_helpers_form_FormFactory::getValidator('FileMimeType', array('mimetype' => array('text/plain', 'text/csv', 'text/comma-separated-values', 'application/csv', 'application/csv-tab-delimited-table'), 'extension' => array('csv', 'txt'))), tao_helpers_form_FormFactory::getValidator('FileSize', array('max' => self::UPLOAD_MAX))));
     $this->form->addElement($fileElt);
     $this->form->createGroup('file', __('Upload CSV File'), array('csv_desc', 'source'));
     $csvSentElt = tao_helpers_form_FormFactory::getElement('import_sent_csv', 'Hidden');
     $csvSentElt->setValue(1);
     $this->form->addElement($csvSentElt);
 }
 /**
  * (non-PHPdoc)
  * @see tao_models_classes_import_ImportHandler::import()
  */
 public function import($class, $form)
 {
     // Clean "csv_select" values from form view.
     // Transform any "csv_select" in "csv_null" in order to
     // have the same importation behaviour for both because
     // semantics are the same.
     $map = $form->getValues('property_mapping');
     $newMap = array();
     foreach ($map as $k => $m) {
         if ($m !== 'csv_select') {
             $newMap[$k] = $map[$k];
         } else {
             $newMap[$k] = 'csv_null';
         }
         $newMap[$k] = str_replace(self::OPTION_POSTFIX, '', $newMap[$k]);
         common_Logger::d('map: ' . $k . ' => ' . $newMap[$k]);
     }
     $options = $form->getValues();
     $options['map'] = $newMap;
     $staticMap = array();
     foreach ($form->getValues('ranged_property') as $propUri => $value) {
         if (strpos($propUri, tao_models_classes_import_CSVMappingForm::DEFAULT_VALUES_SUFFIX) !== false) {
             $cleanUri = str_replace(tao_models_classes_import_CSVMappingForm::DEFAULT_VALUES_SUFFIX, '', $propUri);
             $staticMap[$cleanUri] = $value;
         }
     }
     $options['staticMap'] = array_merge($staticMap, $this->getStaticData());
     $options = array_merge($options, $this->getAdditionAdapterOptions());
     $adapter = new tao_helpers_data_GenerisAdapterCsv($options);
     $adapter->setValidators($this->getValidators());
     //import it!
     $report = $adapter->import($form->getValue('importFile'), $class);
     if ($report->getType() == common_report_Report::TYPE_SUCCESS) {
         @unlink($form->getValue('importFile'));
     }
     return $report;
 }
Example #3
0
 /**
  * @param \core_kernel_classes_Class $class where data will be imported
  * @param array $options contains parameters under key => value format
  *	file => required
  *	map => required
  *	callbacks => optional
  *	field_delimiter => optional
  *  field_encloser => optional
  *  first_row_column_names => optional
  *  multi_values_delimiter => optional
  *  onResourceImported => optional
  *	staticMap => optional
  * @return \common_report_Report
  */
 public function importFile($class, $options)
 {
     if (!isset($options['staticMap']) || !is_array($options['staticMap'])) {
         $options['staticMap'] = $this->getStaticData();
     } else {
         $options['staticMap'] = array_merge($options['staticMap'], $this->getStaticData());
     }
     $options = array_merge($options, $this->getAdditionAdapterOptions());
     // Check if we have a proper UTF-8 file.
     if (@preg_match('//u', file_get_contents($options['file'])) === false) {
         return new \common_report_Report(\common_report_Report::TYPE_ERROR, __("The imported file is not properly UTF-8 encoded."));
     }
     $adapter = new \tao_helpers_data_GenerisAdapterCsv($options);
     $adapter->setValidators($this->getValidators());
     //import it!
     $report = $adapter->import($options['file'], $class);
     if ($report->getType() == \common_report_Report::TYPE_SUCCESS) {
         @unlink($options['file']);
         $report->setData($adapter->getOptions());
     }
     return $report;
 }