Esempio n. 1
0
 public function createattributecsvAction()
 {
     $csvString = '';
     // prepare the header
     $fishBaseAttr = array(Fish::COL_SAMPLE_CODE);
     $imageBaseAttr = array(Image::COL_ORIGINAL_FILENAME, Image::COL_RATIO_EXTERNAL);
     $meta = new Default_MetaData();
     $attribRowset = array_merge($fishBaseAttr, $meta->getAttributesBasic('FISH'), $imageBaseAttr, $meta->getAttributesBasic('IMAGE'));
     /*handle last item differently
      * credit:grobemo
      * 24-Apr-2009 08:13
      * http://de3.php.net/manual/en/control-structures.foreach.php
      */
     $last_item = end($attribRowset);
     foreach ($attribRowset as $attr) {
         if ($attr == $last_item) {
             if (is_array($attr) && array_key_exists(AttributeDescriptor::COL_NAME, $attr)) {
                 $csvString .= $attr[AttributeDescriptor::COL_NAME];
             } else {
                 $csvString .= $attr;
             }
         } else {
             if (is_array($attr) && array_key_exists(AttributeDescriptor::COL_NAME, $attr)) {
                 $csvString .= $attr[AttributeDescriptor::COL_NAME] . ',';
             } else {
                 $csvString .= $attr . ',';
             }
         }
     }
     $csvString .= "\n";
     $this->view->csvString = $csvString;
     // generate the download file
     Zend_Layout::resetMvcInstance();
     $this->render('csvstring');
 }
