Skip to content

Commit

Permalink
Merge pull request #1 from Weble/feature/type-index
Browse files Browse the repository at this point in the history
Multi type index support
  • Loading branch information
sisilvic authored Nov 3, 2021
2 parents c66d1b2 + e7b6371 commit a884c46
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 40 deletions.
2 changes: 1 addition & 1 deletion plugin/config/application.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<param name="algolia_app_id" type="text" description="Algolia Application Id" label="Algolia App Id" default="" />
<param name="algolia_search_key" type="text" description="Algolia Search Key" label="Algolia Search Key" default="" />
<param name="algolia_secret_key" type="text" description="Algolia Secret Key" label="Algolia Secret Key" default="" />
<param name="algolia_index" type="text" description="Algolia Index name" label="Algolia Index Name" default="" />

</params>
</application>
</applications>
52 changes: 35 additions & 17 deletions plugin/src/AlgoliaSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,25 @@ class AlgoliaSync
private array $categories = [];
private Application $application;

public function __construct(\Application $application)
public function __construct(\Type $type)
{
$this->application = $application;
$this->zoo = $application->app;
$this->application = $type->getApplication();


$this->zoo = $this->application->app;
$this->renderer = $this->zoo->renderer->create('item', ['path' => $this->zoo->path]);

if ($application->getParams()->get('global.config.algolia_app_id') && $application->getParams()->get('global.config.algolia_app_id')) {
if ($this->application->getParams()->get('global.config.algolia_app_id') && $this->application->getParams()->get('global.config.algolia_app_id')) {
$this->client = SearchClient::create(
$application->getParams()->get('global.config.algolia_app_id'),
$application->getParams()->get('global.config.algolia_secret_key')
$this->application->getParams()->get('global.config.algolia_app_id'),
$this->application->getParams()->get('global.config.algolia_secret_key')
);
}

if ($this->client && $application->getParams()->get('global.config.algolia_index')) {
$this->index = $this->client->initIndex($application->getParams()->get('global.config.algolia_index'));
if ($this->client && $this->application->getParams()->get('global.config.algolia_index_'. $type->identifier)) {
$this->index = $this->client->initIndex($this->application->getParams()->get('global.config.algolia_index_'. $type->identifier));
}

}

public function isConfigured(): bool
Expand Down Expand Up @@ -121,7 +124,7 @@ public function sync(\Item $item): bool
return $this->index->deleteObject($item->id)->valid();
}

private function algoliaData(\Item $item): ?array
private function algoliaData(\Item $item, $full_related_data = true): ?array
{
if (!$item->isPublished()) {
return null;
Expand Down Expand Up @@ -175,7 +178,7 @@ private function algoliaData(\Item $item): ?array
$key = $elementConfig['altlabel'];
}

$value = $this->elementValueFor($element, $elementConfig);
$value = $this->elementValueFor($element, $elementConfig, $full_related_data);

$data[$key] = $value;

Expand All @@ -197,7 +200,7 @@ private function algoliaData(\Item $item): ?array
return $data;
}

private function elementValueFor(\Element $element, array $params)
private function elementValueFor(\Element $element, array $params, $full_related_data = true)
{
$value = null;
$this->zoo->event->dispatcher->notify($this->zoo->event->create($element, 'algolia:beforeelementdata', ['value' => &$value]));
Expand All @@ -218,6 +221,11 @@ private function elementValueFor(\Element $element, array $params)

if ($element instanceof \ElementTextPro || $element instanceof ElementTextareaPro) {

$data = $element->data();
if ($data === null) {
$data = [];
}

$values = array_filter(array_map(function ($item) use ($params) {
$value = $item['value'] ?? '';
$value = strip_tags($value);
Expand All @@ -229,7 +237,7 @@ private function elementValueFor(\Element $element, array $params)
}

return strlen($value) > 0 ? $value : null;
}, $element->data()));
}, $data));

$repeatable = $element->config->get('repeatable', false);
if ($params['filter']['_limit'] ?? null === 1) {
Expand All @@ -255,6 +263,7 @@ private function elementValueFor(\Element $element, array $params)
return $data;
}


