Skip to content

Commit

Permalink
Add project tab in asset
Browse files Browse the repository at this point in the history
  • Loading branch information
Lainow authored Dec 16, 2024
1 parent b7621c7 commit f59177b
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 12 deletions.
71 changes: 71 additions & 0 deletions phpunit/functional/Item_ProjectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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/>.
*
* ---------------------------------------------------------------------
*/

namespace tests\units;

use DbTestCase;

/* Test for inc/itil_project.class.php */

class Item_ProjectTest extends DbTestCase
{
/**
* Test the presence of the Item_Project tab in the $CFG_GLPI["project_asset_types"]
*
* @return void
*/
public function testItemProjectTab()
{
/** @var array $CFG_GLPI */
global $CFG_GLPI;

$this->login();

foreach ($CFG_GLPI["project_asset_types"] as $itemtype) {
$item = new $itemtype();
$item_id = $item->add(
[
'name' => 'Test project',
'entities_id' => 0
]
);
$this->assertGreaterThan(0, $item_id);

$item->getFromDB($item_id);
$tabs = $item->defineTabs();
$this->assertArrayHasKey('Item_Project$1', $tabs);
}
}
}
1 change: 1 addition & 0 deletions src/Appliance.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function defineTabs($options = [])
->addStandardTab('Item_Ticket', $ong, $options)
->addStandardTab('Item_Problem', $ong, $options)
->addStandardTab('Change_Item', $ong, $options)
->addStandardTab('Item_Project', $ong, $options)
->addStandardTab('ManualLink', $ong, $options)
->addStandardTab('DatabaseInstance', $ong, $options)
->addStandardTab('Notepad', $ong, $options)
Expand Down
1 change: 1 addition & 0 deletions src/Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ public function defineTabs($options = [])
->addStandardTab('Item_Ticket', $ong, $options)
->addStandardTab('Item_Problem', $ong, $options)
->addStandardTab('Change_Item', $ong, $options)
->addStandardTab('Item_Project', $ong, $options)
->addStandardTab('ManualLink', $ong, $options)
->addStandardTab('Lock', $ong, $options)
->addStandardTab('Notepad', $ong, $options)
Expand Down
1 change: 1 addition & 0 deletions src/CommonDevice.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ public function defineTabs($options = [])
$this->addDefaultFormTab($ong);
$this->addImpactTab($ong, $options);
$this->addStandardTab(static::getItem_DeviceType(), $ong, $options);
$this->addStandardTab('Item_Project', $ong, $options);
$this->addStandardTab('Document_Item', $ong, $options);
$this->addStandardTab('Log', $ong, $options);

Expand Down
1 change: 1 addition & 0 deletions src/Computer.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public function defineTabs($options = [])
->addStandardTab('Item_Ticket', $ong, $options)
->addStandardTab('Item_Problem', $ong, $options)
->addStandardTab('Change_Item', $ong, $options)
->addStandardTab('Item_Project', $ong, $options)
->addStandardTab('ManualLink', $ong, $options)
->addStandardTab('Certificate_Item', $ong, $options)
->addStandardTab('Lock', $ong, $options)
Expand Down
1 change: 1 addition & 0 deletions src/DeviceCamera.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function defineTabs($options = [])
->addStandardTab('Item_DeviceCamera_ImageFormat', $ong, $options)
->addStandardTab('Infocom', $ong, $options)
->addStandardTab('Contract_Item', $ong, $options)
->addStandardTab('Item_Project', $ong, $options)
->addStandardTab('Log', $ong, $options);
return $ong;
}
Expand Down
107 changes: 102 additions & 5 deletions src/Item_Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
* ---------------------------------------------------------------------
*/

use Glpi\Application\View\TemplateRenderer;

/**
* Item_Project Class
*
Expand Down Expand Up @@ -222,6 +224,8 @@ public static function showForProject(Project $project)

public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
{
/** @var array $CFG_GLPI */
global $CFG_GLPI;

