AdministrationController.php 2.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php

/**
* 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 Administration Controller
* @author dev@maarch.org
*/

namespace Administration\controllers;

use Entity\models\EntityModel;
use Group\controllers\PrivilegeController;
use Group\models\GroupModel;
use Slim\Http\Request;
use Slim\Http\Response;
22
use User\controllers\UserController;
23
24
25
26
27
28
29
30
31
32
use User\models\UserEntityModel;
use User\models\UserModel;

class AdministrationController
{
    public function getDetails(Request $request, Response $response)
    {
        $count = [];

        if (PrivilegeController::hasPrivilege(['privilegeId' => 'admin_users', 'userId' => $GLOBALS['id']])) {
33
            if (UserController::isRoot(['id' => $GLOBALS['id']])) {
34
35
36
37
38
39
                $users = UserModel::get([
                    'select'    => [1],
                    'where'     => ['status != ?'],
                    'data'      => ['DEL']
                ]);
            } else {
40
                $entities = EntityModel::getAllEntitiesByUserId(['userId' => $GLOBALS['id']]);
41
42
43
                $users = [];
                if (!empty($entities)) {
                    $users = UserEntityModel::getWithUsers([
44
                        'select'    => ['DISTINCT users.id', 'users.user_id', 'firstname', 'lastname', 'status', 'mail'],
45
46
47
48
                        'where'     => ['users_entities.entity_id in (?)', 'status != ?'],
                        'data'      => [$entities, 'DEL']
                    ]);
                }
49
                $usersNoEntities = UserEntityModel::getUsersWithoutEntities(['select' => ['id', 'users.user_id', 'firstname', 'lastname', 'status', 'mail']]);
50
51
52
53
54
55
56
57
58
59
60
                $users = array_merge($users, $usersNoEntities);
            }
            $count['users'] = count($users);
        }

        if (PrivilegeController::hasPrivilege(['privilegeId' => 'admin_groups', 'userId' => $GLOBALS['id']])) {
            $groups = GroupModel::get(['select' => [1]]);
            $count['groups'] = count($groups);
        }

        if (PrivilegeController::hasPrivilege(['privilegeId' => 'manage_entities', 'userId' => $GLOBALS['id']])) {
61
            $entities = EntityModel::get([
62
                'select' => ['entity_id', 'parent_entity_id'],
63
64
65
                'where'  => ['enabled = ?'],
                'data'   => ['Y']
            ]);
66
            $entities = EntityModel::removeOrphanedEntities($entities);
67
68
69
70
71
72
73
            $count['entities'] = count($entities);
        }

        return $response->withJson(['count' => $count]);
    }

}