<commit_before><?php
/**
 * @package     Koowa_Controller
 * @subpackage  Behavior
 * @copyright   Copyright (C) 2007 - 2012 <NAME>. All rights reserved.
 * @license     GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link        http://www.nooku.org
 */

namespace Nooku\Library;

/**
 * Lockable Controller Behavior Class
 *
 * @author      <NAME> <<EMAIL>>
 * @package     Koowa_Controller
 * @subpackage  Behavior
 */
class ControllerBehaviorLockable extends ControllerBehaviorAbstract
{
    /**
     * Constructor
     *
     * @param ObjectConfig $config An optional ObjectConfig object with configuration options
     */
    public function __construct(ObjectConfig $config)
    {
        parent::__construct($config);

        $this->registerCallback('after.read'  , array($this, 'lockEntity'));
        $this->registerCallback('after.save'  , array($this, 'unlockEntity'));
        $this->registerCallback('after.cancel', array($this, 'unlockEntity'));
    }

    /**
     * Render a lock flash message if the row is locked
     *
     * @param   CommandContext	$context A command context object
     * @return 	void
     */
    protected function _afterControllerRead(CommandContext $context)
    {
        $row = $context->result;

        //Add the notice if the row is locked
        if($row->isLockable() && $row->locked())
        {
            $user = $this->getObject('com:users.database.row.user')
                ->set('id', $row->locked_by)
                ->load();

            $date    = $this->getView()->getTemplate()->getHelper('date')->humanize(array('date' => $row->locked_on));
            $message = \JText::sprintf('Locked by %s %s', $user->get('name'), $date);

            $context->user->addFlashMessage($message, 'notice');
        }
    }

    /**
     * Lock callback
     *
     * Only lock if the context contains a row object and the view layout is 'form'.
     *
     * @param   CommandContext  $context The active command context
     * @return  void
     */
    public function lockEntity(CommandContext $context)
    {
        if ($context->result instanceof DatabaseRowInterface)
        {
            $view = $this->getView();

            if ($view instanceof ViewTemplate)
            {
                if($this->getModel()->getState()->isUnique() && $context->result->isLockable()) {
                    $context->result->lock();
                }
            }
        }
    }

    /**
     * Unlock callback
     *
     * @param  CommandContext  $context    The active command context
     * @return void
     */
    public function unlockEntity(CommandContext $context)
    {
        if ($context->result instanceof DatabaseRowInterface && $context->result->isLockable()) {
            $context->result->unlock();
        }
    }
}<commit_msg>re #378 : Removed DatabaseRowInterface check. Use the state and check if it's unique instead.<commit_after><?php
/**
 * @package     Koowa_Controller
 * @subpackage  Behavior
 * @copyright   Copyright (C) 2007 - 2012 <NAME>. All rights reserved.
 * @license     GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link        http://www.nooku.org
 */

namespace Nooku\Library;

/**
 * Lockable Controller Behavior Class
 *
 * @author      <NAME> <<EMAIL>>
 * @package     Koowa_Controller
 * @subpackage  Behavior
 */
class ControllerBehaviorLockable extends ControllerBehaviorAbstract
{
    /**
     * Constructor
     *
     * @param ObjectConfig $config An optional ObjectConfig object with configuration options
     */
    public function __construct(ObjectConfig $config)
    {
        parent::__construct($config);

        $this->registerCallback('after.read'  , array($this, 'lockEntity'));
        $this->registerCallback('after.save'  , array($this, 'unlockEntity'));
        $this->registerCallback('after.cancel', array($this, 'unlockEntity'));
    }

    /**
     * Render a lock flash message if the row is locked
     *
     * @param   CommandContext	$context A command context object
     * @return 	void
     */
    protected function _afterControllerRead(CommandContext $context)
    {
        //Add the notice if the entity is locked
        if($context->result->isLockable() && $context->result->locked())
        {
            $user = $this->getObject('com:users.database.row.user')
                ->set('id', $context->result->locked_by)
                ->load();

            $date    = $this->getView()->getTemplate()->getHelper('date')->humanize(array('date' => $context->result->locked_on));
            $message = \JText::sprintf('Locked by %s %s', $user->get('name'), $date);

            $context->user->addFlashMessage($message, 'notice');
        }
    }

    /**
     * Lock callback
     *
     * @param   CommandContext  $context The active command context
     * @return  void
     */
    public function lockEntity(CommandContext $context)
    {
        if ($this->getModel()->getState()->isUnique() && $context->result->isLockable())
        {
            if ($this->getView() instanceof ViewTemplate) {
                $context->result->lock();
            }
        }
    }

    /**
     * Unlock callback
     *
     * @param  CommandContext  $context    The active command context
     * @return void
     */
    public function unlockEntity(CommandContext $context)
    {
        if ($this->getModel()->getState()->isUnique() && $context->result->isLockable()) {
            $context->result->unlock();
        }
    }
}
{'fraction_non_alphanumeric': 0.14107959743824336, 'fraction_numerical': 0.0038426349496797803, 'mean_word_length': 2.3657635467980294, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 83, 'https://': 0, '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': '3299642', 'n_tokens_mistral': 1524, 'n_tokens_neox': 1479, 'n_words': 345}