Skip to content

Commit

Permalink
Merge pull request #13 from joomlatools/feature/12-ops
Browse files Browse the repository at this point in the history
Add support for different tag operations
  • Loading branch information
ercanozkaya authored Jul 13, 2016
2 parents bfa165d + 0306596 commit 186ac81
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 37 deletions.
128 changes: 93 additions & 35 deletions controller/behavior/taggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand All @@ -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
*/
Expand All @@ -43,59 +46,114 @@ 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())
{
$tags = $entity->getTags();
$operation = $entity->tags_operation;

$package = $this->getMixer()->getIdentifier()->package;
if(!$this->getObject('com:'.$package.'.controller.tag')->canAdd()) {
$status = KDatabase::STATUS_FETCHED;
if ($operation === 'remove') {
$this->_removeTags($entity);
} else if ($operation === 'append') {
$this->_appendTags($entity);
} else {
$status = null;
$this->_replaceTags($entity);
}
}
}
}
}

//Delete tags
if(count($tags))
{
$tags->delete();
$tags->reset();
}
/**
* Replaces the entity tags with the ones sent in the request
*
* @param KModelEntityInterface $entity
*/
protected function _replaceTags(KModelEntityInterface $entity)
{
$tags = $entity->getTags();

//Create tags
if($entity->tags)
{
foreach ($entity->tags as $tag)
{
$config = array(
'data' => array(
'title' => $tag,
'row' => $entity->uuid,
),
'status' => $status,
);

$row = $tags->getTable()->createRow($config);

$tags->insert($row);
$tags->save();
}
}
//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();

Expand Down
7 changes: 5 additions & 2 deletions model/entity/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 186ac81

Please sign in to comment.