From 2e0d26b6ba0c6b1b2b54daf5ea26d6b40bc43e27 Mon Sep 17 00:00:00 2001 From: Ercan Ozkaya Date: Wed, 13 Jul 2016 18:10:23 +0300 Subject: [PATCH 1/3] re #12: Add support for different tag operations --- controller/behavior/taggable.php | 70 ++++++++++++++++++++++---------- model/entity/tag.php | 7 +++- 2 files changed, 53 insertions(+), 24 deletions(-) diff --git a/controller/behavior/taggable.php b/controller/behavior/taggable.php index 4d03b84..1952006 100644 --- a/controller/behavior/taggable.php +++ b/controller/behavior/taggable.php @@ -49,8 +49,6 @@ protected function _setTags(KControllerContextInterface $context) { if ($entity->isIdentifiable() && !$context->response->isError()) { - $tags = $entity->getTags(); - $package = $this->getMixer()->getIdentifier()->package; if(!$this->getObject('com:'.$package.'.controller.tag')->canAdd()) { $status = KDatabase::STATUS_FETCHED; @@ -58,32 +56,60 @@ protected function _setTags(KControllerContextInterface $context) $status = null; } - //Delete tags - if(count($tags)) - { - $tags->delete(); - $tags->reset(); + $tags = $entity->getTags(); + $operation = $entity->tags_operation; + + if (!in_array($entity->tags_operation, array('append', 'replace', 'remove'))) { + $operation = 'replace'; } - //Create tags - if($entity->tags) + if ($operation === 'remove') { - foreach ($entity->tags as $tag) + foreach ($tags as $tag) { + if (in_array($tag->title, $entity->tags)) { + $tag->delete(); + } + } + } + else { + //Delete tags + if($operation === 'replace' && count($tags)) { - $config = array( - 'data' => array( - 'title' => $tag, - 'row' => $entity->uuid, - ), - 'status' => $status, - ); - - $row = $tags->getTable()->createRow($config); - - $tags->insert($row); - $tags->save(); + $tags->delete(); + $tags->reset(); + } + + $existing = array(); + foreach ($tags as $tag) { + $existing[] = $tag->title; + } + + //Create tags + if($entity->tags) + { + foreach ($entity->tags as $tag) + { + if (in_array($tag, $existing)) { + continue; + } + + $config = array( + 'data' => array( + 'title' => $tag, + 'row' => $entity->uuid, + ), + 'status' => $status, + ); + + $row = $tags->getTable()->createRow($config); + + $tags->insert($row); + $tags->save(); + } } } + + } } } diff --git a/model/entity/tag.php b/model/entity/tag.php index 47b229a..e4fce9d 100644 --- a/model/entity/tag.php +++ b/model/entity/tag.php @@ -51,9 +51,12 @@ public function save() $name = $this->getTable()->getName().'_relations'; $table = $this->getObject('com:tags.database.table.relations', array('name' => $name)); - $relation = $table->createRow(array('data' => $data)); - $result = $table->insert($relation); + if (!$table->count($data)) { + $relation = $table->createRow(array('data' => $data)); + + $result = $table->insert($relation); + } } } else $result = parent::save(); From 2820a619bcbc3f135559913c9d0adc9b03358f92 Mon Sep 17 00:00:00 2001 From: Ercan Ozkaya Date: Wed, 13 Jul 2016 19:20:41 +0300 Subject: [PATCH 2/3] re #12: Cleanup code and move into multiple methods --- controller/behavior/taggable.php | 150 ++++++++++++++++++------------- 1 file changed, 90 insertions(+), 60 deletions(-) diff --git a/controller/behavior/taggable.php b/controller/behavior/taggable.php index 1952006..ceef458 100644 --- a/controller/behavior/taggable.php +++ b/controller/behavior/taggable.php @@ -26,7 +26,7 @@ public function __construct(KObjectConfig $config) $this->addCommandCallback('after.add' , '_setTags'); $this->addCommandCallback('after.edit' , '_setTags'); - $this->addCommandCallback('before.delete' , '_removeTags'); + $this->addCommandCallback('before.delete' , '_deleteTags'); } /** @@ -35,6 +35,9 @@ public function __construct(KObjectConfig $config) * If the request data contains a tags array, it will be used as the new tag list. * If the tags field is an empty string, all entity tags are deleted and no new ones are added. * + * Operation mode can be controlled by the tags_operation in the request data. + * Possible values are append|remove|replace, replace being the default. + * * @param KControllerContextInterface $context * @return bool */ @@ -43,85 +46,112 @@ protected function _setTags(KControllerContextInterface $context) $entities = $context->result; $data = $context->getRequest()->getData(); - if ($data->has('tags')) + if ($data->has('tags') && !$context->response->isError()) { foreach($entities as $entity) { - if ($entity->isIdentifiable() && !$context->response->isError()) + if ($entity->isIdentifiable()) { - $package = $this->getMixer()->getIdentifier()->package; - if(!$this->getObject('com:'.$package.'.controller.tag')->canAdd()) { - $status = KDatabase::STATUS_FETCHED; - } else { - $status = null; - } - - $tags = $entity->getTags(); $operation = $entity->tags_operation; - if (!in_array($entity->tags_operation, array('append', 'replace', 'remove'))) { - $operation = 'replace'; + if ($operation === 'remove') { + $this->_removeTags($entity); + } else if ($operation === 'append') { + $this->_appendTags($entity); } + else $this->_replaceTags($entity); + } + } + } + } - if ($operation === 'remove') - { - foreach ($tags as $tag) { - if (in_array($tag->title, $entity->tags)) { - $tag->delete(); - } - } - } - else { - //Delete tags - if($operation === 'replace' && count($tags)) - { - $tags->delete(); - $tags->reset(); - } - - $existing = array(); - foreach ($tags as $tag) { - $existing[] = $tag->title; - } - - //Create tags - if($entity->tags) - { - foreach ($entity->tags as $tag) - { - if (in_array($tag, $existing)) { - continue; - } - - $config = array( - 'data' => array( - 'title' => $tag, - 'row' => $entity->uuid, - ), - 'status' => $status, - ); - - $row = $tags->getTable()->createRow($config); - - $tags->insert($row); - $tags->save(); - } - } - } + /** + * Replaces the entity tags with the ones sent in the request + * + * @param KModelEntityInterface $entity + */ + protected function _replaceTags(KModelEntityInterface $entity) + { + $tags = $entity->getTags(); + + //Delete tags + if(count($tags)) + { + $tags->delete(); + $tags->reset(); + } + + $this->_appendTags($entity); + } + + /** + * Appends the tags sent in the request to the entity + * + * @param KModelEntityInterface $entity + */ + protected function _appendTags(KModelEntityInterface $entity) + { + $package = $this->getMixer()->getIdentifier()->package; + if(!$this->getObject('com:'.$package.'.controller.tag')->canAdd()) { + $status = KDatabase::STATUS_FETCHED; + } else { + $status = null; + } + $tags = $entity->getTags(); + $existing = array(); + foreach ($tags as $tag) { + $existing[] = $tag->title; + } + + //Create tags + if($entity->tags) + { + foreach ($entity->tags as $tag) + { + if (in_array($tag, $existing)) { + continue; } + + $config = array( + 'data' => array( + 'title' => $tag, + 'row' => $entity->uuid, + ), + 'status' => $status, + ); + + $row = $tags->getTable()->createRow($config); + + $tags->insert($row); + $tags->save(); + } + } + } + + /** + * Removes the tags sent in the request from the entity + * @param KModelEntityInterface $entity + */ + protected function _removeTags(KModelEntityInterface $entity) + { + $tags = $entity->getTags(); + + foreach ($tags as $tag) { + if (in_array($tag->title, $entity->tags)) { + $tag->delete(); } } } /** - * Remove tags from an entity + * Remove tags from an entity after it is deleted * * @param KControllerContextInterface $context * @return void */ - protected function _removeTags(KControllerContextInterface $context) + protected function _deleteTags(KControllerContextInterface $context) { $entities = $this->getModel()->fetch(); From 0306596f40e67ac378ce894e119a45192c36c03b Mon Sep 17 00:00:00 2001 From: Ercan Ozkaya Date: Wed, 13 Jul 2016 19:37:54 +0300 Subject: [PATCH 3/3] re #12: Formatting --- controller/behavior/taggable.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/controller/behavior/taggable.php b/controller/behavior/taggable.php index ceef458..84acc17 100644 --- a/controller/behavior/taggable.php +++ b/controller/behavior/taggable.php @@ -58,8 +58,9 @@ protected function _setTags(KControllerContextInterface $context) $this->_removeTags($entity); } else if ($operation === 'append') { $this->_appendTags($entity); + } else { + $this->_replaceTags($entity); } - else $this->_replaceTags($entity); } } } @@ -104,7 +105,7 @@ protected function _appendTags(KModelEntityInterface $entity) foreach ($tags as $tag) { $existing[] = $tag->title; } - + //Create tags if($entity->tags) { @@ -138,7 +139,8 @@ protected function _removeTags(KModelEntityInterface $entity) { $tags = $entity->getTags(); - foreach ($tags as $tag) { + foreach ($tags as $tag) + { if (in_array($tag->title, $entity->tags)) { $tag->delete(); }