1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 
<?php

/* --------------------------------------------------------------
   AdminAccessRoleReader.inc.php 2018-02-19
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2017 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

/**
 * Class AdminAccessRoleReader
 *
 * @category   System
 * @package    AdminAccess
 * @subpackage Readers
 */
class AdminAccessRoleReader implements AdminAccessRoleReaderInterface
{
    /**
     * @var CI_DB_query_builder
     */
    protected $queryBuilder;
    
    /**
     * @var AdminAccessRoleFactoryInterface
     */
    protected $roleFactory;
    
    /**
     * @var string
     */
    protected $rolesTable;
    
    /**
     * @var string
     */
    protected $rolesMetaTable;
    
    /**
     * @var string
     */
    protected $customersTable;
    
    /**
     * @var LanguageProviderInterface
     */
    protected $languageProvider;
    
    
    /**
     * AdminAccessRoleReader constructor.
     *
     * @param CI_DB_query_builder             $queryBuilder     Query builder.
     * @param AdminAccessRoleFactoryInterface $roleFactory      Role Factory.
     * @param LanguageProviderInterface       $languageProvider Language provider.
     */
    public function __construct(CI_DB_query_builder $queryBuilder,
                                AdminAccessRoleFactoryInterface $roleFactory,
                                LanguageProviderInterface $languageProvider)
    {
        $this->queryBuilder     = $queryBuilder;
        $this->roleFactory      = $roleFactory;
        $this->languageProvider = $languageProvider;
        
        $this->rolesTable     = 'admin_access_roles';
        $this->rolesMetaTable = 'admin_access_role_descriptions';
        $this->customersTable = 'admin_access_users';
    }
    
    
    /**
     * Returns all available roles as a role collection.
     *
     * @param IdType $roleId Role ID.
     *
     * @throws RoleNotFoundException When role is not found in db.
     * @return AdminAccessRoleInterface Role object.
     */
    public function getById(IdType $roleId)
    {
        $roleData = $this->queryBuilder->select()
                                       ->from($this->rolesTable)
                                       ->where('admin_access_role_id', $roleId->asInt())
                                       ->get()
                                       ->row_array();
        
        if(empty($roleData))
        {
            throw new RoleNotFoundException($roleId);
        }
        
        $roleData['metaData'] = $this->_getRoleMetaData($roleData['admin_access_role_id']);
        
        return $this->_createAdminAccessRoleByArray($roleData);
    }
    
    
    /**
     * Returns all roles of a certain user by a given customer ID.
     *
     * @return AdminAccessRoleCollection Role collection with all available roles.
     */
    public function getAll()
    {
        $result = $this->queryBuilder->select()->from($this->rolesTable)->order_by('sort_order')->get()->result_array();
        
        foreach($result as $key => $value)
        {
            $result[$key]['metaData'] = $this->_getRoleMetaData($value['admin_access_role_id']);
        }
        
        $rolesArray = [];
        
        foreach($result as $roleData)
        {
            $rolesArray[] = $this->_createAdminAccessRoleByArray($roleData);
        }
        
        $roleCollection = $this->roleFactory->createAdminAccessRoleCollection();
        
        foreach($rolesArray as $role)
        {
            $roleCollection->add($role);
        }
        
        return $roleCollection;
    }
    
    
    /**
     * Returns a role object by a given role ID.
     *
     * @param IdType $customerId Customer ID.
     *
     * @return AdminAccessRoleCollection Role collection with all roles of a certain user.
     *
     * @throws \RoleNotFoundException
     */
    public function getByCustomerId(IdType $customerId)
    {
        $resultArray = $this->queryBuilder->select()
                                          ->from($this->customersTable)
                                          ->join($this->rolesTable,
                                                 $this->customersTable . '.admin_access_role_id = ' . $this->rolesTable
                                                 . '.admin_access_role_id', 'left')
                                          ->where('customer_id', $customerId->asInt())
                                          ->order_by($this->rolesTable . '.sort_order')
                                          ->get()
                                          ->result_array();
        
        $roleCollection = $this->roleFactory->createAdminAccessRoleCollection();
        
        foreach($resultArray as $resultField)
        {
            $roleCollection->add($this->getById(new IdType($resultField['admin_access_role_id'])));
        }
        
        return $roleCollection;
    }
    
    
    /**
     * Returns the role descriptions from the database.
     *
     * @param int $roleId Role ID.
     *
     * @return array Array with role descriptions.
     */
    protected function _getRoleMetaData($roleId)
    {
        $roleMetaData = $this->queryBuilder->select()
                                           ->from($this->rolesMetaTable)
                                           ->where('admin_access_role_id', $roleId)
                                           ->get()
                                           ->result_array();
        
        return $roleMetaData;
    }
    
    
    /**
     * Creates a role object by a given array with the role data.
     *
     * @param array $roleData Role data.
     *
     * @return AdminAccessRoleInterface Role object.
     */
    protected function _createAdminAccessRoleByArray(array $roleData)
    {
        $roleMetaData = $roleData['metaData'];
        $role         = $this->roleFactory->createAdminAccessRole();
        
        $role->setId(new IdType($roleData['admin_access_role_id']));
        $role->setSortOrder(new IntType($roleData['sort_order']));
        $role->setDeletingUnknownGroupGranted(new BoolType($roleData['reading_unknown_group_granted']));
        $role->setReadingUnknownGroupGranted(new BoolType($roleData['writing_unknown_group_granted']));
        $role->setWritingUnknownGroupGranted(new BoolType($roleData['deleting_unknown_group_granted']));
        $role->setProtected(new BoolType($roleData['protected']));
        
        $name        = [];
        $description = [];
        
        foreach($roleMetaData as $meta)
        {
            $languageCode               = $this->languageProvider->getCodeById(new IdType($meta['language_id']))
                                                                 ->asString();
            $name[$languageCode]        = $meta['name'];
            $description[$languageCode] = $meta['description'];
        }
        
        $role->setName(new KeyValueCollection($name));
        $role->setDescription(new KeyValueCollection($description));
        
        return $role;
    }
}