Skip to content

Commit

Permalink
Merge branch 'release/2.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
scholejo committed Oct 2, 2024
2 parents 18ae25b + 1f3fa96 commit 6d8a6fb
Show file tree
Hide file tree
Showing 15 changed files with 565 additions and 157 deletions.
2 changes: 1 addition & 1 deletion .craftplugin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"pluginName":"Elastica","pluginDescription":"A plugin to connect to Elasticsearch and persist elements via hooks","pluginVersion":"1.0.0","pluginAuthorName":"Fork Unstable Media GmbH","pluginVendorName":"fork","pluginAuthorUrl":"http://fork.de","pluginAuthorGithub":"fork","codeComments":"yes","pluginComponents":["controllers","services","settings"],"consolecommandName":"","controllerName":"Elasticsearch","cpsectionName":"","elementName":"","fieldName":"","modelName":"","purchasableName":"","recordName":"","serviceName":"ElasticsearchIndexer","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"}
{"pluginName":"Elastica","pluginDescription":"A plugin to connect to Elasticsearch and persist elements via hooks","pluginVersion":"2.0.0","pluginAuthorName":"Fork Unstable Media GmbH","pluginVendorName":"fork","pluginAuthorUrl":"http://fork.de","pluginAuthorGithub":"fork","codeComments":"yes","pluginComponents":["controllers","services","settings"],"consolecommandName":"","controllerName":"Elasticsearch","cpsectionName":"","elementName":"","fieldName":"","modelName":"","purchasableName":"","recordName":"","serviceName":"ElasticsearchIndexer","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"}
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 2.0.0 - 2024-10-02
### Added
- Added support for Craft 4
- Added optional indexing of categories and assets
- Added a connection status indicator to the utility
- Added an option to define search templates

### Changed
- Include site handle in index name

## 1.0.2.2 - 2022-02-04
### Fixed
- Fix indexing after restoring trashed entries
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Event::on(
$event->addSectionHandles([
'articles',
]);
$event->addCategoryGroupHandles([
'topics',
]);
}
);
// build elasticsearch index data
Expand All @@ -63,7 +66,7 @@ Event::on(
Indexer::EVENT_BEFORE_INDEX_DATA,
function (IndexEvent $event) {
// build your custom data structure to index
$indexData = MyCustomPlugin::$plugin->mySearchService->getIndexData($event->entry);
$indexData = MyCustomPlugin::$plugin->mySearchService->getIndexData($event->sender);
$event->indexData = $indexData;
}
);
Expand All @@ -72,7 +75,7 @@ Event::on(
## Roadmap

- [x] Logo
- [ ] Index categories
- [x] Index categories
- [ ] Maybe include search proxy
- [ ] Exclude sites via settings
- [ ] Show index info / test index in utility
Expand Down
13 changes: 11 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "fork/craft-elastica",
"description": "A plugin to connect to Elasticsearch and persist elements via hooks",
"type": "craft-plugin",
"version": "1.0.2.2",
"version": "2.0.0",
"keywords": [
"craft",
"cms",
Expand All @@ -24,7 +24,8 @@
}
],
"require": {
"craftcms/cms": "^3.0.0-RC1",
"php": "^8.0",
"craftcms/cms": "^4.0.0-alpha",
"elasticsearch/elasticsearch": "^7.0"
},
"autoload": {
Expand All @@ -42,5 +43,13 @@
"indexer": "fork\\elastica\\services\\Indexer"
},
"class": "fork\\elastica\\Elastica"
},
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"allow-plugins": {
"yiisoft/yii2-composer": true,
"craftcms/plugin-installer": true
}
}
}
71 changes: 28 additions & 43 deletions src/Elastica.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
namespace fork\elastica;

use Craft;
use craft\base\Element;
use craft\base\Model;
use craft\base\Plugin;
use craft\elements\Entry;
use craft\events\ModelEvent;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterUserPermissionsEvent;
Expand All @@ -22,7 +23,11 @@
use fork\elastica\services\Indexer;
use fork\elastica\services\Utility as UtilityService;
use fork\elastica\utilities\Utility as CpUtility;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use yii\base\Event;
use yii\base\Exception;

