Skip to content
Snippets Groups Projects
UserModelAbstract.php 23.3 KiB
Newer Older
  • Learn to ignore specific revisions
  •  * Copyright Maarch since 2008 under licence GPLv3.
     * See LICENCE.txt file at the root folder for more details.
     * This file is part of Maarch software.
    
     * @brief User Model
     * @author dev@maarch.org
     */
    
    Damien's avatar
    Damien committed
    namespace User\models;
    
    Damien's avatar
    Damien committed
    use Docserver\models\DocserverModel;
    
    Damien's avatar
    Damien committed
    use SrcCore\models\CoreConfigModel;
    
    use SrcCore\models\DatabaseModel;
    
    Damien's avatar
    Damien committed
    use SrcCore\models\SecurityModel;
    use SrcCore\models\ValidatorModel;
    
    Damien's avatar
    Damien committed
    class UserModelAbstract
    
        public static function get(array $aArgs)
    
            ValidatorModel::arrayType($aArgs, ['select', 'where', 'data', 'orderBy']);
    
    Damien's avatar
    Damien committed
            $aUsers = DatabaseModel::select([
    
    Damien's avatar
    Damien committed
                'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
                'table'     => ['users'],
    
                'where'     => $aArgs['where'],
                'data'      => $aArgs['data'],
                'order_by'  => $aArgs['orderBy']
    
    Damien's avatar
    Damien committed
            ]);
    
            return $aUsers;
        }
    
    
        public static function getById(array $aArgs)
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['id']);
            ValidatorModel::intVal($aArgs, ['id']);
    
    Damien's avatar
    Damien committed
            $aUser = DatabaseModel::select([
    
    Damien's avatar
    Damien committed
                'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
                'table'     => ['users'],
                'where'     => ['id = ?'],
                'data'      => [$aArgs['id']]
    
    Damien's avatar
    Damien committed
            if (empty($aUser)) {
                return [];
            }
    
    
            return $aUser[0];
    
        public static function create(array $aArgs = [])
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['user']);
            ValidatorModel::notEmpty($aArgs['user'], ['userId', 'firstname', 'lastname']);
    
            ValidatorModel::stringType($aArgs['user'], ['userId', 'firstname', 'lastname', 'mail', 'initials', 'thumbprint', 'phone', 'changePassword']);
    
    Damien's avatar
    Damien committed
            DatabaseModel::insert([
    
    Damien's avatar
    Damien committed
                'table'         => 'users',
    
    Damien's avatar
    Damien committed
                'columnsValues' => [
    
    Damien's avatar
    Damien committed
                    'user_id'           => $aArgs['user']['userId'],
                    'firstname'         => $aArgs['user']['firstname'],
                    'lastname'          => $aArgs['user']['lastname'],
                    'mail'              => $aArgs['user']['mail'],
                    'phone'             => $aArgs['user']['phone'],
                    'initials'          => $aArgs['user']['initials'],
                    'thumbprint'        => $aArgs['user']['thumbprint'],
                    'enabled'           => 'Y',
                    'status'            => 'OK',
                    'change_password'   => empty($aArgs['user']['changePassword']) ? 'Y' : $aArgs['user']['changePassword'],
                    'loginmode'         => 'standard',
                    'password'          => SecurityModel::getPasswordHash('maarch')
                ]
    
    Damien's avatar
    Damien committed
            ]);
    
        public static function update(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['id', 'user']);
            ValidatorModel::notEmpty($aArgs['user'], ['firstname', 'lastname']);
            ValidatorModel::intVal($aArgs, ['id']);
    
            ValidatorModel::stringType($aArgs['user'], ['firstname', 'lastname', 'mail', 'initials', 'thumbprint', 'phone', 'enabled']);
    
    Damien's avatar
    Damien committed
            DatabaseModel::update([
    
    Damien's avatar
    Damien committed
                'table'     => 'users',
                'set'       => [
                    'firstname'     => $aArgs['user']['firstname'],
                    'lastname'      => $aArgs['user']['lastname'],
                    'mail'          => $aArgs['user']['mail'],
                    'phone'         => $aArgs['user']['phone'],
                    'initials'      => $aArgs['user']['initials'],
                    'enabled'       => $aArgs['user']['enabled'],
                    'thumbprint'    => $aArgs['user']['thumbprint']
    
    Damien's avatar
    Damien committed
                'where'     => ['id = ?'],
                'data'      => [$aArgs['id']]
    
    Damien's avatar
    Damien committed
            return true;
    
    Damien's avatar
    Damien committed
        public static function delete(array $aArgs)
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['id']);
            ValidatorModel::intVal($aArgs, ['id']);
    
    Damien's avatar
    Damien committed
            DatabaseModel::update([
    
    Damien's avatar
    Damien committed
                'table'     => 'users',
                'set'       => [
                    'status'    => 'DEL',
    
    Damien's avatar
    Damien committed
                'where'     => ['id = ?'],
                'data'      => [$aArgs['id']]
    
    Damien's avatar
    Damien committed
            return true;
    
        public static function getByUserId(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['userId']);
            ValidatorModel::stringType($aArgs, ['userId']);
    
    Damien's avatar
    Damien committed
            $aUser = DatabaseModel::select([
    
    Damien's avatar
    Damien committed
                'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
                'table'     => ['users'],
                'where'     => ['user_id = ?'],
                'data'      => [$aArgs['userId']]
    
            ]);
    
            if (empty($aUser)) {
                return [];
            }
    
            return $aUser[0];
        }
    
        public static function getByEmail(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['mail']);
            ValidatorModel::stringType($aArgs, ['mail']);
    
    Damien's avatar
    Damien committed
            $aUser = DatabaseModel::select([
    
    Damien's avatar
    Damien committed
                'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
                'table'     => ['users'],
                'where'     => ['mail = ? and status = ?'],
                'data'      => [$aArgs['mail'], 'OK'],
                'limit'     => 1
    
        public static function getGroupsById(array $aArgs = [])
        {
            ValidatorModel::notEmpty($aArgs, ['userId']);
            ValidatorModel::stringType($aArgs, ['userId']);
    
            $aGroups = DatabaseModel::select([
                'select'    => ['usergroup_content.group_id', 'usergroups.group_desc', 'usergroup_content.primary_group'],
                'table'     => ['usergroup_content, usergroups'],
                'where'     => ['usergroup_content.group_id = usergroups.group_id', 'usergroup_content.user_id = ?'],
                'data'      => [$aArgs['userId']]
            ]);
    
            return $aGroups;
        }
    
    
        public static function updatePassword(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['id', 'password']);
            ValidatorModel::intVal($aArgs, ['id']);
            ValidatorModel::stringType($aArgs, ['password']);
    
    Damien's avatar
    Damien committed
            DatabaseModel::update([
    
    Damien's avatar
    Damien committed
                'table'     => 'users',
                'set'       => [
                    'password'  => SecurityModel::getPasswordHash($aArgs['password'])
    
    Damien's avatar
    Damien committed
                'where'     => ['id = ?'],
                'data'      => [$aArgs['id']]
    
    Damien's avatar
    Damien committed
            return true;
    
        public static function resetPassword(array $aArgs = [])
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['id']);
            ValidatorModel::intVal($aArgs, ['id']);
    
    Damien's avatar
    Damien committed
            DatabaseModel::update([
    
    Damien's avatar
    Damien committed
                'table'     => 'users',
                'set'       => [
                    'password'  => SecurityModel::getPasswordHash('maarch')
    
    Damien's avatar
    Damien committed
                'where'     => ['id = ?'],
                'data'      => [$aArgs['id']]
    
    Damien's avatar
    Damien committed
            return true;
    
        public static function createSignature(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['userSerialId', 'signatureLabel', 'signaturePath', 'signatureFileName']);
            ValidatorModel::stringType($aArgs, ['signatureLabel', 'signaturePath', 'signatureFileName']);
            ValidatorModel::intVal($aArgs, ['userSerialId']);
    
    Damien's avatar
    Damien committed
            DatabaseModel::insert([
    
    Damien's avatar
    Damien committed
                'table'         => 'user_signatures',
    
    Damien's avatar
    Damien committed
                'columnsValues' => [
    
    Damien's avatar
    Damien committed
                    'user_serial_id'        => $aArgs['userSerialId'],
                    'signature_label'       => $aArgs['signatureLabel'],
                    'signature_path'        => $aArgs['signaturePath'],
                    'signature_file_name'   => $aArgs['signatureFileName']
                ]
    
    Damien's avatar
    Damien committed
            ]);
    
        public static function updateSignature(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['signatureId', 'userSerialId', 'label']);
            ValidatorModel::stringType($aArgs, ['label']);
            ValidatorModel::intVal($aArgs, ['signatureId', 'userSerialId']);
    
    Damien's avatar
    Damien committed
            DatabaseModel::update([
    
    Damien's avatar
    Damien committed
                'table'     => 'user_signatures',
                'set'       => [
                    'signature_label'   => $aArgs['label']
    
    Damien's avatar
    Damien committed
                'where'     => ['user_serial_id = ?', 'id = ?'],
                'data'      => [$aArgs['userSerialId'], $aArgs['signatureId']]
    
        public static function deleteSignature(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['signatureId', 'userSerialId']);
            ValidatorModel::intVal($aArgs, ['signatureId', 'userSerialId']);
    
    Damien's avatar
    Damien committed
            DatabaseModel::delete([
    
    Damien's avatar
    Damien committed
                'table'     => 'user_signatures',
                'where'     => ['user_serial_id = ?', 'id = ?'],
                'data'      => [$aArgs['userSerialId'], $aArgs['signatureId']],
    
    Damien's avatar
    Damien committed
            return true;
    
        public static function createEmailSignature(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['userId', 'title', 'htmlBody']);
            ValidatorModel::stringType($aArgs, ['userId', 'title', 'htmlBody']);
    
    Damien's avatar
    Damien committed
            DatabaseModel::insert([
    
    Damien's avatar
    Damien committed
                'table'         => 'users_email_signatures',
    
    Damien's avatar
    Damien committed
                'columnsValues' => [
    
    Damien's avatar
    Damien committed
                    'user_id'   => $aArgs['userId'],
                    'title'     => $aArgs['title'],
                    'html_body' => $aArgs['htmlBody']
                ]
    
    Damien's avatar
    Damien committed
            ]);
    
    
            return true;
        }
    
        public static function updateEmailSignature(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['id','userId', 'title', 'htmlBody']);
    
    Damien's avatar
    Damien committed
            ValidatorModel::stringType($aArgs, ['userId', 'title', 'htmlBody']);
            ValidatorModel::intVal($aArgs, ['id']);
    
    Damien's avatar
    Damien committed
            DatabaseModel::update([
    
    Damien's avatar
    Damien committed
                'table'     => 'users_email_signatures',
                'set'       => [
                    'title'     => $aArgs['title'],
    
    Damien's avatar
    Damien committed
                'where'     => ['user_id = ?', 'id = ?'],
                'data'      => [$aArgs['userId'], $aArgs['id']]
    
            ]);
    
            return true;
        }
    
        public static function deleteEmailSignature(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['id', 'userId']);
            ValidatorModel::stringType($aArgs, ['userId']);
    
    Damien's avatar
    Damien committed
            DatabaseModel::delete([
    
    Damien's avatar
    Damien committed
                'table'     => 'users_email_signatures',
                'where'     => ['user_id = ?', 'id = ?'],
                'data'      => [$aArgs['userId'], $aArgs['id']]
    
            ]);
    
            return true;
        }
    
        public static function getSignaturesById(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['id']);
            ValidatorModel::intVal($aArgs, ['id']);
    
    Damien's avatar
    Damien committed
            $aReturn = DatabaseModel::select([
    
    Damien's avatar
    Damien committed
                'select'    => ['id', 'user_serial_id', 'signature_label', 'signature_path', 'signature_file_name'],
                'table'     => ['user_signatures'],
                'where'     => ['user_serial_id = ?'],
                'data'      => [$aArgs['id']],
                'order_by'  => ['id']
    
    Damien's avatar
    Damien committed
            $docserver = [];
    
            if (!empty($aReturn)) {
                $docserver = DocserverModel::getByTypeId(['docserver_type_id' => 'TEMPLATES', 'select' => ['path_template']]);
            }
    
    
    Damien's avatar
    Damien committed
            if (!file_exists($docserver['path_template'])) {
                return [];
            }
    
    Damien's avatar
    Damien committed
            $tmpPath = CoreConfigModel::getTmpPath();
    
    Damien's avatar
    Damien committed
            $urlTmpPath = str_replace('rest/', '', \Url::coreurl()) . 'apps/maarch_entreprise/tmp/';
            foreach($aReturn as $key => $value) {
                $pathToSignature = $docserver['path_template'] . str_replace('#', '/', $value['signature_path']) . $value['signature_file_name'];
    
    
                $extension = explode('.', $pathToSignature);
                $extension = $extension[count($extension) - 1];
    
    Damien's avatar
    Damien committed
                $fileNameOnTmp = 'tmp_file_' . $aArgs['id'] . '_' . rand() . '.' . strtolower($extension);
                $filePathOnTmp = $tmpPath . $fileNameOnTmp;
    
                if (file_exists($pathToSignature) && copy($pathToSignature, $filePathOnTmp)) {
    
    Damien's avatar
    Damien committed
                    $aReturn[$key]['pathToSignatureOnTmp'] = $urlTmpPath . $fileNameOnTmp;
    
                } else {
                    $aReturn[$key]['pathToSignatureOnTmp'] = '';
                }
    
                $aReturn[$key]['pathToSignature'] = $pathToSignature;
    
                unset($aReturn[$key]['signature_path'], $aReturn[$key]['signature_file_name']);
    
        public static function getSignatureWithSignatureIdById(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['id', 'signatureId']);
    
    Damien's avatar
    Damien committed
            ValidatorModel::intVal($aArgs, ['id','signatureId']);
    
    Damien's avatar
    Damien committed
            $aReturn = DatabaseModel::select([
    
    Damien's avatar
    Damien committed
                'select'    => ['id', 'user_serial_id', 'signature_label'],
                'table'     => ['user_signatures'],
                'where'     => ['user_serial_id = ?', 'id = ?'],
                'data'      => [$aArgs['id'], $aArgs['signatureId']],
    
        public static function getEmailSignaturesById(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['userId']);
            ValidatorModel::stringType($aArgs, ['userId']);
    
    Damien's avatar
    Damien committed
            $aReturn = DatabaseModel::select([
    
    Damien's avatar
    Damien committed
                'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
                'table'     => ['users_email_signatures'],
                'where'     => ['user_id = ?'],
                'data'      => [$aArgs['userId']],
                'order_by'  => ['id']
    
        public static function getEmailSignatureWithSignatureIdById(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['userId', 'signatureId']);
            ValidatorModel::stringType($aArgs, ['userId']);
            ValidatorModel::intVal($aArgs, ['signatureId']);
    
    Damien's avatar
    Damien committed
            $aReturn = DatabaseModel::select([
    
    Damien's avatar
    Damien committed
                'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
                'table'     => ['users_email_signatures'],
                'where'     => ['user_id = ?', 'id = ?'],
                'data'      => [$aArgs['userId'], $aArgs['signatureId']],
    
    Damien's avatar
    Damien committed
        public static function getLabelledUserById(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::intVal($aArgs, ['id']);
            ValidatorModel::stringType($aArgs, ['userId']);
    
    Damien's avatar
    Damien committed
            if (!empty($aArgs['id'])) {
                $rawUser = UserModel::getById(['id' => $aArgs['id'], 'select' => ['firstname', 'lastname']]);
            } elseif (!empty($aArgs['userId'])) {
                $rawUser = UserModel::getByUserId(['userId' => $aArgs['userId'], 'select' => ['firstname', 'lastname']]);
            }
    
    Damien's avatar
    Damien committed
    
            $labelledUser = '';
            if (!empty($rawUser)) {
    
    Damien's avatar
    Damien committed
                $labelledUser = $rawUser['firstname']. ' ' .$rawUser['lastname'];
    
    Damien's avatar
    Damien committed
            }
    
            return $labelledUser;
        }
    
        public static function getCurrentConsigneById(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['resId']);
            ValidatorModel::intVal($aArgs, ['resId']);
    
    Damien's avatar
    Damien committed
            $aReturn = DatabaseModel::select([
    
    Damien's avatar
    Damien committed
                'select'    => ['process_comment'],
                'table'     => ['listinstance'],
                'where'     => ['res_id = ?', 'process_date is null', 'item_mode in (?)'],
                'data'      => [$aArgs['resId'], ['visa', 'sign']],
                'order_by'  => ['listinstance_id ASC'],
                'limit'     => 1
    
    Damien's avatar
    Damien committed
            ]);
    
            if (empty($aReturn[0])) {
                return '';
            }
    
            return $aReturn[0]['process_comment'];
        }
    
    Damien's avatar
    Damien committed
        public static function getPrimaryGroupById(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['userId']);
            ValidatorModel::stringType($aArgs, ['userId']);
    
    Damien's avatar
    Damien committed
            $aGroup = DatabaseModel::select([
    
    Damien's avatar
    Damien committed
                'select'    => ['usergroup_content.group_id', 'usergroups.group_desc'],
                'table'     => ['usergroup_content, usergroups'],
                'where'     => ['usergroup_content.group_id = usergroups.group_id', 'usergroup_content.user_id = ?', 'usergroup_content.primary_group = ?'],
                'data'      => [$aArgs['userId'], 'Y']
    
    Damien's avatar
    Damien committed
            ]);
    
            if (empty($aGroup[0])) {
                return [];
            }
    
            return $aGroup[0];
        }
    
    
    Damien's avatar
    Damien committed
        public static function getPrimaryEntityByUserId(array $aArgs)
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['userId']);
            ValidatorModel::stringType($aArgs, ['userId']);
    
    Damien's avatar
    Damien committed
            $aEntity = DatabaseModel::select([
    
    Damien's avatar
    Damien committed
                'select'    => ['users_entities.entity_id', 'entities.entity_label', 'users_entities.user_role', 'users_entities.primary_entity'],
                'table'     => ['users_entities, entities'],
                'where'     => ['users_entities.entity_id = entities.entity_id', 'users_entities.user_id = ?', 'users_entities.primary_entity = ?'],
                'data'      => [$aArgs['userId'], 'Y']
    
            ]);
    
            if (empty($aEntity[0])) {
                return [];
            }
    
            return $aEntity[0];
        }
    
    
        public static function getGroupsByUserId(array $aArgs)
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['userId']);
            ValidatorModel::stringType($aArgs, ['userId']);
    
    Damien's avatar
    Damien committed
            $aGroups = DatabaseModel::select([
    
                'select'    => ['usergroups.id', 'usergroup_content.group_id', 'usergroups.group_desc', 'usergroup_content.primary_group', 'usergroup_content.role', 'security.maarch_comment', 'security.where_clause'],
    
    Damien's avatar
    Damien committed
                'table'     => ['usergroup_content, usergroups, security'],
                'where'     => ['usergroup_content.group_id = usergroups.group_id', 'usergroup_content.user_id = ?','usergroups.group_id = security.group_id'],
                'data'      => [$aArgs['userId']]
    
            ]);
    
            return $aGroups;
        }
    
        public static function getEntitiesById(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['userId']);
            ValidatorModel::stringType($aArgs, ['userId']);
    
    Damien's avatar
    Damien committed
            $aEntities = DatabaseModel::select([
    
    Damien's avatar
    Damien committed
                'select'    => ['users_entities.entity_id', 'entities.entity_label', 'users_entities.user_role', 'users_entities.primary_entity'],
                'table'     => ['users_entities, entities'],
                'where'     => ['users_entities.entity_id = entities.entity_id', 'users_entities.user_id = ?'],
                'data'      => [$aArgs['userId']],
                'order_by'  => ['users_entities.primary_entity DESC']
    
        public static function updateStatus(array $aArgs)
    
            ValidatorModel::notEmpty($aArgs, ['id', 'status']);
    
    Damien's avatar
    Damien committed
            ValidatorModel::intVal($aArgs, ['id']);
    
            ValidatorModel::stringType($aArgs, ['status']);
    
    Damien's avatar
    Damien committed
    
            DatabaseModel::update([
    
    Damien's avatar
    Damien committed
                'table'     => 'users',
                'set'       => [
                    'status'    => $aArgs['status']
    
    Damien's avatar
    Damien committed
                ],
    
    Damien's avatar
    Damien committed
                'where'     => ['id = ?'],
                'data'      => [$aArgs['id']]
    
        public static function hasGroup(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['id', 'groupId']);
            ValidatorModel::intVal($aArgs, ['id']);
            ValidatorModel::stringType($aArgs, ['groupId']);
    
    Damien's avatar
    Damien committed
            $user = UserModel::getById(['id' => $aArgs['id'], 'select' => ['user_id']]);
            $groups = UserModel::getGroupsByUserId(['userId' => $user['user_id']]);
    
            foreach ($groups as $value) {
                if ($value['group_id'] == $aArgs['groupId']) {
                    return true;
                }
            }
    
            return false;
        }
    
    
    Damien's avatar
    Damien committed
        public static function addGroup(array $aArgs)
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['id', 'groupId']);
            ValidatorModel::intVal($aArgs, ['id']);
            ValidatorModel::stringType($aArgs, ['groupId', 'role']);
    
    Damien's avatar
    Damien committed
            $user = UserModel::getById(['id' => $aArgs['id'], 'select' => ['user_id']]);
            DatabaseModel::insert([
    
    Damien's avatar
    Damien committed
                'table'         => 'usergroup_content',
    
    Damien's avatar
    Damien committed
                'columnsValues' => [
    
    Damien's avatar
    Damien committed
                    'user_id'       => $user['user_id'],
                    'group_id'      => $aArgs['groupId'],
                    'role'          => $aArgs['role'],
                    'primary_group' => 'Y'
                ]
    
    Damien's avatar
    Damien committed
            ]);
    
        public static function updateGroup(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['id', 'groupId']);
            ValidatorModel::intVal($aArgs, ['id']);
            ValidatorModel::stringType($aArgs, ['groupId', 'role']);
    
    Damien's avatar
    Damien committed
            $user = UserModel::getById(['id' => $aArgs['id'], 'select' => ['user_id']]);
            DatabaseModel::update([
    
    Damien's avatar
    Damien committed
                'table'     => 'usergroup_content',
                'set'       => [
                    'role'      => $aArgs['role']
    
    Damien's avatar
    Damien committed
                'where'     => ['user_id = ?', 'group_id = ?'],
                'data'      => [$user['user_id'], $aArgs['groupId']]
    
    Damien's avatar
    Damien committed
        public static function deleteGroup(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['id', 'groupId']);
            ValidatorModel::intVal($aArgs, ['id']);
            ValidatorModel::stringType($aArgs, ['groupId']);
    
    Damien's avatar
    Damien committed
            $user = UserModel::getById(['id' => $aArgs['id'], 'select' => ['user_id']]);
            DatabaseModel::delete([
    
    Damien's avatar
    Damien committed
                'table'     => 'usergroup_content',
                'where'     => ['group_id = ?', 'user_id = ?'],
                'data'      => [$aArgs['groupId'], $user['user_id']]
    
    Damien's avatar
    Damien committed
            ]);
    
            return true;
        }
    
        public static function hasEntity(array $aArgs = [])
        {
    
    Damien's avatar
    Damien committed
            ValidatorModel::notEmpty($aArgs, ['id', 'entityId']);
            ValidatorModel::intVal($aArgs, ['id']);
            ValidatorModel::stringType($aArgs, ['entityId']);
    
    Damien's avatar
    Damien committed
            $user = UserModel::getById(['id' => $aArgs['id'], 'select' => ['user_id']]);
            $entities = UserModel::getEntitiesById(['userId' => $user['user_id']]);
    
            foreach ($entities as $value) {
                if ($value['entity_id'] == $aArgs['entityId']) {
                    return true;
                }
            }
    
            return false;
        }
    
    
    Damien's avatar
    Damien committed
        public static function updateBasketColor(array $aArgs)
        {
            ValidatorModel::notEmpty($aArgs, ['id', 'groupId', 'basketId', 'color']);
            ValidatorModel::intVal($aArgs, ['id']);
            ValidatorModel::stringType($aArgs, ['groupId', 'basketId', 'color']);
    
            $isPresent = DatabaseModel::select([
    
    Damien's avatar
    Damien committed
                'select'    => ['1'],
                'table'     => ['users_baskets'],
                'where'     => ['user_serial_id = ?', 'group_id = ?', 'basket_id = ?'],
                'data'      => [$aArgs['id'], $aArgs['groupId'], $aArgs['basketId']]
    
    Damien's avatar
    Damien committed
            ]);
    
            if (empty($isPresent)) {
                DatabaseModel::insert(
                    [
    
    Damien's avatar
    Damien committed
                        'table'         => 'users_baskets',
    
    Damien's avatar
    Damien committed
                        'columnsValues' => [
    
    Damien's avatar
    Damien committed
                            'user_serial_id'    => $aArgs['id'],
                            'basket_id'         => $aArgs['basketId'],
                            'group_id'          => $aArgs['groupId'],
                            'color'             => $aArgs['color']
                        ]
    
    Damien's avatar
    Damien committed
                    ]
                );
            } else {
                DatabaseModel::update([
    
    Damien's avatar
    Damien committed
                    'table'     => 'users_baskets',
                    'set'       => [
                        'color'    => $aArgs['color']
    
    Damien's avatar
    Damien committed
                    'where'     => ['user_serial_id = ?', 'group_id = ?', 'basket_id = ?'],
                    'data'      => [$aArgs['id'], $aArgs['groupId'], $aArgs['basketId']]
    
    
        public static function eraseBasketColor(array $aArgs)
        {
            ValidatorModel::notEmpty($aArgs, ['id', 'groupId', 'basketId']);
            ValidatorModel::intVal($aArgs, ['id']);
            ValidatorModel::stringType($aArgs, ['groupId', 'basketId']);
    
            DatabaseModel::delete(
                [
                    'table' => 'users_baskets',
                    'where' => ['user_serial_id = ?', 'group_id = ?', 'basket_id = ?'],
    
    Damien's avatar
    Damien committed
                    'data'  => [$aArgs['id'], $aArgs['groupId'], $aArgs['basketId']]