/**
* Show software installed on a computer
*
* @param $comp Computer object
* @param $withtemplate template case of the view process (default '')
*
* @return nothing
**/
static function showForComputer(Computer $comp, $withtemplate = '')
{
global $DB, $CFG_GLPI;
if (!Software::canView()) {
return false;
}
$computers_id = $comp->getField('id');
$rand = mt_rand();
$canedit = Session::haveRightsOr("software", array(CREATE, UPDATE, DELETE, PURGE));
$entities_id = $comp->fields["entities_id"];
$crit = Session::getSavedOption(__CLASS__, 'criterion', -1);
$where = '';
if ($crit > -1) {
$where = " AND `glpi_softwares`.`softwarecategories_id` = {$crit}";
}
$add_dynamic = '';
if (Plugin::haveImport()) {
$add_dynamic = "`glpi_computers_softwareversions`.`is_dynamic`,";
}
$query = "SELECT `glpi_softwares`.`softwarecategories_id`,\n `glpi_softwares`.`name` AS softname,\n `glpi_computers_softwareversions`.`id`,\n {$add_dynamic}\n `glpi_states`.`name` AS state,\n `glpi_softwareversions`.`id` AS verid,\n `glpi_softwareversions`.`softwares_id`,\n `glpi_softwareversions`.`name` AS version,\n `glpi_softwares`.`is_valid` AS softvalid\n FROM `glpi_computers_softwareversions`\n LEFT JOIN `glpi_softwareversions`\n ON (`glpi_computers_softwareversions`.`softwareversions_id`\n = `glpi_softwareversions`.`id`)\n LEFT JOIN `glpi_states`\n ON (`glpi_states`.`id` = `glpi_softwareversions`.`states_id`)\n LEFT JOIN `glpi_softwares`\n ON (`glpi_softwareversions`.`softwares_id` = `glpi_softwares`.`id`)\n WHERE `glpi_computers_softwareversions`.`computers_id` = '{$computers_id}'\n AND `glpi_computers_softwareversions`.`is_deleted` = '0'\n {$where}\n ORDER BY `softname`, `version`";
$result = $DB->query($query);
$i = 0;
if ((empty($withtemplate) || $withtemplate != 2) && $canedit) {
echo "<form method='post' action='" . $CFG_GLPI["root_doc"] . "/front/computer_softwareversion.form.php'>";
echo "<div class='spaced'><table class='tab_cadre_fixe'>";
echo "<tr class='tab_bg_1'><td class='center'>";
echo _n('Software', 'Software', Session::getPluralNumber()) . " ";
echo "<input type='hidden' name='computers_id' value='{$computers_id}'>";
Software::dropdownSoftwareToInstall("softwareversions_id", $entities_id);
echo "</td><td width='20%'>";
echo "<input type='submit' name='add' value=\"" . _sx('button', 'Install') . "\"\n class='submit'>";
echo "</td>";
echo "</tr>\n";
echo "</table></div>\n";
Html::closeForm();
}
echo "<div class='spaced'>";
$cat = -1;
Session::initNavigateListItems('Software', sprintf(__('%1$s = %2$s'), Computer::getTypeName(1), $comp->getName()));
Session::initNavigateListItems('SoftwareLicense', sprintf(__('%1$s = %2$s'), Computer::getTypeName(1), $comp->getName()));
// Mini Search engine
echo "<table class='tab_cadre_fixe'>";
echo "<tr class='tab_bg_1'><th colspan='2'>" . Software::getTypeName(Session::getPluralNumber()) . "</th></tr>";
echo "<tr class='tab_bg_1'><td class='center'>";
echo __('Category') . "</td><td>";
SoftwareCategory::dropdown(array('value' => $crit, 'toadd' => array('-1' => __('All categories')), 'emptylabel' => __('Uncategorized software'), 'on_change' => 'reloadTab("start=0&criterion="+this.value)'));
echo "</td></tr></table></div>";
$number = $DB->numrows($result);
$start = isset($_REQUEST['start']) ? intval($_REQUEST['start']) : 0;
if ($start >= $number) {
$start = 0;
}
$installed = array();
if ($number) {
echo "<div class='spaced'>";
Html::printAjaxPager('', $start, $number);
if ($canedit) {
$rand = mt_rand();
Html::openMassiveActionsForm('mass' . __CLASS__ . $rand);
$massiveactionparams = array('num_displayed' => $number, 'container' => 'mass' . __CLASS__ . $rand, 'specific_actions' => array('purge' => _x('button', 'Delete permanently')));
Html::showMassiveActions($massiveactionparams);
}
echo "<table class='tab_cadre_fixehov'>";
$header_begin = "<tr>";
$header_top = '';
$header_bottom = '';
$header_end = '';
if ($canedit) {
$header_begin .= "<th width='10'>";
$header_top .= Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand);
$header_bottom .= Html::getCheckAllAsCheckbox('mass' . __CLASS__ . $rand);
$header_end .= "</th>";
}
$header_end .= "<th>" . __('Name') . "</th><th>" . __('Status') . "</th>";
$header_end .= "<th>" . __('Version') . "</th><th>" . __('License') . "</th>";
if (Plugin::haveImport()) {
$header_end .= "<th>" . __('Automatic inventory') . "</th>";
}
$header_end .= "<th>" . SoftwareCategory::getTypeName(1) . "</th>";
$header_end .= "<th>" . __('Valid license') . "</th>";
$header_end .= "</tr>\n";
echo $header_begin . $header_top . $header_end;
for ($row = 0; $data = $DB->fetch_assoc($result); $row++) {
if ($row >= $start && $row < $start + $_SESSION['glpilist_limit']) {
$licids = self::softsByCategory($data, $computers_id, $withtemplate, $canedit, true);
} else {
$licids = self::softsByCategory($data, $computers_id, $withtemplate, $canedit, false);
}
Session::addToNavigateListItems('Software', $data["softwares_id"]);
foreach ($licids as $licid) {
Session::addToNavigateListItems('SoftwareLicense', $licid);
$installed[] = $licid;
}
}
echo $header_begin . $header_bottom . $header_end;
echo "</table>";
if ($canedit) {
$massiveactionparams['ontop'] = false;
Html::showMassiveActions($massiveactionparams);
Html::closeForm();
}
} else {
echo "<p class='center b'>" . __('No item found') . "</p>";
}
echo "</div>\n";
if ((empty($withtemplate) || $withtemplate != 2) && $canedit) {
echo "<form method='post' action='" . $CFG_GLPI["root_doc"] . "/front/computer_softwarelicense.form.php'>";
echo "<div class='spaced'><table class='tab_cadre_fixe'>";
echo "<tr class='tab_bg_1'>";
echo "<td class='center'>";
echo _n('License', 'Licenses', Session::getPluralNumber()) . " ";
echo "<input type='hidden' name='computers_id' value='{$computers_id}'>";
Software::dropdownLicenseToInstall("softwarelicenses_id", $entities_id);
echo "</td><td width='20%'>";
echo "<input type='submit' name='add' value=\"" . _sx('button', 'Add') . "\" class='submit'>";
echo "</td></tr>\n";
echo "</table></div>\n";
Html::closeForm();
}
echo "<div class='spaced'>";
// Affected licenses NOT installed
$query = "SELECT `glpi_softwarelicenses`.*,\n `glpi_computers_softwarelicenses`.`id` AS linkID,\n `glpi_softwares`.`name` AS softname,\n `glpi_softwareversions`.`name` AS version,\n `glpi_states`.`name` AS state\n FROM `glpi_softwarelicenses`\n LEFT JOIN `glpi_computers_softwarelicenses`\n ON (`glpi_computers_softwarelicenses`.softwarelicenses_id\n = `glpi_softwarelicenses`.`id`)\n INNER JOIN `glpi_softwares`\n ON (`glpi_softwarelicenses`.`softwares_id` = `glpi_softwares`.`id`)\n LEFT JOIN `glpi_softwareversions`\n ON (`glpi_softwarelicenses`.`softwareversions_id_use`\n = `glpi_softwareversions`.`id`\n OR (`glpi_softwarelicenses`.`softwareversions_id_use` = '0'\n AND `glpi_softwarelicenses`.`softwareversions_id_buy`\n = `glpi_softwareversions`.`id`))\n LEFT JOIN `glpi_states`\n ON (`glpi_states`.`id` = `glpi_softwareversions`.`states_id`)\n WHERE `glpi_computers_softwarelicenses`.`computers_id` = '{$computers_id}'\n AND `glpi_computers_softwarelicenses`.`is_deleted` = '0'\n {$where}";
if (count($installed)) {
$query .= " AND `glpi_softwarelicenses`.`id` NOT IN (" . implode(',', $installed) . ")";
}
$query .= " ORDER BY `softname`, `version`;";
$req = $DB->request($query);
if ($number = $req->numrows()) {
if ($canedit) {
$rand = mt_rand();
Html::openMassiveActionsForm('massSoftwareLicense' . $rand);
$actions = array('Computer_SoftwareLicense' . MassiveAction::CLASS_ACTION_SEPARATOR . 'install' => _x('button', 'Install'));
if (SoftwareLicense::canUpdate()) {
$actions['purge'] = _x('button', 'Delete permanently');
}
$massiveactionparams = array('num_displayed' => $number, 'container' => 'massSoftwareLicense' . $rand, 'specific_actions' => $actions);
Html::showMassiveActions($massiveactionparams);
}
echo "<table class='tab_cadre_fixehov'>";
$header_begin = "<tr>";
$header_top = '';
$header_bottom = '';
$header_end = '';
if ($canedit) {
$header_begin .= "<th width='10'>";
$header_top .= Html::getCheckAllAsCheckbox('massSoftwareLicense' . $rand);
$header_bottom .= Html::getCheckAllAsCheckbox('massSoftwareLicense' . $rand);
$header_end .= "</th>";
}
$header_end .= "<th>" . __('Name') . "</th><th>" . __('Status') . "</th>";
$header_end .= "<th>" . __('Version') . "</th><th>" . __('License') . "</th>";
$header_end .= "</tr>\n";
echo $header_begin . $header_top . $header_end;
$cat = true;
foreach ($req as $data) {
self::displaySoftsByLicense($data, $computers_id, $withtemplate, $canedit);
Session::addToNavigateListItems('SoftwareLicense', $data["id"]);
}
echo $header_begin . $header_bottom . $header_end;
echo "</table>";
if ($canedit) {
$massiveactionparams['ontop'] = false;
Html::showMassiveActions($massiveactionparams);
Html::closeForm();
}
}
echo "</div>\n";
}