/**
  * Show Licenses of a software
  *
  * @param $software software object
  *
  * @return nothing
  **/
 static function showForSoftware($software)
 {
     global $DB, $CFG_GLPI, $LANG;
     $softwares_id = $software->getField('id');
     $license = new SoftwareLicense();
     $computer = new Computer();
     if (!$software->can($softwares_id, "r")) {
         return false;
     }
     if (isset($_REQUEST["start"])) {
         $start = $_REQUEST["start"];
     } else {
         $start = 0;
     }
     if (isset($_REQUEST["order"]) && $_REQUEST["order"] == "DESC") {
         $order = "DESC";
     } else {
         $order = "ASC";
     }
     if (isset($_REQUEST["sort"]) && !empty($_REQUEST["sort"])) {
         $sort = "`" . $_REQUEST["sort"] . "`";
     } else {
         $sort = "`entity` {$order}, `name`";
     }
     // Righ type is enough. Can add a License on a software we have Read access
     $canedit = haveRight("software", "w");
     // Total Number of events
     $number = countElementsInTable("glpi_softwarelicenses", "glpi_softwarelicenses.softwares_id = {$softwares_id} " . getEntitiesRestrictRequest('AND', 'glpi_softwarelicenses', '', '', true));
     echo "<div class='spaced'>";
     if ($number < 1) {
         echo "<table class='tab_cadre_fixe'>";
         echo "<tr><th>" . $LANG['search'][15] . "</th></tr>\n";
         if ($canedit) {
             echo "<tr class='tab_bg_2'><td class='center'>";
             echo "<a href='softwarelicense.form.php?softwares_id={$softwares_id}'>" . $LANG['software'][8] . "</a>";
             echo "</td></tr>\n";
         }
         echo "</table></div>\n";
         return;
     }
     // Display the pager
     printAjaxPager($LANG['software'][11], $start, $number);
     $rand = mt_rand();
     $query = "SELECT `glpi_softwarelicenses`.*,\n                       `buyvers`.`name` AS buyname,\n                       `usevers`.`name` AS usename,\n                       `glpi_entities`.`completename` AS entity,\n                       `glpi_softwarelicensetypes`.`name` AS typename\n                FROM `glpi_softwarelicenses`\n                LEFT JOIN `glpi_softwareversions` AS buyvers\n                     ON (`buyvers`.`id` = `glpi_softwarelicenses`.`softwareversions_id_buy`)\n                LEFT JOIN `glpi_softwareversions` AS usevers\n                     ON (`usevers`.`id` = `glpi_softwarelicenses`.`softwareversions_id_use`)\n                LEFT JOIN `glpi_entities`\n                     ON (`glpi_entities`.`id` = `glpi_softwarelicenses`.`entities_id`)\n                LEFT JOIN `glpi_softwarelicensetypes`\n                     ON (`glpi_softwarelicensetypes`.`id`\n                          = `glpi_softwarelicenses`.`softwarelicensetypes_id`)\n                WHERE (`glpi_softwarelicenses`.`softwares_id` = '{$softwares_id}') " . getEntitiesRestrictRequest('AND', 'glpi_softwarelicenses', '', '', true) . "\n                ORDER BY {$sort} {$order}\n                LIMIT " . intval($start) . "," . intval($_SESSION['glpilist_limit']);
     initNavigateListItems('SoftwareLicense', $LANG['help'][31] . " = " . $software->fields["name"]);
     if ($result = $DB->query($query)) {
         if ($DB->numrows($result)) {
             if ($canedit) {
                 echo "<form method='post' name='massiveactionlicense_form{$rand}' id='" . "massiveactionlicense_form{$rand}' action=\"" . $CFG_GLPI["root_doc"] . "/front/massiveaction.php\">";
             }
             $sort_img = "<img src=\"" . $CFG_GLPI["root_doc"] . "/pics/" . ($order == "DESC" ? "puce-down.png" : "puce-up.png") . "\" alt='' title=''>";
             echo "<table class='tab_cadre_fixehov'><tr>";
             echo "<th>&nbsp;</th>";
             echo "<th>" . ($sort == "`name`" ? $sort_img : "") . "<a href='javascript:reloadTab(\"sort=name&amp;order=" . ($order == "ASC" ? "DESC" : "ASC") . "&amp;start=0\");'>" . $LANG['common'][16] . "</a></th>";
             if ($software->isRecursive()) {
                 // Ereg to search entity in string for match default order
                 echo "<th>" . (strstr($sort, "entity") ? $sort_img : "") . "<a href='javascript:reloadTab(\"sort=entity&amp;order=" . ($order == "ASC" ? "DESC" : "ASC") . "&amp;start=0\");'>" . $LANG['entity'][0] . "</a></th>";
             }
             echo "<th>" . ($sort == "`serial`" ? $sort_img : "") . "<a href='javascript:reloadTab(\"sort=serial&amp;order=" . ($order == "ASC" ? "DESC" : "ASC") . "&amp;start=0\");'>" . $LANG['common'][19] . "</a></th>";
             echo "<th>" . ($sort == "`number`" ? $sort_img : "") . "<a href='javascript:reloadTab(\"sort=number&amp;order=" . ($order == "ASC" ? "DESC" : "ASC") . "&amp;start=0\");'>" . $LANG['tracking'][29] . "</a></th>";
             echo "<th>" . $LANG['software'][9] . "</th>";
             echo "<th>" . ($sort == "`typename`" ? $sort_img : "") . "<a href='javascript:reloadTab(\"sort=typename&amp;order=" . ($order == "ASC" ? "DESC" : "ASC") . "&amp;start=0\");'>" . $LANG['common'][17] . "</a></th>";
             echo "<th>" . ($sort == "`buyname`" ? $sort_img : "") . "<a href='javascript:reloadTab(\"sort=buyname&amp;order=" . ($order == "ASC" ? "DESC" : "ASC") . "&amp;start=0\");'>" . $LANG['software'][1] . "</a></th>";
             echo "<th>" . ($sort == "`usename`" ? $sort_img : "") . "<a href='javascript:reloadTab(\"sort=usename&amp;order=" . ($order == "ASC" ? "DESC" : "ASC") . "&amp;start=0\");'>" . $LANG['software'][2] . "</a></th>";
             echo "<th>" . ($sort == "`expire`" ? $sort_img : "") . "<a href='javascript:reloadTab(\"sort=expire&amp;order=" . ($order == "ASC" ? "DESC" : "ASC") . "&amp;start=0\");'>" . $LANG['software'][32] . "</a></th>";
             echo "</tr>\n";
             $tot_assoc = 0;
             for ($tot = 0; $data = $DB->fetch_assoc($result);) {
                 addToNavigateListItems('SoftwareLicense', $data['id']);
                 echo "<tr class='tab_bg_2'>";
                 if ($license->can($data['id'], "w")) {
                     echo "<td><input type='checkbox' name='item[" . $data["id"] . "]' value='1'></td>";
                 } else {
                     echo "<td>&nbsp;</td>";
                 }
                 echo "<td><a href='softwarelicense.form.php?id=" . $data['id'] . "'>" . $data['name'] . (empty($data['name']) ? $data['id'] : "") . "</a></td>";
                 if ($software->isRecursive()) {
                     echo "<td>" . $data['entity'] . "</td>";
                 }
                 echo "<td>" . $data['serial'] . "</td>";
                 echo "<td class='right'>" . ($data['number'] > 0 ? $data['number'] . "&nbsp;&nbsp;" : $LANG['software'][4]) . "</td>";
                 $nb_assoc = Computer_SoftwareLicense::countForLicense($data['id']);
                 $tot_assoc += $nb_assoc;
                 echo "<td class='right'>{$nb_assoc}&nbsp;&nbsp;</td>";
                 echo "<td>" . $data['typename'] . "</td>";
                 echo "<td>" . $data['buyname'] . "</td>";
                 echo "<td>" . $data['usename'] . "</td>";
                 echo "<td class='center'>" . convDate($data['expire']) . "</td>";
                 echo "</tr>";
                 if ($data['number'] < 0) {
                     // One illimited license, total is illimited
                     $tot = -1;
                 } else {
                     if ($tot >= 0) {
                         // Not illimited, add the current number
                         $tot += $data['number'];
                     }
                 }
             }
             echo "<tr class='tab_bg_1'>";
             echo "<td colspan='" . ($software->isRecursive() ? 4 : 3) . "' class='right b'>" . $LANG['common'][33] . "</td>";
             echo "<td class='right b'>" . ($tot > 0 ? $tot . "&nbsp;&nbsp;" : $LANG['software'][4]) . "</td>";
             echo "<td class='right b'>{$tot_assoc}&nbsp;&nbsp;</td>";
             echo "<td colspan='4' class='center'>";
             if ($canedit) {
                 echo "<a href='softwarelicense.form.php?softwares_id={$softwares_id}'>" . $LANG['software'][8] . "</a>";
             }
             echo "</td></tr>";
             echo "</table>\n";
             if ($canedit) {
                 openArrowMassive("massiveactionlicense_form{$rand}", true);
                 Dropdown::showForMassiveAction('SoftwareLicense', 0, array('softwares_id' => $softwares_id));
                 closeArrowMassive();
                 echo "</form>";
             }
         } else {
             echo $LANG['search'][15];
         }
     }
     echo "</div>";
 }
}
if (!isset($_REQUEST['glpi_tab'])) {
    exit;
}
if (!isset($_POST["sort"])) {
    $_POST["sort"] = "";
}
if (!isset($_POST["order"])) {
    $_POST["order"] = "";
}
if (!isset($_POST["withtemplate"])) {
    $_POST["withtemplate"] = "";
}
checkRight("software", "r");
$license = new SoftwareLicense();
if ($_POST["id"] > 0 && $license->can($_POST["id"], 'r')) {
    switch ($_REQUEST['glpi_tab']) {
        case -1:
            Infocom::showForItem($license);
            Document::showAssociated($license);
            Plugin::displayAction($license, $_REQUEST['glpi_tab']);
            break;
        case 2:
            Computer_SoftwareLicense::showForLicense($license);
            break;
        case 4:
            Infocom::showForItem($license);
            break;
        case 5:
            Document::showAssociated($license);
            break;