Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unrelated elements when changing item type #865

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions application/models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,31 @@ public function getItemTypeElements()
return $indexedElements;
}

/**
* Get a complete set of Element IDs associated with this Item.
*
* @uses Mixin_ElementText::getElementsBySetName()
* @uses Table_Element::findByItemType()
* @return array Element IDs that are associated with the item type of
* the item together with Dublin Core element set.
*/
public function getAssociatedElementIds()
{
$elementIds = array();
$dublinCoreElements = $this->getElementsBySetName('Dublin Core');
foreach ($dublinCoreElements as $element) {
$elementIds[] = $element->id;
}
if ($this->item_type_id) {
$itemTypeElements = $this->ItemTypeElements;
foreach ($itemTypeElements as $element) {
$elementIds[] = $element->id;
}
}

return $elementIds;
}

/**
* Get a property for display.
*
Expand Down
12 changes: 12 additions & 0 deletions application/models/Mixin/ElementText.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,11 @@ public function saveElementTexts()

$existingTexts = $this->_textsByElementId;
$elementIdsFromForm = array_keys($this->_elementsOnForm);
$associatedElementIds = array();
if (method_exists($this->_record, 'getAssociatedElementIds')) {
// Element IDs that are associated to current record
$associatedElementIds = (array) $this->_record->getAssociatedElementIds();
}

foreach ($this->_textsToSave as $textRecord) {
if ($this->_replaceElementTexts || in_array($textRecord->element_id, $elementIdsFromForm)) {
Expand All @@ -671,13 +676,20 @@ public function saveElementTexts()
}

// Delete all the remaining, un-matched old texts
$elementIdsToDelete = array();
foreach ($existingTexts as $element_id => $texts) {
if ($this->_replaceElementTexts || in_array($element_id, $elementIdsFromForm)) {
foreach ($texts as $text) {
$text->delete();
}
} elseif ($associatedElementIds && !in_array($element_id, $associatedElementIds)) {
// if the element is not part of associated elements, delete it
$elementIdsToDelete[] = $element_id;
}
}
if ($elementIdsToDelete) {
$this->deleteElementTextsByElementId($elementIdsToDelete);
}

// Cause texts to be re-loaded if accessed after save.
$this->_recordsAreLoaded = false;
Expand Down