Skip to content

Commit

Permalink
Add #4
Browse files Browse the repository at this point in the history
Add #5
  • Loading branch information
tsmr committed Nov 24, 2024
1 parent 071c1e9 commit 0e5d802
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 70 deletions.
60 changes: 57 additions & 3 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ function plugin_vip_uninstall() {
"glpi_notepads",
"glpi_dropdowntranslations"];

foreach ($tables_glpi as $table_glpi)
$DB->query("DELETE FROM `$table_glpi` WHERE `itemtype` LIKE 'PluginVip%';");

foreach ($tables_glpi as $table_glpi) {
$DB->delete($table_glpi, ['itemtype' => ['LIKE' => 'PluginVip%']]);
}
//drop rules
$Rule = new Rule();
$a_rules = $Rule->find(['sub_type' => 'PluginVipRuleVip']);
Expand Down Expand Up @@ -243,3 +243,57 @@ function plugin_vip_executeActions($options) {
$vip = new PluginVipRuleVip();
return $vip->executeActions($options['action'], $options['output'], $options['params']);
}

function plugin_vip_redefine_api_schemas(array $data): array {
if (!Session::haveRight('plugin_vip', READ)) {
return $data;
}
foreach ($data['schemas'] as &$schema) {
if (!isset($schema['x-itemtype'])) {
continue;
}
switch ($schema['x-itemtype']) {
case 'User':
$schema['properties']['vip_groups'] = [
'type' => \Glpi\Api\HL\Doc\Schema::TYPE_ARRAY,
'items' => [
'type' => \Glpi\Api\HL\Doc\Schema::TYPE_OBJECT,
'x-join' => [
// This is the join with the desired data
'table' => 'glpi_plugin_vip_groups',
'fkey' => 'groups_id',
'field' => 'id', // This table uses the group ID as the primary key "id"
'ref-join' => [
// This is the linking join between the main item and the data needed
'table' => 'glpi_groups_users',
'fkey' => 'id',
'field' => 'users_id',
]
],
'properties' => [
'id' => [
'type' => \Glpi\Api\HL\Doc\Schema::TYPE_INTEGER,
'x-readonly' => true,
],
'name' => [
'type' => \Glpi\Api\HL\Doc\Schema::TYPE_STRING,
'x-readonly' => true,
],
'color' => [
'type' => \Glpi\Api\HL\Doc\Schema::TYPE_STRING,
'x-readonly' => true,
'x-field' => 'vip_color'
],
'icon' => [
'type' => \Glpi\Api\HL\Doc\Schema::TYPE_STRING,
'x-readonly' => true,
'x-field' => 'vip_icon'
],
]
]
];
break;
}
}
return $data;
}
52 changes: 36 additions & 16 deletions inc/dashboard.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,36 +72,56 @@ public function getWidgetContentForItem($widgetId)
$groups[] = $mygroup["id"];
}

$query = "SELECT `glpi_tickets`.`id` AS tickets_id,
`glpi_tickets`.`status` AS status,
`glpi_tickets`.`time_to_resolve` AS time_to_resolve
FROM `glpi_tickets`
LEFT JOIN `glpi_entities` ON (`glpi_tickets`.`entities_id` = `glpi_entities`.`id`)
LEFT JOIN `glpi_groups_tickets` ON (`glpi_tickets`.`id` = `glpi_groups_tickets`.`tickets_id`
AND `glpi_groups_tickets`.`type` = " . CommonITILActor::ASSIGN . ")
WHERE `glpi_tickets`.`is_deleted` = '0'
AND `glpi_tickets`.`status` NOT IN (" . CommonITILObject::INCOMING . "," . CommonITILObject::SOLVED . "," . CommonITILObject::CLOSED . ") ";
$criteria = [
'SELECT' => [
'glpi_tickets.id AS tickets_id', 'glpi_tickets.status AS status', 'glpi_tickets.time_to_resolve AS time_to_resolve'
],
'FROM' => 'glpi_tickets',
'LEFT JOIN' => [
'glpi_entities' => [
'ON' => [
'glpi_tickets' => 'entities_id',
'glpi_entities' => 'id'
]
],
'glpi_groups_tickets' => [
'ON' => [
'glpi_tickets' => 'id',
'glpi_groups_tickets' => 'tickets_id', [
'AND' => [
'glpi_groups_tickets.type' => CommonITILActor::ASSIGN
]
]
]
]
],
'WHERE' => [
'glpi_tickets.is_deleted' => '0',
'NOT' => ['glpi_tickets.status' => [CommonITILObject::INCOMING, CommonITILObject::SOLVED, CommonITILObject::CLOSED]]
],
'ORDER' => ['glpi_tickets.time_to_resolve' => 'DESC']
];
if (count($groups) > 0) {
$query .= "AND `glpi_groups_tickets`.`groups_id` IN (" . implode(",", $groups) . ")";
$criteria['WHERE']['glpi_groups_tickets.groups_id'] = $groups;
}
$query .= "ORDER BY `glpi_tickets`.`time_to_resolve` DESC";//

