Skip to content
Open
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
49 changes: 26 additions & 23 deletions src/controllers/CollectionsController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Typesense plugin for Craft CMS 5.x
*
Expand All @@ -12,16 +13,12 @@

use Craft;
use craft\helpers\Queue;

use craft\web\Controller;
use Http\Client\Exception;
use percipiolondon\typesense\events\DocumentEvent;
use percipiolondon\typesense\helpers\CollectionHelper;
use percipiolondon\typesense\jobs\SyncDocumentsJob;

use percipiolondon\typesense\Typesense;


use Typesense\Exceptions\TypesenseClientError;
use yii\base\InvalidConfigException;
use yii\di\NotInstantiableException;
Expand Down Expand Up @@ -101,6 +98,12 @@ public function actionCollections(): Response
$indexes = Typesense::$plugin->getSettings()->collections;

foreach ($indexes as $index) {

$nameOverride = false;
if (is_array($index->section)) {
$nameOverride = 'Multiple';
}

$element = $index->criteria->one();

switch ($index->elementType) {
Expand All @@ -110,9 +113,9 @@ public function actionCollections(): Response
if ($volume) {
$variables['sections'][] = [
'id' => 1,
'name' => $volume->name,
'name' => ($nameOverride ? $nameOverride : $volume->name),
'handle' => $volume->handle,
'type' => 'Asset: ' . $volume->handle,
'type' => 'Asset: ' . ($nameOverride ? $nameOverride : $volume->handle),
'entryCount' => $index->criteria->count(),
'index' => $index->indexName,
];
Expand All @@ -125,9 +128,9 @@ public function actionCollections(): Response
if ($section) {
$variables['sections'][] = [
'id' => $section->id,
'name' => $section->name,
'name' => ($nameOverride ? $nameOverride : $section->name),
'handle' => $section->handle,
'type' => 'Entry: ' . $element->type->handle,
'type' => 'Entry: ' . ($nameOverride ? $nameOverride : $element->type->handle),
'entryCount' => $index->criteria->count(),
'index' => $index->indexName,
];
Expand All @@ -140,9 +143,9 @@ public function actionCollections(): Response
if ($group) {
$variables['sections'][] = [
'id' => $group->id,
'name' => $group->name,
'name' => ($nameOverride ? $nameOverride : $group->name),
'handle' => $group->handle,
'type' => 'Category: ' . $element->group->handle,
'type' => 'Category: ' . ($nameOverride ? $nameOverride : $element->group->handle),
'entryCount' => $index->criteria->count(),
'index' => $index->indexName,
];
Expand All @@ -154,9 +157,9 @@ public function actionCollections(): Response
if ($type) {
$variables['sections'][] = [
'id' => $type->id,
'name' => $type->name,
'name' => ($nameOverride ? $nameOverride : $type->name),
'handle' => $type->handle,
'type' => 'Product: ' . $element->type->handle,
'type' => 'Product: ' . ($nameOverride ? $nameOverride : $element->type->handle),
'entryCount' => $index->criteria->count(),
'index' => $index->indexName,
];
Expand All @@ -168,9 +171,9 @@ public function actionCollections(): Response
if ($type) {
$variables['sections'][] = [
'id' => $type->id,
'name' => $type->name,
'name' => ($nameOverride ? $nameOverride : $type->name),
'handle' => $type->handle,
'type' => 'Variant: ' . $type->handle,
'type' => 'Variant: ' . ($nameOverride ? $nameOverride : $type->handle),
'entryCount' => $index->criteria->count(),
'index' => $index->indexName,
];
Expand All @@ -180,9 +183,9 @@ public function actionCollections(): Response
case 'craft\shopify\elements\Product':
$variables['sections'][] = [
'id' => 'shopify-products',
'name' => 'Products',
'name' => ($nameOverride ? $nameOverride : 'Products'),
'handle' => 'products',
'type' => 'Shopify: Products',
'type' => 'Shopify: ' . ($nameOverride ? $nameOverride : 'Products'),
'entryCount' => $index->criteria->count(),
'index' => $index->indexName,
];
Expand Down Expand Up @@ -454,13 +457,13 @@ public function actionDocuments(): Response|string
// Render the template
return $this->renderTemplate('typesense/documents/index', $variables);
// $request = Craft::$app->getRequest();
// $index = $request->getBodyParam('index');
//
// if (isset(Typesense::$plugin->getClient()->client()->collections[$index])) {
// return $this->asJson(Typesense::$plugin->getClient()->client()->collections[$index]->documents->export());
// }
//
// return "this index doesn't exist";
// $index = $request->getBodyParam('index');
//
// if (isset(Typesense::$plugin->getClient()->client()->collections[$index])) {
// return $this->asJson(Typesense::$plugin->getClient()->client()->collections[$index]->documents->export());
// }
//
// return "this index doesn't exist";
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/jobs/SyncDocumentsJob.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Typesense plugin for Craft CMS 5.x
*
Expand All @@ -12,7 +13,6 @@

use Craft;
use craft\queue\BaseJob;

use percipiolondon\typesense\helpers\CollectionHelper;
use percipiolondon\typesense\Typesense;

Expand Down Expand Up @@ -118,6 +118,6 @@ public function execute($queue): void
*/
protected function defaultDescription(): string
{
return Craft::t('typesense', ($this->criteria['type'] ?? 'Unkown') . ' documents for ' . $this->criteria['index']);
return Craft::t('typesense', ($this->criteria['type'] ?? 'Unknown') . ' documents for ' . $this->criteria['index']);
}
}