Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added fluent support #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Extensions/CMSMainExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use SilverStripe\Core\Extension;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\ORM\DataList;

class CMSMainExtension extends Extension
{
Expand All @@ -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 {
Expand Down
84 changes: 74 additions & 10 deletions src/Extensions/SearchDocumentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions src/Model/SearchDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -26,6 +27,7 @@ class SearchDocument extends DataObject
'OriginID' => 'Int',
'Title' => 'Text',
'Content' => 'Text',
'Locale' => 'Varchar'
];

private static $searchable_fields = [
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
13 changes: 11 additions & 2 deletions src/ORM/Connect/MySQLDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -65,7 +75,6 @@ public function searchEngine(

if ($extraFilter) {
$extraFilters[$documentClass] = " AND $extraFilter";

if ($alternativeFileFilter) {
$extraFilters[$fileClass] = " AND $alternativeFileFilter";
} else {
Expand Down
7 changes: 5 additions & 2 deletions src/Tasks/GenerateSearchDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -48,7 +49,9 @@ public function run($request)
$record->ClassName,
$record->getGenerateSearchLink()) . '<br>';
try {
SearchDocumentGenerator::make_document_for($record);
foreach ($locales as $locale) {
SearchDocumentGenerator::make_document_for($record, $locale);
}
} catch (Exception $e) {
}
}
Expand Down