Skip to content

Commit

Permalink
Merge pull request #32 from craftpulse/develop-v5
Browse files Browse the repository at this point in the history
5.5.1
  • Loading branch information
cookie10codes authored Jul 15, 2024
2 parents 82a127f + fa8998b commit bd2d2a3
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 70 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.5.0",
"version": "5.5.1",
"keywords": [
"craft",
"cms",
Expand Down
156 changes: 88 additions & 68 deletions src/Typesense.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -355,60 +353,22 @@ function (ElementEvent $event) {
return;
}

$entry = $event->element;
$id = $entry->id;
$sectionHande = $entry->section->handle ?? null;
$type = $entry->type->handle ?? null;
$collection = 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 (($entry->enabled && $entry->getEnabledForSite()) && $entry->getStatus() === 'live') {
// element is enabled --> save to Typesense
if ($collection !== null) {
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);
}
} 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 ($collection !== null) {
self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->delete(['filter_by' => 'id: ' . $id]);
}
}
}
);
Expand All @@ -417,38 +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;
$id = $entry->id;
$type = $entry->type->handle ?? null;
$collection = 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;
}

$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 ($collection !== null) {
self::$plugin->getClient()->client()->collections[$collection->indexName]->documents->delete(['filter_by' => 'id: ' . $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
Expand Down

0 comments on commit bd2d2a3

Please sign in to comment.