Source of file SearchableDataObject.php
Size: 3,652 Bytes - Last Modified: 2021-12-24T06:43:04+00:00
/var/www/docs.ssmods.com/process/src/code/SearchableDataObject.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 | <?php /** * SearchableDataObject - extension that let the DO to auto update the search table * after a write * * @author Gabriele Brosulo <gabriele.brosulo@zirak.it> * @creation-date 12-May-2014 */ class SearchableDataObject extends DataExtension { private function deleteDo(DataObject $do) { $id = $do->ID; $class = $do->class; DB::query("DELETE FROM \"SearchableDataObjects\" WHERE ID=$id AND ClassName='$class'"); } public function onAfterWrite() { parent::onAfterWrite(); if (in_array('Searchable', class_implements($this->owner->class))) { if ($this->owner->hasExtension('Versioned')) { $filterID = array('ID' => $this->owner->ID); $filter = $filterID + $this->owner->getSearchFilter(); $do = Versioned::get_by_stage($this->owner->class, 'Live')->filter($filter)->first(); } else { $filterID = "`{$this->findParentClass()}`.`ID`={$this->owner->ID}"; $do = DataObject::get($this->owner->class, $filterID, false)->filter($this->owner->getSearchFilter())->first(); } if ($do) { PopulateSearch::insert($do); } else { $this->deleteDo($this->owner); } } elseif ($this->owner instanceof Page) { // Page is versioned but usually doesn't implement Searchable $page = Versioned::get_by_stage('Page', 'Live')->filter(array( 'ID' => $this->owner->ID, 'ShowInSearch' => 1, ))->first(); if ($page) { PopulateSearch::insertPage($page); } else { $this->deleteDo($this->owner); } } } /** * Remove the entry from the search table before deleting it */ public function onBeforeDelete() { parent::onBeforeDelete(); $this->deleteDo($this->owner); } /** * Check and create the required table during dev/build */ public function augmentDatabase() { $connection = DB::getConn(); $schema = DB::get_schema(); $isMySQL = ($connection->getDatabaseServer() === 'mysql'); $unsigned = ($isMySQL) ? 'unsigned' : ''; $extraOptions = ($isMySQL) ? ' ENGINE=InnoDB' : ''; // construct query to create table with custom primary key $sql = join(' ', [ 'CREATE TABLE IF NOT EXISTS "SearchableDataObjects" (', '"ID" int(10) ' . $unsigned . ' NOT NULL,', '"ClassName" ' . $schema->varchar(['precision' => 255]) . ',', '"Title" ' . $schema->varchar(['precision' => 255]) . ' NOT NULL,', '"Content" ' . $schema->text([]) . ' NOT NULL,', '"PageID" ' . $schema->int(['precision' => 11, 'null' => 'NOT NULL', 'default' => 0]) . ',', 'PRIMARY KEY("ID", "ClassName")', ')', $extraOptions, ]); // add table DB::query($sql); // add search index requirement DB::require_index( 'SearchableDataObjects', 'Title', array('value' => '"Title", "Content"', 'type' => 'fulltext') ); } /** * Recursive function to find the parent class of the current data object */ private function findParentClass($class = null) { if (is_null($class)) { $class = $this->owner->class; } $parent = singleton($class)->parentClass(); return $parent === 'DataObject' ? $class : $this->findParentClass($parent); } } |