softwarelicense.class.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * @version $Id: softwarelicense.class.php 20593 2013-03-30 17:13:00Z yllen $
00004  -------------------------------------------------------------------------
00005  GLPI - Gestionnaire Libre de Parc Informatique
00006  Copyright (C) 2003-2013 by the INDEPNET Development Team.
00007 
00008  http://indepnet.net/   http://glpi-project.org
00009  -------------------------------------------------------------------------
00010 
00011  LICENSE
00012 
00013  This file is part of GLPI.
00014 
00015  GLPI is free software; you can redistribute it and/or modify
00016  it under the terms of the GNU General Public License as published by
00017  the Free Software Foundation; either version 2 of the License, or
00018  (at your option) any later version.
00019 
00020  GLPI is distributed in the hope that it will be useful,
00021  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  GNU General Public License for more details.
00024 
00025  You should have received a copy of the GNU General Public License
00026  along with GLPI. If not, see <http://www.gnu.org/licenses/>.
00027  --------------------------------------------------------------------------
00028  */
00029 
00030 /** @file
00031 * @brief
00032 */
00033 
00034 if (!defined('GLPI_ROOT')) {
00035    die("Sorry. You can't access directly to this file");
00036 }
00037 
00038 /// License class
00039 class SoftwareLicense extends CommonDBTM {
00040 
00041    // From CommonDBTM
00042    public $dohistory                   = true;
00043 
00044    static protected $forward_entity_to = array('Infocom');
00045 
00046 
00047    static function getTypeName($nb=0) {
00048       return _n('License', 'Licenses', $nb);
00049    }
00050 
00051 
00052    static function canCreate() {
00053       return Session::haveRight('software', 'w');
00054    }
00055 
00056 
00057    static function canView() {
00058       return Session::haveRight('software', 'r');
00059    }
00060 
00061 
00062    function pre_updateInDB() {
00063 
00064       // Clean end alert if expire is after old one
00065       if (isset($this->oldvalues['expire'])
00066           && ($this->oldvalues['expire'] < $this->fields['expire'])) {
00067 
00068          $alert = new Alert();
00069          $alert->clear($this->getType(), $this->fields['id'], Alert::END);
00070       }
00071    }
00072 
00073 
00074    function prepareInputForAdd($input) {
00075 
00076       // Unset to set to default using mysql default value
00077       if (empty($input['expire'])) {
00078          unset ($input['expire']);
00079       }
00080 
00081       return $input;
00082    }
00083 
00084 
00085    /**
00086     * @since version 0.84
00087    **/
00088    function cleanDBonPurge() {
00089 
00090       $csl = new Computer_SoftwareLicense();
00091       $csl->cleanDBonItemDelete('SoftwareLicense', $this->fields['id']);
00092 
00093       $class = new Alert();
00094       $class->cleanDBonItemDelete($this->getType(), $this->fields['id']);
00095    }
00096 
00097 
00098    function post_addItem() {
00099       global $CFG_GLPI;
00100 
00101       $itemtype = 'Software';
00102       $dupid    = $this->fields["softwares_id"];
00103 
00104       if (isset($this->input["_duplicate_license"])) {
00105          $itemtype = 'SoftwareLicense';
00106          $dupid    = $this->input["_duplicate_license"];
00107       }
00108 
00109       // Add infocoms if exists for the licence
00110       Infocom::cloneItem('Software', $dupid, $this->fields['id'], $this->getType());
00111    }
00112 
00113    /**
00114     * @since version 0.84
00115     *
00116     * @see CommonDBTM::getPreAdditionalInfosForName
00117    **/
00118    function getPreAdditionalInfosForName() {
00119 
00120       $soft = new Software();
00121       if ($soft->getFromDB($this->fields['softwares_id'])) {
00122          return $soft->getName();
00123       }
00124       return '';
00125    }
00126 
00127    function defineTabs($options=array()) {
00128 
00129       $ong = array();
00130       $this->addStandardTab('Computer_SoftwareLicense', $ong, $options);
00131       $this->addStandardTab('Infocom', $ong, $options);
00132       $this->addStandardTab('Contract_Item', $ong, $options);
00133       $this->addStandardTab('Document_Item', $ong, $options);
00134       $this->addStandardTab('Log', $ong, $options);
00135       return $ong;
00136    }
00137 
00138 
00139    /**
00140     * Print the Software / license form
00141     *
00142     * @param $ID        integer  Id of the version or the template to print
00143     * @param $options   array    of possible options:
00144     *     - target form target
00145     *     - softwares_id ID of the software for add process
00146     *
00147     * @return true if displayed  false if item not found or not right to display
00148    **/
00149    function showForm($ID, $options=array()) {
00150       global $CFG_GLPI;
00151 
00152       $softwares_id = -1;
00153       if (isset($options['softwares_id'])) {
00154          $softwares_id = $options['softwares_id'];
00155       }
00156 
00157       if (!Session::haveRight("software","w")) {
00158          return false;
00159       }
00160 
00161       if ($ID < 0) {
00162          // Create item
00163          $this->fields['softwares_id'] = $softwares_id;
00164          $this->fields['number']       = 1;
00165          $soft                         = new Software();
00166          if ($soft->getFromDB($softwares_id)
00167              && in_array($_SESSION['glpiactive_entity'], getAncestorsOf('glpi_entities',
00168                                                                         $soft->getEntityID()))) {
00169             $options['entities_id'] = $soft->getEntityID();
00170          }
00171       }
00172 
00173       $this->initForm($ID, $options);
00174       $this->showTabs($options);
00175       $this->showFormHeader($options);
00176 
00177       echo "<tr class='tab_bg_1'>";
00178       echo "<td>".Software::getTypeName(1)."</td>";
00179       echo "<td>";
00180       if ($ID > 0) {
00181          $softwares_id = $this->fields["softwares_id"];
00182       } else {
00183          echo "<input type='hidden' name='softwares_id' value='$softwares_id'>";
00184       }
00185       echo "<a href='software.form.php?id=".$softwares_id."'>".
00186              Dropdown::getDropdownName("glpi_softwares", $softwares_id)."</a>";
00187       echo "</td>";
00188       echo "<td>".__('Type')."</td>";
00189       echo "<td>";
00190       SoftwareLicenseType::dropdown(array('value' => $this->fields["softwarelicensetypes_id"]));
00191       echo "</td></tr>\n";
00192 
00193       echo "<tr class='tab_bg_1'>";
00194       echo "<td>".__('Name')."</td>";
00195       echo "<td>";
00196       Html::autocompletionTextField($this,"name");
00197       echo "</td>";
00198       echo "<td>".__('Serial number')."</td>";
00199       echo "<td>";
00200       Html::autocompletionTextField($this,"serial");
00201       echo "</td></tr>\n";
00202 
00203       echo "<tr class='tab_bg_1'>";
00204       echo "<td>".__('Purchase version')."</td>";
00205       echo "<td>";
00206       SoftwareVersion::dropdown(array('name'         => "softwareversions_id_buy",
00207                                       'softwares_id' => $this->fields["softwares_id"],
00208                                       'value'        => $this->fields["softwareversions_id_buy"]));
00209       echo "</td>";
00210       echo "<td>".__('Inventory number')."</td>";
00211       echo "<td>";
00212       Html::autocompletionTextField($this,"otherserial");
00213       echo "</td></tr>\n";
00214 
00215       echo "<tr class='tab_bg_1'>";
00216       echo "<td>".__('Version in use')."</td>";
00217       echo "<td>";
00218       SoftwareVersion::dropdown(array('name'         => "softwareversions_id_use",
00219                                       'softwares_id' => $this->fields["softwares_id"],
00220                                       'value'        => $this->fields["softwareversions_id_use"]));
00221       echo "</td>";
00222       echo "<td rowspan='".(($ID > 0) ?'4':'3')."' class='middle'>".__('Comments')."</td>";
00223       echo "<td class='center middle' rowspan='".(($ID > 0) ?'4':'3')."'>";
00224       echo "<textarea cols='45' rows='5' name='comment' >".$this->fields["comment"]."</textarea>";
00225       echo "</td></tr>\n";
00226 
00227       echo "<tr class='tab_bg_1'>";
00228       echo "<td>"._x('quantity', 'Number')."</td>";
00229       echo "<td>";
00230       Dropdown::showInteger("number", $this->fields["number"], 1, 1000, 1,
00231                             array(-1 => __('Unlimited')));
00232       echo "</td></tr>\n";
00233 
00234       echo "<tr class='tab_bg_1'>";
00235       echo "<td>".__('Expiration')."</td>";
00236       echo "<td>";
00237       Html::showDateFormItem('expire', $this->fields["expire"]);
00238       Alert::displayLastAlert('SoftwareLicense', $ID);
00239       echo "</td></tr>\n";
00240 
00241       if ($ID > 0) {
00242          echo "<tr class='tab_bg_1'>";
00243          echo "<td>".__('Last update')."</td>";
00244          echo "<td>".($this->fields["date_mod"] ? Html::convDateTime($this->fields["date_mod"])
00245                                                 : __('Never'));
00246          echo "</td></tr>";
00247       }
00248 
00249       $this->showFormButtons($options);
00250       $this->addDivForTabs();
00251 
00252       return true;
00253    }
00254 
00255 
00256    /**
00257     * Is the license may be recursive
00258     *
00259     * @return boolean
00260    **/
00261    function maybeRecursive () {
00262 
00263       $soft = new Software();
00264       if (isset($this->fields["softwares_id"])
00265           && $soft->getFromDB($this->fields["softwares_id"])) {
00266          return $soft->isRecursive();
00267       }
00268       return false;
00269    }
00270 
00271 
00272    /**
00273     * @see CommonDBTM::getSpecificMassiveActions()
00274    **/
00275    function getSpecificMassiveActions($checkitem=NULL) {
00276 
00277       $isadmin = static::canUpdate();
00278       $actions = parent::getSpecificMassiveActions($checkitem);
00279 
00280       if (Session::haveRight('transfer','r')
00281           && Session::isMultiEntitiesMode()
00282           && $isadmin) {
00283          $actions['add_transfer_list'] = _x('button', 'Add to transfer list');
00284       }
00285       return $actions;
00286    }
00287 
00288 
00289    function getSearchOptions() {
00290 
00291       // Only use for History (not by search Engine)
00292       $tab                       = array();
00293 
00294       $tab[2]['table']           = $this->getTable();
00295       $tab[2]['field']           = 'name';
00296       $tab[2]['name']            = __('Name');
00297       $tab[2]['datatype']        = 'itemlink';
00298 
00299       $tab[3]['table']           = $this->getTable();
00300       $tab[3]['field']           = 'serial';
00301       $tab[3]['name']            = __('Serial number');
00302       $tab[3]['datatype']        = 'string';
00303 
00304       $tab[162]['table']         = $this->getTable();
00305       $tab[162]['field']         = 'otherserial';
00306       $tab[162]['name']          = __('Inventory number');
00307       $tab[162]['massiveaction'] = false;
00308       $tab[162]['datatype']      = 'string';
00309 
00310       $tab[4]['table']           = $this->getTable();
00311       $tab[4]['field']           = 'number';
00312       $tab[4]['name']            = _x('quantity', 'Number');
00313       $tab[4]['datatype']        = 'number';
00314       $tab[4]['max']             = 100;
00315       $tab[4]['toadd']           = array(-1 => __('Unlimited'));
00316 
00317       $tab[5]['table']           = 'glpi_softwarelicensetypes';
00318       $tab[5]['field']           = 'name';
00319       $tab[5]['name']            = __('Type');
00320       $tab[5]['datatype']        = 'dropdown';
00321 
00322       $tab[6]['table']           = 'glpi_softwareversions';
00323       $tab[6]['field']           = 'name';
00324       $tab[6]['linkfield']       = 'softwareversions_id_buy';
00325       $tab[6]['name']            = __('Purchase version');
00326       $tab[6]['datatype']        = 'dropdown';
00327       $tab[6]['displaywith']     = array('states_id');
00328 
00329       $tab[7]['table']           = 'glpi_softwareversions';
00330       $tab[7]['field']           = 'name';
00331       $tab[7]['linkfield']       = 'softwareversions_id_use';
00332       $tab[7]['name']            = __('Version in use');
00333       $tab[7]['datatype']        = 'dropdown';
00334       $tab[7]['displaywith']     = array('states_id');
00335 
00336       $tab[8]['table']           = $this->getTable();
00337       $tab[8]['field']           = 'expire';
00338       $tab[8]['name']            = __('Expiration');
00339       $tab[8]['datatype']        = 'date';
00340 
00341       $tab[16]['table']          = $this->getTable();
00342       $tab[16]['field']          = 'comment';
00343       $tab[16]['name']           = __('Comments');
00344       $tab[16]['datatype']       = 'text';
00345 
00346       return $tab;
00347    }
00348 
00349 
00350    /**
00351     * Give cron information
00352     *
00353     * @param $name : task's name
00354     *
00355     * @return arrray of information
00356    **/
00357       static function cronInfo($name) {
00358       return array('description' => __('Send alarms on expired licenses'));
00359    }
00360 
00361 
00362    /**
00363     * Cron action on softwares : alert on expired licences
00364     *
00365     * @param $task to log, if NULL display (default NULL)
00366     *
00367     * @return 0 : nothing to do 1 : done with success
00368    **/
00369    static function cronSoftware($task=NULL) {
00370       global $DB, $CFG_GLPI;
00371 
00372       $cron_status = 1;
00373 
00374       if (!$CFG_GLPI['use_mailing']) {
00375          return 0;
00376       }
00377 
00378       $message      = array();
00379       $items_notice = array();
00380       $items_end    = array();
00381 
00382       foreach (Entity::getEntitiesToNotify('use_licenses_alert') as $entity => $value) {
00383          $before = Entity::getUsedConfig('send_licenses_alert_before_delay', $entity);
00384          // Check licenses
00385          $query = "SELECT `glpi_softwarelicenses`.*,
00386                           `glpi_softwares`.`name` AS softname
00387                    FROM `glpi_softwarelicenses`
00388                    INNER JOIN `glpi_softwares`
00389                         ON (`glpi_softwarelicenses`.`softwares_id` = `glpi_softwares`.`id`)
00390                    LEFT JOIN `glpi_alerts`
00391                         ON (`glpi_softwarelicenses`.`id` = `glpi_alerts`.`items_id`
00392                             AND `glpi_alerts`.`itemtype` = 'SoftwareLicense'
00393                             AND `glpi_alerts`.`type` = '".Alert::END."')
00394                    WHERE `glpi_alerts`.`date` IS NULL
00395                          AND `glpi_softwarelicenses`.`expire` IS NOT NULL
00396                          AND DATEDIFF(`glpi_softwarelicenses`.`expire`,
00397                                       CURDATE()) < '$before'
00398                          AND `glpi_softwares`.`is_template` = '0'
00399                          AND `glpi_softwares`.`is_deleted` = '0'
00400                          AND `glpi_softwares`.`entities_id` = '".$entity."'";
00401 
00402          $message = "";
00403          $items   = array();
00404 
00405          foreach ($DB->request($query) as $license) {
00406             $name     = $license['softname'].' - '.$license['name'].' - '.$license['serial'];
00407             //TRANS: %1$s the license name, %2$s is the expiration date
00408             $message .= sprintf(__('License %1$s expired on %2$s'),
00409                                 Html::convDate($license["expire"]), $name)."<br>\n";
00410             $items[$license['id']] = $license;
00411          }
00412 
00413          if (!empty($items)) {
00414             $alert                  = new Alert();
00415             $options['entities_id'] = $entity;
00416             $options['licenses']    = $items;
00417 
00418             if (NotificationEvent::raiseEvent('alert', new self(), $options)) {
00419                $entityname = Dropdown::getDropdownName("glpi_entities", $entity);
00420                if ($task) {
00421                   //TRANS: %1$s is the entity, %2$s is the message
00422                   $task->log(sprintf(__('%1$s: %2$s')."\n", $entityname, $message));
00423                   $task->addVolume(1);
00424                 } else {
00425                   Session::addMessageAfterRedirect(sprintf(__('%1$s: %2$s'),
00426                                                            $entityname, $message));
00427                }
00428 
00429                $input["type"]     = Alert::END;
00430                $input["itemtype"] = 'SoftwareLicense';
00431 
00432                // add alerts
00433                foreach ($items as $ID => $consumable) {
00434                   $input["items_id"] = $ID;
00435                   $alert->add($input);
00436                   unset($alert->fields['id']);
00437                }
00438 
00439             } else {
00440                $entityname = Dropdown::getDropdownName('glpi_entities', $entity);
00441                //TRANS: %s is entity name
00442                $msg = sprintf(__('%1$s: %2$s'), $entityname, __('Send licenses alert failed'));
00443                if ($task) {
00444                   $task->log($msg);
00445                } else {
00446                   Session::addMessageAfterRedirect($msg, false, ERROR);
00447                }
00448             }
00449          }
00450        }
00451       return $cron_status;
00452    }
00453 
00454 
00455    /**
00456     * Get number of bought licenses of a version
00457     *
00458     * @param $softwareversions_id   version ID
00459     * @param $entity                to search for licenses in (default = all active entities)
00460     *                               (default '')
00461     *
00462     * @return number of installations
00463    */
00464    static function countForVersion($softwareversions_id, $entity='') {
00465       global $DB;
00466 
00467       $query = "SELECT COUNT(*)
00468                 FROM `glpi_softwarelicenses`
00469                 WHERE `softwareversions_id_buy` = '$softwareversions_id' " .
00470                       getEntitiesRestrictRequest('AND', 'glpi_softwarelicenses', '', $entity);
00471 
00472       $result = $DB->query($query);
00473 
00474       if ($DB->numrows($result) != 0) {
00475          return $DB->result($result, 0, 0);
00476       }
00477       return 0;
00478    }
00479 
00480 
00481    /**
00482     * Get number of licensesof a software
00483     *
00484     * @param $softwares_id software ID
00485     *
00486     * @return number of licenses
00487    **/
00488    static function countForSoftware($softwares_id) {
00489       global $DB;
00490 
00491       $query = "SELECT `id`
00492                 FROM `glpi_softwarelicenses`
00493                 WHERE `softwares_id` = '$softwares_id'
00494                       AND `number` = '-1' " .
00495                       getEntitiesRestrictRequest('AND', 'glpi_softwarelicenses', '', '', true);
00496 
00497       $result = $DB->query($query);
00498       if ($DB->numrows($result)) {
00499          // At least 1 unlimited license, means unlimited
00500          return -1;
00501       }
00502 
00503       $query = "SELECT SUM(`number`)
00504                 FROM `glpi_softwarelicenses`
00505                 WHERE `softwares_id` = '$softwares_id'
00506                       AND `number` > '0' " .
00507                       getEntitiesRestrictRequest('AND', 'glpi_softwarelicenses', '', '', true);
00508 
00509       $result = $DB->query($query);
00510       $nb     = $DB->result($result,0,0);
00511       return ($nb ? $nb : 0);
00512    }
00513 
00514 
00515    /**
00516     * Show Licenses of a software
00517     *
00518     * @param $software Software object
00519     *
00520     * @return nothing
00521    **/
00522    static function showForSoftware(Software $software) {
00523       global $DB, $CFG_GLPI;
00524 
00525       $softwares_id  = $software->getField('id');
00526       $license       = new self();
00527       $computer      = new Computer();
00528 
00529       if (!$software->can($softwares_id,"r")) {
00530          return false;
00531       }
00532       if (isset($_POST["start"])) {
00533          $start = $_POST["start"];
00534       } else {
00535          $start = 0;
00536       }
00537 
00538 
00539       if (isset($_POST["order"]) && ($_POST["order"] == "DESC")) {
00540          $order = "DESC";
00541       } else {
00542          $order = "ASC";
00543       }
00544 
00545       if (isset($_POST["sort"]) && !empty($_POST["sort"])) {
00546          $sort = "`".$_POST["sort"]."`";
00547       } else {
00548          $sort = "`entity` $order, `name`";
00549       }
00550 
00551 
00552       // Righ type is enough. Can add a License on a software we have Read access
00553       $canedit             = Session::haveRight("software", "w");
00554       $showmassiveactions  = $canedit;
00555 
00556       // Total Number of events
00557       $number = countElementsInTable("glpi_softwarelicenses",
00558                                      "glpi_softwarelicenses.softwares_id = $softwares_id " .
00559                                           getEntitiesRestrictRequest('AND', 'glpi_softwarelicenses',
00560                                                                      '', '', true));
00561       echo "<div class='spaced'>";
00562 
00563       Session::initNavigateListItems('SoftwareLicense',
00564             //TRANS : %1$s is the itemtype name, %2$s is the name of the item (used for headings of a list)
00565                                      sprintf(__('%1$s = %2$s'), Software::getTypeName(1),
00566                                              $software->getName()));
00567 
00568       if ($number < 1) {
00569          echo "<table class='tab_cadre_fixe'>";
00570          echo "<tr><th>".__('No item found')."</th></tr>\n";
00571 
00572          if ($canedit) {
00573             echo "<tr class='tab_bg_2'><td class='center'>";
00574             echo "<a class='vsubmit' href='softwarelicense.form.php?softwares_id=$softwares_id'>".
00575                    _sx('button', 'Add a license')."</a>";
00576             echo "</td></tr>\n";
00577          }
00578 
00579          echo "</table></div>\n";
00580          return;
00581       }
00582 
00583       // Display the pager
00584       Html::printAjaxPager(self::getTypeName(2), $start, $number);
00585 
00586       $rand  = mt_rand();
00587       $query = "SELECT `glpi_softwarelicenses`.*,
00588                        `buyvers`.`name` AS buyname,
00589                        `usevers`.`name` AS usename,
00590                        `glpi_entities`.`completename` AS entity,
00591                        `glpi_softwarelicensetypes`.`name` AS typename
00592                 FROM `glpi_softwarelicenses`
00593                 LEFT JOIN `glpi_softwareversions` AS buyvers
00594                      ON (`buyvers`.`id` = `glpi_softwarelicenses`.`softwareversions_id_buy`)
00595                 LEFT JOIN `glpi_softwareversions` AS usevers
00596                      ON (`usevers`.`id` = `glpi_softwarelicenses`.`softwareversions_id_use`)
00597                 LEFT JOIN `glpi_entities`
00598                      ON (`glpi_entities`.`id` = `glpi_softwarelicenses`.`entities_id`)
00599                 LEFT JOIN `glpi_softwarelicensetypes`
00600                      ON (`glpi_softwarelicensetypes`.`id`
00601                           = `glpi_softwarelicenses`.`softwarelicensetypes_id`)
00602                 WHERE (`glpi_softwarelicenses`.`softwares_id` = '$softwares_id') " .
00603                        getEntitiesRestrictRequest('AND', 'glpi_softwarelicenses', '', '', true) ."
00604                 ORDER BY $sort $order
00605                 LIMIT ".intval($start)."," . intval($_SESSION['glpilist_limit']);
00606 
00607       if ($result = $DB->query($query)) {
00608          if ($num_displayed = $DB->numrows($result)) {
00609             if ($showmassiveactions) {
00610                Html::openMassiveActionsForm('mass'.__CLASS__.$rand);
00611                $massiveactionparams = array('num_displayed'
00612                                               => $num_displayed,
00613                                             'extraparams'
00614                                               => array('options'
00615                                                         => array('condition'
00616                                                                  => "`glpi_softwareversions`.`softwares_id` = $softwares_id")));
00617 
00618                Html::showMassiveActions(__CLASS__, $massiveactionparams);
00619             }
00620             $sort_img = "<img src=\"" . $CFG_GLPI["root_doc"] . "/pics/" .
00621                         (($order == "DESC") ? "puce-down.png" : "puce-up.png") ."\" alt='' title=''>";
00622 
00623             echo "<table class='tab_cadre_fixehov'><tr>";
00624             echo "<th>";
00625             Html::checkAllAsCheckbox('mass'.__CLASS__.$rand);
00626             echo "</th>";
00627             echo "<th>".(($sort == "`name`") ?$sort_img:"").
00628                  "<a href='javascript:reloadTab(\"sort=name&amp;order=".
00629                    (($order == "ASC") ?"DESC":"ASC")."&amp;start=0\");'>".__('Name')."</a></th>";
00630 
00631             if ($software->isRecursive()) {
00632                // Ereg to search entity in string for match default order
00633                echo "<th>".(strstr($sort,"entity")?$sort_img:"").
00634                     "<a href='javascript:reloadTab(\"sort=entity&amp;order=".
00635                       (($order == "ASC") ?"DESC":"ASC")."&amp;start=0\");'>".__('Entity')."</a></th>";
00636             }
00637 
00638             echo "<th>".(( $sort== "`serial`") ?$sort_img:"").
00639                  "<a href='javascript:reloadTab(\"sort=serial&amp;order=".
00640                    (($order == "ASC")?"DESC":"ASC")."&amp;start=0\");'>".__('Serial number').
00641                  "</a></th>";
00642             echo "<th>".(($sort == "`number`") ?$sort_img:"").
00643                  "<a href='javascript:reloadTab(\"sort=number&amp;order=".
00644                    (($order == "ASC") ?"DESC":"ASC")."&amp;start=0\");'>"._x('quantity', 'Number').
00645                  "</a></th>";
00646             echo "<th>".__('Affected computers')."</th>";
00647             echo "<th>".(($sort == "`typename`") ?$sort_img:"").
00648                  "<a href='javascript:reloadTab(\"sort=typename&amp;order=".
00649                    (($order == "ASC") ?"DESC":"ASC")."&amp;start=0\");'>".__('Type')."</a></th>";
00650             echo "<th>".(($sort == "`buyname`") ?$sort_img:"").
00651                  "<a href='javascript:reloadTab(\"sort=buyname&amp;order=".
00652                    (($order == "ASC") ?"DESC":"ASC")."&amp;start=0\");'>".__('Purchase version').
00653                  "</a></th>";
00654             echo "<th>".(($sort == "`usename`") ?$sort_img:"").
00655                  "<a href='javascript:reloadTab(\"sort=usename&amp;order=".
00656                    (($order == "ASC") ?"DESC":"ASC")."&amp;start=0\");'>".__('Version in use').
00657                  "</a></th>";
00658             echo "<th>".(($sort == "`expire`") ?$sort_img:"").
00659                  "<a href='javascript:reloadTab(\"sort=expire&amp;order=".
00660                    (($order == "ASC") ?"DESC":"ASC")."&amp;start=0\");'>".__('Expiration').
00661                  "</a></th>";
00662             echo "</tr>\n";
00663 
00664             $tot_assoc = 0;
00665             for ($tot=0 ; $data=$DB->fetch_assoc($result) ; ) {
00666                Session::addToNavigateListItems('SoftwareLicense', $data['id']);
00667                $expired = true;
00668                if (is_null($data['expire'])
00669                   || ($data['expire'] > date('Y-m-d'))) {
00670                   $expired = false;
00671                }
00672                echo "<tr class='tab_bg_2".($expired?'_2':'')."'>";
00673 
00674                if ($license->can($data['id'], "w")) {
00675                   echo "<td>".Html::getMassiveActionCheckBox(__CLASS__, $data["id"])."</td>";
00676                } else {
00677                   echo "<td>&nbsp;</td>";
00678                }
00679 
00680                echo "<td><a href='softwarelicense.form.php?id=".$data['id']."'>".$data['name'].
00681                           (empty($data['name']) ?"(".$data['id'].")" :"")."</a></td>";
00682 
00683                if ($software->isRecursive()) {
00684                   echo "<td>".$data['entity']."</td>";
00685                }
00686                echo "<td>".$data['serial']."</td>";
00687                echo "<td class='numeric'>".
00688                       (($data['number'] > 0) ?$data['number']:__('Unlimited'))."</td>";
00689                $nb_assoc   = Computer_SoftwareLicense::countForLicense($data['id']);
00690                $tot_assoc += $nb_assoc;
00691                echo "<td class='numeric'>".$nb_assoc."</td>";
00692                echo "<td>".$data['typename']."</td>";
00693                echo "<td>".$data['buyname']."</td>";
00694                echo "<td>".$data['usename']."</td>";
00695                echo "<td class='center'>".Html::convDate($data['expire'])."</td>";
00696                echo "</tr>";
00697 
00698                if ($data['number'] < 0) {
00699                   // One illimited license, total is illimited
00700                   $tot = -1;
00701                } else if ($tot >= 0) {
00702                   // Expire license not count
00703                   if (!$expired) {
00704                      // Not illimited, add the current number
00705                      $tot += $data['number'];
00706                   }
00707                }
00708             }
00709             echo "<tr class='tab_bg_1'>";
00710             echo "<td colspan='".
00711                    ($software->isRecursive()?4:3)."' class='right b'>".__('Total')."</td>";
00712             echo "<td class='numeric'>".(($tot > 0)?$tot."":__('Unlimited')).
00713                  "</td>";
00714             echo "<td class='numeric'>".$tot_assoc."</td>";
00715             echo "<td colspan='4' class='center'>";
00716 
00717             if ($canedit) {
00718                echo "<a class='vsubmit' href='softwarelicense.form.php?softwares_id=$softwares_id'>".
00719                       _sx('button', 'Add a license')."</a>";
00720             }
00721 
00722             echo "</td></tr>";
00723             echo "</table>\n";
00724 
00725             if ($showmassiveactions) {
00726                $massiveactionparams['ontop'] = false;
00727                Html::showMassiveActions(__CLASS__, $massiveactionparams);
00728 
00729                Html::closeForm();
00730             }
00731 
00732          } else {
00733             _e('No item found');
00734          }
00735       }
00736       Html::printAjaxPager(self::getTypeName(2), $start, $number);
00737 
00738       echo "</div>";
00739    }
00740 
00741 
00742    /**
00743     * Display debug information for current object
00744    **/
00745    function showDebug() {
00746 
00747       $license = array('softname' => '',
00748                        'name'     => '',
00749                        'serial'   => '',
00750                        'expire'   => '');
00751 
00752       $options['entities_id'] = $this->getEntityID();
00753       $options['licenses']    = array($license);
00754       NotificationEvent::debugEvent($this, $options);
00755    }
00756 
00757 
00758    /**
00759     * Get fields to display in the unicity error message
00760     *
00761     * @return an array which contains field => label
00762    */
00763    function getUnicityFieldsToDisplayInErrorMessage() {
00764 
00765       return array('id'           => __('ID'),
00766                    'serial'       => __('Serial number'),
00767                    'entities_id'  => __('Entity'),
00768                    'softwares_id' => _n('Software', 'Software', 1));
00769    }
00770 
00771 
00772    function getTabNameForItem(CommonGLPI $item, $withtemplate=0) {
00773 
00774       if (!$withtemplate) {
00775          switch ($item->getType()) {
00776             case 'Software' :
00777                if ($_SESSION['glpishow_count_on_tabs']) {
00778                   $count = self::countForSoftware($item->getID());
00779                   return self::createTabEntry(self::getTypeName(2),
00780                                               (($count >= 0) ? $count : '&infin;'));
00781                }
00782                return self::getTypeName(2);
00783          }
00784       }
00785       return '';
00786    }
00787 
00788 
00789    static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) {
00790 
00791       if ($item->getType()=='Software') {
00792          self::showForSoftware($item);
00793       }
00794       return true;
00795    }
00796 
00797 }
00798 ?>