Source of file SearchablePageExtension.php
Size: 2,537 Bytes - Last Modified: 2021-12-24T06:46:48+00:00
/var/www/docs.ssmods.com/process/src/src/Model/SearchablePageExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 | <?php namespace EniaGroup\ElementalSiteSearch\Model; use SilverStripe\View\SSViewer; use SilverStripe\ORM\DataExtension; use SilverStripe\Core\Config\Config; use SilverStripe\Forms\CheckboxField; use SilverStripe\ORM\Connect\MySQLSchemaManager; use SilverStripe\CMS\Controllers\ModelAsController; class SearchablePageExtension extends DataExtension { /** * {@inheritDoc} */ private static $db = [ 'Title' => 'Varchar(255)', 'SearchContent' => 'Text', 'Keywords' => 'Text', 'ShowInSearch' => 'Boolean', 'Weight' => 'Int' ]; /** * {@inheritDoc} */ private static $indexes = [ 'SearchFields' => [ 'type' => 'fulltext', 'columns' => ['Title', 'SearchContent','keywords'] ] ]; /** * {@inheritDoc} */ private static $defaults = [ 'ShowInSearch' => true, 'Weight' => 1 ]; /** * {@inheritDoc} */ private static $create_table_options = [ MySQLSchemaManager::ID => 'ENGINE=MyISAM' ]; /** * {@inheritDoc} */ public function onBeforeWrite() { parent::onBeforeWrite(); $this->getOwner()->SearchContent = $this->collateSearchContent(); } /** * Generate the search content to use for the searchable object * * We just retrive it from the templates. * * @return string */ protected function collateSearchContent() { // Get the original theme $originalThemes = SSViewer::get_themes(); // Get the page content if we have any $content = $this->getOwner()->Content; try { // Enable frontend themes in order to correctly render the elements as they would be for the frontend Config::nest(); SSViewer::set_themes(SSViewer::config()->get('themes')); // Get the elements content $content .= $this->getOwner()->getElementsForSearch(); // Clean up the content $content = preg_replace('/\s+/', ' ', $content); // Return themes back for the CMS Config::unnest(); } finally { // Restore themes SSViewer::set_themes($originalThemes); } return $content; } /** * {@inheritDoc} */ public function updateSettingsFields(&$fields) { $fields->insertAfter('ShowInMenus', CheckboxField::create('ShowInSearch', 'Show in search?')); } } |