DropdownTranslation::showTranslations PHP Method

showTranslations() static public method

Display all translated field for a dropdown
static public showTranslations ( CommonDropdown $item ) : true;
$item CommonDropdown a Dropdown item
return true;
    static function showTranslations(CommonDropdown $item)
    {
        global $DB, $CFG_GLPI;
        $rand = mt_rand();
        $canedit = $item->can($item->getID(), UPDATE);
        if ($canedit) {
            echo "<div id='viewtranslation" . $item->getType() . $item->getID() . "{$rand}'></div>\n";
            echo "<script type='text/javascript' >\n";
            echo "function addTranslation" . $item->getType() . $item->getID() . "{$rand}() {\n";
            $params = array('type' => __CLASS__, 'parenttype' => get_class($item), $item->getForeignKeyField() => $item->getID(), 'id' => -1);
            Ajax::updateItemJsCode("viewtranslation" . $item->getType() . $item->getID() . "{$rand}", $CFG_GLPI["root_doc"] . "/ajax/viewsubitem.php", $params);
            echo "};";
            echo "</script>\n";
            echo "<div class='center'>" . "<a class='vsubmit' href='javascript:addTranslation" . $item->getType() . $item->getID() . "{$rand}();'>" . __('Add a new translation') . "</a></div><br>";
        }
        $query = "SELECT *\n                FROM `" . getTableForItemType(__CLASS__) . "`\n                WHERE `itemtype` = '" . get_class($item) . "'\n                      AND `items_id` = '" . $item->getID() . "'\n                      AND `field` <> 'completename'\n                ORDER BY `language` ASC";
        $results = $DB->query($query);
        if ($DB->numrows($results)) {
            if ($canedit) {
                Html::openMassiveActionsForm('mass' . __CLASS__ . $rand);
                $massiveactionparams = array('container' => 'mass' . __CLASS__ . $rand);
                Html::showMassiveActions($massiveactionparams);
            }
            echo "<div class='center'>";
            echo "<table class='tab_cadre_fixehov'><tr class='tab_bg_2'>";
            echo "<th colspan='4'>" . __("List of translations") . "</th></tr><tr>";
            if ($canedit) {
                echo "<th width='10'>";
                Html::checkAllAsCheckbox('mass' . __CLASS__ . $rand);
                echo "</th>";
            }
            echo "<th>" . __("Language") . "</th>";
            echo "<th>" . __("Field") . "</th>";
            echo "<th>" . __("Value") . "</th></tr>";
            while ($data = $DB->fetch_array($results)) {
                $onhover = '';
                if ($canedit) {
                    $onhover = "style='cursor:pointer'\n                           onClick=\"viewEditTranslation" . $data['itemtype'] . $data['id'] . "{$rand}();\"";
                }
                echo "<tr class='tab_bg_1'>";
                if ($canedit) {
                    echo "<td class='center'>";
                    Html::showMassiveActionCheckBox(__CLASS__, $data["id"]);
                    echo "</td>";
                }
                echo "<td {$onhover}>";
                if ($canedit) {
                    echo "\n<script type='text/javascript' >\n";
                    echo "function viewEditTranslation" . $data['itemtype'] . $data['id'] . "{$rand}() {\n";
                    $params = array('type' => __CLASS__, 'parenttype' => get_class($item), $item->getForeignKeyField() => $item->getID(), 'id' => $data["id"]);
                    Ajax::updateItemJsCode("viewtranslation" . $item->getType() . $item->getID() . "{$rand}", $CFG_GLPI["root_doc"] . "/ajax/viewsubitem.php", $params);
                    echo "};";
                    echo "</script>\n";
                }
                echo Dropdown::getLanguageName($data['language']);
                echo "</td><td {$onhover}>";
                $searchOption = $item->getSearchOptionByField('field', $data['field']);
                echo $searchOption['name'] . "</td>";
                echo "<td {$onhover}>" . $data['value'] . "</td>";
                echo "</tr>";
            }
            echo "</table>";
            if ($canedit) {
                $massiveactionparams['ontop'] = false;
                Html::showMassiveActions($massiveactionparams);
                Html::closeForm();
            }
        } else {
            echo "<table class='tab_cadre_fixe'><tr class='tab_bg_2'>";
            echo "<th class='b'>" . __("No translation found") . "</th></tr></table>";
        }
        return true;
    }

Usage Example

 /**
  * @param $item            CommonGLPI object
  * @param $tabnum          (default 1)
  * @param $withtemplate    (default 0)
  **/
 static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
 {
     if (DropdownTranslation::canBeTranslated($item)) {
         DropdownTranslation::showTranslations($item);
     }
     return true;
 }