Skip to content

Commit

Permalink
Merge pull request #1303 from dpfaffenbauer/feature/index-version
Browse files Browse the repository at this point in the history
[IndexBundle] allow configuring if versions should be indexed or not
  • Loading branch information
dpfaffenbauer authored Feb 25, 2020
2 parents 6798f45 + e808076 commit f46f66f
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 16 deletions.
56 changes: 56 additions & 0 deletions features/index/index_version.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@index @index_version
Feature: Adding a object index
In order to make my catalog searchable
I want to create a new index
But when the user saves the object as version, the index should not change

Background:
Given there is a pimcore class "BooleanTest"
And the definition has a checkbox field "enabled"
And the definition has a checkbox field "booleanTest"
And the definition has a localized input field "name"
And the definitions parent class is set to "\CoreShop\Behat\Model\Index\TestIndex"
And the site has a index "boolean" for behat-class "BooleanTest" with type "mysql"
And the index has following fields:
| key | name | type | getter | columnType | interpreter |
| booleanTest | booleanTest | checkbox | | BOOLEAN | |
And there is an instance of behat-class "BooleanTest" with key "test1"
And the object-instance has following values:
| key | value | type |
| enabled | true | checkbox |
| booleanTest | false | checkbox |
Then the index column "booleanTest" for object-instance "test1" should have value "0"

Scenario: Save object normally
Given the object-instance has following values:
| key | value | type |
| enabled | true | checkbox |
| booleanTest | true | checkbox |
Then the index column "booleanTest" for object-instance "test1" should have value "1"

Scenario: Save object as version, index should not update
Given the object-instance has following values as version:
| key | value | type |
| enabled | true | checkbox |
| booleanTest | true | checkbox |
Then the index column "booleanTest" for object-instance "test1" should have value "0"

Scenario: Save object normally when index allows version changes
Given the index allows version changes
And the object-instance has following values:
| key | value | type |
| enabled | true | checkbox |
| booleanTest | true | checkbox |
Then the index column "booleanTest" for object-instance "test1" should have value "1"

Scenario: Save object as version when index allows version changes
Given the index allows version changes
And the object-instance has following values as version:
| key | value | type |
| enabled | true | checkbox |
| booleanTest | true | checkbox |
Then the index column "booleanTest" for object-instance "test1" should have value "1"




9 changes: 9 additions & 0 deletions src/CoreShop/Behat/Context/Setup/IndexContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ public function theSiteHasAIndexForClassWithType($name, ClassDefinition $class,
$this->createIndex($name, $class->getName(), $type);
}

/**
* @Given /the (index) allows version changes$/
*/
public function theIndexAllowsVersionChanges(IndexInterface $index)
{
$index->setIndexLastVersion(true);
$this->saveIndex($index);
}

/**
* @Given /the (index) has following fields:/
*/
Expand Down
17 changes: 15 additions & 2 deletions src/CoreShop/Behat/Context/Setup/PimcoreClassContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,21 @@ public function theObjectInstanceIsNotPublished(Concrete $instance)
* @Given /the (object-instance) has following values:/
*/
public function theObjectInstanceHasFollowingValues(Concrete $object, TableNode $table)
{
$this->setObjectValuesFromTable($object, $table);
$object->save();
}

/**
* @Given /the (object-instance) has following values as version:/
*/
public function theObjectInstanceHasFollowingValuesAsVersion(Concrete $object, TableNode $table)
{
$this->setObjectValuesFromTable($object, $table);
$object->saveVersion();
}

private function setObjectValuesFromTable(Concrete $object, TableNode $table)
{
$hash = $table->getHash();

Expand Down Expand Up @@ -726,8 +741,6 @@ public function theObjectInstanceHasFollowingValues(Concrete $object, TableNode
break;
}
}

$object->save();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace CoreShop\Bundle\CoreBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Pimcore\Migrations\Migration\AbstractPimcoreMigration;

class Version20200224160300 extends AbstractPimcoreMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
$this->addSql(' ALTER TABLE coreshop_index ADD indexLastVersion TINYINT(1) DEFAULT \'0\' NOT NULL;');
}

/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,24 @@ public function onPostUpdate(ElementEventInterface $event)
return;
}

InheritanceHelper::useInheritedValues(function () use ($object) {
$this->indexUpdaterService->updateIndices($object);
$isVersionEvent = $event->hasArgument('saveVersionOnly') && true === $event->getArgument('saveVersionOnly');

InheritanceHelper::useInheritedValues(function () use ($object, $isVersionEvent) {
$this->indexUpdaterService->updateIndices($object, $isVersionEvent);
});

$classDefinition = ClassDefinition::getById($object->getClassId());
if ($classDefinition->getAllowInherit() || $classDefinition->getAllowVariants()) {
$this->updateInheritableChildren($object);
$this->updateInheritableChildren($object, $isVersionEvent);
}
}
}

