Exemplo n.º 1
0
 /**
  * @Request({"filter": "array", "page":"int"})
  * @Response("extension://system/views/admin/user/index.razr")
  */
 public function indexAction($filter = null, $page = 0)
 {
     if ($filter) {
         $this['session']->set('user.filter', $filter);
     } else {
         $filter = $this['session']->get('user.filter', []);
     }
     $query = $this->users->query();
     if (isset($filter['status'])) {
         if (is_numeric($filter['status'])) {
             $filter['status'] = (int) $filter['status'];
             $query->where(['status' => intval($filter['status'])]);
             if (!$filter['status']) {
                 $query->where('access IS NOT NULL');
             }
         } elseif ('new' == $filter['status']) {
             $query->where(['status' => User::STATUS_BLOCKED, 'access IS NULL']);
         }
     }
     if (isset($filter['search']) && strlen($filter['search'])) {
         $query->where(function ($query) use($filter) {
             $query->orWhere(['username LIKE :search', 'name LIKE :search', 'email LIKE :search'], ['search' => "%{$filter['search']}%"]);
         });
     }
     $role = isset($filter['role']) && is_numeric($filter['role']) ? intval($filter['role']) : null;
     $permission = isset($filter['permission']) && strlen($filter['permission']) ? $filter['permission'] : null;
     if ($role || $permission) {
         if ($role) {
             $query->whereExists(function ($query) use($role) {
                 $query->from('@system_user_role u')->where(['@system_user.id = u.user_id', 'u.role_id' => $role]);
             });
         }
         if ($permission) {
             $sql = $this->getPermissionSql($permission);
             $query->whereExists(function ($query) use($sql) {
                 $query->from('@system_user_role ur')->join('@system_role r', 'ur.role_id = r.id')->where(['@system_user.id = ur.user_id', $sql]);
             });
         }
     }
     $limit = self::USERS_PER_PAGE;
     $count = $query->count();
     $total = ceil($count / $limit);
     $page = max(0, min($total - 1, $page));
     $users = $query->offset($page * $limit)->limit($limit)->related('roles')->orderBy('name')->get();
     $roles = $this->getRoles();
     if ($this['request']->isXmlHttpRequest()) {
         return $this['response']->json(['table' => $this['view']->render('extension://system/views/admin/user/table.razr', ['users' => $users]), 'total' => $total]);
     }
     return ['head.title' => __('Users'), 'users' => $users, 'statuses' => User::getStatuses(), 'roles' => $roles, 'permissions' => $this['permissions'], 'filter' => $filter, 'total' => $total];
 }