diff --git a/application/models/Item.php b/application/models/Item.php index 38f32b1333..3df03601fb 100644 --- a/application/models/Item.php +++ b/application/models/Item.php @@ -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. * diff --git a/application/models/Mixin/ElementText.php b/application/models/Mixin/ElementText.php index c5739da237..04836dfd5a 100644 --- a/application/models/Mixin/ElementText.php +++ b/application/models/Mixin/ElementText.php @@ -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)) { @@ -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;