<?php
/*
 * @version $Id: appliance.class.php 183 2011-11-18 13:22:39Z remi $
 -------------------------------------------------------------------------
 appliances - Appliances plugin for GLPI
 Copyright (C) 2003-2011 by the appliances Development Team.

 https://forge.indepnet.net/projects/appliances
 -------------------------------------------------------------------------

 LICENSE

 This file is part of appliances.

 appliances 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.

 appliances 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 appliances. If not, see <http://www.gnu.org/licenses/>.
 --------------------------------------------------------------------------
 */

// ----------------------------------------------------------------------
// Original Author of file: GRISARD Jean Marc & CAILLAUD Xavier
// Purpose of file:
// ----------------------------------------------------------------------

if (!defined('GLPI_ROOT')) {
   die("Sorry. You can't access directly to this file");
}


class PluginAppliancesAppliance extends CommonDBTM {

   static $types = array('Computer', 'Monitor', 'NetworkEquipment', 'Peripheral', 'Phone',
                         'Printer', 'Software');

   public $dohistory = true;


   static function getTypeName($nb=0) {
      global $LANG;

      if ($nb>1) {
         return $LANG['plugin_appliances']['title'][1];
      }
      return $LANG['plugin_appliances'][1];
   }


   function canCreate() {
      return plugin_appliances_haveRight('appliance', 'w');
   }


   function canView() {
      return plugin_appliances_haveRight('appliance', 'r');
   }


   /**
    * Retrieve an Appliance from the database using its externalid (unique index)
    *
    * @param $extid string externalid
    *
    * @return true if succeed else false
   **/
   function getFromDBbyExternalID($extid) {
      global $DB;

      $query = "SELECT *
                FROM `".$this->getTable()."`
                WHERE `externalid` = '$extid'";

      if ($result = $DB->query($query)) {
         if ($DB->numrows($result) != 1) {
            return false;
         }
         $this->fields = $DB->fetch_assoc($result);
         if (is_array($this->fields) && count($this->fields)) {
            return true;
         }
      }
      return false;
   }


