/** * Insert the collection into the database * */ public function import() { $collectionMetadata = $this->collectionMetadata(); $elementTexts = $this->elementTexts(); if ($this->record && $this->record->exists()) { $collectionMetadata['overwriteElementTexts'] = true; update_collection($this->record, $collectionMetadata, $elementTexts); } else { try { $this->record = insert_collection($collectionMetadata, $elementTexts); } catch (Exception $e) { _log($e); } $this->addOmekaApiImportRecordIdMap(); } }
public function testCanUpdateEmptyCollection() { $oldCollection = insert_collection(); $this->assertInstanceOf('Collection', $oldCollection); $this->assertTrue($oldCollection->exists()); $this->assertEquals(0, $oldCollection->public); $this->assertEquals(0, $oldCollection->featured); $elementTexts = $oldCollection->getAllElementTexts(); $this->assertCount(0, $elementTexts); $titleText = 'foo'; $descriptionTextA = 'bar'; $descriptionTextB = 'soap'; $isHtml = true; $isPublic = true; $isFeatured = true; $metadata = array('public' => $isPublic, 'featured' => $isFeatured); $elementTexts = array('Dublin Core' => array('Title' => array(array('text' => $titleText, 'html' => $isHtml)), 'Description' => array(array('text' => $descriptionTextA, 'html' => $isHtml), array('text' => $descriptionTextB, 'html' => $isHtml)))); $updatedCollection = update_collection($oldCollection, $metadata, $elementTexts); $this->assertInstanceOf('Collection', $oldCollection); $this->assertTrue($updatedCollection->exists()); $this->assertEquals($isPublic ? 1 : 0, $oldCollection->public); $this->assertEquals($isFeatured ? 1 : 0, $oldCollection->featured); $titleElementTexts = $oldCollection->getElementTexts('Dublin Core', 'Title'); $this->assertCount(1, $titleElementTexts); $titleElementText = $titleElementTexts[0]; $this->assertEquals($titleText, $titleElementText->text); $this->assertEquals($isHtml ? 1 : 0, $titleElementText->html); $descriptionElementTexts = $oldCollection->getElementTexts('Dublin Core', 'Description'); $this->assertCount(2, $descriptionElementTexts); $descriptionElementTextA = $descriptionElementTexts[0]; $this->assertEquals($descriptionTextA, $descriptionElementTextA->text); $this->assertEquals($isHtml ? 1 : 0, $descriptionElementTextA->html); $descriptionElementTextB = $descriptionElementTexts[1]; $this->assertEquals($descriptionTextB, $descriptionElementTextB->text); $this->assertEquals($isHtml ? 1 : 0, $descriptionElementTextB->html); }
// Update the picture information if (!isset($_REQUEST['cancel'])) { $allow_comments = isset($_REQUEST['allow_comments']) ? $_REQUEST['allow_comments'] : ''; $action_result = update_picture($_REQUEST['pid'], $_REQUEST['caption'], $allow_comments, $_REQUEST['description']); } break; case 'update-album': // Update the album information if (!isset($_REQUEST['cancel'])) { $action_result = update_album($_POST['pid'], $_POST['name'], $_POST['description'], $_POST['thumbnail_id']); } break; case 'update-collection': // Update the collection information if (!isset($_REQUEST['cancel'])) { $action_result = update_collection($_POST['pid'], $_POST['name'], $_POST['description'], $_POST['thumbnail_id']); } break; case 'update-comment': // Update the comment information if (!isset($_REQUEST['cancel'])) { $action_result = update_comment($_POST['pid'], $_POST['author'], $_POST['email'], $_POST['url'], $_POST['comment']); } break; case 'add-collection': // Add a new collection $action_result = add_collection($_POST['name'], $_POST['description']); break; case 'add-album': // Add a new album $action_result = add_album($_POST['name'], $_POST['description'], $_POST['parent_collection']);
/** * Adds metadata and extra data to an existing record. * * @param Record $record An existing and checked record object. * @param string $action Allowed actions are "Update", "Add" and "Replace". * * @return Record|boolean * The updated record or false if metadata can't be updated. */ protected function _updateRecord($record, $action = CsvImport_ColumnMap_Action::DEFAULT_ACTION) { $map =& $this->_currentMap; // Check action. if (!in_array($action, array(CsvImport_ColumnMap_Action::ACTION_UPDATE, CsvImport_ColumnMap_Action::ACTION_ADD, CsvImport_ColumnMap_Action::ACTION_REPLACE))) { return false; } $recordType = get_class($record); // Builder doesn't allow action "Update", only add and replace, and // doesn't manage file directly. // Prepare element texts. $elementTexts = $map[CsvImport_ColumnMap::TYPE_ELEMENT]; // Trim metadata to avoid spaces. $elementTexts = $this->_trimElementTexts($elementTexts); // Keep only non empty fields to avoid removing them to allow update. if ($action == CsvImport_ColumnMap_Action::ACTION_ADD || $action == CsvImport_ColumnMap_Action::ACTION_REPLACE) { $elementTexts = array_values(array_filter($elementTexts, 'self::_removeEmptyElement')); } // Overwrite existing element text values if wanted. if ($action == CsvImport_ColumnMap_Action::ACTION_UPDATE || $action == CsvImport_ColumnMap_Action::ACTION_REPLACE) { foreach ($elementTexts as $key => $content) { if ($content['element_id']) { $record->deleteElementTextsbyElementId((array) $content['element_id']); } } } // To reset keys is needed to avoid bug when there is no DC Title. $elementTexts = array_values($elementTexts); // Update the specific metadata and the element texts. // Update is different for each record type. switch ($recordType) { case 'Item': $recordMetadata = $this->_getItemMetadataFromMappedRow(); // Create collection if needed. $collectionId = $this->_getMappedValue(CsvImport_ColumnMap::TYPE_COLLECTION); if (!empty($collectionId) && empty($recordMetadata[Builder_Item::COLLECTION_ID])) { $collection = $this->_createRecordFromIdentifier($collectionId, 'Collection', $this->_defaultValues['IdentifierField']); if ($collection) { $recordMetadata[Builder_Item::COLLECTION_ID] = $collection->id; } } // Update specific data of the item. switch ($action) { case CsvImport_ColumnMap_Action::ACTION_UPDATE: if (empty($recordMetadata[Builder_Item::ITEM_TYPE_ID]) || empty($recordMetadata[Builder_Item::ITEM_TYPE_NAME])) { // TODO Currently, item type cannot be reset. // $recordMetadata[Builder_Item::ITEM_TYPE_ID] = null; unset($recordMetadata[Builder_Item::ITEM_TYPE_ID]); unset($recordMetadata[Builder_Item::ITEM_TYPE_NAME]); } break; case CsvImport_ColumnMap_Action::ACTION_ADD: case CsvImport_ColumnMap_Action::ACTION_REPLACE: if (empty($recordMetadata[Builder_Item::COLLECTION_ID])) { $recordMetadata[Builder_Item::COLLECTION_ID] = $record->collection_id; } if (empty($recordMetadata[Builder_Item::ITEM_TYPE_ID])) { $recordMetadata[Builder_Item::ITEM_TYPE_ID] = $record->item_type_id; } if (empty($recordMetadata[Builder_Item::ITEM_TYPE_NAME])) { if (!empty($record->item_type_id)) { $recordMetadata[Builder_Item::ITEM_TYPE_ID] = $record->item_type_id; } unset($recordMetadata[Builder_Item::ITEM_TYPE_NAME]); } break; } if (empty($recordMetadata[Builder_Item::TAGS])) { unset($recordMetadata[Builder_Item::TAGS]); } $record = update_item($record, $recordMetadata, $elementTexts); break; case 'File': $record->addElementTextsByArray($elementTexts); $record->save(); break; case 'Collection': $recordMetadata = $this->_getCollectionMetadataFromMappedRow(); $record = update_collection($record, $recordMetadata, $elementTexts); break; default: return false; } // Update the extra metadata. $extraData = $map[CsvImport_ColumnMap::TYPE_EXTRA_DATA]; $this->_setExtraData($record, $extraData, $action); if ($recordType == 'Item') { $fileUrls = $map[CsvImport_ColumnMap::TYPE_FILE]; // Check errors for files. $result = $this->_updateAttachedFilesOfItem($record, $fileUrls, false, $action); if (!$result) { return false; } } return $record; }
/** * Update metadata and extra data to an existing record. * * @param Record $record An existing and checked record object. * @param array $document A normalized document. * @return Record|boolean The updated record or false if error. */ protected function _updateRecord($record, $document) { // Check action. $action = $document['process']['action']; if (!in_array($action, array(ArchiveFolder_Importer::ACTION_UPDATE, ArchiveFolder_Importer::ACTION_ADD, ArchiveFolder_Importer::ACTION_REPLACE))) { $message = __('Only update actions are allowed here, not "%s".', $action); throw new ArchiveFolder_ImporterException($message); } $recordType = get_class($record); if ($document['process']['record type'] != $recordType) { $message = __('The record type "%s" is not the same than the record to update ("%s").', $document['process']['record type'], $recordType); throw new ArchiveFolder_ImporterException($message); } // The Builder doesn't allow action "Update", only "Add" and "Replace", // and it doesn't manage file directly. // Prepare element texts. $elementTexts = $document['metadata']; // Trim metadata to avoid spaces. $elementTexts = $this->_trimElementTexts($elementTexts); // Keep only the non empty metadata to avoid removing them with Omeka // methods, and to allow update. if ($action == ArchiveFolder_Importer::ACTION_ADD || $action == ArchiveFolder_Importer::ACTION_REPLACE) { $elementTexts = $this->_removeEmptyElements($elementTexts); } // Overwrite existing element text values if wanted. if ($action == ArchiveFolder_Importer::ACTION_UPDATE || $action == ArchiveFolder_Importer::ACTION_REPLACE) { $elementsToDelete = array(); foreach ($elementTexts as $elementSetName => $elements) { foreach ($elements as $elementName => $elementTexts) { $element = $this->_getElementFromIdentifierField($elementSetName . ':' . $elementName); if ($element) { $elementsToDelete[] = $element->id; } } } if ($elementsToDelete) { $record->deleteElementTextsbyElementId($elementsToDelete); } } // Update the specific metadata and the element texts. // Update is different for each record type. switch ($recordType) { case 'Item': // Check and create collection if needed. // TODO Add an option to create or not a default collection. $collection = null; if (!empty($document['specific']['collection'])) { $collection = $this->_createCollectionFromIdentifier($document['specific']['collection']); $document['specific'][Builder_Item::COLLECTION_ID] = $collection->id; unset($document['specific']['collection']); } // Update specific data of the item. switch ($action) { case ArchiveFolder_Importer::ACTION_UPDATE: // The item type is cleaned: only the name is available, // if any. if (empty($document['specific'][Builder_Item::ITEM_TYPE_NAME])) { // TODO Currently, item type cannot be reset. // $recordMetadata[Builder_Item::ITEM_TYPE_NAME] = null; unset($document['specific'][Builder_Item::ITEM_TYPE_NAME]); } break; case ArchiveFolder_Importer::ACTION_ADD: case ArchiveFolder_Importer::ACTION_REPLACE: if (empty($document['specific'][Builder_Item::COLLECTION_ID])) { $document['specific'][Builder_Item::COLLECTION_ID] = $record->collection_id; } if (empty($document['specific'][Builder_Item::ITEM_TYPE_NAME])) { if (!empty($record->item_type_id)) { $document['specific'][Builder_Item::ITEM_TYPE_ID] = $record->item_type_id; } unset($document['specific'][Builder_Item::ITEM_TYPE_NAME]); } break; } if (empty($document['specific'][Builder_Item::TAGS])) { unset($document['specific'][Builder_Item::TAGS]); } $record = update_item($record, $document['specific'], $document['metadata']); break; case 'File': $record->addElementTextsByArray($document['metadata']); $record->save(); break; case 'Collection': $record = update_collection($record, $document['specific'], $document['metadata']); break; default: $message = __('Record type "%s" is not allowed.', $recordType); throw new ArchiveFolder_ImporterException($message); } // Update the extra metadata. $this->_setExtraData($record, $document['extra'], $action); $this->_archiveRecord($record, $document['process']['index'], $document['process']['name']); return $record; }
/** * Create a new collection from a simple raw title. * * @param string $title * @return Collection */ private function _createCollectionFromTitle($title) { $collection = new Collection(); $collection->save(); update_collection($collection, array(), array('Dublin Core' => array('Title' => array(array('text' => $title, 'html' => false))))); return $collection; }
set_option('archive_repertory_collection_folder', 'none'); // Allow to manage upgrade from 2.6 and 2.8. $prefix = get_option('archive_repertory_collection_prefix'); $prefix = $prefix ? $prefix : get_option('archive_repertory_collection_identifier_prefix'); // Allow to upgrade from old releases. $collectionNames = get_option('archive_repertory_collection_names'); $collectionNames = $collectionNames ? $collectionNames : get_option('archive_repertory_collection_folders'); $collectionNames = $collectionNames ? $collectionNames : get_option('archive_repertory_collection_string_folders'); $collectionNames = $collectionNames ? unserialize($collectionNames) : array(); $collections = get_records('Collection', array(), 0); foreach ($collections as $collection) { if (!empty($collectionNames[$collection->id])) { $identifier = $prefix . $collectionNames[$collection->id]; $identifiers = $this->_getRecordIdentifiers($collection, 'Dublin Core:Identifier'); if (!in_array($identifier, $identifiers)) { $collection = update_collection($collection, array(), array('Dublin Core' => array('Identifier' => array(array('text' => $identifier, 'html' => false))))); } } } set_option('archive_repertory_collection_folder', 'Dublin Core:Identifier'); $flash = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger'); $flash->addMessage(__('If no error appears, the upgrade process works fine.') . ' ' . __('The identifier of each collection has been moved to "Dublin Core:Identifier".')); } // Save folder names of collections in all cases. $this->_setCollectionFolderNames(); delete_option('archive_repertory_collection_folders'); delete_option('archive_repertory_collection_string_folders'); } if (version_compare($oldVersion, '2.9.1', '<')) { // Allow to manage upgrade from 2.6 and 2.8. $prefix = get_option('archive_repertory_collection_prefix');
// show the edit form $output .= edit_comment_form($_GET["pid"]); } } } } } else { if (!empty($_POST["action"])) { if ($_POST['action'] == 'update-picture') { $action_result = update_picture($_POST['pid'], $_POST['caption'], $_POST['allow_comments'], $_POST['description']); } else { if ($_POST['action'] == 'update-album') { $action_result = update_album($_POST['pid'], $_POST['name'], $_POST['description'], $_POST['thumbnail_id']); } else { if ($_POST["action"] == "update-collection") { $action_result = update_collection($_POST["pid"], $_POST["name"], $_POST["description"], $_POST["thumbnail_id"]); } else { if ($_POST["action"] == "update-comment") { $action_result = update_comment($_POST["pid"], $_POST["author"], $_POST["email"], $_POST["url"], $_POST["comment"]); } else { if ($_POST["action"] == "add-collection") { $action_result = add_collection($_POST["name"], $_POST["description"]); } else { if ($_POST["action"] == "add-album") { $action_result = add_album($_POST["name"], $_POST["description"], $_POST["parent_collection"]); } } } } } }