Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(Approval) Twig Update #17900

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
312 changes: 130 additions & 182 deletions src/CommonITILValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,15 +688,15 @@ public static function getStatusColor($value)
break;

case self::REFUSED:
$style = "#cf9b9b";
$style = "#ff0000";
break;

case self::ACCEPTED:
$style = "#9BA563";
$style = "#43e900";
break;

default:
$style = "#cf9b9b";
$style = "#ff0000";
}
return $style;
}
Expand Down Expand Up @@ -966,204 +966,152 @@ public function showSummary(CommonDBTM $item)
$tID = $item->fields['id'];

$tmp = [static::$items_id => $tID];
$canadd = $this->can(-1, CREATE, $tmp);
$rand = mt_rand();

if ($canadd) {
$itemtype = static::$itemtype;
echo "<form method='post' name=form action='" . $itemtype::getFormURL() . "'>";
}
echo "<table class='tab_cadre_fixe'>";
echo "<tr>";
echo "<th colspan='3'>" . htmlspecialchars(self::getTypeName(Session::getPluralNumber())) . "</th>";
echo "</tr>";

echo "<tr class='tab_bg_1'>";
echo "<td>" . __s('Global approval status') . "</td>";
echo "<td colspan='2'>";
echo TicketValidation::getStatus($item->fields["global_validation"], true);
echo "</td></tr>";

echo "<tr>";
echo "<th colspan='2'>" . _sx('item', 'State') . "</th>";
echo "<th colspan='2'>";
echo htmlspecialchars(self::getValidationStats($tID));
echo "</th>";
echo "</tr>";

echo "<tr class='tab_bg_1'>";
echo "<td>" . __s('Minimum validation required') . "</td>";
if ($canadd) {
echo "<td>";
echo $item->getValueToSelect(
'validation_percent',
'validation_percent',
$item->fields["validation_percent"]
);
echo "</td>";
echo "<td><input type='submit' name='update' class='btn btn-outline-secondary' value='" .
_sx('button', 'Save') . "'>";
if (!empty($tID)) {
echo "<input type='hidden' name='id' value='$tID'>";
}
echo "</td>";
} else {
echo "<td colspan='2'>";
echo Dropdown::getValueWithUnit($item->fields["validation_percent"], "%");
echo "</td>";
}
echo "</tr>";
echo "</table>";
if ($canadd) {
Html::closeForm();
}

$iterator = $DB->Request([
'FROM' => $this->getTable(),
'WHERE' => [static::$items_id => $item->getField('id')],
'ORDER' => 'submission_date DESC'
]);

$colonnes = ['', _sx('item', 'State'), __s('Request date'), __s('Approval requester'),
__s('Request comments'), __s('Approval status'),
__s('Requested approver type'), __s('Requested approver'),
];
$nb_colonnes = count($colonnes);