if ($element instanceof \ElementFilesPro) {

if (!$element->data()) {
Expand Down Expand Up @@ -311,16 +320,23 @@ private function elementValueFor(\Element $element, array $params)
return $values ? array_shift($values) : null;
}


if ($element instanceof \ElementRelatedItemsPro) {
$related_items = $element->getRelatedItems(true);

$related_items = $element->getRelatedItems(true);
$items = [];

foreach ($related_items as $item)
{
$data = $this->algoliaData($item);
foreach ($related_items as $item) {

if ($full_related_data) {
$data = $this->algoliaData($item, false);
} else {
$data = [];
$data['id'] = $item->id;
}

if ($data) {
$items[] = $this->algoliaData(($item));
$items[] = $data;
}
}

Expand All @@ -345,6 +361,7 @@ private function elementValueFor(\Element $element, array $params)
];
}


if ($element instanceof \ElementRepeatable) {

$values = array_filter(array_map(function ($item) {
Expand All @@ -359,6 +376,7 @@ private function elementValueFor(\Element $element, array $params)
return array_shift($values);
}


if ($element instanceof \ElementItemTag) {
return $element->getItem()->getTags();
}
Expand Down
45 changes: 25 additions & 20 deletions plugin/src/AlgoliaSyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,41 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

foreach ($applications as $application) {
$algoliaSync = new AlgoliaSync($application);
if (!$algoliaSync->isConfigured()) {
continue;
}
foreach ($application->getTypes() as $app_type) {

$output->writeln('Import Items from ' . $application->name);
if ($type !== null && $type !== $app_type->identifier) {
continue;
}

/** @var Item[] $items */
if ($ids) {
$items = $zoo->table->item->all(['conditions' => 'application_id = ' . $application->id . ' AND id IN ( ' . implode(",", $ids) . ')']);
$total = count($items);
} else {
$items = $zoo->table->item->findAll($application->id);
$total = $application->getItemCount();
}
$algoliaSync = new AlgoliaSync($app_type);

if (!$algoliaSync->isConfigured()) {
continue;
}
$output->writeln("\nImport Items from " . $application->name . " of type " . $app_type->getName());

/** @var Item[] $items */
if ($ids) {
$items = $zoo->table->item->all(['conditions' => 'type = ' . $app_type->identifier . ' AND id IN ( ' . implode(",", $ids) . ')']);
$total = count($items);
} else {
//$items = $zoo->table->item->findAll($application->id);
$items = $zoo->table->item->getByType($app_type->identifier, $application->id);
$total = count($items);
}

$progress = new ProgressBar($output, $total);
$progress = new ProgressBar($output, $total);

foreach ($items as $item) {
foreach ($items as $item) {

if ($type !== null && $type !== $item->getType()->id) {
continue;
$progress->advance();
$algoliaSync->sync($item);
}

$progress->advance();
$algoliaSync->sync($item);
$progress->finish();

}

$progress->finish();
}

return 0;
Expand Down
22 changes: 20 additions & 2 deletions plugin/zooalgolia.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function sync(AppEvent $event)
/** @var \Item $item */
$item = $event->getSubject();

$algoliaSync = new AlgoliaSync($item->getApplication());
$algoliaSync = new AlgoliaSync($item->getType());
$algoliaSync->sync($item);
}

Expand All @@ -83,7 +83,7 @@ public function removeItems(AppEvent $event)
/** @var \Item $item */
$item = $event->getSubject();

$algoliaSync = new AlgoliaSync($item->getApplication());
$algoliaSync = new AlgoliaSync($item->getType());
$algoliaSync->batchDelete([$event->getSubject()->id]);
}

Expand Down Expand Up @@ -197,13 +197,31 @@ private function generateNewXmlFile(\SimpleXMLElement $old_xml, \SimpleXMLElemen

// Add the parameters for this group
foreach ($param->param as $p) {

// If it doesn't exists already
if (!count($ops->xpath("param[@name='" . $p->attributes()->name . "']"))) {

// Push changes with default
$p->attributes()->default = $this->params->get($p->attributes()->name, $p->attributes()->default);
$this->appendChild($ops, $p);
$app_file_changed = true;
}

}

foreach ($app->getTypes() as $type) {
if (!count($ops->xpath("param[@name='algolia_index_" . $type->identifier . "']"))) {

$p = new SimpleXMLElement('<param></param>');
$p->addAttribute('name', 'algolia_index_' . $type->identifier);
$p->addAttribute('type', 'text');
$p->addAttribute('label', 'Algolia Index for '. $type->name);
$p->addAttribute('description', '');
$p->addAttribute('default', '');

$this->appendChild($ops, $p);
$app_file_changed = true;
}
}
}

Expand Down

0 comments on commit a884c46

Please sign in to comment.