Skip to content

Commit

Permalink
fix: syntax for virtual page integration
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Aug 30, 2024
1 parent 07a8ff9 commit 600debe
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
6 changes: 5 additions & 1 deletion _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ SilverStripe\CMS\Model\SiteTree:
SilverStripe\Subsites\Pages\SubsitesVirtualPage:
extensions:
- Wilr\SilverStripe\Algolia\Extensions\SubsitesVirtualPageExtension
ilverStripe\CMS\Model\VirtualPage:
SilverStripe\CMS\Model\VirtualPage:
extensions:
- Wilr\SilverStripe\Algolia\Extensions\VirtualPageExtension
non_virtual_fields:
- AlgoliaUUID
- AlgoliaIndexed
- AlgoliaError
12 changes: 9 additions & 3 deletions src/Extensions/AlgoliaObjectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,15 @@ private function updateAlgoliaFields($fields)

if ($table && count($fields)) {
$sets = [];

foreach ($fields as $field => $value) {
$sets[] = "$field = $value";
}

$set = implode(', ', $sets);
DB::query(sprintf('UPDATE %s SET %s WHERE ID = %s', $table, $set, $this->owner->ID));
$query = sprintf('UPDATE %s SET %s WHERE ID = %s', $table, $set, $this->owner->ID);

DB::query($query);

if ($this->owner->hasExtension(Versioned::class) && $this->owner->hasStages()) {
DB::query(
Expand Down Expand Up @@ -311,10 +315,12 @@ public function onBeforeWrite()
public function assignAlgoliaUUID($writeImmediately = true)
{
$uuid = Uuid::uuid4();
$this->owner->AlgoliaUUID = $uuid->toString();
$value = $uuid->toString();

$this->owner->AlgoliaUUID = $value;

if ($writeImmediately) {
$this->updateAlgoliaFields(['AlgoliaUUID' => "'" . $this->owner->AlgoliaUUID . "'"]);
$this->updateAlgoliaFields(['AlgoliaUUID' => "'$value'"]);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Extensions/SubsitesVirtualPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function exportObjectToAlgolia($toIndex)
}

$attributes->push('objectClassName', $originalObject->ClassName);

$specs = $originalObject->config()->get('algolia_index_fields');
$attributes = $indexer->addSpecsToAttributes($originalObject, $attributes, $specs);

Expand Down
13 changes: 9 additions & 4 deletions src/Tasks/AlgoliaReindexItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Wilr\SilverStripe\Algolia\Tasks;

use SilverStripe\CMS\Model\VirtualPage;
use SilverStripe\Core\Environment;
use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DataObject;
Expand All @@ -24,14 +25,14 @@ public function run($request)
if ($request->getVar('class')) {
$targetClass = $request->getVar('class');
} else {
echo 'Missing class';
echo 'Missing class argument';
exit;
}

if ($request->getVar('id')) {
$id = $request->getVar('id');
} else {
echo 'Missing class';
echo 'Missing id argument';
exit;
}

Expand All @@ -44,15 +45,19 @@ public function run($request)

// Set AlgoliaUUID, in case it wasn't previously set
if (!$obj->AlgoliaUUID) {
echo 'No AlgoliaUUID set on object, generating one...' . PHP_EOL;
$obj->assignAlgoliaUUID(true);
}


$result = $obj->doImmediateIndexInAlgolia();

echo sprintf(
'Indexed: %s, UUID: %s',
'Indexed: %s%sUUID: %s%s%s',
$result ? 'true' : 'false',
$obj->AlgoliaUUID,
PHP_EOL,
$obj->AlgoliaUUID ? $obj->AlgoliaUUID : 'No ID set',
PHP_EOL,
$obj->AlgoliaError ? 'Error from Algolia: ' . $obj->AlgoliaError : ''
);
}
Expand Down

0 comments on commit 600debe

Please sign in to comment.