Skip to content

Commit

Permalink
Add User itemtype to Contracts linked items
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLvr authored Dec 9, 2024
1 parent ebc5b5e commit a8e86d5
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 24 deletions.
11 changes: 9 additions & 2 deletions front/contract_item.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@

Session::checkCentralAccess();

$contract_item = new Contract_Item();

if (isset($_POST["add"])) {
if (!isset($_POST['contracts_id']) || empty($_POST['contracts_id'])) {
$message = sprintf(
Expand All @@ -54,6 +52,15 @@
Html::back();
}

if (isset($_POST['itemtype']) && $_POST['itemtype'] == 'User') {
$contract_item = new Contract_User();
// convert form data to match the Contract_User case
$_POST['users_id'] = $_POST['items_id'];
unset($_POST['itemtype'], $_POST['items_id']);
} else {
$contract_item = new Contract_Item();
}

$contract_item->check(-1, CREATE, $_POST);
if ($contract_item->add($_POST)) {
Event::log(
Expand Down
2 changes: 2 additions & 0 deletions inc/relation.constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
'_glpi_contracts_suppliers' => 'contracts_id',
'glpi_entities' => 'contracts_id_default',
'_glpi_tickets_contracts' => 'contracts_id',
'_glpi_contracts_users' => 'contracts_id',
],

'glpi_contracttypes' => [
Expand Down Expand Up @@ -1656,6 +1657,7 @@
'users_id',
'users_id_substitute',
],
'_glpi_contracts_users' => 'users_id',
],

'glpi_usertitles' => [
Expand Down
55 changes: 55 additions & 0 deletions install/migrations/update_10.0.x_to_11.0.0/contract_user.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

/**
* @var \Migration $migration
* @var array $ADDTODISPLAYPREF
* @var \DBmysql $DB
*/

$default_charset = DBConnection::getDefaultCharset();
$default_collation = DBConnection::getDefaultCollation();

if (!$DB->tableExists('glpi_contracts_users')) {
$query = "CREATE TABLE `glpi_contracts_users` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`contracts_id` int unsigned NOT NULL DEFAULT '0',
`users_id` int unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`contracts_id`,`users_id`),
KEY `item` (`users_id`)
) ENGINE=InnoDB DEFAULT CHARSET = {$default_charset} COLLATE = {$default_collation} ROW_FORMAT=DYNAMIC";
$DB->doQuery($query);
}
10 changes: 10 additions & 0 deletions install/mysql/glpi-empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10086,4 +10086,14 @@ CREATE TABLE `glpi_assets_customfielddefinitions` (
KEY `system_name` (`system_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;

DROP TABLE IF EXISTS `glpi_contracts_users`;
CREATE TABLE `glpi_contracts_users` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`contracts_id` int unsigned NOT NULL DEFAULT '0',
`users_id` int unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`contracts_id`, `users_id`),
KEY `item` (`users_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;

SET FOREIGN_KEY_CHECKS=1;
34 changes: 34 additions & 0 deletions phpunit/functional/ContractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,38 @@ public function testGetSpecificValueToDisplay($field, $values, $expected)
$contract = new \Contract();
$this->assertEquals($expected, $contract->getSpecificValueToDisplay($field, $values));
}

public function testLinkUser()
{
$this->login();
$this->setEntity('_test_root_entity', true);

$contract = new \Contract();
$input = [
'name' => 'A test contract',
'entities_id' => 0
];
$cid = $contract->add($input);
$this->assertGreaterThan(0, $cid);

$user = new \User();
$uid = $user->add([
'name' => 'Test User',
'firstname' => 'Test',
'realname' => 'User',
'entities_id' => 0
]);
$this->assertGreaterThan(0, $uid);

$link_user = new \Contract_User();
$link_id = $link_user->add([
'users_id' => $uid,
'contracts_id' => $cid
]);
$this->assertGreaterThan(0, $link_id);

$this->assertTrue($link_user->getFromDB($link_id));
$relation_items = $link_user->getItemsAssociatedTo($contract->getType(), $cid);
$this->assertCount(1, $relation_items, 'Original Contract_User not found!');
}
}
86 changes: 64 additions & 22 deletions src/Contract_Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,11 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
case Contract::class:
if ($_SESSION['glpishow_count_on_tabs']) {
$nb = self::countForMainItem($item);
$nb += countElementsInTable(Contract_User::getTable(), ['contracts_id' => $item->fields['id']]);
}
return self::createTabEntry(_n('Item', 'Items', Session::getPluralNumber()), $nb, $item::class, 'ti ti-package');

return self::createTabEntry(_n('Affected item', 'Affected items', Session::getPluralNumber()), $nb, $item::class, 'ti ti-package');
default:
if (
$_SESSION['glpishow_count_on_tabs']
&& in_array($item::class, $CFG_GLPI["contract_types"], true)
) {
if (in_array($item::class, $CFG_GLPI["contract_types"], true)) {
$nb = self::countForItem($item);
}
return self::createTabEntry(Contract::getTypeName(Session::getPluralNumber()), $nb, $item::class);
Expand Down Expand Up @@ -394,8 +391,11 @@ public static function showForItem(CommonDBTM $item, $withtemplate = 0)
**/
public static function showForContract(Contract $contract, $withtemplate = 0)
{
/** @var \DBmysql $DB */
global $DB;
/**
* @var \DBmysql $DB
* @var array $CFG_GLPI
*/
global $DB, $CFG_GLPI;

$instID = $contract->fields['id'];

Expand Down Expand Up @@ -482,10 +482,43 @@ public static function showForContract(Contract $contract, $withtemplate = 0)
foreach ($iterator as $objdata) {
$data[$itemtype][$objdata['id']] = $objdata;
$used[$itemtype][$objdata['id']] = $objdata['id'];
$totalnb++;
}
}
}

// Add contract users
$contract_users_table = Contract_User::getTable();
$users_table = User::getTable();
$user_params = [
'SELECT' => [
"$users_table.*",
"$contract_users_table.id AS linkid",
],
'FROM' => $contract_users_table,
'LEFT JOIN' => [
$users_table => [
'FKEY' => [
$contract_users_table => 'users_id',
$users_table => 'id'
]
],
],
'WHERE' => [
"$contract_users_table.contracts_id" => $instID
],
'ORDER' => "$users_table.name"
];

$user_iterator = $DB->request($user_params);

$data[User::class] = [];
foreach ($user_iterator as $userdata) {
$data[User::class][$userdata['id']] = $userdata;
$used[User::class][$userdata['id']] = $userdata['id'];
$totalnb++;
}

if (
$canedit
&& (((int) $contract->fields['max_links_allowed'] === 0)
Expand All @@ -494,6 +527,7 @@ public static function showForContract(Contract $contract, $withtemplate = 0)
) {
$twig_params = [
'contract' => $contract,
'contract_types' => array_merge($CFG_GLPI["contract_types"], [User::class]),
'entity_restrict' => $contract->fields['is_recursive']
? getSonsOf('glpi_entities', $contract->fields['entities_id'])
: $contract->fields['entities_id'],
Expand All @@ -509,7 +543,7 @@ public static function showForContract(Contract $contract, $withtemplate = 0)
<input type="hidden" name="contracts_id" value="{{ contract.getID() }}">
<input type="hidden" name="_glpi_csrf_token" value="{{ csrf_token() }}">
{{ fields.dropdownItemsFromItemtypes('', null, {
itemtypes: config('contract_types'),
itemtypes: contract_types,
entity_restrict: entity_restrict,
checkright: true,
used: used
Expand All @@ -531,7 +565,7 @@ public static function showForContract(Contract $contract, $withtemplate = 0)
foreach ($data as $itemtype => $datas) {
foreach ($datas as $objdata) {
$entry = [
'itemtype' => self::class,
'itemtype' => $itemtype === User::class ? Contract_User::class : self::class,
'id' => $objdata['linkid'],
'row_class' => isset($objdata['is_deleted']) && $objdata['is_deleted'] ? 'table-danger' : '',
'type' => $itemtype::getTypeName(1),
Expand All @@ -540,23 +574,31 @@ public static function showForContract(Contract $contract, $withtemplate = 0)
$item->getFromResultSet($objdata);
$entry['name'] = $item->getLink();

if (!isset($entity_cache[$objdata['entity']])) {
$entity_cache[$objdata['entity']] = Dropdown::getDropdownName(
"glpi_entities",
$objdata['entity']
);
if (isset($objdata['entity'])) {
if (!isset($entity_cache[$objdata['entity']])) {
$entity_cache[$objdata['entity']] = Dropdown::getDropdownName(
"glpi_entities",
$objdata['entity']
);
}
$entry['entity'] = $entity_cache[$objdata['entity']];
} else {
$entry['entity'] = '-';
}
$entry['entity'] = $entity_cache[$objdata['entity']];
$entry['serial'] = $objdata['serial'] ?? '-';
$entry['otherserial'] = $objdata['otherserial'] ?? '-';

if (!isset($state_cache[$objdata['states_id']])) {
$state_cache[$objdata['states_id']] = Dropdown::getDropdownName(
"glpi_states",
$objdata['states_id']
);
if (isset($objdata['states_id'])) {
if (!isset($state_cache[$objdata['states_id']])) {
$state_cache[$objdata['states_id']] = Dropdown::getDropdownName(
"glpi_states",
$objdata['states_id']
);
}
$entry['status'] = $state_cache[$objdata['states_id']];
} else {
$entry['status'] = '-';
}
$entry['status'] = $state_cache[$objdata['states_id']];
$entries[] = $entry;
}
}
Expand Down
Loading

0 comments on commit a8e86d5

Please sign in to comment.