Esempio n. 2
0
 /**
  * grouping function
  * checks the CSV file and prepares the datasets for import, and gives detailled arrays back for single steps for further processing and report
  * 1. checks the CSV file column against uploaded files
  * 2. checks the header
  * 3. checks the columns with value list entries and changes from strings to ids
  * 4. splits and checks the datasets against fish and image form
  * @param $key the subdirectory for csv and image files
  * @param $systemAttributes INPUT from flex assigning, elements order is 1:1 to csvHeadings
  * @param $csvHeadings INPUT from flex assigning, elements order is 1:1 to systemAttributes
  * @param $filenames filenames of files to upload, including import.csv
  * @return unknown_type
  */
 public function checkCsv($key, $systemAttributes, $csvHeadings, $filenames)
 {
     $this->key = $key;
     $this->logger = new Ble422_ArrayLogger(self::RELATIVE_PATH_IMPORT_LOGS . $this->key . '_import_log.txt');
     try {
         $parser = new Ble422_CsvParser(self::RELATIVE_PATH_UPLOAD_CACHE . $this->key . '/import.csv', TRUE, ',', NULL, TRUE);
     } catch (Exception $e) {
         return array('returnCode' => 'initial error', 'message' => $e->getMessage);
     }
     //print_r($this->parser->getHeadings());
     $headings = $parser->getHeadings();
     if (count($headings) <= 1) {
         return array('returnCode' => 'initial error', 'message' => 'Error: zero or only one heading found, wrong delitimer?');
     }
     $datasets = array();
     $datasets = $parser->get(0);
     if (empty($datasets)) {
         return array('returnCode' => 'initial error', 'message' => 'Error: no dataset found');
     }
     if (empty($filenames)) {
         return array('returnCode' => 'initial error', 'message' => 'Error: no filenames found');
     }
     //echo $this->datasets[0]['Über2'];
     //echo $this->datasets[1]['Über3'];
     if (count($systemAttributes) >= 1 && count($csvHeadings) >= 1) {
         $useManualAssociation = TRUE;
         if (count($systemAttributes) != count($csvHeadings)) {
             return array('returnCode' => 'initial error', 'message' => 'Error: number of system attributes and CSV file headings not equal');
         }
         //delete the additional information from the attribute string
         foreach ($systemAttributes as &$attr) {
             $pos = strrpos($attr, '#');
             if ($pos === false) {
                 // not found
             } else {
                 //- 1 because space sign
                 $attr = substr($attr, 0, $pos - 1);
             }
         }
         //$headingsArray = $this->createHeadingsArray();
         $datasets = $this->getDatasetsWithNewHeadings($datasets, $systemAttributes, $csvHeadings);
         $headings = $systemAttributes;
     } else {
         $useManualAssociation = FALSE;
     }
     //additional array (keys without spaces) for xml XXX has no recursive handling --------------
     $datasetsForXml = array();
     foreach ($datasets as $rowNo => $dataset) {
         $newDataset = array();
         foreach ($dataset as $keyName => $value) {
             $newKeyName = str_replace(' ', '_', $keyName);
             $newDataset += array($newKeyName => $value);
         }
         $datasetsForXml[$rowNo] = $newDataset;
     }
     //----------------------------------------------------------
     $this->logger->log(array('user' => AuthQuery::getUserName()));
     $this->logger->log(array('originalInput' => $datasets));
     //XXX handle association array added info
     try {
         if (!$useManualAssociation) {
             $fileNamesCsv = $this->extractColumn($datasets, Image::COL_ORIGINAL_FILENAME);
         } else {
             $fileNamesCsv = $this->extractColumn($datasets, Image::COL_ORIGINAL_FILENAME);
         }
     } catch (OutOfBoundsException $e) {
         return array('returnCode' => 'initial error', 'message' => "Error: column not found\n" . $e->getMessage);
     }
     try {
         $this->extractColumn($datasets, Fish::COL_SAMPLE_CODE);
     } catch (OutOfBoundsException $e) {
         return array('returnCode' => 'initial error', 'message' => "Error: column not found\n" . $e->getMessage);
     }
     $resultsCheckFileCsvAssoc = $this->checkFileCsvAssoc($fileNamesCsv, $filenames);
     $this->logger->log($resultsCheckFileCsvAssoc, 'resultsCheckFileCsvAssoc');
     $this->rowHasUniqueUploadedFile = $resultsCheckFileCsvAssoc['rowHasUniqueUploadedFile'];
     $fishBaseAttr = array(Fish::COL_SAMPLE_CODE);
     $imageBaseAttr = array(Image::COL_ORIGINAL_FILENAME, Image::COL_RATIO_EXTERNAL);
     $meta = new Default_MetaData();
     $attribRowset = array_merge($fishBaseAttr, $meta->getAttributesBasic('FISH'), $imageBaseAttr, $meta->getAttributesBasic('IMAGE'));
     $resultsCheckHeader = $this->checkHeader($headings, $attribRowset);
     $this->logger->log($resultsCheckHeader, 'resultsCheckHeader');
     //print_r($resultsCheckHeader);
     $this->matchingCols = $resultsCheckHeader['matchingColumns'];
     //get matching columns which have value list attributes
     $matchingColsWithValuelist = $resultsCheckHeader['matchingColumns'];
     foreach ($matchingColsWithValuelist as $key => $match) {
         if (is_array($match)) {
             //meta data
             if ($match[AttributeDescriptor::COL_VALUE_LIST] == 0) {
                 unset($matchingColsWithValuelist[$key]);
             }
         } else {
             //base date
             unset($matchingColsWithValuelist[$key]);
         }
     }
     $attribRowsetWithValuelist = array_merge($meta->getAttributesAndValuelist('FISH'), $meta->getAttributesAndValuelist('IMAGE'));
     $resultsCheckValuelistCells = $this->checkValuelistCells($datasets, $matchingColsWithValuelist, $attribRowsetWithValuelist);
     $this->logger->log($resultsCheckValuelistCells, 'resultsCheckValuelistCells');
     //		//additional array (keys without spaces) for xml --------------
     //		//copy the array
     //		$resultsCheckValuelistCellsForXml = array();
     //		foreach ($resultsCheckValuelistCells as $key => $val) {
     //			$resultsCheckValuelistCellsForXml[$key] = $val;
     //		}
     //		//replace the subarray 'modifiedDatasets'
     //				foreach ($resultsCheckValuelistCells['modifiedDatasets'] as $rowNo => $attrValPair) {
     //					$newDataset = array();
     //					foreach ($attrValPair as $keyName => $value) {
     //						$newKeyName = str_replace(' ', '_', $keyName);
     //						$newDataset += array($newKeyName => $value);
     //					}
     //					$resultsCheckValuelistCellsForXml['modifiedDatasets'][$rowNo] = $newDataset;
     //				}
     //
     //		//----------------------------------------------------------
     $modifiedDatasets = $resultsCheckValuelistCells['modifiedDatasets'];
     //function gets matching cols from member variable
     $resultsValidateAgainstForms = $this->validateAgainstForms($modifiedDatasets);
     $this->logger->log($resultsValidateAgainstForms, 'resultsValidateAgainstForms');
     //set member array preparedDatasets = all successful validated datasets
     $validRows = $resultsValidateAgainstForms['validRows'];
     $preparedDatasets = array();
     foreach ($validRows as $rowNo => $dataset) {
         $preparedDatasets[$rowNo]['fishFormDataset'] = $resultsValidateAgainstForms['fishFormDatasets'][$rowNo];
         $preparedDatasets[$rowNo]['imageFormDataset'] = $resultsValidateAgainstForms['imageFormDatasets'][$rowNo];
         //XXX append original filename out of original datasets
         //$preparedDatasets[$rowNo][Image::COL_ORIGINAL_FILENAME] = $datasets[$rowNo][Image::COL_ORIGINAL_FILENAME];
     }
     $this->preparedDatasets = $preparedDatasets;
     $this->logger->log($this->preparedDatasets, 'preparedDatasets');
     $resultsCheckDatasetsAgainstDatabase = $this->checkDatasetsAgainstDatabase($datasets);
     $this->logger->log($resultsCheckDatasetsAgainstDatabase, 'resultsCheckDatasetsAgainstDatabase');
     //aggregate returnCodes
     $returnCode = '';
     if ($resultsCheckFileCsvAssoc['returnCode'] == 'warning' || $resultsCheckHeader['returnCode'] == 'warning' || $resultsCheckValuelistCells['returnCode'] == 'warning' || $resultsValidateAgainstForms['returnCode'] == 'warning' || $resultsCheckDatasetsAgainstDatabase['returnCode'] == 'warning') {
         $returnCode = 'warning';
     }
     if ($resultsCheckFileCsvAssoc['returnCode'] == 'error' || $resultsCheckHeader['returnCode'] == 'error' || $resultsCheckValuelistCells['returnCode'] == 'error' || $resultsValidateAgainstForms['returnCode'] == 'error' || $resultsCheckDatasetsAgainstDatabase['returnCode'] == 'error') {
         $returnCode = 'error';
     }
     if ($returnCode == '') {
         $returnCode = 'success';
     }
     $resultsCheckFileCsvAssocForXml = $this->reduceCheckResultForXml($resultsCheckFileCsvAssoc);
     $resultsCheckHeaderForXml = $this->reduceCheckResultForXml($resultsCheckHeader);
     $resultsCheckValuelistCellsForXml = $this->reduceCheckResultForXml($resultsCheckValuelistCells);
     $resultsValidateAgainstFormsForXml = $this->reduceCheckResultForXml($resultsValidateAgainstForms);
     $resultsCheckDatasetsAgainstDatabaseForXml = $this->reduceCheckResultForXml($resultsCheckDatasetsAgainstDatabase);
     //		//give upload process the valid unique filenames
     //		$resultsCheckFileCsvAssocForXml['rowHasUniqueUploadedFile'] = $this->rowHasUniqueUploadedFile;
     $result = array('returnCode' => $returnCode, 'originalInput' => $datasetsForXml, 'resultsCheckFileCsvAssoc' => $resultsCheckFileCsvAssocForXml, 'resultsCheckHeader' => $resultsCheckHeaderForXml, 'resultsCheckValuelistCells' => $resultsCheckValuelistCellsForXml, 'resultsValidateAgainstForms' => $resultsValidateAgainstFormsForXml, 'resultsCheckDatasetsAgainstDatabase' => $resultsCheckDatasetsAgainstDatabaseForXml);
     //		print_r($result);
     //		print_r($this->preparedDatasets);
     $this->saveToNamespace();
     return $result;
 }