Skip to content

Commit

Permalink
Merge pull request #30 from craftpulse/develop-v4
Browse files Browse the repository at this point in the history
5.4.2
  • Loading branch information
cookie10codes authored May 17, 2024
2 parents a38fca9 + 3e11389 commit 3fab79d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 73 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ 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.4.2 - 2024-05-17

### Fixed
- Removed the deletion method with the table

## 5.4.1 - 2024-05-10

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "craftpulse/craft-typesense",
"description": "Craft Plugin that synchronises with Typesense",
"type": "craft-plugin",
"version": "5.4.1",
"version": "5.4.2",
"keywords": [
"craft",
"cms",
Expand Down
94 changes: 22 additions & 72 deletions src/Typesense.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
use percipiolondon\typesense\base\PluginTrait;
use percipiolondon\typesense\helpers\CollectionHelper;
use percipiolondon\typesense\helpers\FileLog;
use percipiolondon\typesense\jobs\DeleteDocumentJob;
// use percipiolondon\typesense\jobs\DeleteDocumentJob;
use percipiolondon\typesense\models\Settings;
use percipiolondon\typesense\services\CollectionService;
use percipiolondon\typesense\services\TypesenseService;
use percipiolondon\typesense\variables\TypesenseVariable;
use percipiolondon\typesense\records\DeletionRecord;
// use percipiolondon\typesense\records\DeletionRecord;


use Typesense\Exceptions\ObjectNotFound;
Expand Down Expand Up @@ -380,89 +380,39 @@ function (ElementEvent $event) {
/* DELETE EVENT */
Event::on(
Elements::class,
Elements::EVENT_BEFORE_DELETE_ELEMENT,
Elements::EVENT_AFTER_DELETE_ELEMENT,
function (ElementEvent $event) {
$element = $event->element;
$entry = $event->element;
$section = $entry->section->handle ?? null;
$type = $entry->type->handle ?? null;
$collection = null;
$resolver = null;

if (ElementHelper::isDraftOrRevision($element)) {
if (ElementHelper::isDraftOrRevision($entry)) {
// don’t do anything with drafts or revisions
return;
}

if (!($event->element instanceof Entry)) {
return;
}

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) {
$record = DeletionRecord::findOne(['elementId' => $entry->id, 'siteId' => $site['siteId']]);

if ($record) {
$record->id = $resolver['id'];
} else {
$record = new DeletionRecord();
$record->elementId = $entry->id;
$record->siteId = $site['siteId'];
$record->typesenseId = $resolver['id'];
$record->save(false);
}
// 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']]);
}
}
if ($section) {
if ($type) {
$section = $section . '.' . $type;
}
}
}
);

Event::on(
Elements::class,
Elements::EVENT_AFTER_DELETE_ELEMENT,
function (ElementEvent $event) {
$element = $event->element;
$collection = CollectionHelper::getCollectionBySection($section);

if (ElementHelper::isDraftOrRevision($element)) {
// don’t do anything with drafts or revisions
return;
//create collection if it doesn't exist
if (!$collection instanceof \percipiolondon\typesense\TypesenseCollectionIndex) {
self::$plugin->getCollections()->saveCollections();
$collection = CollectionHelper::getCollectionBySection($section);
}
}

if (!($event->element instanceof Entry)) {
return;
if ($collection) {
$resolver = $collection->schema['resolver']($entry);
}

$entry = $element;
$section = ($entry->section->handle ?? null) . '.' . ($entry->type->handle ?? null);
$documents = DeletionRecord::findAll(['elementId' => $entry->id]);

foreach($documents as $document) {
Queue::push(new DeleteDocumentJob([
'criteria' => [
'section' => $section,
'documentId' => $document->typesenseId,
]
]));
$document->delete();
if ($resolver) {
self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->delete(['filter_by' => 'id: ' . $resolver['id']]);
}
}
);
Expand Down

0 comments on commit 3fab79d

Please sign in to comment.