/** * @inheritDoc IElementType::onAfterMoveElementInStructure() * * @param BaseElementModel $element * @param int $structureId * * @return null */ public function onAfterMoveElementInStructure(BaseElementModel $element, $structureId) { // Was the category moved within its group's structure? if ($element->getGroup()->structureId == $structureId) { // Update its URI craft()->elements->updateElementSlugAndUri($element, true, true, true); // Make sure that each of the category's ancestors are related wherever the category is related $newRelationValues = array(); $ancestorIds = $element->getAncestors()->ids(); $sources = craft()->db->createCommand()->select('fieldId, sourceId, sourceLocale')->from('relations')->where('targetId = :categoryId', array(':categoryId' => $element->id))->queryAll(); foreach ($sources as $source) { $existingAncestorRelations = craft()->db->createCommand()->select('targetId')->from('relations')->where(array('and', 'fieldId = :fieldId', 'sourceId = :sourceId', 'sourceLocale = :sourceLocale', array('in', 'targetId', $ancestorIds)), array(':fieldId' => $source['fieldId'], ':sourceId' => $source['sourceId'], ':sourceLocale' => $source['sourceLocale']))->queryColumn(); $missingAncestorRelations = array_diff($ancestorIds, $existingAncestorRelations); foreach ($missingAncestorRelations as $categoryId) { $newRelationValues[] = array($source['fieldId'], $source['sourceId'], $source['sourceLocale'], $categoryId); } } if ($newRelationValues) { craft()->db->createCommand()->insertAll('relations', array('fieldId', 'sourceId', 'sourceLocale', 'targetId'), $newRelationValues); } } }