   function getSearchOptions() {
      global $LANG;

      $tab = array();

      $tab['common'] = $LANG['plugin_appliances']['title'][1];

      $tab[1]['table']         = 'glpi_plugin_appliances_appliances';
      $tab[1]['field']         = 'name';
      $tab[1]['name']          = $LANG['common'][16];
      $tab[1]['datatype']      = 'itemlink';
      $tab[1]['itemlink_type'] = $this->getType();
      $tab[1]['displaytype']   = 'text';
      $tab[1]['checktype']     = 'text';
      $tab[1]['injectable']    = true;

      $tab[2]['table']        = 'glpi_plugin_appliances_appliancetypes';
      $tab[2]['field']        = 'name';
      $tab[2]['name']         = $LANG['common'][17];
      $tab[2]['displaytype']  = 'dropdown';
      $tab[2]['checktype']    = 'text';
      $tab[2]['injectable']   = true;

      $tab[32]['table']       = 'glpi_states';
      $tab[32]['field']       = 'completename';
      $tab[32]['name']        = $LANG['state'][0];
      $tab[32]['displaytype'] = 'dropdown';
      $tab[32]['checktype']   = 'text';
      $tab[32]['injectable']  = true;

      $tab += Location::getSearchOptionsToAdd();

      $tab[3]['displaytype']  = 'dropdown';
      $tab[3]['checktype']    = 'text';
      $tab[3]['injectable']   = true;

      $tab[4]['table']        = 'glpi_plugin_appliances_appliances';
      $tab[4]['field']        =  'comment';
      $tab[4]['name']         =  $LANG['common'][25];
      $tab[4]['datatype']     =  'text';
      $tab[4]['displaytype']  = 'multiline_text';
      $tab[4]['injectable']   = true;

      $tab[5]['table']         = 'glpi_plugin_appliances_appliances_items';
      $tab[5]['field']         = 'items_id';
      $tab[5]['name']          = $LANG['plugin_appliances'][7];
      $tab[5]['massiveaction'] = false;
      $tab[5]['forcegroupby']  =  true;
      $tab[5]['injectable']    = false;
      $tab[5]['joinparams']    = array('jointype' => 'child');

      $tab[6]['table']        = 'glpi_users';
      $tab[6]['field']        = 'name';
      $tab[6]['name']         = $LANG['common'][34];
      $tab[6]['displaytype']  = 'user';
      $tab[6]['checktype']    = 'text';
      $tab[6]['injectable']   = true;

      $tab[8]['table']        = 'glpi_groups';
      $tab[8]['field']        = 'completename';
      $tab[8]['name']         = $LANG['common'][35];
      $tab[8]['condition']    = '`is_itemgroup`';
      $tab[8]['displaytype']  = 'dropdown';
      $tab[8]['checktype']    = 'text';
      $tab[8]['injectable']   = true;

      $tab[24]['table']       = 'glpi_users';
      $tab[24]['field']       = 'name';
      $tab[24]['linkfield']   = 'users_id_tech';
      $tab[24]['name']        = $LANG['common'][10];
      $tab[24]['displaytype'] = 'user';
      $tab[24]['checktype']   = 'text';
      $tab[24]['injectable']  = true;

      $tab[49]['table']       = 'glpi_groups';
      $tab[49]['field']       = 'completename';
      $tab[49]['linkfield']   = 'groups_id_tech';
      $tab[49]['name']        = $LANG['common'][109];
      $tab[49]['condition']   = '`is_assign`';
      $tab[49]['displaytype'] = 'dropdown';
      $tab[49]['checktype']   = 'text';
      $tab[49]['injectable']  = true;

      $tab[9]['table']         = 'glpi_plugin_appliances_appliances';
      $tab[9]['field']         = 'date_mod';
      $tab[9]['name']          = $LANG['common'][26];
      $tab[9]['massiveaction'] = false;
      $tab[9]['datatype']      = 'datetime';
      $tab[9]['displaytype']   = 'date';
      $tab[9]['checktype']     = 'date';
      $tab[9]['injectable']    = true;

      $tab[10]['table']       = 'glpi_plugin_appliances_environments';
      $tab[10]['field']       = 'name';
      $tab[10]['name']        = $LANG['plugin_appliances'][3];
      $tab[10]['displaytype'] = 'dropdown';
      $tab[10]['checktype']   = 'text';
      $tab[10]['injectable']  = true;

      $tab[11]['table']       = 'glpi_plugin_appliances_appliances';
      $tab[11]['field']       = 'is_helpdesk_visible';
      $tab[11]['name']        = $LANG['software'][46];
      $tab[11]['datatype']    = 'bool';
      $tab[11]['displaytype'] = 'bool';
      $tab[11]['checktype']   = 'decimal';
      $tab[11]['injectable']  = true;

      $tab[12]['table']       = 'glpi_plugin_appliances_appliances';
      $tab[12]['field']       = 'serial';
      $tab[12]['name']        = $LANG['common'][19];
      $tab[12]['displaytype'] = 'text';
      $tab[12]['checktype']   = 'text';
      $tab[12]['injectable']  = true;

      $tab[13]['table']       = 'glpi_plugin_appliances_appliances';
      $tab[13]['field']       = 'otherserial';
      $tab[13]['name']        = $LANG['common'][20];
      $tab[13]['displaytype'] = 'text';
      $tab[13]['checktype']   = 'text';
      $tab[13]['injectable']  = true;

      $tab[31]['table']       = 'glpi_plugin_appliances_appliances';
      $tab[31]['field']        = 'id';
      $tab[31]['name']         = $LANG['common'][2];
      $tab[31]['massiveaction'] = false;
      $tab[31]['injectable']   = false;

      $tab[80]['table']       = 'glpi_entities';
      $tab[80]['field']       = 'completename';
      $tab[80]['name']        = $LANG['entity'][0];
      $tab[80]['injectable']  = false;

      $tab[7]['table']         = 'glpi_plugin_appliances_appliances';
      $tab[7]['field']         = 'is_recursive';
      $tab[7]['name']          = $LANG['entity'][9];
      $tab[7]['massiveaction'] = false;
      $tab[7]['datatype']      = 'bool';
      $tab[7]['displaytype']   = 'bool';
      $tab[7]['checktype']     = 'decimal';
      $tab[7]['injectable']    = true;

      return $tab;
   }


   function cleanDBonPurge() {

      $temp = new PluginAppliancesAppliance_Item();
      $temp->deleteByCriteria(array('plugin_appliances_appliances_id' => $this->fields['id']));

      $temp = new PluginAppliancesOptvalue();
      $temp->deleteByCriteria(array('plugin_appliances_appliances_id' => $this->fields['id']));
   }


