Exemplo n.º 1
0
    public function userLiveSearch($params)
    {
        $acls = isset($params->acl) ? explode('&', $params->acl) : false;
        if ($acls === false) {
            $params->query = $params->query . '%';
            $this->u->sql('SELECT `u`.*, `ar`.`role_name` AS role FROM users as u
					    LEFT JOIN `acl_roles` AS ar ON `ar`.`id` = `u`.`role_id`
						    WHERE `u`.`fname` LIKE ?
						       OR `u`.`lname` LIKE ?
						       OR `u`.`username` LIKE ?');
            $records = $this->u->all([$params->query, $params->query, $params->query]);
        } else {
            foreach ($acls as &$acl) {
                $acl = '`ap`.`perm_key` = \'' . $acl . '\'';
            }
            $count = count($acls);
            $where = implode(' OR ', $acls);
            $sql = "SELECT `u`.*, `ar`.`role_name` AS role FROM users AS u\n                 \tLEFT JOIN `acl_roles` AS ar ON `ar`.`id` = `u`.`role_id`\n \t\t\t\t\tWHERE `u`.`id` IN (\n\t\t\t\t\t    SELECT  `up`.`id` FROM `users` AS up\n\t\t\t\t\t    LEFT JOIN `acl_role_perms` AS arp ON `arp`.`role_id` = `up`.`role_id`\n\t\t\t\t\t    LEFT JOIN `acl_permissions` AS ap ON `ap`.`id` = `arp`.`perm_id`\n \t\t\t\t\t\tWHERE `arp`.`value` = 1 AND ( {$where} )\n\t\t\t\t\t   \tGROUP BY `up`.`id`\n\t\t\t\t\t    HAVING COUNT(`up`.`id`) = {$count}\n\t\t\t\t\t) AND (\n\t\t                fname LIKE ? OR lname LIKE ? OR username LIKE ?\n\t                )";
            $this->u->sql($sql);
            $params->query = $params->query . '%';
            $records = $this->u->all([$params->query, $params->query, $params->query]);
        }
        return ['total' => count($records), 'data' => array_slice($records, $params->start, $params->limit)];
    }