<?php
class PermissionTable extends AppModel
{
    var $name = 'PermissionTable';
    var $useTable = 'permissions';

    public function getPermissionByUrlAndUserId($unique_url,$user_id) {
        $user_permission = $this->find('first', array(
            'conditions' => array('PermissionTable.unique_url' => $unique_url, 'PermissionTable.user_id' => $user_id)
        ));
        if(empty($user_permission)) {
            return false;
        }
        return $user_permission;

    }
    /*    public function getPermissionByUrl($unique_url,$custom_select = false,$isOwner = false) {

        $options = array(
            'conditions' => array('PermissionTable.unique_url' => $unique_url)
        );

        if ($custom_select) {
            $options['fields'] = array('PermissionTable.profile_id', 'PermissionTable.role');
        }
        if($isOwner) {
            $options['conditions']['PermissionTable.role !='] = OWNER;
        }

        $permissions = $this->find('all', $options);

        if ($custom_select) {
            return array_map(function ($item) {
                return [
                    'profile_id' => $item['PermissionTable']['profile_id'],
                    'role' => $item['PermissionTable']['role']
                ];
            }, $permissions);
        }
        return array_map(function ($item) {
            return [
                'user_id' => $item['PermissionTable']['user_id'],
                'profile_id' => $item['PermissionTable']['profile_id'],
                'role' => $item['PermissionTable']['role']
            ];
        }, $permissions);

    }
    */

    public function getPermissionByUrl($unique_url,$custom_select = false,$isOwner = false){
        $options = array(
            'conditions' => array(
                'PermissionTable.unique_url' => $unique_url
            ),
            'joins' => array(
                array(
                    'table' => 'users',
                    'alias' => 'User',
                    'type' => 'INNER',
                    'conditions' => array(
                        'User.id = PermissionTable.user_id'
                    )
                )
            )
        );

        if ($isOwner) {
            $options['conditions']['PermissionTable.role !='] = OWNER;
        }


        $options['fields'] = array(
            'PermissionTable.user_id',
            'User.profile_id',
            'PermissionTable.role'
        );

        $permissions = $this->find('all', $options);

        if ($custom_select) {
            return array_map(function ($item) {
                return [
                    'profile_id' => $item['User']['profile_id'],
                    'role' => $item['PermissionTable']['role']
                ];
            }, $permissions);
        }

        return array_map(function ($item) {
            return [
                'user_id' => $item['PermissionTable']['user_id'],
                'profile_id' => $item['User']['profile_id'],
                'role' => $item['PermissionTable']['role']
            ];
        }, $permissions);
    }

    public function addPermission($request)
    {
        if (isset($request[0])) {
            return $this->saveMany($request);
        } else {
            return $this->save($request);
        }
    }

    public function deleteByCondition($deleteConditions)
    {
        return $this->deleteAll(
            array($deleteConditions)
        );
    }
}

