public function deleteAction() { //delete fish //delete metadata fish done by DB $request = $this->getRequest(); $fishId = intval($this->getRequest()->getParam(Fish::COL_ID)); $fish = new Fish(); $rowset = $fish->find($fishId); if (count($rowset) == 1) { //note: delete of metadata is executed from db $fish->delete($fish->getAdapter()->quoteInto(Fish::COL_ID . ' = ?', $fishId)); } $redirect = new Zend_Controller_Action_Helper_Redirector(); $redirect->setGoto('search', 'search', 'fish'); }
/** * * @param $key the subdirectory for csv and image files * @return unknown_type importImages/importFishes arrays with the IDs as key to see where dataimport eventually went wrong */ public function beginImport($key) { $this->key = $key; $this->logger = new Ble422_ArrayLogger(self::RELATIVE_PATH_IMPORT_LOGS . $this->key . '_import_log.txt'); $this->loadFromNamespace(); // foreach ($this->rowHasUniqueUploadedFile as $key => $value) { $preparedDatasets[$key] = $this->preparedDatasets[$key]; } // $importedFishes = array(); $importedImages = array(); $i = 0; $j = 0; $fishForm = new Fish_Form_Edit(); $fish = new Fish(); $imageForm = new Image_Form_Edit(); $image = new Image(); $numberCopiedFiles = 0; $dbAdapter = $fish->getAdapter(); $dbAdapter->beginTransaction(); try { //changed to local preparedDatasets to filter datasets with uploaded files used in other datasets of import foreach ($preparedDatasets as $rowNo => $dataset) { if (Default_SimpleQuery::isValueInTableColumn($dataset['fishFormDataset'][Fish::COL_SAMPLE_CODE], $fish, Fish::COL_SAMPLE_CODE, 'string')) { //get id from already existing fish dataset and go on if ($values = Default_SimpleQuery::getValuesFromTableColumnWhere($fish, Fish::COL_ID, Fish::COL_SAMPLE_CODE, $dataset['fishFormDataset'][Fish::COL_SAMPLE_CODE], 'string')) { //test for amount of values, must be 1 if (count($values) == 1) { $fishId = $values[0]; } else { throw new Zend_Exception('Error: more/less than 1 id found for fish sample code'); } } } else { //create new fish dataset $fishBaseData = array(Fish::COL_SAMPLE_CODE => $dataset['fishFormDataset'][Fish::COL_SAMPLE_CODE], Fish::COL_USER_ID => $this->userId); $fishMetaData = $dataset['fishFormDataset']; //unset($fishMetaData[Fish::COL_SAMPLE_CODE]); $fishForm->populate($fishMetaData); //insert fish $fishId = $fish->insert($fishBaseData); //now update fish with fish meta data $fish->updateFishAndMetadata($fishForm, $fishId, $fishBaseData); $importedFishes[$i][Fish::COL_ID] = $fishId; $importedFishes[$i]['sourceCsvRow'] = $rowNo; $i++; } // check wether a image shall be imported ------------------------ $qu = new Default_ReferenceQuery(); $filenames = $qu->getImageNames($dataset['fishFormDataset'][Fish::COL_SAMPLE_CODE]); //case non-sensitive foreach ($filenames as &$file) { $file = strtolower($file); } $importImageFile = false; if (!empty($filenames)) { //case non-sensitive if (in_array(strtolower($dataset['imageFormDataset'][Image::COL_ORIGINAL_FILENAME]), $filenames) != FALSE) { // Image is already in database $importImageFile = false; } else { $importImageFile = true; } } else { $importImageFile = true; } // end check ------------------------------------------------------ // import the images if ($importImageFile) { //copy file to new path and rename $completeSource = self::RELATIVE_PATH_UPLOAD_CACHE . $this->key . '/' . $dataset['imageFormDataset'][Image::COL_ORIGINAL_FILENAME]; $creator_guid = new Ble422_Guid(); $guid = $creator_guid->__toString(); $path_parts = pathinfo($completeSource); $originalFileName = $path_parts['basename']; //used later to create Image dataset $newFileNameWithGuid = $guid . '.' . strtolower($path_parts['extension']); //save extension in lower-case, needed for further processing in flex //relative path with new filename, prefix dot&slash required $completeDestination = './' . Image::RELATIVE_UPLOAD_PATH . '/' . $newFileNameWithGuid; if (!copy($completeSource, $completeDestination)) { throw new Zend_Exception("Error: copy fails, source: {$fileName}, destination: {$completeDestination}"); } $ratio = $dataset['imageFormDataset'][Image::COL_RATIO_EXTERNAL]; $this->logger->log(array('received original file' => $completeSource)); $this->logger->log(array('copied uploaded file' => $completeDestination)); $numberCopiedFiles++; //TODO write protect files //create other image files $tn_ratio = $image->processImage($completeDestination); //case sensibility: original file name is saved like spelled in CSV file $imageId = $image->insertImageDataset($completeDestination, $originalFileName, $fishId, $guid, $this->userId, $ratio, $tn_ratio); //create image base and meta datasets $imageMetaData = $dataset['imageFormDataset']; $imageForm->populate($imageMetaData); //$imageId = $image->insert($imageBaseData); $image->updateImageAndMetadata($imageForm, $imageId); $importedImages[$j]['sourceCsvRow'] = $rowNo; $importedImages[$j][Image::COL_ID] = $imageId; $importedImages[$j][Image::COL_FISH_ID] = $fishId; $importedImages[$j][Image::COL_ORIGINAL_FILENAME] = $originalFileName; $importedImages[$j]['completeDestination'] = $completeDestination; $relativePathAndFileNameWorkingCopy = './' . Image::RELATIVE_PATH_IMAGE_SHRINKED_WORKING_COPIES . '/' . $guid . '.jpg'; $relativePathAndFileNameThumbnail = './' . Image::RELATIVE_PATH_IMAGE_THUMBNAILS . '/' . $guid . '.jpg'; $importedImages[$j]['completeWorkingCopy'] = $relativePathAndFileNameWorkingCopy; $importedImages[$j]['completeThumbnail'] = $relativePathAndFileNameThumbnail; $j++; } } $dbAdapter->commit(); $this->logger->log(array('datasets committed, number of copied files into system' => $numberCopiedFiles)); $returnCode = 'success'; //delete upload cache dir Ble422_FileHelper::delete_directory(self::RELATIVE_PATH_UPLOAD_CACHE . $this->key); } catch (Exception $e) { $returnCode = 'error'; $dbAdapter->rollBack(); //delete copied/created files, else there will be orphaned files foreach ($importedImages as $image) { unlink($image['completeDestination']); unlink($image['completeWorkingCopy']); unlink($image['completeThumbnail']); } $this->logger->log(array('Error exception' => $e->getMessage()), 'ERROR, roll back of imported datasets and unsetting of files, see below'); $this->logger->log(array('Roll back of importedFishes' => $importedFishes)); $this->logger->log(array('Roll back of importedImages' => $importedImages)); //delete upload cache dir Ble422_FileHelper::delete_directory(self::RELATIVE_PATH_UPLOAD_CACHE . $this->key); echo $e->getMessage(); } $returnArray = array('returnCode' => $returnCode, 'importFishes' => $importedFishes, 'importImages' => $importedImages, 'numberCopiedFiles' => $numberCopiedFiles); $this->logger->log(array('data import' => $returnArray)); return $returnArray; }