/**
* @param AbstractObject $object
* @param bool $isVersionChange
*/
private function updateInheritableChildren(AbstractObject $object)
private function updateInheritableChildren(AbstractObject $object, bool $isVersionChange)
{
if (!$object->hasChildren($this->validObjectTypes)) {
return;
Expand All @@ -76,10 +79,10 @@ private function updateInheritableChildren(AbstractObject $object)
/** @var AbstractObject $child */
foreach ($children as $child) {
if (get_class($child) === get_class($object)) {
InheritanceHelper::useInheritedValues(function () use ($child) {
$this->indexUpdaterService->updateIndices($child);
InheritanceHelper::useInheritedValues(function () use ($child, $isVersionChange) {
$this->indexUpdaterService->updateIndices($child, $isVersionChange);
});
$this->updateInheritableChildren($child);
$this->updateInheritableChildren($child, $isVersionChange);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/CoreShop/Bundle/IndexBundle/Form/Type/IndexType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CoreShop\Bundle\ResourceBundle\Form\Registry\FormTypeRegistryInterface;
use CoreShop\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use CoreShop\Bundle\ResourceBundle\Form\Type\PimcoreClassChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
Expand Down Expand Up @@ -47,7 +48,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->add('name', TextType::class)
->add('worker', IndexWorkerChoiceType::class)
->add('class', PimcoreClassChoiceType::class)
->add('columns', IndexColumnCollectionType::class);
->add('columns', IndexColumnCollectionType::class)
->add('indexLastVersion', CheckboxType::class);

$builder
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace CoreShop\Bundle\IndexBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Pimcore\Migrations\Migration\AbstractPimcoreMigration;

class Version20200224161101 extends AbstractPimcoreMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
$this->addSql(' ALTER TABLE coreshop_index ADD indexLastVersion TINYINT(1) DEFAULT \'0\' NOT NULL;');
}

/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ CoreShop\Component\Index\Model\Index:
configuration:
column: configuration
type: array
indexLastVersion:
column: indexLastVersion
type: boolean
options:
default: false
creationDate:
type: datetime
gedmo:
Expand All @@ -41,4 +46,4 @@ CoreShop\Component\Index\Model\Index:
cascade:
- all
orderBy:
name: ASC
name: ASC
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ CoreShop\Component\Index\Model\Index:
columns:
expose: true
type: array
groups: [Detailed]
groups: [Detailed]
indexLastVersion:
type: boolean
groups: [Detailed]
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ coreshop.index.item = Class.create(coreshop.resource.item, {
this.getIndexWorkerConfig(value);
}.bind(this)
}
},
{
xtype: 'checkbox',
fieldLabel: t('coreshop_index_last_version'),
name: 'indexLastVersion',
checked: this.data.indexLastVersion
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ coreshop_filters_nested: 'Nested'
coreshop_index_mysql_index: 'MySQL Index'
coreshop_index_mysql_index_localized: 'MySQL Index Localized'
processmanager_type_coreshop_index: 'CoreShop Index'
coreshop_index_last_version: 'Always Index Last Version, even if Version is not published yet'
21 changes: 21 additions & 0 deletions src/CoreShop/Component/Index/Model/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class Index extends AbstractResource implements IndexInterface
*/
protected $columns;

/**
* @var bool
*/
protected $indexLastVersion = false;

public function __construct()
{
$this->columns = new ArrayCollection();
Expand Down Expand Up @@ -173,4 +178,20 @@ public function hasColumn(IndexColumnInterface $column)
{
return $this->columns->contains($column);
}

/**
* {@inheritdoc}
*/
public function getIndexLastVersion()
{
return $this->indexLastVersion;
}

/**
* {@inheritdoc}
*/
public function setIndexLastVersion($indexLastVersion)
{
$this->indexLastVersion = $indexLastVersion;
}
}
10 changes: 10 additions & 0 deletions src/CoreShop/Component/Index/Model/IndexInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,14 @@ public function getConfiguration();
* @param array $configuration
*/
public function setConfiguration($configuration);

/**
* @return bool
*/
public function getIndexLastVersion();

/**
* @param bool $indexLastVersion
*/
public function setIndexLastVersion($indexLastVersion);
}
12 changes: 9 additions & 3 deletions src/CoreShop/Component/Index/Service/IndexUpdaterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public function __construct(RepositoryInterface $indexRepository, ServiceRegistr
/**
* {@inheritdoc}
*/
public function updateIndices($subject)
public function updateIndices($subject, bool $isVersionEvent = false)
{
$this->operationOnIndex($subject, 'update');
$this->operationOnIndex($subject, 'update', $isVersionEvent);
}

/**
Expand All @@ -61,8 +61,9 @@ public function removeIndices($subject)
/**
* @param string $subject
* @param string $operation
* @param bool $isVersionChange
*/
private function operationOnIndex($subject, $operation = 'update')
private function operationOnIndex($subject, $operation = 'update', bool $isVersionChange = false)
{
$indices = $this->indexRepository->findAll();

Expand All @@ -75,6 +76,11 @@ private function operationOnIndex($subject, $operation = 'update')
continue;
}

//Don't store version changes into the index!
if ($isVersionChange && !$index->getIndexLastVersion()) {
continue;
}

/**
* @var IndexableInterface $subject
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ interface IndexUpdaterServiceInterface
* Update all Indices with $subject.
*
* @param mixed $subject
* @param bool $isVersionChange
*/
public function updateIndices($subject);
public function updateIndices($subject, bool $isVersionChange = false);

/**
* Remove all Indices with $subject.
Expand Down

0 comments on commit f46f66f

Please sign in to comment.