CommonDBTM::listTemplates PHP Method

listTemplates() static public method

static public listTemplates ( $itemtype, $target, $add )
$itemtype
$target
$add (default 0)
    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, '?') ? '&amp;' : '?') . "id=-1&amp;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, '?') ? '&amp;' : '?') . "id=" . $data['id'] . "&amp;withtemplate=1";
                    $target_modify = $target . $modify_params;
                    echo "<tr><td class='tab_bg_1 center'>";
                    echo "<a href=\"{$target_modify}\">";
                    echo "&nbsp;&nbsp;&nbsp;{$templname}&nbsp;&nbsp;&nbsp;</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, '?') ? '&amp;' : '?') . "id=" . $data['id'] . "&amp;withtemplate=2";
                    $target_add = $target . $add_params;
                    echo "<tr><td class='tab_bg_1 center' colspan='2'>";
                    echo "<a href=\"{$target_add}\">";
                    echo "&nbsp;&nbsp;&nbsp;{$templname}&nbsp;&nbsp;&nbsp;</a></td>";
                }
                echo "</tr>";
            }
            if ($item->canCreate() && !$add) {
                $create_params = (strpos($target, '?') ? '&amp;' : '?') . "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";
        }
    }

Usage Example

Exemplo n.º 1
0
GLPI 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 2 of the License, or
(at your option) any later version.

GLPI 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 GLPI. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/
// Based on:
// IRMA, Information Resource-Management and Administration
// Christian Bauer
/** @file
* @brief
*/
include '../inc/includes.php';
Session::checkCentralAccess();
if (isset($_GET["itemtype"])) {
    $link = Toolbox::getItemTypeFormURL($_GET["itemtype"]);
    $item = str_replace(".form.php", "", $link);
    $item = str_replace("front/", "", $item);
    Html::header(__('Manage templates...'), $_SERVER['PHP_SELF'], "inventory", $item);
    CommonDBTM::listTemplates($_GET["itemtype"], $link, $_GET["add"]);
    Html::footer();
}
CommonDBTM