$widget = PluginMydashboardHelper::getWidgetsFromDBQuery('table', $query);
$it = new DBmysqlIterator($DB);
$it->buildQuery($criteria);
$widget = PluginMydashboardHelper::getWidgetsFromDBQuery('table', $it->getSql());
$headers = [__('ID'),
_n('Requester', 'Requesters', 2),
__('Status'),
__('Time to resolve'),
__('Assigned to technicians')];
$widget->setTabNames($headers);

$result = $DB->query($query);
$nb = $DB->numrows($result);
$result = $DB->request($criteria);
$nb = count($result);

$datas = [];
$tickets = [];

if ($nb) {
while ($data = $DB->fetchAssoc($result)) {
foreach ($result as $data) {
$ticket = new Ticket();
$ticket->getFromDB($data['tickets_id']);
if ($ticket->countUsers(CommonITILActor::REQUESTER)) {
Expand Down
47 changes: 30 additions & 17 deletions inc/profile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,11 @@ public static function migrateOneProfile($profiles_id)
return true;
}

foreach ($DB->request(
'glpi_plugin_vip_profiles',
"`profiles_id`='$profiles_id'"
) as $profile_data) {
$it = $DB->request([
'FROM' => 'glpi_plugin_vip_profiles',
'WHERE' => ['profiles_id' => $profiles_id]
]);
foreach ($it as $profile_data) {
$matching = ['show_vip_tab' => 'plugin_vip'];
$current_rights = ProfileRight::getProfileRights($profiles_id, array_values($matching));
foreach ($matching as $old => $new) {
Expand All @@ -196,10 +197,10 @@ public static function migrateOneProfile($profiles_id)
break;
}

$query = "UPDATE `glpi_profilerights`
SET `rights`='" . $right . "'
WHERE `name`='$new' AND `profiles_id`='$profiles_id'";
$DB->query($query);
$DB->update('glpi_profilerights', ['rights' => $right], [
'name' => $new,
'profiles_id' => $profiles_id
]);
}
}
}
Expand All @@ -224,13 +225,21 @@ public static function initProfile()
}

//Migration old rights in new ones
foreach ($DB->request("SELECT `id` FROM `glpi_profiles`") as $prof) {
$it = $DB->request([
'SELECT' => ['id'],
'FROM' => 'glpi_profiles'
]);
foreach ($it as $prof) {
self::migrateOneProfile($prof['id']);
}
foreach ($DB->request("SELECT *
FROM `glpi_profilerights`
WHERE `profiles_id`='" . $_SESSION['glpiactiveprofile']['id'] . "'
AND `name` LIKE '%plugin_vip%'") as $prof) {
$it = $DB->request([
'FROM' => 'glpi_profilerights',
'WHERE' => [
'profiles_id' => $_SESSION['glpiactiveprofile']['id'],
'name' => ['LIKE', '%plugin_vip%']
]
]);
foreach ($it as $prof) {
if (isset($_SESSION['glpiactiveprofile'])) {
$_SESSION['glpiactiveprofile'][$prof['name']] = $prof['rights'];
}
Expand All @@ -244,10 +253,14 @@ public static function changeProfile()
{
global $DB;

foreach ($DB->request("SELECT *
FROM `glpi_profilerights`
WHERE `profiles_id`='" . $_SESSION['glpiactiveprofile']['id'] . "'
AND `name` LIKE '%plugin_vip%'") as $prof) {
$it = $DB->request([
'FROM' => 'glpi_profilerights',
'WHERE' => [
'profiles_id' => $_SESSION['glpiactiveprofile']['id'],
'name' => ['LIKE', '%plugin_vip%']
]
]);
foreach ($it as $prof) {
$_SESSION['glpiactiveprofile'][$prof['name']] = $prof['rights'];
}
}
Expand Down
79 changes: 47 additions & 32 deletions inc/ticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,24 @@ public static function isUserVip($uid)
global $DB;

if ($uid) {
$vipquery = "SELECT `glpi_plugin_vip_groups`.`id`
FROM `glpi_groups_users`
LEFT JOIN `glpi_plugin_vip_groups`
ON `glpi_plugin_vip_groups`.`id` = `glpi_groups_users`.`groups_id`
WHERE `glpi_plugin_vip_groups`.`isvip` = 1
AND `glpi_groups_users`.`users_id` = " . $uid;

$result = $DB->query($vipquery);
$nb = $DB->numrows($result);
if ($nb > 0) {
while ($uids = $DB->fetchArray($result)) {
return $uids['id'];
}
$result = $DB->request([
'SELECT' => ['glpi_plugin_vip_groups.id'],
'FROM' => 'glpi_groups_users',
'LEFT JOIN' => [
'glpi_plugin_vip_groups' => [
'ON' => [
'glpi_plugin_vip_groups' => 'id',
'glpi_groups_users' => 'groups_id'
]
]
],
'WHERE' => [
'glpi_plugin_vip_groups.isvip' => 1,
'glpi_groups_users.users_id' => $uid
]
]);
if (count($result) > 0) {
return $result->current()['id'];
}
}

Expand All @@ -73,17 +78,25 @@ public static function getUserVipList($entities)
{
global $DB;

$vip = [];
$vipquery = "SELECT `glpi_groups_users`.`users_id`
FROM `glpi_groups_users`
LEFT JOIN `glpi_plugin_vip_groups`
ON `glpi_plugin_vip_groups`.`id` = `glpi_groups_users`.`groups_id`
WHERE `glpi_plugin_vip_groups`.`isvip` = 1";

$result = $DB->query($vipquery);
$nb = $DB->numrows($result);
if ($nb > 0) {
while ($uids = $DB->fetchArray($result)) {
$vip = [];

$result = $DB->request([
'SELECT' => ['glpi_groups_users.users_id'],
'FROM' => 'glpi_groups_users',
'LEFT JOIN' => [
'glpi_plugin_vip_groups' => [
'ON' => [
'glpi_plugin_vip_groups' => 'id',
'glpi_groups_users' => 'groups_id'
]
]
],
'WHERE' => [
'glpi_plugin_vip_groups.isvip' => 1
]
]);
if (count($result) > 0) {
foreach ($result as $uids) {
$vip[] = $uids['users_id'];
}
}
Expand All @@ -100,14 +113,16 @@ public static function isTicketVip($ticketid)
global $DB;

if ($ticketid > 0) {
$userquery = "SELECT `users_id`
FROM `glpi_tickets_users`
WHERE `type` = " . CommonITILActor::REQUESTER . "
AND `tickets_id` = " . $ticketid;
$userresult = $DB->query($userquery);
$nb = $DB->numrows($userresult);
if ($nb > 0) {
while ($uids = $DB->fetchArray($userresult)) {
$userresult = $DB->request([
'SELECT' => ['users_id'],
'FROM' => 'glpi_tickets_users',
'WHERE' => [
'type' => CommonITILActor::REQUESTER,
'tickets_id' => $ticketid
]
]);
if (count($userresult) > 0) {
foreach ($userresult as $uids) {
$isuservip = self::isUserVip($uids['users_id']);
if ($isuservip > 0) {
return $isuservip;
Expand Down
12 changes: 10 additions & 2 deletions inc/vip.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ public static function afterAdd(User $user)
if (isset($fields['groups_id'])) {
$groupuser = new Group_User();

if (!$groupuser->find(["`users_id` = " . $user->getID() . " AND `groups_id` =" . $fields['groups_id']])) {
$result = $groupuser->find([
'users_id' => $user->getID(),
'groups_id' => $fields['groups_id']
]);
if (!$result) {
$groupuser->add(['users_id' => $user->getID(),
'groups_id' => $fields['groups_id']]);
}
Expand Down Expand Up @@ -132,7 +136,11 @@ public static function afterUpdate(User $user)
if (isset($fields['groups_id'])) {
$groupuser = new Group_User();

if (!$groupuser->find(["`users_id` = " . $user->getID() . " AND `groups_id` =" . $fields['groups_id']])) {
$result = $groupuser->find([
'users_id' => $user->getID(),
'groups_id' => $fields['groups_id']
]);
if (!$result) {
$groupuser->add(['users_id' => $user->getID(),
'groups_id' => $fields['groups_id']]);
}
Expand Down
6 changes: 6 additions & 0 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
--------------------------------------------------------------------------
*/

use Glpi\Plugin\Hooks;

define('PLUGIN_VIP_VERSION', '1.8.3');

if (!defined("PLUGIN_VIP_DIR")) {
Expand Down Expand Up @@ -83,6 +85,10 @@ function plugin_init_vip() {
Plugin::registerClass('PluginVipRuleVipCollection', [
'rulecollections_types' => true
]);

// Cannot be placed inside any permission check as the plugin is initialized before the API router authenticates the user
//TODO activate in GLPI 11 version
// $PLUGIN_HOOKS[Hooks::REDEFINE_API_SCHEMAS]['vip'] = 'plugin_vip_redefine_api_schemas';
}

function plugin_version_vip() {
Expand Down

0 comments on commit 0e5d802

Please sign in to comment.