public function createAction()
 {
     $expId = intval($this->getRequest()->getParam(Expertise::COL_ID));
     $keyId = intval($this->getRequest()->getParam(KeyTable::COL_ID));
     $expTable = new Expertise();
     $expRow = $expTable->find($expId);
     $keyTable = new KeyTable();
     $keyRow = $keyTable->find($keyId);
     if ($keyRow->count() != 0 || $expRow->count() != 0) {
         $keyArray = $keyRow->toArray();
         $expArray = $expRow->toArray();
         // create CE row
         $ceTable = new CalibrationExercise();
         $ceName = $expArray[0][Expertise::COL_SPECIES] . ' / ' . AuthQuery::getUserName();
         //TODO \r in der Datenbank
         $ceDescription = 'Area: ' . $expArray[0][Expertise::COL_AREA] . '\\r' . 'Subject: ' . $expArray[0][Expertise::COL_SUBJECT] . '\\r' . 'KeyName: ' . $keyArray[0][KeyTable::COL_NAME];
         $ceData = array(CalibrationExercise::COL_NAME => $ceName, CalibrationExercise::COL_DESCRIPTION => $ceDescription, CalibrationExercise::COL_KEY_TABLE_ID => $keyArray[0][KeyTable::COL_ID], CalibrationExercise::COL_EXPERTISE_ID => $expArray[0][Expertise::COL_ID], CalibrationExercise::COL_COMPAREABLE => 1, CalibrationExercise::COL_IS_STOPPED => 0, CalibrationExercise::COL_TRAINING => 1);
         $ceId = $ceTable->insert($ceData);
         // create participant row
         $partTable = new Participant();
         $partData = array(Participant::COL_CE_ID => $ceId, Participant::COL_USER_ID => AuthQuery::getUserId(), Participant::COL_NUMBER => 1);
         $partId = $partTable->insert($partData);
         // add all possible shown attributes
         $dbAdapter = $ceTable->getAdapter();
         $selectAttr = $dbAdapter->select();
         $selectAttr->from(AttributeDescriptor::TABLE_NAME);
         $selectAttr->orWhere(AttributeDescriptor::COL_GROUP . '=?', 'fish');
         $selectAttr->orWhere(AttributeDescriptor::COL_GROUP . '=?', 'image');
         $attrArray = $dbAdapter->fetchAll($selectAttr);
         $ceHasAttrTable = new CeHasAttributeDescriptor();
         foreach ($attrArray as $attr) {
             $attrData = array(CeHasAttributeDescriptor::COL_ATDE_ID => $attr[AttributeDescriptor::COL_ID], CeHasAttributeDescriptor::COL_CAEX_ID => $ceId);
             $ceHasAttrTable->insert($attrData);
         }
         //get images for exp/key
         $refQuery = new Default_ReferenceQuery();
         $images = $refQuery->getImages($expId, $keyId);
         // create imageset
         //			$selectImages = $dbAdapter->select();
         //			$imagesArray = $dbAdapter->fetchAll($selectImages);
         $imageSetTable = new CeHasImage();
         foreach ($images as $image) {
             $data = array(CeHasImage::COL_IMAGE_ID => $image, CeHasImage::COL_CALIBRATION_EXERCISE_ID => $ceId);
             $imageSetTable->insert($data);
         }
         //$this->render('form');
         $Redirect = new Zend_Controller_Action_Helper_Redirector();
         $Redirect->setGotoSimple('index', 'make', 'annotation', array(CalibrationExercise::COL_ID => $ceId));
     } else {
         throw new Zend_Controller_Exception('Error at craeting a new training Calibration Exercise.', 505);
     }
 }
Exemple #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;
 }