/**
* Craft plugins are very much like little applications in and of themselves. We’ve made
Expand All @@ -38,7 +43,7 @@
* @package Elastica
* @since 1.0.0
*
* @property \fork\elastica\services\Utility $utility
* @property UtilityService $utility
* @property Indexer $indexer
* @property Settings $settings
* @method Settings getSettings()
Expand All @@ -65,7 +70,7 @@ class Elastica extends Plugin
*
* @var string
*/
public $schemaVersion = '1.0.0';
public string $schemaVersion = '2.0.0';

// Public Methods
// =========================================================================
Expand Down Expand Up @@ -101,54 +106,33 @@ function (RegisterComponentTypesEvent $event) {
}

// elasticsearch index actions
Event::on(Entry::class, Entry::EVENT_AFTER_SAVE, function (ModelEvent $event) {
Event::on(Element::class, Element::EVENT_AFTER_SAVE, function (ModelEvent $event) {
$this->indexer->handleAfterSaveEvent($event);
});
Event::on(Entry::class, Entry::EVENT_AFTER_RESTORE, function (Event $event) {
Event::on(Element::class, Element::EVENT_AFTER_RESTORE, function (Event $event) {
$this->indexer->handleAfterRestoreEvent($event);
});
Event::on(Entry::class, Entry::EVENT_AFTER_DELETE, function (Event $event) {
Event::on(Element::class, Element::EVENT_AFTER_DELETE, function (Event $event) {
$this->indexer->handleAfterDeleteEvent($event);
});

Event::on(
UserPermissions::class,
UserPermissions::EVENT_REGISTER_PERMISSIONS,
function(RegisterUserPermissionsEvent $event) {
$event->permissions['Elastica'] = [
'elasticaIndexTemplates' => [
'label' => 'Set index templates in elasticsearch',
],
$event->permissions[] = [
'heading' => 'Elastica',
'permissions' => [
'elasticaIndexTemplates' => [
'label' => 'Set index templates in elasticsearch',
],
'elasticaSearchTemplates' => [
'label' => 'Add/update search templates in elasticsearch',
],
]
];
}
);

/**
* Logging in Craft involves using one of the following methods:
*
* Craft::trace(): record a message to trace how a piece of code runs. This is mainly for development use.
* Craft::info(): record a message that conveys some useful information.
* Craft::warning(): record a warning message that indicates something unexpected has happened.
* Craft::error(): record a fatal error that should be investigated as soon as possible.
*
* Unless `devMode` is on, only Craft::warning() & Craft::error() will log to `craft/storage/logs/web.log`
*
* It's recommended that you pass in the magic constant `__METHOD__` as the second parameter, which sets
* the category to the method (prefixed with the fully qualified class name) where the constant appears.
*
* To enable the Yii debug toolbar, go to your user account in the AdminCP and check the
* [] Show the debug toolbar on the front end & [] Show the debug toolbar on the Control Panel
*
* http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html
*/
//Craft::info(
// Craft::t(
// 'elasticsearch',
// '{name} plugin loaded',
// ['name' => $this->name]
// ),
// __METHOD__
//);
}

// Protected Methods
Expand All @@ -157,9 +141,9 @@ function(RegisterUserPermissionsEvent $event) {
/**
* Creates and returns the model used to store the plugin’s settings.
*
* @return \craft\base\Model|null
* @return Model|null
*/
protected function createSettingsModel()
protected function createSettingsModel(): ?Model
{
return new Settings();
}
Expand All @@ -170,16 +154,17 @@ protected function createSettingsModel()
*
* @return string The rendered settings HTML
*
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
* @throws \yii\base\Exception
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
* @throws Exception
*/
protected function settingsHtml(): string
{
return Craft::$app->view->renderTemplate(
'elastica/settings',
[
'connectionStatus' => $this->indexer->getConnectionStatus(),
'settings' => $this->getSettings(),
]
);
Expand Down
6 changes: 6 additions & 0 deletions src/assetbundles/elastica/dist/css/Elastica.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@
* @package Elastica
* @since 1.0.0
*/
.elastica-icon {
float: left;
height: 60px;
margin-right: 15px;
width: 60px;
}
4 changes: 3 additions & 1 deletion src/events/IndexEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace fork\elastica\events;

use craft\elements\Entry;
use yii\base\Event;

/**
Expand All @@ -20,7 +21,8 @@ class IndexEvent extends Event
// =========================================================================

/**
* @var \craft\elements\Entry
* @var Entry
* @deprecated use $sender instead
*/
public $entry;

Expand Down
105 changes: 91 additions & 14 deletions src/events/IndexerInitEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ class IndexerInitEvent extends Event
*/
protected $sectionHandles = [];

/**
* list of group handles whose categories need to be (re-)indexed by the plugin
*
* @var string[]
*/
protected $categoryGroupHandles = [];

/**
* list of volume handles whose assets need to be (re-)indexed by the plugin
*
* @var string[]
*/
protected $volumeHandles = [];

/**
* Returns the section handles.
*
Expand All @@ -29,25 +43,32 @@ public function getSectionHandles(): array
return $this->sectionHandles;
}

///**
// * Sets the section handles.
// *
// * @param string[] $sectionHandles
// */
//public function setSectionHandles(array $sectionHandles)
//{
// // ensure we have no duplicates and no non-empty strings
// $this->sectionHandles = array_filter(array_unique($this->sectionHandles), function ($handle) {
// return !empty($handle) && is_string($handle);
// });
//}
/**
* Returns the category group handles.
*
* @return string[]
*/
public function getCategoryGroupHandles(): array
{
return $this->categoryGroupHandles;
}

/**
* Returns the volume handles.
*
* @return string[]
*/
public function getVolumeHandles(): array
{
return $this->volumeHandles;
}

/**
* Adds a new set of section handles.
*
* @param string[] $handles
*/
public function addSectionHandles(array $handles)
public function addSectionHandles(array $handles): void
{
foreach ($handles as $handle) {
// ensure the handle is a non-empty string
Expand All @@ -62,11 +83,67 @@ public function addSectionHandles(array $handles)
*
* @param string $handle
*/
public function addSectionHandle(string $handle)
public function addSectionHandle(string $handle): void
{
// ensure the given handle is not empty and only added once
if (!empty($handle) && !in_array($handle, $this->sectionHandles)) {
$this->sectionHandles[] = $handle;
}
}

/**
* Adds a new set of category group handles.
*
* @param string[] $handles
*/
public function addCategoryGroupHandles(array $handles): void
{
foreach ($handles as $handle) {
// ensure the handle is a non-empty string
if (!empty($handle) && is_string($handle)) {
$this->addCategoryGroupHandle($handle);
}
}
}

/**
* Adds a new category group handle.
*
* @param string $handle
*/
public function addCategoryGroupHandle(string $handle): void
{
// ensure the given handle is not empty and only added once
if (!empty($handle) && !in_array($handle, $this->categoryGroupHandles)) {
$this->categoryGroupHandles[] = $handle;
}
}

/**
* Adds a new set of volume handles.
*
* @param string[] $handles
*/
public function addVolumeHandles(array $handles): void
{
foreach ($handles as $handle) {
// ensure the handle is a non-empty string
if (!empty($handle) && is_string($handle)) {
$this->addVolumeHandle($handle);
}
}
}

/**
* Adds a new volume handle.
*
* @param string $handle
*/
public function addVolumeHandle(string $handle): void
{
// ensure the given handle is not empty and only added once
if (!empty($handle) && !in_array($handle, $this->volumeHandles)) {
$this->volumeHandles[] = $handle;
}
}
}
Loading

0 comments on commit 6d8a6fb

Please sign in to comment.