From 6b0ddfc3476015c1acda45584aa69542907b1404 Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Thu, 2 May 2024 18:15:25 +0200 Subject: [PATCH 1/2] feat: improved the deleting flow for custom created indexes other than entry ID --- src/Typesense.php | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Typesense.php b/src/Typesense.php index e5c5071..c40a304 100755 --- a/src/Typesense.php +++ b/src/Typesense.php @@ -356,10 +356,10 @@ function (ElementEvent $event) { } $entry = $event->element; - $id = $entry->id; $sectionHande = $entry->section->handle ?? null; $type = $entry->type->handle ?? null; $collection = null; + $resolver = null; if (ElementHelper::isDraftOrRevision($entry)) { // don’t do anything with drafts or revisions @@ -388,17 +388,17 @@ function (ElementEvent $event) { } } + if ($collection) { + $resolver = $collection->schema['resolver']($entry); + } + if (($entry->enabled && $entry->getEnabledForSite()) && $entry->getStatus() === 'live') { // element is enabled --> save to Typesense - if ($collection !== null) { + if ($resolver) { Craft::info('Typesense edit / add / delete document based of: ' . $entry->title, __METHOD__); try { - $resolver = $collection->schema['resolver']($entry); - - if ($resolver) { - self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->upsert($resolver); - } + self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->upsert($resolver); } catch (ObjectNotFound | ServerError $e) { Craft::$app->session->setFlash('error', Craft::t('typesense', 'There was an issue saving your action, check the logs for more info')); Craft::error($e->getMessage(), __METHOD__); @@ -406,8 +406,9 @@ function (ElementEvent $event) { } } else { // element is disabled --> delete from Typesense - if ($collection !== null) { - self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->delete(['filter_by' => 'id: ' . $id]); + if ($resolver) { + Craft::info('Typesense delete document based of: ' . $entry->title, __METHOD__); + self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->delete(['filter_by' => 'id: ' . $resolver['id']]); } } } @@ -421,9 +422,9 @@ function (ElementEvent $event) { function (ElementEvent $event) { $entry = $event->element; $section = $entry->section->handle ?? null; - $id = $entry->id; $type = $entry->type->handle ?? null; $collection = null; + $resolver = null; if (ElementHelper::isDraftOrRevision($entry)) { // don’t do anything with drafts or revisions @@ -444,8 +445,12 @@ function (ElementEvent $event) { } } - if ($collection !== null) { - self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->delete(['filter_by' => 'id: ' . $id]); + if ($collection) { + $resolver = $collection->schema['resolver']($entry); + } + + if ($resolver) { + self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->delete(['filter_by' => 'id: ' . $resolver['id']]); } } ); From 29dba92f3823f743441fc20b306f838a842bc62c Mon Sep 17 00:00:00 2001 From: Stefanie Gevaert Date: Mon, 15 Jul 2024 07:35:36 +0200 Subject: [PATCH 2/2] fix: check for the deletion on the documents --- CHANGELOG.md | 7 +- composer.json | 2 +- src/Typesense.php | 159 +++++++++++++++++++++++++--------------------- 3 files changed, 94 insertions(+), 74 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf14711..4d7b06a 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,12 @@ 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/). -### 5.5.0 - 202404-09 +### 5.5.1 - 2024-07-15 + +### Fixed +- Check for the deletion on the documents + +### 5.5.0 - 2024-04-09 ### Changed - Craft 5 release diff --git a/composer.json b/composer.json index fe72a53..bc6f8d4 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "craftpulse/craft-typesense", "description": "Craft Plugin that synchronises with Typesense", "type": "craft-plugin", - "version": "5.5.0", + "version": "5.5.1", "keywords": [ "craft", "cms", diff --git a/src/Typesense.php b/src/Typesense.php index c40a304..6432946 100755 --- a/src/Typesense.php +++ b/src/Typesense.php @@ -28,7 +28,6 @@ use percipiolondon\typesense\base\PluginTrait; use percipiolondon\typesense\helpers\CollectionHelper; use percipiolondon\typesense\helpers\FileLog; -use percipiolondon\typesense\helpers\ProjectConfigDataHelper; use percipiolondon\typesense\models\Settings; use percipiolondon\typesense\services\CollectionService; use percipiolondon\typesense\services\TypesenseService; @@ -38,7 +37,6 @@ use Typesense\Exceptions\ObjectNotFound; use Typesense\Exceptions\ServerError; use yii\base\Event; -use yii\db\Expression; /** * Craft plugins are very much like little applications in and of themselves. We’ve made @@ -355,61 +353,22 @@ function (ElementEvent $event) { return; } - $entry = $event->element; - $sectionHande = $entry->section->handle ?? null; - $type = $entry->type->handle ?? null; - $collection = null; - $resolver = null; + $element = $event->element; - if (ElementHelper::isDraftOrRevision($entry)) { + if (ElementHelper::isDraftOrRevision($element)) { // don’t do anything with drafts or revisions return; } - if ($sectionHande) { - $section = ''; + $this->_afterSave($element); - if ($type) { - $section = $sectionHande . '.' . $type; - } - - $collection = CollectionHelper::getCollectionBySection($section); - - // get the generic type if specific doesn't exist - if (is_null($collection)) { - $section = $sectionHande . '.all'; - $collection = CollectionHelper::getCollectionBySection($section); - } - - //create collection if it doesn't exist - if (!$collection instanceof \percipiolondon\typesense\TypesenseCollectionIndex) { - self::$plugin->getCollections()->saveCollections(); - $collection = CollectionHelper::getCollectionBySection($section); - } - } - - if ($collection) { - $resolver = $collection->schema['resolver']($entry); - } - - if (($entry->enabled && $entry->getEnabledForSite()) && $entry->getStatus() === 'live') { - // element is enabled --> save to Typesense - if ($resolver) { - Craft::info('Typesense edit / add / delete document based of: ' . $entry->title, __METHOD__); - - try { - self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->upsert($resolver); - } catch (ObjectNotFound | ServerError $e) { - Craft::$app->session->setFlash('error', Craft::t('typesense', 'There was an issue saving your action, check the logs for more info')); - Craft::error($e->getMessage(), __METHOD__); + if ($event->name === Elements::EVENT_AFTER_RESTORE_ELEMENT) { + foreach($element->getSupportedSites() as $site) { + if ($site['siteId'] ?? null) { + $entry = Entry::find()->id($element->id)->siteId($site['siteId'])->one(); + $this->_afterSave($entry); } } - } else { - // element is disabled --> delete from Typesense - if ($resolver) { - Craft::info('Typesense delete document based of: ' . $entry->title, __METHOD__); - self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->delete(['filter_by' => 'id: ' . $resolver['id']]); - } } } ); @@ -418,42 +377,98 @@ function (ElementEvent $event) { /* DELETE EVENT */ Event::on( Elements::class, - Elements::EVENT_AFTER_DELETE_ELEMENT, + Elements::EVENT_BEFORE_DELETE_ELEMENT, function (ElementEvent $event) { - $entry = $event->element; - $section = $entry->section->handle ?? null; - $type = $entry->type->handle ?? null; - $collection = null; - $resolver = null; + $element = $event->element; - if (ElementHelper::isDraftOrRevision($entry)) { + if (ElementHelper::isDraftOrRevision($element)) { // don’t do anything with drafts or revisions return; } - if ($section) { - if ($type) { - $section = $section . '.' . $type; + foreach($element->getSupportedSites() as $site) { + if ($site['siteId'] ?? null) { + $entry = Entry::find()->id($element->id)->siteId($site['siteId'])->one(); + + if ($entry) { + $section = $entry->section->handle ?? null; + $type = $entry->type->handle ?? null; + $collection = null; + $resolver = null; + if ($section) { + if ($type) { + $section = $section . '.' . $type; + } + + $collection = CollectionHelper::getCollectionBySection($section); + } + + if ($collection) { + $resolver = $collection->schema['resolver']($entry); + } + + if ($resolver) { + Craft::info('Typesense delete document based of: ' . $entry->title . ' - ' . $entry->getSite()->handle, __METHOD__); + self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->delete(['filter_by' => 'id: ' . $resolver['id']]); + } + } } + } + } + ); + } + private function _afterSave(Entry $entry): void + { + $sectionHande = $entry->section->handle ?? null; + $type = $entry->type->handle ?? null; + $collection = null; + $resolver = null; - $collection = CollectionHelper::getCollectionBySection($section); + if ($sectionHande) { + $section = ''; - //create collection if it doesn't exist - if (!$collection instanceof \percipiolondon\typesense\TypesenseCollectionIndex) { - self::$plugin->getCollections()->saveCollections(); - $collection = CollectionHelper::getCollectionBySection($section); - } - } + if ($type) { + $section = $sectionHande . '.' . $type; + } - if ($collection) { - $resolver = $collection->schema['resolver']($entry); - } + $collection = CollectionHelper::getCollectionBySection($section); + + // get the generic type if specific doesn't exist + if (is_null($collection)) { + $section = $sectionHande . '.all'; + $collection = CollectionHelper::getCollectionBySection($section); + } - if ($resolver) { - self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->delete(['filter_by' => 'id: ' . $resolver['id']]); + //create collection if it doesn't exist + if (!$collection instanceof \percipiolondon\typesense\TypesenseCollectionIndex) { + self::$plugin->getCollections()->saveCollections(); + $collection = CollectionHelper::getCollectionBySection($section); + } + } + + if ($collection) { + $resolver = $collection->schema['resolver']($entry); + } + + if (($entry->enabled && $entry->getEnabledForSite()) && $entry->getStatus() === 'live') { + // element is enabled --> save to Typesense + if ($resolver) { + Craft::info('Typesense edit / add / delete document based of: ' . $entry->title, __METHOD__); + + try { + self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->upsert($resolver); + } catch (ObjectNotFound | ServerError $e) { + Craft::$app->session->setFlash('error', Craft::t('typesense', 'There was an issue saving your action, check the logs for more info')); + Craft::error($e->getMessage(), __METHOD__); } } - ); + } else { + // element is disabled --> delete from Typesense + if ($resolver) { + Craft::info('Typesense delete document based of: ' . $entry->title, __METHOD__); + self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->delete(['filter_by' => 'id: ' . $resolver['id']]); + } + } } private function _registerVariable(): void