   function defineTabs($options=array()) {
      global $LANG;

      $ong = array();
      $this->addStandardTab('PluginAppliancesAppliance_Item', $ong, $options);
      $this->addStandardTab('PluginAppliancesOptvalue', $ong, $options);
      $this->addStandardTab('Ticket', $ong, $options);
      $this->addStandardTab('Infocom', $ong, $options);
      $this->addStandardTab('Contract_Item', $ong, $options);
      $this->addStandardTab('Document', $ong, $options);
      $this->addStandardTab('Note', $ong, $options);
      $this->addStandardTab('Log', $ong, $options);

      return $ong;
   }


   /**
    * Return the SQL command to retrieve linked object
    *
    * @return a SQL command which return a set of (itemtype, items_id)
   **/
   function getSelectLinkedItem() {

      return "SELECT `itemtype`, `items_id`
              FROM `glpi_plugin_appliances_appliances_items`
              WHERE `plugin_appliances_appliances_id` = '" . $this->fields['id']."'";
   }


   function showForm ($ID, $options=array()) {
      global $CFG_GLPI, $LANG;

      if ($ID>0) {
         $this->check($ID,'r');
      } else {
         $this->check(-1, 'w');
         $this->getEmpty();
      }

      $canedit = $this->can($ID,'w');
      $canrecu = $this->can($ID,'recursive');

      $this->showTabs($options);
      $this->showFormHeader($options);

      echo "<tr class='tab_bg_1'>";
      echo "<td>".$LANG['common'][16]."&nbsp;:</td><td>";
      Html::autocompletionTextField($this, "name", array('size' => 34));
      echo "</td><td>".$LANG['state'][0]."&nbsp;:</td><td>";
      Dropdown::show('State', array('value' => $this->fields["states_id"]));
      echo "</td></tr>";

      echo "<tr class='tab_bg_1'>";
      echo "<td>".$LANG['common'][15]."&nbsp;:</td><td>";
      if ($canedit) {
         Dropdown::show('Location', array('value'  => $this->fields["locations_id"],
                                          'entity' => $this->fields["entities_id"]));
      } else {
         echo Dropdown::getDropdownName("glpi_locations",$this->fields["locations_id"]);
      }
      echo "</td><td>".$LANG['common'][17]."&nbsp;:</td><td>";
      Dropdown::show('PluginAppliancesApplianceType',
                      array('value'  => $this->fields["plugin_appliances_appliancetypes_id"],
                            'entity' => $this->fields["entities_id"]));
      echo "</td></tr>";

      echo "<tr class='tab_bg_1'>";
      echo "<td>".$LANG['common'][10]."&nbsp;:</td><td>";
      if ($canedit) {
         User::dropdown(array('name'   => 'users_id_tech',
                              'value'  => $this->fields['users_id_tech'],
                              'right'  => 'interface',
                              'entity' => $this->fields['entities_id']));
      } else {
         echo getUsername($this->fields['users_id_tech']);
      }
      echo "</td><td>".$LANG['plugin_appliances'][3]."&nbsp;:</td><td>";
      Dropdown::show('PluginAppliancesEnvironment',
                     array('value' => $this->fields["plugin_appliances_environments_id"]));
      echo "</td></tr>";

      echo "<tr class='tab_bg_1'>";
      echo "<td>".$LANG['common'][109]."&nbsp;:</td><td>";
      if ($canedit) {
         Dropdown::show('Group', array('name'      => 'groups_id_tech',
                                       'value'     => $this->fields['groups_id_tech'],
                                       'entity'    => $this->fields['entities_id'],
                                       'condition' => '`is_assign`'));
      } else {
         echo Dropdown::getDropdownName("glpi_groups", $this->fields["groups_id"]);
      }
      echo "</td><td>".$LANG['common'][19]."&nbsp;:</td><td>";
      Html::autocompletionTextField($this,'serial');
      echo "</td></tr>";

      echo "<tr class='tab_bg_1'>";
      echo "<td>".$LANG['common'][34]."&nbsp;:</td>";
      echo "<td>";
      User::dropdown(array('value'  => $this->fields["users_id"],
                           'entity' => $this->fields["entities_id"],
                           'right'  => 'all'));
      echo "</td><td>".$LANG['common'][20]."&nbsp;:</td><td>";
      Html::autocompletionTextField($this,'otherserial');
      echo "</td></tr>\n";

      echo "<tr class='tab_bg_1'>";
      echo "<td>".$LANG['common'][35]."&nbsp;:</td>";
      echo "<td>";
      Dropdown::show('Group', array('value'     => $this->fields["groups_id"],
                                    'entity'    => $this->fields["entities_id"],
                                    'condition' => '`is_itemgroup`'));
      echo "</td>";
      echo "<td rowspan='4'>".$LANG['common'][25]."&nbsp;:</td>";
      echo "<td rowspan='4' class='middle'>";
      echo "<textarea cols='45' rows='5' name='comment' >".$this->fields["comment"]."</textarea>";
      echo "</td></tr>";


      echo "<tr class='tab_bg_1'>";
      echo "<td>" . $LANG['software'][46] . "&nbsp;:</td><td>";
      Dropdown::showYesNo('is_helpdesk_visible',$this->fields['is_helpdesk_visible']);
      echo "</td></tr>";

      echo "<tr class='tab_bg_1'>";
      // dropdown relationtype added
      echo "<td>".$LANG['plugin_appliances'][22]."&nbsp;:</td><td>";
      if ($canedit
          && !($ID
               && countElementsInTable(array("glpi_plugin_appliances_relations",
                                             "glpi_plugin_appliances_appliances_items"),
                                       "glpi_plugin_appliances_relations.plugin_appliances_appliances_items_id
                                          = glpi_plugin_appliances_appliances_items.id
                                        AND glpi_plugin_appliances_appliances_items.plugin_appliances_appliances_id
                                          = $ID"))) {
         PluginAppliancesRelation::dropdownType("relationtype", $this->fields["relationtype"]);
      } else {
         echo PluginAppliancesRelation::getTypeName($this->fields["relationtype"]);
         $rand    = mt_rand();
         $comment = $LANG['common'][84];
         $image   = "/pics/lock.png";
         echo "&nbsp;<img alt='' src='".$CFG_GLPI["root_doc"].$image.
               "' onmouseout=\"cleanhide('comment_relationtypes$rand')\" ".
               " onmouseover=\"cleandisplay('comment_relationtypes$rand')\">";
         echo "<span class='over_link' id='comment_relationtypes$rand'>$comment</span>";
      }
      echo "</td></tr>";

      $datestring = $LANG['common'][26]."&nbsp;: ";
      $date       = Html::convDateTime($this->fields["date_mod"]);
      echo "<tr class='tab_bg_1'>";
      echo "<td colspan='2' class='center'>".$datestring.$date;
      echo "</td></tr>";

      $this->showFormButtons($options);
      echo "<div id='tabcontent'></div>";
      echo "<script type='text/javascript'>loadDefaultTab();</script>";

      return true;
   }


   /**
    * Show for PDF the current applicatif
    *
    * @param $pdf object for the output
   **/
   function show_PDF ($pdf) {
      global $LANG, $DB;

      $pdf->setColumnsSize(50,50);
      $col1 = '<b>'.$LANG["common"][2].' '.$this->fields['id'].'</b>';
      if (isset($this->fields["date_mod"])) {
         $col2 = $LANG["common"][26].' : '.Html::convDateTime($this->fields["date_mod"]);
      } else {
         $col2 = '';
      }
      $pdf->displayTitle($col1, $col2);

      $pdf->displayLine(
         '<b><i>'.$LANG['common'][16].' :</i></b> '.$this->fields['name'],
         '<b><i>'.$LANG['state'][0].' :</i></b> '.
               Html::clean(Dropdown::getDropdownName('glpi_states', $this->fields['states_id'])));

      $pdf->displayLine(
         '<b><i>'.$LANG['common'][15].' :</i></b> '.
            Html::clean(Dropdown::getDropdownName('glpi_locations', $this->fields['locations_id'])),
         '<b><i>'.$LANG['common'][17].' :</i></b> '.
            Html::clean(Dropdown::getDropdownName('glpi_plugin_appliances_appliancetypes',
                                                 $this->fields['plugin_appliances_appliancetypes_id'])));

      $pdf->displayLine(
         '<b><i>'.$LANG['common'][10].' :</i></b> '.getUserName($this->fields['users_id_tech']),
         '<b><i>'.$LANG['plugin_appliances'][3].' :</i></b> '.
            Html::clean(Dropdown::getDropdownName('glpi_plugin_appliances_environments',
                                                 $this->fields['plugin_appliances_environments_id'])));

      $pdf->displayLine(
         '<b><i>'.$LANG['common'][109].' :</i></b> '.
            Html::clean(Dropdown::getDropdownName('glpi_groups', $this->fields['groups_id_tech'])),
         '<b><i>'.$LANG['common'][19].' :</i></b> '.$this->fields['serial']);

      $pdf->displayLine(
         '<b><i>'.$LANG['common'][34].' :</i></b> '.getUserName($this->fields['users_id']),
         '<b><i>'.$LANG['common'][20].' :</i></b> '.$this->fields['otherserial']);

      $pdf->displayLine(
         '<b><i>'.$LANG['common'][35].' :</i></b> '.
            Html::clean(Dropdown::getDropdownName('glpi_groups', $this->fields['groups_id'])),
         '');

      $pdf->displayLine(
         '<b><i>'.$LANG['software'][46].' :</i></b> '.
            Dropdown::getYesNo($this->fields['is_helpdesk_visible']),
         '<b><i>'.$LANG['plugin_appliances'][22].' :</i></b> '.
            Html::clean(PluginAppliancesRelation::getTypeName($this->fields['relationtype'])));

      $pdf->displayText('<b><i>'.$LANG['common'][25].' : </i></b>', $this->fields['comment']);

      $pdf->displaySpace();
   }


   /**
    * Diplay a dropdown to select an Appliance
    *
    * Parameters which could be used in options array :
    *    - name : string / name of the select (default is plugin_appliances_appliances_id)
    *    - entity : integer or array / restrict to a defined entity or array of entities
    *                   (default '' : current entity)
    *    - used : array / Already used items ID: not to display in dropdown (default empty)
    *
    * @param $options possible options
    *
    * @return nothing (HTML display)
   **/
   static function dropdown($options=array()) {
      global $DB, $CFG_GLPI;

      // Defautl values
      $p['name']     = 'plugin_appliances_appliances_id';
      $p['entity']   = '';
      $p['used']     = array();

      if (is_array($options) && count($options)) {
         foreach ($options as $key => $val) {
            $p[$key]=$val;
         }
      }

      $rand = mt_rand();

      $where =" WHERE `glpi_plugin_appliances_appliances`.`is_deleted` = '0' ".
                      getEntitiesRestrictRequest("AND", "glpi_plugin_appliances_appliances", '',
                                                 $p['entity'], true);

      if (count($p['used'])) {
         $where .= " AND `id` NOT IN ('".implode("','", $p['used'])."')";
      }

      $query = "SELECT *
                FROM `glpi_plugin_appliances_appliancetypes`
                WHERE `id` IN (SELECT DISTINCT `plugin_appliances_appliancetypes_id`
                               FROM `glpi_plugin_appliances_appliances`
                               $where)
                GROUP BY `name`";
      $result = $DB->query($query);

      echo "<select name='_type' id='type_appliances'>\n";
      echo "<option value='0'>------</option>\n";
      while ($data = $DB->fetch_assoc($result)) {
         echo "<option value='".$data['id']."'>".$data['name']."</option>\n";
      }
      echo "</select>\n";

      $params = array('type_appliances' => '__VALUE__',
                      'entity_restrict' => $p['entity'],
                      'rand'            => $rand,
                      'myname'          => $p['name'],
                      'used'            => $p['used']);

      Ajax::updateItemOnSelectEvent("type_appliances", "show_".$p['name'].$rand,
                                  $CFG_GLPI["root_doc"].
                                    "/plugins/appliances/ajax/dropdownTypeAppliances.php",
                                  $params);

      echo "<span id='show_".$p['name']."$rand'>";
      $_POST["entity_restrict"] = $p['entity'];
      $_POST["type_appliances"] = 0;
      $_POST["myname"]          = $p['name'];
      $_POST["rand"]            = $rand;
      $_POST["used"]            = $p['used'];
      include (GLPI_ROOT."/plugins/appliances/ajax/dropdownTypeAppliances.php");
      echo "</span>\n";

      return $rand;
   }


   /**
    * For other plugins, add a type to the linkable types
    *
    * @since version 1.8.0
    *
    * @param $type string class name
   **/
   static function registerType($type) {
      if (!in_array($type, self::$types)) {
         self::$types[] = $type;
      }
   }


   /**
    * Type than could be linked to a Appliance
    *
    * @param $all boolean, all type, or only allowed ones
    *
    * @return array of types
   **/
   static function getTypes($all=false) {

      if ($all) {
         return self::$types;
      }

      // Only allowed types
      $types = self::$types;

      foreach ($types as $key => $type) {
         if (!($item = getItemForItemtype($type))) {
            continue;
         }

         if (!$item->canView()) {
            unset($types[$key]);
         }
      }
      return $types;
   }


   static function methodTestAppliance($params, $protocol) {
      global $PLUGIN_HOOKS;

      if (isset ($params['help'])) {
         return array('help' => 'bool,optional');
      }

      $resp = array('glpi' => GLPI_VERSION);

      $plugin = new Plugin();
      foreach ($PLUGIN_HOOKS['webservices'] as $name => $fct) {
         if ($plugin->getFromDBbyDir($name)) {
            $resp[$name] = $plugin->fields['version'];
         }
      }

      return $resp;
   }


   static function methodListAppliances($params, $protocol) {
      global $DB, $CFG_GLPI;

      // TODO add some search options (name, type, ...)

      if (isset ($params['help'])) {
         return array(  'help'      => 'bool,optional',
                        'id2name'   => 'bool,optional',
                        'count'     => 'bool,optional',
                        'start'     => 'integer,optional',
                        'limit'     => 'integer,optional' );
      }

      if (!Session::getLoginUserID()) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTAUTHENTICATED);
      }

      $resp  = array ();
      $start = 0;
      if (isset ($params['start']) && is_numeric($params['start'])) {
         $start = $params['start'];
      }

      $limit = $CFG_GLPI["list_limit_max"];
      if (isset ($params['limit']) && is_numeric($params['limit'])) {
         $limit = $params['limit'];
      }

      $orders = array();
      if (isset ($params['order'])) {
         if (is_array($params['order'])) {
            $tab = $params['order'];
         } else {
            $tab = array($params['order']=>'DESC');
         }

         foreach ($tab as $key => $val) {
            if ($val != 'ASC') {
               $val = 'DESC';
            }

            //TODO A revoir
            if (in_array($key, array('date_mod', 'entities_id', 'externalid', 'groups_id', 'id',
                                     'name', 'users_id'))) {
               $orders[] ="`$key` $val";
            } else {
               return PluginWebservicesMethodCommon::Error($protocol,
                                                           WEBSERVICES_ERROR_BADPARAMETER, '',
                                                           'order=$key');
            }
         }
      }

      if (count($orders)) {
         $order = implode(',',$orders);
      } else {
         $order = "`name` DESC";
      }

      $where = getEntitiesRestrictRequest(' WHERE', 'glpi_plugin_appliances_appliances');

      if (isset ($params['count'])) {
         $query = "SELECT COUNT(DISTINCT `id`) AS count
                   FROM `glpi_plugin_appliances_appliances`
                   $where";

         foreach ($DB->request($query) as $data) {
            $resp = $data;
         }

      } else {
         $where = "";
         if (isset ($params['id2name'])) {
            // TODO : users_name and groups_name ?
            $query = "SELECT `glpi_plugin_appliances_appliances`.*,
                             `glpi_plugin_appliances_appliancetypes`.`name` AS plugin_appliances_appliancetypes_name,
                             `glpi_plugin_appliances_environments`.`name` AS plugin_appliances_environments_name
                      FROM `glpi_plugin_appliances_appliances`
                      LEFT JOIN `glpi_plugin_appliances_appliancetypes`
                           ON `glpi_plugin_appliances_appliancetypes`.`id`
                                 =`glpi_plugin_appliances_appliances`.`plugin_appliances_appliancetypes_id`
                      LEFT JOIN `glpi_plugin_appliances_environments`
                           ON `glpi_plugin_appliances_environments`.`id`
                                 =`glpi_plugin_appliances_appliances`.`plugin_appliances_environments_id`
                      ORDER BY $order
                      LIMIT $start,$limit";

         } else {
            // TODO review list of fields (should probably be minimal, or configurable)
            $query = "SELECT `glpi_plugin_appliances_appliances`.*
                      FROM `glpi_plugin_appliances_appliances`
                      ORDER BY $order
                      LIMIT $start,$limit";
         }

         foreach ($DB->request($query) as $data) {
            $resp[] = $data;
         }
      }
      return $resp;
   }


