silverstripe-fulltextsearch/code/search/SearchUpdater_ObjectHandler.php
Damian Mooyman bbaf4276af
BUG Fix delete / unpublish
Split SearchUpdater classes into separate files
Fixes #134
2017-03-09 12:34:20 +13:00

72 lines
2.1 KiB
PHP

<?php
/**
* Delete operations do not use database manipulations.
*
* If a delete has been requested, force a write on objects that should be
* indexed. This causes the object to be marked for deletion from the index.
*/
class SearchUpdater_ObjectHandler extends DataExtension
{
public function onAfterDelete()
{
// Calling delete() on empty objects does nothing
if (!$this->owner->ID) {
return;
}
// Force SearchUpdater to mark this record as dirty
// Note: Some extensions require entire hierarchy passed to augmentWrite()
$manipulation = array();
foreach (ClassInfo::ancestry($this->owner) as $class) {
if (!is_subclass_of($class, 'DataObject')) {
continue;
}
$manipulation[$class] = array(
'fields' => array(),
'id' => $this->owner->ID,
// Note: 'delete' command not actually handled by manipulations,
// but added so that SearchUpdater can detect the deletion
'command' => 'delete'
);
}
$this->owner->extend('augmentWrite', $manipulation);
SearchUpdater::handle_manipulation($manipulation);
}
/**
* Forces this object to trigger a re-index in the current state
*/
public function triggerReindex()
{
if (!$this->owner->ID) {
return;
}
$id = $this->owner->ID;
$class = $this->owner->ClassName;
$state = SearchVariant::current_state($class);
$base = ClassInfo::baseDataClass($class);
$key = "$id:$base:" . serialize($state);
$statefulids = array(
array(
'id' => $id,
'state' => $state
)
);
$writes = array(
$key => array(
'base' => $base,
'class' => $class,
'id' => $id,
'statefulids' => $statefulids,
'fields' => array()
)
);
SearchUpdater::process_writes($writes);
}
}