if (!$withtemplate) {
$nb = 0;
Expand All @@ -233,10 +237,10 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
return self::createTabEntry(_n('Item', 'Items', Session::getPluralNumber()), $nb, $item::getType(), 'ti ti-package');

default:
// Not used now
if (
Session::haveRight("project", Project::READALL)
&& ($item instanceof CommonDBTM)
Project::canView()
&& $item instanceof CommonDBTM
&& in_array($item->getType(), $CFG_GLPI["project_asset_types"])
) {
if ($_SESSION['glpishow_count_on_tabs']) {
// Direct one
Expand Down Expand Up @@ -265,16 +269,109 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)

public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
{
/** @var array $CFG_GLPI */
global $CFG_GLPI;

switch ($item->getType()) {
case 'Project':
self::showForProject($item);
break;

default:
// Not defined and used now
// Project::showListForItem($item);
if (
Project::canView()
&& $item instanceof CommonDBTM
&& in_array($item->getType(), $CFG_GLPI["project_asset_types"])
) {
self::showForAsset($item);
}
break;
}
return true;
}

private static function showForAsset(CommonDBTM $item): void
{

$item_project = new self();
$item_projects = $item_project->find([
'itemtype' => $item->getType(),
'items_id' => $item->getID()
]);

$used = $entries = [];

foreach ($item_projects as $value) {
$used[] = $value['projects_id'];
$project = new Project();
$result = $project->getFromDB($value['projects_id']);

if ($result === false) {
continue;
}

$priority = CommonITILObject::getPriorityName($project->fields['priority']);
$prioritycolor = $_SESSION["glpipriority_" . $project->fields['priority']];
$state = ProjectState::getById($project->fields['projectstates_id']);

if (!$project->can($project->fields['id'], READ)) {
$data = [
'name' => $project->getLink(),
'projectstates_id' => '',
'priority' => '',
'percent_done' => '',
];
} else {
$data = [
'name' => $project->getLink(),
'projectstates_id' => $state !== false
? [
'content' => $state->fields['name'],
'color' => $state->fields['color']
] : '',
'priority' => [
'content' => $priority,
'color' => $prioritycolor
],
'percent_done' => Html::getProgressBar((float)$project->fields['percent_done'])
];
}
$entries[] = array_merge($project->fields, $data);
}

$cols = [
'columns' => [
"name" => __('Name'),
"priority" => __('Priority'),
"code" => __('Code'),
"projectstates_id" => _n('State', 'States', 1),
"percent_done" => __('Percent done'),
"creation_date" => __('Creation date'),
"content" => __('Description'),
],
'formatters' => [
'name' => 'raw_html',
'priority' => 'badge',
'projectstates_id' => 'badge',
'percent_done' => 'raw_html',
'creation_date' => 'date',
]
];

TemplateRenderer::getInstance()->display('pages/tools/item_project.html.twig', [
'item' => $item,
'used' => $used,
'datatable_params' => [
'is_tab' => true,
'nopager' => true,
'nofilter' => true,
'nosort' => true,
'columns' => $cols['columns'],
'formatters' => $cols['formatters'],
'entries' => $entries,
'total_number' => count($entries),
'filtered_number' => count($entries),
]
]);
}
}
1 change: 1 addition & 0 deletions src/Monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public function defineTabs($options = [])
$this->addStandardTab('Item_Ticket', $ong, $options);
$this->addStandardTab('Item_Problem', $ong, $options);
$this->addStandardTab('Change_Item', $ong, $options);
$this->addStandardTab('Item_Project', $ong, $options);
$this->addStandardTab('ManualLink', $ong, $options);
$this->addStandardTab('Lock', $ong, $options);
$this->addStandardTab('Notepad', $ong, $options);
Expand Down
1 change: 1 addition & 0 deletions src/NetworkEquipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public function defineTabs($options = [])
->addStandardTab('Item_Ticket', $ong, $options)
->addStandardTab('Item_Problem', $ong, $options)
->addStandardTab('Change_Item', $ong, $options)
->addStandardTab('Item_Project', $ong, $options)
->addStandardTab('ManualLink', $ong, $options)
->addStandardTab('Lock', $ong, $options)
->addStandardTab('Notepad', $ong, $options)
Expand Down
1 change: 1 addition & 0 deletions src/Peripheral.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public function defineTabs($options = [])
$this->addStandardTab('Item_Ticket', $ong, $options);
$this->addStandardTab('Item_Problem', $ong, $options);
$this->addStandardTab('Change_Item', $ong, $options);
$this->addStandardTab('Item_Project', $ong, $options);
$this->addStandardTab('ManualLink', $ong, $options);
$this->addStandardTab('Lock', $ong, $options);
$this->addStandardTab('Notepad', $ong, $options);
Expand Down
1 change: 1 addition & 0 deletions src/Phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public function defineTabs($options = [])
$this->addStandardTab('Item_Ticket', $ong, $options);
$this->addStandardTab('Item_Problem', $ong, $options);
$this->addStandardTab('Change_Item', $ong, $options);
$this->addStandardTab('Item_Project', $ong, $options);
$this->addStandardTab('ManualLink', $ong, $options);
$this->addStandardTab('Lock', $ong, $options);
$this->addStandardTab('Notepad', $ong, $options);
Expand Down
1 change: 1 addition & 0 deletions src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public function defineTabs($options = [])
$this->addStandardTab('Item_Ticket', $ong, $options);
$this->addStandardTab('Item_Problem', $ong, $options);
$this->addStandardTab('Change_Item', $ong, $options);
$this->addStandardTab('Item_Project', $ong, $options);
$this->addStandardTab('ManualLink', $ong, $options);
$this->addStandardTab('Lock', $ong, $options);
$this->addStandardTab('Notepad', $ong, $options);
Expand Down
2 changes: 1 addition & 1 deletion src/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -2855,7 +2855,7 @@ public static function showListForCentral(string $itemtype): void
),
],
[
'content' => $project->fields['percent_done'] . '%',
'content' => Html::getProgressBar((float)$project->fields['percent_done'])
]
]
];
Expand Down
2 changes: 1 addition & 1 deletion src/ProjectTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,7 @@ public static function showListForCentral(string $itemtype): void
'content' => $project->getLink(),
],
[
'content' => $projecttask->fields['percent_done'] . '%'
'content' => Html::getProgressBar((float)$projecttask->fields['percent_done'])
]
]
];
Expand Down
1 change: 1 addition & 0 deletions src/Software.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public function defineTabs($options = [])
$this->addStandardTab('Item_Ticket', $ong, $options);
$this->addStandardTab('Item_Problem', $ong, $options);
$this->addStandardTab('Change_Item', $ong, $options);
$this->addStandardTab('Item_Project', $ong, $options);
$this->addStandardTab('ManualLink', $ong, $options);
$this->addStandardTab('Notepad', $ong, $options);
$this->addStandardTab('Reservation', $ong, $options);
Expand Down
23 changes: 18 additions & 5 deletions templates/components/datatable.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,24 @@
{% set initials = entry_data['initials'] %}
{% set bg_color = img is not empty ? 'inherit' : entry_data['initials_bg'] %}
<span class="avatar {{ avatar_size }} rounded"
style="{% if img is not null %} background-image: url({{ img }}); {% endif %} background-color: {{ bg_color }}">
{% if img is empty %}
{{ initials }}
{% endif %}
</span>
style="{% if img is not null %} background-image: url({{ img }}); {% endif %} background-color: {{ bg_color }}">
{% if img is empty %}
{{ initials }}
{% endif %}
</span>
{% elseif formatter == "badge" %}
{% set entry_data = entry[colkey] %}
{% set content = entry_data['content'] %}
{% set color = entry_data['color'] ?? '#BBBBBB' %}
{% if not (color matches '/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/') %}
{% set color = '#BBBBBB' %}
{% endif %}
{% if content is not empty %}
<div class="badge_block" style="border-color: {{ color }}">
<span class="me-1" style="background: {{ color }}"></span>
{{ content }}
</div>
{% endif %}
{% else %}
{{ entry[colkey] }}
{% endif %}
Expand Down
Loading

0 comments on commit f59177b

Please sign in to comment.