   static function methodDeleteAppliance($params, $protocol) {
      global $DB;

      if (isset ($params['help'])) {
         return array('help'  => 'bool,optional',
                      'force' => 'boolean,optional',
                      'id'    => 'string' );
      }

      if (!Session::getLoginUserID()) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTAUTHENTICATED);
      }

      if (!isset ($params['id'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER);
      }

      $force = 0;
      if (isset($params['force'])){
         $force = 1;
      }

      $id        = $params['id'];
      $appliance = new self();
      if (!$appliance->can($id, 'd')) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED);
      }

      if (!$appliance->delete(array("id" => $id),$force)) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_FAILED);
      }

      return array("id" => $id);
   }


   static function methodUpdateAppliance($params, $protocol) {
      global $DB;

      // TODO : add more fields + factorize field translation with methodAddAppliance

      if (isset ($params['help'])) {
         return array('help'                                  => 'bool,optional',
                      'is_helpdesk_visible'                   => 'bool,optional',
                      'is_recursive'                          => 'bool,optional',
                      'name'                                  => 'string,optional',
                      'plugin_appliances_appliancetypes_id'   => 'integer,optional',
                      'plugin_appliances_appliancetypes_name' => 'string,optional',
                      'externalid'                            => 'string,optional',
                      'id'                                    => 'string');
      }

      if (!Session::getLoginUserID()) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTAUTHENTICATED);
      }

      if (!isset($params['id']) || !is_numeric($params['id'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER);
      }

      if (isset($params['is_helpdesk_visible']) && !is_numeric($params['is_helpdesk_visible'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_BADPARAMETER, '',
                                                     'is_helpdesk_visible');
      }

      if (isset($params['is_recursive']) && !is_numeric($params['is_recursive'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_BADPARAMETER, '',
                                                     'is_recursive');
      }

      $id        = intval($params['id']);
      $appliance = new self();
      if (!$appliance->can($id, 'w')) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED);
      }

      $input = array('id' => $id);
      if (isset($params['name'])) {
         $input['name'] = addslashes($params['name']);
      }

      if (isset($params['externalid'])) {
         if (empty($params['externalid'])) {
            $input['externalid'] = 'NULL';
         } else {
            $input['externalid'] = addslashes($params['externalid']);
         }
      }

      // Old field name for compatibility
      if (isset($params['notes'])) {
         $input['notepad'] = addslashes($params['notes']);
      }
      foreach (array('comment', 'notepad', 'serial', 'otherserial') as $field) {
         if (isset($params[$field])) {
            $input[$field] = addslashes($params[$field]);
         }
      }

      if (isset($params['is_helpdesk_visible'])) {
         $input['is_helpdesk_visible'] = ($params['is_helpdesk_visible'] ? 1 : 0);
      }

      if (isset($params['is_recursive'])) {
         $input['is_recursive'] = ($params['is_recursive'] ? 1 : 0);
      }

      if (isset($params['plugin_appliances_appliancetypes_name'])) {
         $type   = new PluginAppliancesApplianceType();
         $input2 = array();
         $input2['entities_id']  = (isset($input['entities_id'])? $input['entities_id']
                                                                : $appliance->fields['entities_id']);
         $input2['is_recursive'] = (isset($input['is_recursive'])? $input['is_recursive']
                                                                 : $appliance->fields['entities_id']);
         $input2['name']         = addslashes($params['plugin_appliances_appliancetypes_name']);
         $input['plugin_appliances_appliancetypes_id'] = $type->import($input2);

      } else if (isset($params['plugin_appliances_appliancetypes_id'])) {
         $input['plugin_appliances_appliancetypes_id']
                     = intval($params['plugin_appliances_appliancetypes_id']);
      }

      if ($appliance->update($input)) {
         // Does not detect unicity error on externalid :(
         return $appliance->methodGetAppliance(array('id' => $id), $protocol);
      }

      return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_FAILED);
   }


   static function methodAddAppliance($params, $protocol) {
      global $DB;

      // TODO : add more fields
      if (isset ($params['help'])) {
         return array('help'                                  => 'bool,optional',
                      'name'                                  => 'string',
                      'entities_id'                           => 'integer,optional',
                      'is_helpdesk_visible'                   => 'bool,optional',
                      'is_recursive'                          => 'bool,optional',
                      'comment'                               => 'string,optional',
                      'externalid'                            => 'string,optional',
                      'plugin_appliances_appliancetypes_id'   => 'integer,optional',
                      'plugin_appliances_appliancetypes_name' => 'string,optional');
      }

      if (!Session::getLoginUserID()) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTAUTHENTICATED);
      }

      if (!isset($params['name'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER);
      }

      if (isset($params['is_helpdesk_visible']) && !is_numeric($params['is_helpdesk_visible'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_BADPARAMETER, '',
                                                     'is_helpdesk_visible');
      }

      if (isset($params['is_recursive']) && !is_numeric($params['is_recursive'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_BADPARAMETER, '',
                                                     'is_recursive');
      }
      $input = array();
      $input['name'] = addslashes($params['name']);

      if (isset($params['entities_id'])) {
         $input['entities_id'] = intval($params['entities_id']);
      } else {
         $input['entities_id'] = $_SESSION["glpiactive_entity"];
      }

      if (isset($params['is_recursive'])) {
         // TODO check if canUnrecurs
         $input['is_recursive'] = ($params['is_recursive'] ? 1 : 0);
      }

      if (isset($params['externalid']) && !empty($params['externalid'])) {
         $input['externalid'] = addslashes($params['externalid']);
      }

      if (isset($params['plugin_appliances_appliancetypes_name'])) {
         $type   = new PluginAppliancesApplianceType();
         $input2 = array();
         $input2['entities_id']  = $input['entities_id'];
         $input2['is_recursive'] = $input['is_recursive'];
         $input2['name']         = addslashes($params['plugin_appliances_appliancetypes_name']);
         $input['plugin_appliances_appliancetypes_id'] = $type->import($input2);

      } else if (isset($params['plugin_appliances_appliancetypes_id'])) {
         // TODO check if this id exists and is readable and is available in appliance entity
         $input['plugin_appliances_appliancetypes_id']
                  = intval($params['plugin_appliances_appliancetypes_id']);
      }

      if (isset($params['is_helpdesk_visible'])) {
         $input['is_helpdesk_visible'] = ($params['is_helpdesk_visible'] ? 1 : 0);
      }

      // Old field name for compatibility
      if (isset($params['notes'])) {
         $input['notepad'] = addslashes($params['notes']);
      }
      foreach (array('comment', 'notepad', 'serial', 'otherserial') as $field) {
         if (isset($params[$field])) {
            $input[$field] = addslashes($params[$field]);
         }
      }

      $appliance = new self();
      if (!$appliance->can(-1, 'w', $input)) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED);
      }

      $id = $appliance->add($input);
      if ($id) {
         // Return the newly created object
         return $appliance->methodGetAppliance(array('id'=>$id), $protocol);
      }

      return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_FAILED);
   }


   static function methodGetAppliance($params, $protocol) {
      global $DB;

      if (isset ($params['help'])) {
         return array(  'help'               => 'bool,optional',
                        'id2name'            => 'bool,optional',
                        'externalid OR id'   => 'string' );
      }

      if (!Session::getLoginUserID()) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTAUTHENTICATED);
      }

      if (!isset($params['externalid']) && !isset($params['id'])) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER);
      }

      $appli = new self();
      $found = false;

      if (isset($params['id'])) {
         $found = $appli->getFromDB(intval($params['id']));

      } else if (isset($params['externalid'])){
         $found = $appli->getFromDBbyExternalID(addslashes($params["externalid"]));
      }

      if (!$found || !$appli->can($appli->fields["id"],'r')) {
         return PluginWebservicesMethodCommon::Error($protocol, WEBSERVICES_ERROR_NOTFOUND);
      }
      $resp = $appli->fields;

      if (isset($params['id2name'])) {
         $resp['plugin_appliances_appliancetypes_name']
            = Html::clean(Dropdown::getDropdownName('glpi_plugin_appliances_appliancetypes',
                                                    $resp['plugin_appliances_appliancetypes_id']));
         $resp['plugin_appliances_environments_name']
            = Html::clean(Dropdown::getDropdownName('glpi_plugin_appliances_environments',
                                                    $resp['plugin_appliances_environments_id']));
         $resp['users_name']
            = Html::clean(Dropdown::getDropdownName('glpi_users', $resp['users_id']));
         $resp['groups_name']
            = Html::clean(Dropdown::getDropdownName('glpi_groups', $resp['groups_id']));
      }
      return $resp;
   }

   static function updateSchema(Migration $migration) {
      global $LANG;

      $migration->displayTitle($LANG['install'][4].' : '.self::getTypeName(9));
      $table = getTableForItemType(__CLASS__);

      // Version 1.6.1
      $migration->changeField($table, 'notes', 'notepad', 'text');

      // Version 1.8.0
      $migration->addKey($table, 'users_id');
      $migration->addKey($table, 'groups_id');
      $migration->addKey($table, 'plugin_appliances_appliancetypes_id');
      $migration->addKey($table, 'plugin_appliances_environments_id');

      $migration->addField($table, 'states_id', 'integer', array('after' => 'date_mod'));
      $migration->addKey($table, 'states_id');

      $migration->addField($table, 'users_id_tech', 'integer', array('after' => 'users_id'));
      $migration->addKey($table, 'users_id_tech');

      $migration->addField($table, 'groups_id_tech', 'integer', array('after' => 'groups_id'));
      $migration->addKey($table, 'groups_id_tech');
   }
}

?>