echo "<table class='tab_cadre_fixehov'>";
echo "<tr class='noHover'><th colspan='" . $nb_colonnes . "'>" . __s('Approvals for the ticket') .
"</th></tr>";
Session::initNavigateListItems(
static::class,
//TRANS : %1$s is the itemtype name, %2$s is the name of the item (used for headings of a list)
sprintf(
__('%1$s = %2$s'),
$item::getTypeName(1),
$item->fields["name"]
)
);
$values = [];
foreach ($iterator as $row) {
$canedit = $this->canEdit($row["id"]);
Session::addToNavigateListItems($this->getType(), $row["id"]);
$status = sprintf(
'<div class="badge fw-normal fs-4 text-wrap" style="border-color: %s;border-width: 2px;">%s</div>',
htmlspecialchars(self::getStatusColor($row['status'])),
htmlspecialchars(self::getStatus($row['status']))
);

if ($canadd) {
/** @var CommonITILObject $item */
if (
!in_array($item->fields['status'], array_merge(
$item->getSolvedStatusArray(),
$item->getClosedStatusArray()
))
) {
echo "<tr class='tab_bg_1 noHover'><td class='center' colspan='" . $nb_colonnes . "'>";
echo "<a class='btn btn-outline-secondary' href='javascript:viewAddValidation" . $tID . "$rand();'>";
echo __s('Send an approval request') . "</a></td></tr>\n";
}
}
if (count($iterator)) {
$header = "<tr>";
foreach ($colonnes as $colonne) {
$header .= "<th>" . $colonne . "</th>";
$comment_submission = RichText::getEnhancedHtml($this->fields['comment_submission'], ['images_gallery' => true]);
$type_name = null;
$target_name = null;
if ($row["itemtype_target"] === User::class) {
$type_name = User::getTypeName();
$target_name = getUserName($row["items_id_target"]);
} elseif (is_a($row["itemtype_target"], CommonDBTM::class, true)) {
$target = new $row["itemtype_target"]();
$type_name = $target::getTypeName();
if ($target->getFromDB($row["items_id_target"])) {
$target_name = $target->getName();
}
}
$header .= "</tr>";
echo $header;

Session::initNavigateListItems(
$this->getType(),
//TRANS : %1$s is the itemtype name, %2$s is the name of the item (used for headings of a list)
sprintf(
__('%1$s = %2$s'),
$item->getTypeName(1),
$item->fields["name"]
)
);
$is_answered = $row['status'] !== self::WAITING && $row['users_id_validate'] > 0;
$comment_validation = RichText::getEnhancedHtml($this->fields['comment_validation'] ?? '', ['images_gallery' => true]);

$doc_item = new Document_Item();
$docs = $doc_item->find([
"itemtype" => static::class,
"items_id" => $this->getID(),
"timeline_position" => ['>', CommonITILObject::NO_TIMELINE]
]);

foreach ($iterator as $row) {
$canedit = $this->canEdit($row["id"]);
Session::addToNavigateListItems($this->getType(), $row["id"]);
$bgcolor = htmlspecialchars(self::getStatusColor($row['status']));
$status = htmlspecialchars(self::getStatus($row['status']));

echo "<tr class='tab_bg_1'>";

echo "<td>";
if ($canedit) {
echo "<span class='far fa-edit' style='cursor:pointer' title='" . __s('Edit') . "' ";
echo "onClick=\"viewEditValidation" . ((int)$item->fields['id']) . ((int)$row["id"]) . "$rand();\"";
echo " id='viewvalidation" . ((int)$this->fields[static::$items_id]) . ((int)$row["id"]) . "$rand'";
echo "></span>";
echo "\n<script type='text/javascript' >\n";
echo "function viewEditValidation" . ((int)$item->fields['id']) . ((int)$row["id"]) . "$rand() {\n";
$params = ['type' => $this->getType(),
'parenttype' => static::$itemtype,
static::$items_id => $this->fields[static::$items_id],
'id' => $row["id"]
];
Ajax::updateItemJsCode(
"viewvalidation" . $item->fields['id'] . "$rand",
$CFG_GLPI["root_doc"] . "/ajax/viewsubitem.php",
$params
$document = "";
foreach ($docs as $docs_values) {
$doc = new Document();
if ($doc->getFromDB($docs_values['documents_id'])) {
$document .= sprintf(
'<a href="%s">%s</a><br />',
htmlspecialchars($doc->getLinkURL()),
htmlspecialchars($doc->getName())
);
echo "};";
echo "</script>\n";
}
echo "</td>";

echo "<td><div style='background-color:" . $bgcolor . ";'>" . $status . "</div></td>";
echo "<td>" . Html::convDateTime($row["submission_date"]) . "</td>";
echo "<td>" . htmlspecialchars(getUserName($row["users_id"])) . "</td>";
$comment_submission = RichText::getEnhancedHtml($this->fields['comment_submission'], ['images_gallery' => true]);
echo "<td><div class='rich_text_container'>" . $comment_submission . "</div></td>";
echo "<td>" . Html::convDateTime($row["validation_date"]) . "</td>";
$type_name = null;
$target_name = null;
if ($row["itemtype_target"] === 'User') {
$type_name = User::getTypeName();
$target_name = getUserName($row["items_id_target"]);
} elseif (is_a($row["itemtype_target"], CommonDBTM::class, true)) {
$target = new $row["itemtype_target"]();
$type_name = $target->getTypeName();
if ($target->getFromDB($row["items_id_target"])) {
$target_name = $target->getName();
}
}
echo "<td>" . htmlspecialchars($type_name) . "</td>";
echo "<td>" . htmlspecialchars($target_name) . "</td>";
$is_answered = $row['status'] !== self::WAITING && $row['users_id_validate'] > 0;
echo "<td>" . ($is_answered ? htmlspecialchars(getUserName($row["users_id_validate"])) : '') . "</td>";
$comment_validation = RichText::getEnhancedHtml($this->fields['comment_validation'] ?? '', ['images_gallery' => true]);
echo "<td><div class='rich_text_container'>" . $comment_validation . "</div></td>";

$doc_item = new Document_Item();
$docs = $doc_item->find(["itemtype" => $this->getType(),
"items_id" => $this->getID(),
"timeline_position" => ['>', CommonITILObject::NO_TIMELINE]
}

$script = "";
if ($canedit) {
$edit_title = __s('Edit');
$item_id = htmlspecialchars($item->fields['id']);
$row_id = htmlspecialchars($row["id"]);
$rand = htmlspecialchars($rand);
$view_validation_id = htmlspecialchars($this->fields[static::$items_id]);
$root_doc = htmlspecialchars($CFG_GLPI["root_doc"]);
$params_json = json_encode([
'type' => static::class,
'parenttype' => static::$itemtype,
static::$items_id => $this->fields[static::$items_id],
'id' => $row["id"]
]);
$out = "";
foreach ($docs as $docs_values) {
$doc = new Document();
$doc->getFromDB($docs_values['documents_id']);
$out .= "<a ";
$out .= "href=\"" . Document::getFormURLWithID($docs_values['documents_id']) . "\">";
$out .= htmlspecialchars($doc->getField('name')) . "</a><br>";
}
echo "<td>" . $out . "</td>";

echo "</tr>";
$script = <<<HTML
<span class="far fa-edit" style="cursor:pointer" title="{$edit_title}"
onclick="viewEditValidation{$item_id}{$row_id}{$rand}();"
id="viewvalidation{$view_validation_id}{$row_id}{$rand}">
</span>
<script>
function viewEditValidation{$item_id}{$row_id}{$rand}() {
$('#viewvalidation{$item_id}{$rand}').load('$root_doc/ajax/viewsubitem.php', $params_json);
};
</script>
HTML;
}
echo $header;
} else {
//echo "<div class='center b'>".__('No item found')."</div>";
echo "<tr class='tab_bg_1 noHover'><th colspan='" . $nb_colonnes . "'>";
echo __s('No item found') . "</th></tr>\n";
}
echo "</table>";

echo "<div id='viewvalidation" . $tID . "$rand'></div>\n";

if ($canadd) {
echo "<script type='text/javascript' >\n";
echo "function viewAddValidation" . $tID . "$rand() {\n";
$params = ['type' => $this->getType(),
'parenttype' => static::$itemtype,
static::$items_id => $tID,
'id' => -1
$values[] = [
'edit' => $script,
'status' => $status,
'type_name' => $type_name,
'target_name' => $target_name,
'is_answered' => $is_answered,
'comment_submission' => $comment_submission,
'comment_validation' => $comment_validation,
'document' => $document,
'submission_date' => $row["submission_date"],
'validation_date' => $row["validation_date"],
'user' => getUserName($row["users_id"]),
];
Ajax::updateItemJsCode(
"viewvalidation" . $tID . "$rand",
$CFG_GLPI["root_doc"] . "/ajax/viewsubitem.php",
$params
);
echo "};";
echo "</script>";
}

TemplateRenderer::getInstance()->display('components/itilobject/validation.html.twig', [
'canadd' => $this->can(-1, CREATE, $tmp),
'item' => $item,
'itemtype' => static::$itemtype,
'tID' => $tID,
'donestatus' => array_merge($item->getSolvedStatusArray(), $item->getClosedStatusArray()),
'validation' => $this,
'rand' => $rand,
'items_id' => static::$items_id,
]);

TemplateRenderer::getInstance()->display('components/datatable.html.twig', [
'is_tab' => true,
'nopager' => true,
'nofilter' => true,
'nosort' => true,
'columns' => [
'edit' => '',
'status' => _x('item', 'State'),
'submission_date' => __('Request date'),
'user' => __('Approval requester'),
'comment_submission' => __('Request comments'),
'validation_date' => __('Approval date'),
'type_name' => __('Requested approver type'),
'target_name' => __('Requested approver'),
'comment_validation' => __('Approval Comment'),
'document' => __('Documents'),
],
'formatters' => [
'edit' => 'raw_html',
'status' => 'raw_html',
'submission_date' => 'date',
'comment_submission' => 'raw_html',
'validation_date' => 'date',
'comment_validation' => 'raw_html',
'document' => 'raw_html',
],
'entries' => $values,
'total_number' => count($values),
'showmassiveactions' => false,
]);
}


Expand Down
Loading