static function listTemplates($itemtype, $target, $add = 0)
{
global $DB;
if (!($item = getItemForItemtype($itemtype))) {
return false;
}
if (!$item->maybeTemplate()) {
return false;
}
// Avoid to get old data
$item->clearSavedInput();
//Check is user have minimum right r
if (!$item->canView() && !$item->canCreate()) {
return false;
}
$query = "SELECT *\n FROM `" . $item->getTable() . "`\n WHERE `is_template` = '1' ";
if ($item->isEntityAssign()) {
$query .= getEntitiesRestrictRequest('AND', $item->getTable(), 'entities_id', $_SESSION['glpiactiveentities'], $item->maybeRecursive());
}
$query .= " ORDER by `template_name`";
if (Session::isMultiEntitiesMode()) {
$colspan = 3;
} else {
$colspan = 2;
}
if ($result = $DB->query($query)) {
echo "<div class='center'><table class='tab_cadre'>";
if ($add) {
$blank_params = (strpos($target, '?') ? '&' : '?') . "id=-1&withtemplate=2";
$target_blank = $target . $blank_params;
echo "<tr><th>" . $item->getTypeName(1) . "</th>";
echo "<th>" . __('Choose a template') . "</th></tr>";
echo "<tr><td class='tab_bg_1 center' colspan='{$colspan}'>";
echo "<a href=\"{$target_blank}\">" . __('Blank Template') . "</a></td>";
echo "</tr>";
} else {
echo "<tr><th>" . $item->getTypeName(1) . "</th>";
if (Session::isMultiEntitiesMode()) {
echo "<th>" . __('Entity') . "</th>";
}
echo "<th>" . __('Templates') . "</th></tr>";
}
while ($data = $DB->fetch_assoc($result)) {
$templname = $data["template_name"];
if ($_SESSION["glpiis_ids_visible"] || empty($data["template_name"])) {
$templname = sprintf(__('%1$s (%2$s)'), $templname, $data["id"]);
}
if (Session::isMultiEntitiesMode()) {
$entity = Dropdown::getDropdownName('glpi_entities', $data['entities_id']);
}
if ($item->canCreate() && !$add) {
$modify_params = (strpos($target, '?') ? '&' : '?') . "id=" . $data['id'] . "&withtemplate=1";
$target_modify = $target . $modify_params;
echo "<tr><td class='tab_bg_1 center'>";
echo "<a href=\"{$target_modify}\">";
echo " {$templname} </a></td>";
if (Session::isMultiEntitiesMode()) {
echo "<td class='tab_bg_1 center'>{$entity}</td>";
}
echo "<td class='tab_bg_2 center b'>";
Html::showSimpleForm($target, 'purge', _x('button', 'Delete permanently'), array('withtemplate' => 1, 'id' => $data['id']));
echo "</td>";
} else {
$add_params = (strpos($target, '?') ? '&' : '?') . "id=" . $data['id'] . "&withtemplate=2";
$target_add = $target . $add_params;
echo "<tr><td class='tab_bg_1 center' colspan='2'>";
echo "<a href=\"{$target_add}\">";
echo " {$templname} </a></td>";
}
echo "</tr>";
}
if ($item->canCreate() && !$add) {
$create_params = (strpos($target, '?') ? '&' : '?') . "withtemplate=1";
$target_create = $target . $create_params;
echo "<tr><td class='tab_bg_2 center b' colspan='3'>";
echo "<a href=\"{$target_create}\">" . __('Add a template...') . "</a>";
echo "</td></tr>";
}
echo "</table></div>\n";
}
}