<commit_before><?php
/**
 * Nooku Framework - http://www.nooku.org
 *
 * @copyright	Copyright (C) 2011 - 2013 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link		git://git.assembla.com/nooku-framework.git for the canonical source repository
 */

namespace Nooku\Component\Tags;

use Nooku\Library;

/**
 * Taggable Controller Behavior
 *
 * @author  <NAME> <http://nooku.assembla.com/profile/johanjanssens>
 * @package Nooku\Component\Tags
 */
class ControllerBehaviorTaggable extends Library\BehaviorAbstract
{
    public function __construct(Library\ObjectConfig $config)
    {
        parent::__construct($config);

        $this->_container = $config->container;

        $this->addCommandCallback('after.add'   , '_saveTags');
        $this->addCommandCallback('after.edit'  , '_saveTags');
        $this->addCommandCallback('after.delete', '_deleteTags');
    }

    protected function _saveTags(Library\ControllerContextInterface $context)
    {
		if (!$context->response->isError())
        {
            $entity = $context->result;
            $table  = $entity->getTable()->getBase();

            // Remove all existing relations
            if($entity->id && $entity->getTable()->getBase())
            {
                $relations = $this->getObject('com:tags.model.relations')
                    ->row($entity->id)
                    ->table($table)
                    ->fetch();

                $relations->delete();
            }

            if($entity->tags)
            {
                // Save tags as relations
                foreach ($entity->tags as $tag)
                {
                    $properties = array(
                        'id'    => $tag,
                        'row'   => $entity->id,
                        'table' => $table
                    );

                    $relation = $this->getObject('com:tags.model.relations')
                        ->setState($properties)
                        ->fetch();

                    if($relation->isNew)
                    {
                        $relation->setProperties($properties);
                        $relation->save();
                    }
                }
            }

            return true;
		}
	}
	
	protected function _deleteTags(Library\ControllerContextInterface $context)
    {
        $entity = $context->result;
        $status = $entity->getStatus();

        if($status == $entity::STATUS_DELETED || $status == 'trashed')<commit_msg>Fixed relation saving in ControllerBehaviorTaggable<commit_after><?php
/**
 * Nooku Framework - http://www.nooku.org
 *
 * @copyright	Copyright (C) 2011 - 2013 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link		git://git.assembla.com/nooku-framework.git for the canonical source repository
 */

namespace Nooku\Component\Tags;

use Nooku\Library;

/**
 * Taggable Controller Behavior
 *
 * @author  <NAME> <http://nooku.assembla.com/profile/johanjanssens>
 * @package Nooku\Component\Tags
 */
class ControllerBehaviorTaggable extends Library\BehaviorAbstract
{
    public function __construct(Library\ObjectConfig $config)
    {
        parent::__construct($config);

        $this->_container = $config->container;

        $this->addCommandCallback('after.add'   , '_saveTags');
        $this->addCommandCallback('after.edit'  , '_saveTags');
        $this->addCommandCallback('after.delete', '_deleteTags');
    }

    protected function _saveTags(Library\ControllerContextInterface $context)
    {
		if (!$context->response->isError())
        {
            $entity = $context->result;
            $table  = $entity->getTable()->getBase();

            // Remove all existing relations
            if($entity->id && $entity->getTable()->getBase())
            {
                $relations = $this->getObject('com:tags.model.relations')
                    ->row($entity->id)
                    ->table($table)
                    ->fetch();

                $relations->delete();
            }

            if($entity->tags)
            {
                // Save tags as relations
                foreach ($entity->tags as $tag)
                {
                    $properties = array(
                        'id'    => $tag,
                        'row'   => $entity->id,
                        'table' => $table
                    );

                    $relation = $this->getObject('com:tags.model.relations')
                        ->setState($properties)
                        ->fetch();

                    if($relation->isNew())
                    {
                        $relation = $this->getObject('com:tags.model.relations')->create();
                        $relation->setProperties($properties);
                        $relation->save();
                    }
                }
            }

            return true;
		}
	}
	
	protected function _deleteTags(Library\ControllerContextInterface $context)
    {
        $entity = $context->result;
        $status = $entity->getStatus();

        if($status == $entity::STATUS_DELETED || $status == 'trashed')
{'fraction_non_alphanumeric': 0.1389745566692367, 'fraction_numerical': 0.003469545104086353, 'mean_word_length': 1.671987641606591, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '521126', 'n_tokens_mistral': 1436, 'n_tokens_neox': 1333, 'n_words': 233}