<commit_before><?php
/**
 * Belgian Police Web Platform - Streets Component
 *
 * @copyright	Copyright (C) 2012 - 2013 Timble CVBA. (http://www.timble.net)
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link		https://github.com/belgianpolice/internet-platform
 */

namespace Nooku\Component\Streets;

use Nooku\Library;

/**
 * Locatable Controller Behavior
 */
class ControllerBehaviorLocatable extends Library\BehaviorAbstract
{
    protected function _saveRelations(Library\CommandContext $context)
    {
        if ($context->error) {
            return;
        }

        $row   = $context->result;
        $table = $row->getTable()->getBase();

        // Remove all existing relations
        if($row->id && $table)
        {
            $rows = $this->getObject('com:streets.model.relations')
                ->row($row->id)
                ->table($table)
                ->getRowset();

            $rows->delete();
        }

        // Force array
        $streets = (array) $row->streets;

        if($streets)
        {
            // Save streets as relations
            foreach ($streets as $street)
            {
                $relation = $this->getObject('com:streets.database.row.relation');
                $relation->streets_street_id    = $street;
                $relation->row		            = $row->id;
                $relation->table                = $table;

                if(!$relation->load()) {
                    $relation->save();
                }
            }
        }

        return true;
    }

    protected function _afterControllerAdd(Library\CommandContext $context)
    {
        $this->_saveRelations($context);
    }

    protected function _afterControllerEdit(Library\CommandContext $context)
    {
        $this->_saveRelations($context);
    }

    protected function _afterControllerDelete(Library\CommandContext $context)
    {
        $status = $context->result->getStatus();

        if($status == Library\Database::STATUS_DELETED || $status == 'trashed')
        {
            $id = $context->result->get('id');
            $table = $context->result->getTable()->getBase();

            if(!empty($id) && $id != 0)
            {
                $rows = $this->getObject('com:streets.model.relations')
                    ->row($id)
                    ->table($table)
                    ->getRowset();

                $rows->delete();
            }
        }
    }
}<commit_msg>#24 - Don't save empty street relation<commit_after><?php
/**
 * Belgian Police Web Platform - Streets Component
 *
 * @copyright	Copyright (C) 2012 - 2013 Timble CVBA. (http://www.timble.net)
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link		https://github.com/belgianpolice/internet-platform
 */

namespace Nooku\Component\Streets;

use Nooku\Library;

/**
 * Locatable Controller Behavior
 */
class ControllerBehaviorLocatable extends Library\BehaviorAbstract
{
    protected function _saveRelations(Library\CommandContext $context)
    {
        if ($context->error) {
            return;
        }

        $row   = $context->result;
        $table = $row->getTable()->getBase();

        // Remove all existing relations
        if($row->id && $table)
        {
            $rows = $this->getObject('com:streets.model.relations')
                ->row($row->id)
                ->table($table)
                ->getRowset();

            $rows->delete();
        }

        if($row->streets)
        {
            // Force array
            $streets = (array) $row->streets;

            // Save streets as relations
            foreach ($streets as $street)
            {
                $relation = $this->getObject('com:streets.database.row.relation');
                $relation->streets_street_id    = $street;
                $relation->row		            = $row->id;
                $relation->table                = $table;

                if(!$relation->load()) {
                    $relation->save();
                }
            }
        }

        return true;
    }

    protected function _afterControllerAdd(Library\CommandContext $context)
    {
        $this->_saveRelations($context);
    }

    protected function _afterControllerEdit(Library\CommandContext $context)
    {
        $this->_saveRelations($context);
    }

    protected function _afterControllerDelete(Library\CommandContext $context)
    {
        $status = $context->result->getStatus();

        if($status == Library\Database::STATUS_DELETED || $status == 'trashed')
        {
            $id = $context->result->get('id');
            $table = $context->result->getTable()->getBase();

            if(!empty($id) && $id != 0)
            {
                $rows = $this->getObject('com:streets.model.relations')
                    ->row($id)
                    ->table($table)
                    ->getRowset();

                $rows->delete();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.14017749092375958, 'fraction_numerical': 0.004437273093989512, 'mean_word_length': 1.6819902650081124, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 72, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, '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': '2489606', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1315, 'n_words': 233}