Commit 87ce81a5 authored by Johan Cwiklinski's avatar Johan Cwiklinski Committed by Johan Cwiklinski

Move getDropdownUsers to Dropdown class

parent 9339347e
......@@ -45,89 +45,4 @@ if (strpos($_SERVER['PHP_SELF'], "getDropdownUsers.php")) {
}
Session::checkLoginUser();
if (!isset($_POST['right'])) {
$_POST['right'] = "all";
}
// Default view : Nobody
if (!isset($_POST['all'])) {
$_POST['all'] = 0;
}
$used = [];
if (isset($_POST['used'])) {
$used = $_POST['used'];
}
if (!isset($_POST['value'])) {
$_POST['value'] = 0;
}
if (!isset($_POST['page'])) {
$_POST['page'] = 1;
$_POST['page_limit'] = $CFG_GLPI['dropdown_max'];
}
$entity_restrict = -1;
if (isset($_POST['entity_restrict'])) {
$entity_restrict = Toolbox::jsonDecode($_POST['entity_restrict']);
}
$start = intval(($_POST['page']-1)*$_POST['page_limit']);
$searchText = (isset($_POST['searchText']) ? $_POST['searchText'] : null);
$result = User::getSqlSearchResult(false, $_POST['right'], $entity_restrict,
$_POST['value'], $used, $searchText, $start,
intval($_POST['page_limit']));
$users = [];
// Count real items returned
$count = 0;
if ($DB->numrows($result)) {
while ($data = $DB->fetch_assoc($result)) {
$users[$data["id"]] = formatUserName($data["id"], $data["name"], $data["realname"],
$data["firstname"]);
$logins[$data["id"]] = $data["name"];
}
}
/*
if (!function_exists('dpuser_cmp')) {
function dpuser_cmp($a, $b) {
return strcasecmp($a, $b);
}
}
// Sort non case sensitive
uasort($users, 'dpuser_cmp');
*/
$datas = [];
// Display first if empty search
if ($_POST['page'] == 1 && empty($_POST['searchText'])) {
if ($_POST['all'] == 0) {
array_push($datas, ['id' => 0,
'text' => Dropdown::EMPTY_VALUE]);
} else if ($_POST['all'] == 1) {
array_push($datas, ['id' => 0,
'text' => __('All')]);
}
}
if (count($users)) {
foreach ($users as $ID => $output) {
$title = sprintf(__('%1$s - %2$s'), $output, $logins[$ID]);
array_push($datas, ['id' => $ID,
'text' => $output,
'title' => $title]);
$count++;
}
}
$ret['results'] = $datas;
$ret['count'] = $count;
echo json_encode($ret);
echo Dropdown::getDropdownUsers($_POST);
......@@ -3319,4 +3319,92 @@ class Dropdown {
return ($json === true) ? json_encode($ret) : $ret;
}
/**
* Get dropdown users
*
* @param array $post Posted values
* @param boolean $json Encode to JSON, default to true
*
* @return string|array
*/
public static function getDropdownUsers($post, $json = true) {
global $DB, $CFG_GLPI;
if (!isset($post['right'])) {
$post['right'] = "all";
}
// Default view : Nobody
if (!isset($post['all'])) {
$post['all'] = 0;
}
$used = [];
if (isset($post['used'])) {
$used = $post['used'];
}
if (!isset($post['value'])) {
$post['value'] = 0;
}
if (!isset($post['page'])) {
$post['page'] = 1;
$post['page_limit'] = $CFG_GLPI['dropdown_max'];
}
$entity_restrict = -1;
if (isset($post['entity_restrict'])) {
$entity_restrict = Toolbox::jsonDecode($post['entity_restrict']);
}
$start = intval(($post['page']-1)*$post['page_limit']);
$searchText = (isset($post['searchText']) ? $post['searchText'] : null);
$result = User::getSqlSearchResult(false, $post['right'], $entity_restrict,
$post['value'], $used, $searchText, $start,
intval($post['page_limit']));
$users = [];
// Count real items returned
$count = 0;
if ($DB->numrows($result)) {
while ($data = $DB->fetch_assoc($result)) {
$users[$data["id"]] = formatUserName($data["id"], $data["name"], $data["realname"],
$data["firstname"]);
$logins[$data["id"]] = $data["name"];
}
}
$datas = [];
// Display first if empty search
if ($post['page'] == 1 && empty($post['searchText'])) {
if ($post['all'] == 0) {
array_push($datas, ['id' => 0,
'text' => Dropdown::EMPTY_VALUE]);
} else if ($post['all'] == 1) {
array_push($datas, ['id' => 0,
'text' => __('All')]);
}
}
if (count($users)) {
foreach ($users as $ID => $output) {
$title = sprintf(__('%1$s - %2$s'), $output, $logins[$ID]);
array_push($datas, ['id' => $ID,
'text' => $output,
'title' => $title]);
$count++;
}
}
$ret['results'] = $datas;
$ret['count'] = $count;
return ($json === true) ? json_encode($ret) : $ret;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment