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

Release 2.0.3 #43

Merged
merged 4 commits into from
Jun 3, 2024
Merged
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
21 changes: 21 additions & 0 deletions src/SearchTerm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Werkbot\Search;

use SilverStripe\ORM\DataObject;

class SearchTerm extends DataObject
{
private static $singular_name = 'Search Term';
private static $plural_name = 'Search Terms';
private static $table_name = 'SearchTerm';

private static $db = [
'SearchTermText' => 'Text',
'SortOrder' => 'Int',
];

private static $has_one = [
'SearchTermOf' => DataObject::class,
];
}
106 changes: 105 additions & 1 deletion src/SearchableExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,35 @@

namespace Werkbot\Search;

use SilverStripe\Forms\Tab;
use SilverStripe\Forms\TabSet;
use SilverStripe\Core\ClassInfo;
use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Versioned\Versioned;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;

class SearchableExtension extends DataExtension
{
private static $has_many = [
'SearchTerms' => SearchTerm::class . '.SearchTermOf',
];

private static $owns = [
'SearchTerms',
];

/*
Column names for the "Title" and "Content" search fields
Override these to set them to a different column name
Expand All @@ -19,6 +42,73 @@ class SearchableExtension extends DataExtension
"getSearchableTitle" => "Text",
"getSearchableSummary" => 'HTMLText',
];
/**
* updateCMSFields
* Adds the SearchTerms GridField to the CMS tab
* This should only be applied to Data objects
*
* @param FieldList $fields
* @return void
**/
public function updateCMSFields(FieldList $fields)
{
if (DataObject::getSchema()->baseDataClass($this->owner->ClassName) != "SilverStripe\CMS\Model\SiteTree") {
$this->addSearchSettingFields($fields);
}
parent::updateCMSFields($fields);
}
/**
* updateSettingsFields
* Adds the SearchTerms GridField to the settings tab
* This should only be applied to SiteTree objects
*
* @param FieldList $fields
* @return void
**/
public function updateSettingsFields(FieldList $fields)
{
if (DataObject::getSchema()->baseDataClass($this->owner->ClassName) == "SilverStripe\CMS\Model\SiteTree") {
$this->addSearchSettingFields($fields);
}
}

public function addSearchSettingFields(FieldList &$fields)
{
$fields->addFieldToTab('Root', new TabSet('Search', new Tab('Main')));

if ($this->owner->hasField("ShowInSearch")) {
$fields->removeByName('ShowInSearch');
$ShowInSearch = CheckboxField::create("ShowInSearch", $this->owner->fieldLabel('ShowInSearch'));
$ShowInSearchGroup = FieldGroup::create(
'Settings',
$ShowInSearch
);
$fields->addFieldToTab('Root.Search.Main', $ShowInSearchGroup);
}

$SearchTermsGridField = GridField::create(
'SearchTerms',
'Enter Search Terms',
$this->owner->SearchTerms(),
GridFieldConfig::create()
->addComponent(GridFieldButtonRow::create('before'))
->addComponent(GridFieldToolbarHeader::create())
->addComponent(GridFieldEditableColumns::create())
->addComponent(GridFieldDeleteAction::create())
->addComponent(GridFieldAddNewInlineButton::create())
->addComponent(new GridFieldOrderableRows('SortOrder'))
);

$SearchTermsGridField->getConfig()->getComponentByType(GridFieldEditableColumns::class)->setDisplayFields(array(
'SearchTermText' => function ($record, $column, $grid) {
return TextField::create($column)
->setAttribute('placeholder', 'Enter search term');
}
));

$fields->addFieldToTab('Root.Search.Main', $SearchTermsGridField);
}

/**
* getIndexQuery
* This query is used when building the index
Expand All @@ -36,6 +126,10 @@ class SearchableExtension extends DataExtension
SiteTree
ON
SiteTree.ID = Page.ID
LEFT JOIN
SearchTerm
ON
SearchTerm.SearchTermOfID = Page.ID AND SearchTerm.SearchTermOfClass = SiteTree.ClassName
WHERE
SiteTree.ShowInSearch = '1'";
**/
Expand Down Expand Up @@ -115,7 +209,17 @@ public function getSearchableSummary()
**/
public function getSearchableContent()
{
return $this->getSearchableSummary();
$content = "";
foreach ($this->owner->SearchTerms() as $term) {
$content .= $term->SearchTermText . " ";
}
if ($this->owner->SearchableExtension_Summary_ColumnName) {
$content .= $this->owner->{$this->owner->SearchableExtension_Summary_ColumnName};
} else {
$content .= $this->owner->Content;
}

return $content;
}
/**
* getSearchableSummaryColumnName
Expand Down
Loading