<?php
/**
 * @package     DOCman
 * @copyright   Copyright (C) 2019 Timble CVBA. (http://www.timble.net)
 * @license     GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link        http://www.joomlatools.com
 */

namespace Foliolabs\Docman;

use Foliolabs\Library;

class ModelBehaviorGroupable extends Library\ModelBehaviorAbstract
{
    public function onMixin(Library\ObjectMixable $mixer)
    {
        parent::onMixin($mixer);

        $mixer->getState()->insert('group', 'int');
    }

    protected function _beforeFetch(Library\ModelContextInterface $context)
    {
        $query = $context->getQuery();

        $query->join('docman_usergroups_users AS rel', 'rel.wp_user_id = tbl.ID', 'INNER')
              ->join('docman_usergroups AS usergroups', 'rel.docman_usergroup_id = usergroups.docman_usergroup_id', 'INNER');

        $state = $context->getState();

        if ($group = $state->group) {
            $query->where('usergroups.docman_usergroup_id IN :group')->bind(['group' => (array) $group]);
        }
    }
}