diff --git a/src/Extensions/CMSMainExtension.php b/src/Extensions/CMSMainExtension.php index cde2167..9d88053 100644 --- a/src/Extensions/CMSMainExtension.php +++ b/src/Extensions/CMSMainExtension.php @@ -14,6 +14,7 @@ use SilverStripe\Core\Extension; use SilverStripe\Forms\Form; use SilverStripe\Forms\FormAction; +use SilverStripe\ORM\DataList; class CMSMainExtension extends Extension { @@ -38,7 +39,14 @@ public function makeSearch($data, Form $form) $id = $owner->currentPageID(); $record = $owner->getRecord($id); if($record) { - SearchDocumentGenerator::make_document_for($record); + if (SearchDocumentGenerator::is_transalated()) { + $list = DataList::create('TractorCow\\Fluent\\Model\\Locale'); + foreach ($list as $locale) { + SearchDocumentGenerator::make_document_for($record, $locale->Locale); + } + } else { + SearchDocumentGenerator::make_document_for($record); + } $message = 'Search document generator'; } else { diff --git a/src/Extensions/SearchDocumentGenerator.php b/src/Extensions/SearchDocumentGenerator.php index 77fe4f1..ef47062 100644 --- a/src/Extensions/SearchDocumentGenerator.php +++ b/src/Extensions/SearchDocumentGenerator.php @@ -11,7 +11,10 @@ use \Exception; use SilverStripe\Control\Director; +use SilverStripe\Core\ClassInfo; +use SilverStripe\Core\Injector\Injector; use SilverStripe\ORM\DataExtension; +use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataObject; use SilverStripe\Versioned\Versioned; use SilverStripe\View\TemplateGlobalProvider; @@ -85,10 +88,59 @@ public function onAfterArchive() self::delete_doc($this->owner); } - public static function make_document_for(DataObject $object) + public static function is_transalated() { + $class = 'TractorCow\\Fluent\\State\\FluentState'; + return ClassInfo::exists($class); + } + + public static function get_current_locale() + { + $locale = null; + $class = 'TractorCow\\Fluent\\State\\FluentState'; + if (ClassInfo::exists($class)) { + $stage = Injector::inst()->get($class); + $locale = $stage->getLocale(); + } + return $locale; + } + + public static function set_locale($locale) + { + $class = 'TractorCow\\Fluent\\State\\FluentState'; + if (ClassInfo::exists($class)) { + $stage = Injector::inst()->get($class); + $stage->setLocale($locale); + } + } + + public static function get_locales() + { + if (self::is_transalated()) { + $list = []; + $locale = 'TractorCow\\Fluent\\Model\\Locale'; + foreach (DataList::create($locale) as $object) { + $list[] = $object->Locale; + } + return $list; + } + return ['']; + } + + + public function getCurrentLocale() + { + return self::get_current_locale(); + } + + public static function make_document_for(DataObject $object, $locale = null) + { + if(self::case_create_document($object)) { - $doc = self::find_or_make_document($object); + if (!$locale) { + $locale = self::get_current_locale(); + } + $doc = self::find_or_make_document($object, $locale); $doc->makeSearchContent(); } else { @@ -122,34 +174,46 @@ public static function is_versioned(DataObject $object) public static function delete_doc(DataObject $object) { - $doc = self::find_document($object); - if($doc) { + foreach (self::find_documents($object) as $doc) { $doc->delete(); } } - public static function find_or_make_document(DataObject $object) + public static function find_or_make_document(DataObject $object, $locale = null) { - $doc = self::find_document($object); + $doc = self::find_document($object, $locale); if(!$doc) { $doc = new SearchDocument([ 'Type' => get_class($object), - 'OriginID' => $object->ID + 'OriginID' => $object->ID, + 'Locale' => $locale ]); $doc->write(); } return $doc; } - public static function find_document(DataObject $object) + public static function find_document(DataObject $object, $locale = null) { - $doc = SearchDocument::get()->filter([ + $filters = [ 'Type' => get_class($object), 'OriginID' => $object->ID - ])->first(); + ]; + if ($locale) { + $filters['Locale'] = $locale; + } + $doc = SearchDocument::get()->filter($filters)->first(); return $doc; } + public static function find_documents(DataObject $object) + { + return SearchDocument::get()->filter([ + 'Type' => get_class($object), + 'OriginID' => $object->ID + ]); + } + public static function is_search() { return isset($_REQUEST['SearchGen']) ? true : false; diff --git a/src/Model/SearchDocument.php b/src/Model/SearchDocument.php index bc46694..72b1d5e 100644 --- a/src/Model/SearchDocument.php +++ b/src/Model/SearchDocument.php @@ -17,6 +17,7 @@ use SilverStripe\ORM\DataObject; use SilverStripe\Versioned\Versioned; use SilverStripe\View\SSViewer; +use SilverStripers\ElementalSearch\Extensions\SearchDocumentGenerator; class SearchDocument extends DataObject { @@ -26,6 +27,7 @@ class SearchDocument extends DataObject 'OriginID' => 'Int', 'Title' => 'Text', 'Content' => 'Text', + 'Locale' => 'Varchar' ]; private static $searchable_fields = [ @@ -47,6 +49,13 @@ public function Origin() public function makeSearchContent() { + + $locale = null; + if (SearchDocumentGenerator::is_transalated()) { + $locale = SearchDocumentGenerator::get_current_locale(); + SearchDocumentGenerator::set_locale($this->Locale); + } + $origin = $this->Origin(); if (!$origin) { return; @@ -135,6 +144,11 @@ public function makeSearchContent() // CMS layout can break on the response. (SilverStripe 4.1.1) SSViewer::set_themes($oldThemes); } + + if (SearchDocumentGenerator::is_transalated()) { + SearchDocumentGenerator::set_locale($locale); + } + return implode($output); } diff --git a/src/ORM/Connect/MySQLDatabase.php b/src/ORM/Connect/MySQLDatabase.php index ef92a9e..c5fff37 100644 --- a/src/ORM/Connect/MySQLDatabase.php +++ b/src/ORM/Connect/MySQLDatabase.php @@ -24,6 +24,7 @@ use SilverStripe\ORM\Queries\SQLSelect; use SilverStripe\ORM\Connect\MySQLDatabase as SS_MySQLDatabase; use SilverStripe\Versioned\Versioned; +use SilverStripers\ElementalSearch\Extensions\SearchDocumentGenerator; use SilverStripers\ElementalSearch\Model\SearchDocument; use SilverStripers\ElementalSearch\ORM\Search\FulltextSearchable; @@ -56,7 +57,16 @@ public function searchEngine( $keywords = $this->escapeString($keywords); $htmlEntityKeywords = htmlentities($keywords, ENT_NOQUOTES, 'UTF-8'); - $extraFilters = array($documentClass => '', $fileClass => ''); + $extraFilters = [ + $documentClass => '', + $fileClass => '' + ]; + if (SearchDocumentGenerator::is_transalated() && ($locale = SearchDocumentGenerator::get_current_locale())) { + $extraFilters = [ + $documentClass => sprintf(' AND "Locale" = \'%s\'', $locale), + $fileClass => '' + ]; + } $boolean = ''; if ($booleanSearch) { @@ -65,7 +75,6 @@ public function searchEngine( if ($extraFilter) { $extraFilters[$documentClass] = " AND $extraFilter"; - if ($alternativeFileFilter) { $extraFilters[$fileClass] = " AND $alternativeFileFilter"; } else { diff --git a/src/Tasks/GenerateSearchDocument.php b/src/Tasks/GenerateSearchDocument.php index d1ff5ea..157e134 100644 --- a/src/Tasks/GenerateSearchDocument.php +++ b/src/Tasks/GenerateSearchDocument.php @@ -27,7 +27,7 @@ class GenerateSearchDocument extends BuildTask protected $description = 'Generate search documents for items.'; - private static $segment = 'make-search-docs'; + private static $segment = 'make-search-docs'; /** * Implement this method in the task subclass to @@ -40,6 +40,7 @@ public function run($request) { set_time_limit(50000); $classes = $this->getAllSearchDocClasses(); + $locales = SearchDocumentGenerator::get_locales(); foreach ($classes as $class) { foreach ($list = DataList::create($class) as $record) { echo sprintf( @@ -48,7 +49,9 @@ public function run($request) $record->ClassName, $record->getGenerateSearchLink()) . '
'; try { - SearchDocumentGenerator::make_document_for($record); + foreach ($locales as $locale) { + SearchDocumentGenerator::make_document_for($record, $locale); + } } catch (Exception $e) { } }