public function testCsvFileParsing()
 {
     // + Subtest 1: Unicode CSV file with header row.
     // --------------------------------------------------------------------------------
     $path = dirname(__FILE__) . self::CSV_FILE_USERS_HEADER_UNICODE;
     $csvFile = new tao_helpers_data_CsvFile();
     $csvFile->load($path);
     // - test column mapping.
     $expectedMapping = array('label', 'First Name', 'Last Name', 'Login', 'Mail', 'password', 'UserUILg');
     $this->assertEquals($expectedMapping, $csvFile->getColumnMapping(), 'The column mapping should be ' . var_export($expectedMapping, true) . '.');
     $this->assertEquals($csvFile->count(), 16, 'The CSV file contains ' . $csvFile->count() . ' rows instead of 16.');
     $this->assertEquals($csvFile->getColumnCount(), 7, 'The CSV file contains ' . $csvFile->getColumnCount() . ' columns instead of 7.');
     // - test some row retrievals.
     $expectedRow = array('TAO Jérôme Bogaerts', 'Jérôme', 'Bogaerts', 'jbogaerts', '*****@*****.**', 'jbogaerts', 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN');
     $this->assertEquals($csvFile->getRow(0), $expectedRow);
     $expectedRow = array('label' => 'TAO Isabelle Jars', 'First Name' => 'Isabelle', 'Last Name' => 'Jars', 'Login' => 'ijars', 'Mail' => '*****@*****.**', 'password' => 'ijars', 'UserUILg' => 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN');
     $this->assertEquals($csvFile->getRow(4, true), $expectedRow);
     // + Subtest 2: Unicode CSV file withouth header row.
     // --------------------------------------------------------------------------------
     $path = dirname(__FILE__) . self::CSV_FILE_USERS_NO_HEADER_UNICODE;
     $csvFile = new tao_helpers_data_CsvFile($options = array('first_row_column_names' => false));
     $csvFile->load($path);
     // - test column mapping.
     $expectedMapping = array();
     $this->assertEquals($expectedMapping, $csvFile->getColumnMapping(), 'The column mapping should be ' . var_export($expectedMapping, true) . '.');
     $this->assertEquals($csvFile->count(), 16, 'The CSV file contains ' . $csvFile->count() . ' rows instead of 16.');
     $this->assertEquals($csvFile->getColumnCount(), 7, 'The CSV file contains ' . $csvFile->getColumnCount() . ' columns instead of 7.');
     // - test some row retrievals.
     $expectedRow = array('TAO Jérôme Bogaerts', 'Jérôme', 'Bogaerts', 'jbogaerts', '*****@*****.**', 'jbogaerts', 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN');
     $this->assertEquals($csvFile->getRow(0), $expectedRow);
     $expectedRow = array('TAO Matteo Mellis', 'Matteo', 'Mellis', 'mmellis', '*****@*****.**', 'mmellis', 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN');
     $this->assertEquals($csvFile->getRow(15), $expectedRow);
 }
 /**
  * Constructs the Import form container
  * In need of a major refactoring, which will
  * probably involve refactoring the Form engine as well
  */
 private function createImportFormContainer()
 {
     $sourceContainer = new tao_models_classes_import_CsvUploadForm();
     $sourceForm = $sourceContainer->getForm();
     foreach ($sourceForm->getElements() as $element) {
         $element->feed();
     }
     if (isset($_POST['importFile'])) {
         $file = $_POST['importFile'];
     } else {
         $sourceForm->getElement('source')->feed();
         $fileInfo = $sourceForm->getValue('source');
         $file = $fileInfo['uploaded_file'];
     }
     $properties = array(tao_helpers_Uri::encode(RDFS_LABEL) => __('Label'));
     $rangedProperties = array();
     $classUri = \tao_helpers_Uri::decode($_POST['classUri']);
     $class = new core_kernel_classes_Class($classUri);
     $classProperties = $this->getClassProperties($class);
     foreach ($classProperties as $property) {
         if (!in_array($property->getUri(), $this->getExludedProperties())) {
             //@todo manage the properties with range
             $range = $property->getRange();
             $properties[tao_helpers_Uri::encode($property->getUri())] = $property->getLabel();
             if ($range instanceof core_kernel_classes_Resource && $range->getUri() != RDFS_LITERAL) {
                 $rangedProperties[tao_helpers_Uri::encode($property->getUri())] = $property->getLabel();
             }
         }
     }
     //load the csv data from the file (uploaded in the upload form) to get the columns
     $csv_data = new tao_helpers_data_CsvFile($sourceForm->getValues());
     $csv_data->load($file);
     $values = $sourceForm->getValues();
     $values[tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES] = !empty($values[tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES]);
     $values['importFile'] = $file;
     $myFormContainer = new tao_models_classes_import_CSVMappingForm($values, array('class_properties' => $properties, 'ranged_properties' => $rangedProperties, 'csv_column' => $this->getColumnMapping($csv_data, $sourceForm->getValue(tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES)), tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES => $sourceForm->getValue(tao_helpers_data_CsvFile::FIRST_ROW_COLUMN_NAMES)));
     return $myFormContainer;
 }
 /**
  * enable you to load the data in the csvFile to an associative array
  * the options
  *
  * @access public
  * @author Jerome Bogaerts, <*****@*****.**>
  * @param  string $csvFile
  * @return tao_helpers_data_CsvFile
  */
 public function load($csvFile)
 {
     $returnValue = null;
     $csv = new tao_helpers_data_CsvFile($this->options);
     $csv->load($csvFile);
     $this->loadedFile = $csv;
     $returnValue = $this->loadedFile;
     return $returnValue;
 }
Exemple #4
0
 public function testCSVFileParsingWithMultipleFieldValues()
 {
     $path = dirname(__FILE__) . self::CSV_FILE_USERS_HEADER_UNICODE_WITH_MULTI_FIELD_VALUE;
     // Without multi values
     //---------------------------------------------------------------------------------
     $csvFile = new tao_helpers_data_CsvFile();
     $csvFile->load($path);
     // - test column mapping.
     $expectedMapping = array('label', 'First Name', 'Last Name', 'Login', 'Mail', 'password', 'UserUILg');
     $this->assertEquals($expectedMapping, $csvFile->getColumnMapping(), 'The column mapping should be ' . var_export($expectedMapping, true) . '.');
     $this->assertEquals($csvFile->count(), 16, 'The CSV file contains ' . $csvFile->count() . ' rows instead of 16.');
     $this->assertEquals($csvFile->getColumnCount(), 7, 'The CSV file contains ' . $csvFile->getColumnCount() . ' columns instead of 7.');
     // - test some row retrievals.
     $expectedRow = array('TAO Jérôme Bogaerts', 'Jérôme', 'Bogaerts', 'jbogaerts', 'jerome.bogaerts@tudor.lu|jerome@example.com', 'jbogaerts', 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN');
     $this->assertEquals($csvFile->getRow(0), $expectedRow);
     $expectedRow = array('label' => 'TAO Isabelle Jars', 'First Name' => 'Isabelle', 'Last Name' => 'Jars', 'Login' => 'ijars', 'Mail' => '*****@*****.**', 'password' => 'ijars', 'UserUILg' => 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN');
     $this->assertEquals($csvFile->getRow(4, true), $expectedRow);
     $expectedRow = array('label' => 'TAO Igor Ribassin', 'First Name' => 'Igor', 'Last Name' => 'Ribassin', 'Login' => 'iribassin', 'Mail' => '*****@*****.**', 'password' => 'iribassin', 'UserUILg' => 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN|http://www.tao.lu/Ontologies/TAO.rdf#LangAR|http://www.tao.lu/Ontologies/TAO.rdf#LangDE');
     $this->assertEquals($csvFile->getRow(13, true), $expectedRow);
     // With multi values
     //---------------------------------------------------------------------------------
     $csvFile = new tao_helpers_data_CsvFile(['multi_values_delimiter' => '|']);
     $csvFile->load($path);
     // - test column mapping.
     $expectedMapping = array('label', 'First Name', 'Last Name', 'Login', 'Mail', 'password', 'UserUILg');
     $this->assertEquals($expectedMapping, $csvFile->getColumnMapping(), 'The column mapping should be ' . var_export($expectedMapping, true) . '.');
     $this->assertEquals($csvFile->count(), 16, 'The CSV file contains ' . $csvFile->count() . ' rows instead of 16.');
     $this->assertEquals($csvFile->getColumnCount(), 7, 'The CSV file contains ' . $csvFile->getColumnCount() . ' columns instead of 7.');
     // - test some row retrievals.
     $expectedRow = array('TAO Jérôme Bogaerts', 'Jérôme', 'Bogaerts', 'jbogaerts', ['*****@*****.**', '*****@*****.**'], 'jbogaerts', 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN');
     $this->assertEquals($csvFile->getRow(0), $expectedRow);
     $expectedRow = array('label' => 'TAO Isabelle Jars', 'First Name' => 'Isabelle', 'Last Name' => 'Jars', 'Login' => 'ijars', 'Mail' => '*****@*****.**', 'password' => 'ijars', 'UserUILg' => 'http://www.tao.lu/Ontologies/TAO.rdf#LangEN');
     $this->assertEquals($csvFile->getRow(4, true), $expectedRow);
     $expectedRow = array('label' => 'TAO Igor Ribassin', 'First Name' => 'Igor', 'Last Name' => 'Ribassin', 'Login' => 'iribassin', 'Mail' => '*****@*****.**', 'password' => 'iribassin', 'UserUILg' => ['http://www.tao.lu/Ontologies/TAO.rdf#LangEN', 'http://www.tao.lu/Ontologies/TAO.rdf#LangAR', 'http://www.tao.lu/Ontologies/TAO.rdf#LangDE']);
     $this->assertEquals($csvFile->getRow(13, true), $expectedRow);
 }
 /**
  * @param \tao_helpers_data_CsvFile $csv_data
  * @param boolean $firstRowAsColumnName
  * @return array
  */
 protected function getColumnMapping($csv_data, $firstRowAsColumnName = true)
 {
     //build the mapping form
     if (!$csv_data->count()) {
         return array();
     }
     // 'class properties' contains an associative array(str:'propertyUri' => 'str:propertyLabel') describing properties belonging to the target class.
     // 'ranged properties' contains an associative array(str:'propertyUri' => 'str:propertyLabel')  describing properties belonging to the target class and that have a range.
     // 'csv_column' contains an array(int:columnIndex => 'str:columnLabel') that will be used to create the selection of possible CSV column to map in views.
     // 'csv_column' might have NULL values for 'str:columnLabel' meaning that there was no header row with column names in the CSV file.
     // Format the column mapping option for the form.
     if ($firstRowAsColumnName && null != $csv_data->getColumnMapping()) {
         // set the column label for each entry.
         // $csvColMapping = array('label', 'comment', ...)
         return $csv_data->getColumnMapping();
     } else {
         // set an empty value for each entry of the array
         // to describe that column names are unknown.
         // $csvColMapping = array(null, null, ...)
         return array_fill(0, $csv_data->getColumnCount(), null);
     }
 }
Exemple #6
0
 public function testGetColumnMapping()
 {
     $file = dirname(__FILE__) . self::CSV_FILE_USERS_HEADER_UNICODE;
     $importer = new CsvBasicImporter();
     $class = new ReflectionClass('oat\\tao\\model\\import\\CsvBasicImporter');
     $method = $class->getMethod('getColumnMapping');
     $method->setAccessible(true);
     $csv_data = new \tao_helpers_data_CsvFile();
     $csv_data->load($file);
     $map = $method->invokeArgs($importer, array($csv_data, false));
     $this->assertEquals(array_fill(0, 7, null), $map);
     $expectedHeader = array('label', 'First Name', 'Last Name', 'Login', 'Mail', 'password', 'UserUILg');
     $map = $method->invokeArgs($importer, array($csv_data, true));
     $this->assertEquals($expectedHeader, $map);
 }
Exemple #7
0
 public function getDataSample($file, $options = array(), $size = 5, $associative = true)
 {
     $csv_data = new \tao_helpers_data_CsvFile($options);
     $csv_data->load($file);
     $count = min($size, $csv_data->count());
     $data = array();
     for ($i = 0; $i < $count; $i++) {
         $data[] = $csv_data->getRow($i, $associative);
     }
     return $data;
 }