diff --git a/front/import.php b/front/import.php index 55a32ce..65131a9 100644 --- a/front/import.php +++ b/front/import.php @@ -27,21 +27,25 @@ -------------------------------------------------------------------------- */ -include ('../../../inc/includes.php'); +include('../../../inc/includes.php'); -Html::header(PluginManufacturersimportsPreImport::getTypeName(), '', - "tools", "pluginmanufacturersimportsmenu"); +Html::header( + PluginManufacturersimportsPreImport::getTypeName(), + '', + "tools", + "pluginmanufacturersimportsmenu" +); $preimport = new PluginManufacturersimportsPreImport(); if ($preimport->canView() || Session::haveRight("config", UPDATE)) { - $values = $_GET; - if (isset($_POST["typechoice"])) { - $values = $_POST; - } - PluginManufacturersimportsPreImport::searchForm($values); - PluginManufacturersimportsPreImport::seePreImport($values); + $values = $_GET; + if (isset($_POST["typechoice"])) { + $values = $_POST; + } + PluginManufacturersimportsPreImport::searchForm($values); + PluginManufacturersimportsPreImport::seePreImport($values); } else { - Html::displayRightError(); + Html::displayRightError(); } Html::footer(); diff --git a/hook.php b/hook.php index 3ea679d..42c48b6 100644 --- a/hook.php +++ b/hook.php @@ -27,239 +27,236 @@ -------------------------------------------------------------------------- */ -function plugin_manufacturersimports_install() { - global $DB; - - include_once(PLUGIN_MANUFACTURERSIMPORTS_DIR . "/inc/profile.class.php"); - include_once(PLUGIN_MANUFACTURERSIMPORTS_DIR . "/inc/config.class.php"); - - $migration = new Migration("3.0.0"); - $update = false; - - //Root of SQL files for DB installation or upgrade - $sql_root = PLUGIN_MANUFACTURERSIMPORTS_DIR . "/sql/"; - - if (!$DB->tableExists("glpi_plugin_manufacturersimports_configs")) { - - $DB->runFile($sql_root . "/empty-3.0.0.sql"); - - } else if ($DB->tableExists("glpi_plugin_suppliertag_config") - && !$DB->fieldExists("glpi_plugin_suppliertag_config", "FK_entities")) { - - $update = true; - $DB->runFile($sql_root . "/update-1.1.sql"); - $DB->runFile($sql_root . "/update-1.2.0.sql"); - $DB->runFile($sql_root . "/update-1.3.0.sql"); - $DB->runFile($sql_root . "/update-1.4.1.sql"); - $DB->runFile($sql_root . "/update-1.5.0.sql"); - $DB->runFile($sql_root . "/update-1.7.0.sql"); - - } else if ($DB->tableExists("glpi_plugin_suppliertag_profiles") - && $DB->fieldExists("glpi_plugin_suppliertag_profiles", "interface")) { - - $update = true; - $DB->runFile($sql_root . "/update-1.2.0.sql"); - $DB->runFile($sql_root . "/update-1.3.0.sql"); - $DB->runFile($sql_root . "/update-1.4.1.sql"); - $DB->runFile($sql_root . "/update-1.5.0.sql"); - $DB->runFile($sql_root . "/update-1.7.0.sql"); - - } else if (!$DB->tableExists("glpi_plugin_manufacturersimports_profiles") - && !$DB->fieldExists("glpi_plugin_manufacturersimports_configs", "supplier_key")) { - - $update = true; - $DB->runFile($sql_root . "/update-1.3.0.sql"); - $DB->runFile($sql_root . "/update-1.4.1.sql"); - $DB->runFile($sql_root . "/update-1.5.0.sql"); - $DB->runFile($sql_root . "/update-1.7.0.sql"); - - } else if (!$DB->fieldExists("glpi_plugin_manufacturersimports_configs", "supplier_key")) { - $DB->runFile($sql_root . "/update-1.7.0.sql"); - } else if (!$DB->fieldExists("glpi_plugin_manufacturersimports_configs", "supplier_secret")) { - $DB->runFile($sql_root . "/update-2.1.0.sql"); - } - if (!$DB->fieldExists("glpi_plugin_manufacturersimports_configs", "warranty_url")) { - $DB->runFile($sql_root . "/update-2.1.3.sql"); - } - - $query = "UPDATE `glpi_plugin_manufacturersimports_configs` +function plugin_manufacturersimports_install() +{ + global $DB; + + include_once(PLUGIN_MANUFACTURERSIMPORTS_DIR . "/inc/profile.class.php"); + include_once(PLUGIN_MANUFACTURERSIMPORTS_DIR . "/inc/config.class.php"); + + $migration = new Migration("3.0.0"); + $update = false; + + //Root of SQL files for DB installation or upgrade + $sql_root = PLUGIN_MANUFACTURERSIMPORTS_DIR . "/sql/"; + + if (!$DB->tableExists("glpi_plugin_manufacturersimports_configs")) { + $DB->runFile($sql_root . "/empty-3.0.0.sql"); + } elseif ($DB->tableExists("glpi_plugin_suppliertag_config") + && !$DB->fieldExists("glpi_plugin_suppliertag_config", "FK_entities")) { + $update = true; + $DB->runFile($sql_root . "/update-1.1.sql"); + $DB->runFile($sql_root . "/update-1.2.0.sql"); + $DB->runFile($sql_root . "/update-1.3.0.sql"); + $DB->runFile($sql_root . "/update-1.4.1.sql"); + $DB->runFile($sql_root . "/update-1.5.0.sql"); + $DB->runFile($sql_root . "/update-1.7.0.sql"); + } elseif ($DB->tableExists("glpi_plugin_suppliertag_profiles") + && $DB->fieldExists("glpi_plugin_suppliertag_profiles", "interface")) { + $update = true; + $DB->runFile($sql_root . "/update-1.2.0.sql"); + $DB->runFile($sql_root . "/update-1.3.0.sql"); + $DB->runFile($sql_root . "/update-1.4.1.sql"); + $DB->runFile($sql_root . "/update-1.5.0.sql"); + $DB->runFile($sql_root . "/update-1.7.0.sql"); + } elseif (!$DB->tableExists("glpi_plugin_manufacturersimports_profiles") + && !$DB->fieldExists("glpi_plugin_manufacturersimports_configs", "supplier_key")) { + $update = true; + $DB->runFile($sql_root . "/update-1.3.0.sql"); + $DB->runFile($sql_root . "/update-1.4.1.sql"); + $DB->runFile($sql_root . "/update-1.5.0.sql"); + $DB->runFile($sql_root . "/update-1.7.0.sql"); + } elseif (!$DB->fieldExists("glpi_plugin_manufacturersimports_configs", "supplier_key")) { + $DB->runFile($sql_root . "/update-1.7.0.sql"); + } elseif (!$DB->fieldExists("glpi_plugin_manufacturersimports_configs", "supplier_secret")) { + $DB->runFile($sql_root . "/update-2.1.0.sql"); + } + if (!$DB->fieldExists("glpi_plugin_manufacturersimports_configs", "warranty_url")) { + $DB->runFile($sql_root . "/update-2.1.3.sql"); + } + + $query = "UPDATE `glpi_plugin_manufacturersimports_configs` SET `Supplier_url` = 'https://www.dell.com/support/home/product-support/servicetag/', `warranty_url` ='https://apigtwb2c.us.dell.com/PROD/sbil/eapi/v5/asset-entitlements?servicetags=', `token_url` = 'https://apigtwb2c.us.dell.com/auth/oauth/v2/token' WHERE `name` ='" . PluginManufacturersimportsConfig::DELL . "'"; - $DB->query($query); + $DB->query($query); - $query = "UPDATE `glpi_plugin_manufacturersimports_configs` + $query = "UPDATE `glpi_plugin_manufacturersimports_configs` SET `Supplier_url` = 'https://www.lenovo.com/us/en/warrantyApos?serialNumber=' WHERE `name` ='" . PluginManufacturersimportsConfig::LENOVO . "'"; - $DB->query($query); + $DB->query($query); - $query = "UPDATE `glpi_plugin_manufacturersimports_configs` + $query = "UPDATE `glpi_plugin_manufacturersimports_configs` SET `Supplier_url` = 'http://support.ts.fujitsu.com/Warranty/WarrantyStatus.asp?lng=com&IDNR' WHERE `name` ='" . PluginManufacturersimportsConfig::FUJITSU . "'"; - $DB->query($query); + $DB->query($query); - $query = "UPDATE `glpi_plugin_manufacturersimports_configs` + $query = "UPDATE `glpi_plugin_manufacturersimports_configs` SET `Supplier_url` = 'https://www.wortmann.de/fr-fr/profile/snsearch.aspx?SN=' WHERE `name` ='" . PluginManufacturersimportsConfig::WORTMANN_AG . "'"; - $DB->query($query); + $DB->query($query); - $query = "UPDATE `glpi_plugin_manufacturersimports_configs` - SET `Supplier_url` = 'https://css.api.hp.com/oauth/v1/token' + $query = "UPDATE `glpi_plugin_manufacturersimports_configs` + SET `Supplier_url` = 'https://warrantyapiproxy.azurewebsites.net/api/HP?serial=' WHERE `name` ='" . PluginManufacturersimportsConfig::HP . "'"; - $DB->query($query); + $DB->query($query); - /* Version 1.9.1 */ - $cron = new CronTask(); - if (!$cron->getFromDBbyName('PluginManufacturersimportsDell', 'DataRecoveryDELL')) { - CronTask::Register('PluginManufacturersimportsDell', 'DataRecoveryDELL', WEEK_TIMESTAMP, ['state' => CronTask::STATE_DISABLE]); - } + /* Version 1.9.1 */ + $cron = new CronTask(); + if (!$cron->getFromDBbyName('PluginManufacturersimportsDell', 'DataRecoveryDELL')) { + CronTask::Register('PluginManufacturersimportsDell', 'DataRecoveryDELL', WEEK_TIMESTAMP, ['state' => CronTask::STATE_DISABLE]); + } - if ($update) { - foreach ($DB->request('glpi_plugin_manufacturersimports_profiles') as $data) { - $query = "UPDATE `glpi_plugin_manufacturersimports_profiles` + if ($update) { + foreach ($DB->request('glpi_plugin_manufacturersimports_profiles') as $data) { + $query = "UPDATE `glpi_plugin_manufacturersimports_profiles` SET `profiles_id` = '" . $data["id"] . "' WHERE `id` = '" . $data["id"] . "';"; - $DB->query($query); - } - - $migration->dropField('glpi_plugin_manufacturersimports_profiles', 'name'); - - Plugin::migrateItemType( - [2150 => 'PluginManufacturersimportsModel', - 2151 => 'PluginManufacturersimportsConfig'], - ["glpi_savedsearches", "glpi_savedsearches_users", "glpi_displaypreferences", - "glpi_documents_items", "glpi_infocoms", "glpi_logs", "glpi_items_tickets"], - ["glpi_plugin_manufacturersimports_models", "glpi_plugin_manufacturersimports_logs"]); - } - - //Migrate profiles to the system introduced in 0.85 - PluginManufacturersimportsProfile::initProfile(); - PluginManufacturersimportsProfile::createFirstAccess($_SESSION['glpiactiveprofile']['id']); - //Drop old profile table : not used anymore - $migration->dropTable('glpi_plugin_manufacturersimports_profiles'); - - return true; + $DB->query($query); + } + + $migration->dropField('glpi_plugin_manufacturersimports_profiles', 'name'); + + Plugin::migrateItemType( + [2150 => 'PluginManufacturersimportsModel', + 2151 => 'PluginManufacturersimportsConfig'], + ["glpi_savedsearches", "glpi_savedsearches_users", "glpi_displaypreferences", + "glpi_documents_items", "glpi_infocoms", "glpi_logs", "glpi_items_tickets"], + ["glpi_plugin_manufacturersimports_models", "glpi_plugin_manufacturersimports_logs"] + ); + } + + //Migrate profiles to the system introduced in 0.85 + PluginManufacturersimportsProfile::initProfile(); + PluginManufacturersimportsProfile::createFirstAccess($_SESSION['glpiactiveprofile']['id']); + //Drop old profile table : not used anymore + $migration->dropTable('glpi_plugin_manufacturersimports_profiles'); + + return true; } -function plugin_manufacturersimports_uninstall() { - - include_once(PLUGIN_MANUFACTURERSIMPORTS_DIR . "/inc/profile.class.php"); - include_once(PLUGIN_MANUFACTURERSIMPORTS_DIR . "/inc/menu.class.php"); - - $migration = new Migration("1.9.1"); - $tables = ["glpi_plugin_manufacturersimports_configs", - "glpi_plugin_manufacturersimports_models", - "glpi_plugin_manufacturersimports_logs"]; - foreach ($tables as $table) { - $migration->dropTable($table); - } - - //old versions - $tables = ["glpi_plugin_suppliertag_config", - "glpi_plugin_suppliertag_profiles", - "glpi_plugin_suppliertag_models", - "glpi_plugin_suppliertag_imported"]; - foreach ($tables as $table) { - $migration->dropTable($table); - } - - $cron = new CronTask; - if ($cron->getFromDBbyName('PluginManufacturersimportsDell', 'DataRecoveryDELL')) { - CronTask::Unregister('DataRecoveryDELL'); - } - - $profileRight = new ProfileRight(); - - foreach (PluginManufacturersimportsProfile::getAllRights() as $right) { - $profileRight->deleteByCriteria(['name' => $right['field']]); - } - - //Remove rigth from $_SESSION['glpiactiveprofile'] if exists - PluginManufacturersimportsProfile::removeRightsFromSession(); - - //Remove entries in GLPI's menu and breadcrumb - PluginManufacturersimportsMenu::removeRightsFromSession(); - return true; +function plugin_manufacturersimports_uninstall() +{ + include_once(PLUGIN_MANUFACTURERSIMPORTS_DIR . "/inc/profile.class.php"); + include_once(PLUGIN_MANUFACTURERSIMPORTS_DIR . "/inc/menu.class.php"); + + $migration = new Migration("1.9.1"); + $tables = ["glpi_plugin_manufacturersimports_configs", + "glpi_plugin_manufacturersimports_models", + "glpi_plugin_manufacturersimports_logs"]; + foreach ($tables as $table) { + $migration->dropTable($table); + } + + //old versions + $tables = ["glpi_plugin_suppliertag_config", + "glpi_plugin_suppliertag_profiles", + "glpi_plugin_suppliertag_models", + "glpi_plugin_suppliertag_imported"]; + foreach ($tables as $table) { + $migration->dropTable($table); + } + + $cron = new CronTask; + if ($cron->getFromDBbyName('PluginManufacturersimportsDell', 'DataRecoveryDELL')) { + CronTask::Unregister('DataRecoveryDELL'); + } + + $profileRight = new ProfileRight(); + + foreach (PluginManufacturersimportsProfile::getAllRights() as $right) { + $profileRight->deleteByCriteria(['name' => $right['field']]); + } + + //Remove rigth from $_SESSION['glpiactiveprofile'] if exists + PluginManufacturersimportsProfile::removeRightsFromSession(); + + //Remove entries in GLPI's menu and breadcrumb + PluginManufacturersimportsMenu::removeRightsFromSession(); + return true; } -function plugin_manufacturersimports_postinit() { - foreach (PluginManufacturersimportsConfig::getTypes(true) as $type) { - CommonGLPI::registerStandardTab($type, 'PluginManufacturersimportsConfig'); - } +function plugin_manufacturersimports_postinit() +{ + foreach (PluginManufacturersimportsConfig::getTypes(true) as $type) { + CommonGLPI::registerStandardTab($type, 'PluginManufacturersimportsConfig'); + } } // Define dropdown relations -function plugin_manufacturersimports_getDatabaseRelations() { - - if (Plugin::isPluginActive("manufacturersimports")) { - return ["glpi_entities" - => ["glpi_plugin_manufacturersimports_configs" - => "entities_id"], - "glpi_manufacturers" - => ["glpi_plugin_manufacturersimports_configs" - => "manufacturers_id"], - "glpi_suppliers" - => ["glpi_plugin_manufacturersimports_configs" - => "suppliers_id"], - "glpi_documentcategories" - => ["glpi_plugin_manufacturersimports_configs" - => "documentcategories_id"], - "glpi_documents" - => ["glpi_plugin_manufacturersimports_logs" - => "documents_id"] - ]; - } else { - return []; - } +function plugin_manufacturersimports_getDatabaseRelations() +{ + if (Plugin::isPluginActive("manufacturersimports")) { + return ["glpi_entities" + => ["glpi_plugin_manufacturersimports_configs" + => "entities_id"], + "glpi_manufacturers" + => ["glpi_plugin_manufacturersimports_configs" + => "manufacturers_id"], + "glpi_suppliers" + => ["glpi_plugin_manufacturersimports_configs" + => "suppliers_id"], + "glpi_documentcategories" + => ["glpi_plugin_manufacturersimports_configs" + => "documentcategories_id"], + "glpi_documents" + => ["glpi_plugin_manufacturersimports_logs" + => "documents_id"] + ]; + } else { + return []; + } } ////// SEARCH FUNCTIONS ///////() { -function plugin_manufacturersimports_getAddSearchOptions($itemtype) { - - $sopt = []; - - if (in_array($itemtype, PluginManufacturersimportsConfig::getTypes())) { - //TODO change right manufacturersimports READ - if (Session::haveRight('config', READ)) { - $sopt[2150]['table'] = 'glpi_plugin_manufacturersimports_models'; - $sopt[2150]['field'] = 'model_name'; - $sopt[2150]['linkfield'] = ''; - $sopt[2150]['name'] = _n('Suppliers import', - 'Suppliers imports', - 2, - 'manufacturersimports') - . " - " . __('Model number', 'manufacturersimports'); - $sopt[2150]['forcegroupby'] = true; - $sopt[2150]['joinparams'] = ['jointype' => 'itemtype_item']; - $sopt[2150]['massiveaction'] = false; - } - } - return $sopt; +function plugin_manufacturersimports_getAddSearchOptions($itemtype) +{ + $sopt = []; + + if (in_array($itemtype, PluginManufacturersimportsConfig::getTypes())) { + //TODO change right manufacturersimports READ + if (Session::haveRight('config', READ)) { + $sopt[2150]['table'] = 'glpi_plugin_manufacturersimports_models'; + $sopt[2150]['field'] = 'model_name'; + $sopt[2150]['linkfield'] = ''; + $sopt[2150]['name'] = _n( + 'Suppliers import', + 'Suppliers imports', + 2, + 'manufacturersimports' + ) + . " - " . __('Model number', 'manufacturersimports'); + $sopt[2150]['forcegroupby'] = true; + $sopt[2150]['joinparams'] = ['jointype' => 'itemtype_item']; + $sopt[2150]['massiveaction'] = false; + } + } + return $sopt; } //force groupby for multible links to items -function plugin_manufacturersimports_forceGroupBy($type) { - - return true; - switch ($type) { - case 'PluginManufacturersimportsModel': - return true; - break; - } - return false; +function plugin_manufacturersimports_forceGroupBy($type) +{ + return true; + switch ($type) { + case 'PluginManufacturersimportsModel': + return true; + break; + } + return false; } ////// SPECIFIC MODIF MASSIVE FUNCTIONS /////// -function plugin_manufacturersimports_MassiveActions($type) { - - if (Plugin::isPluginActive('manufacturersimports')) { - if (in_array($type, PluginManufacturersimportsConfig::getTypes(true))) { - return ['PluginManufacturersimportsModel' . MassiveAction::CLASS_ACTION_SEPARATOR . "add_model" - => __('Add new material brand number', 'manufacturersimports')]; - } - } - return []; +function plugin_manufacturersimports_MassiveActions($type) +{ + if (Plugin::isPluginActive('manufacturersimports')) { + if (in_array($type, PluginManufacturersimportsConfig::getTypes(true))) { + return ['PluginManufacturersimportsModel' . MassiveAction::CLASS_ACTION_SEPARATOR . "add_model" + => __('Add new material brand number', 'manufacturersimports')]; + } + } + return []; } diff --git a/inc/config.class.php b/inc/config.class.php index 89300ba..5de7442 100644 --- a/inc/config.class.php +++ b/inc/config.class.php @@ -28,696 +28,732 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } /** * Class PluginManufacturersimportsConfig */ -class PluginManufacturersimportsConfig extends CommonDBTM { - - static $rightname = "plugin_manufacturersimports"; - static $types = ['Computer', 'Monitor', - 'NetworkEquipment', - 'Peripheral', 'Printer']; - public $dohistory = true; - - //Manufacturers constants - const DELL = "Dell"; - const LENOVO = "Lenovo"; - const HP = "HP"; - const FUJITSU = "Fujitsu"; - const TOSHIBA = "Toshiba"; - const WORTMANN_AG = "Wortmann_AG"; - - static function getTypeName($nb = 0) { - return _n('Manufacturer', 'Manufacturers', $nb); - } - - static function getIcon() { - return PluginManufacturersimportsMenu::getIcon(); - } - - public function defineTabs($options = []) { - $ong = []; - $this->addDefaultFormTab($ong); - $this->addStandardTab('Log', $ong, $options); - return $ong; - } - - function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { - if (in_array($item->getType(), self::getTypes(true)) - && Session::haveRight(static::$rightname, READ) - && !isset($withtemplate) || empty($withtemplate)) { - - $suppliername = self::checkManufacturerName($item->getType(), - $item->getID()); - if ($suppliername) { - return PluginManufacturersimportsPreImport::getTypeName(2); - } - } - return ''; - } - - static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { - if (in_array($item->getType(), self::getTypes(true))) { - PluginManufacturersimportsModel::showModelForm(get_class($item), $item->getID()); - } - return true; - } - - /** - * Preconfig datas for standard system - * - * @param $type type of standard system : AD - * - * @return nothing - **/ - function preconfig($type) { - - switch ($type) { - case self::DELL: - case self::HP: - case self::FUJITSU: - case self::LENOVO: - case self::TOSHIBA: - case self::WORTMANN_AG: - $supplierclass = "PluginManufacturersimports" . $type; - $supplier = new $supplierclass(); - $infos = $supplier->getSupplierInfo(); - $this->fields["name"] = $infos["name"]; - $this->fields["supplier_url"] = $infos["supplier_url"]; - - if ($type == self::HP) { - $this->fields["supplier_secret"] = 'abcdefabcdefAaBBBBB'; +class PluginManufacturersimportsConfig extends CommonDBTM +{ + public static $rightname = "plugin_manufacturersimports"; + public static $types = ['Computer', 'Monitor', + 'NetworkEquipment', + 'Peripheral', 'Printer']; + public $dohistory = true; + + //Manufacturers constants + const DELL = "Dell"; + const LENOVO = "Lenovo"; + const HP = "HP"; + const FUJITSU = "Fujitsu"; + const TOSHIBA = "Toshiba"; + const WORTMANN_AG = "Wortmann_AG"; + + public static function getTypeName($nb = 0) + { + return _n('Manufacturer', 'Manufacturers', $nb); + } + + public static function getIcon() + { + return PluginManufacturersimportsMenu::getIcon(); + } + + public function defineTabs($options = []) + { + $ong = []; + $this->addDefaultFormTab($ong); + $this->addStandardTab('Log', $ong, $options); + return $ong; + } + + public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) + { + if (in_array($item->getType(), self::getTypes(true)) + && Session::haveRight(static::$rightname, READ) + && !isset($withtemplate) || empty($withtemplate)) { + $suppliername = self::checkManufacturerName( + $item->getType(), + $item->getID() + ); + if ($suppliername) { + return PluginManufacturersimportsPreImport::getTypeName(2); } - break; - default: - $this->post_getEmpty(); - break; - } - } - - function post_addItem() { - global $DB; - - if ($this->fields["is_recursive"]) { - $dbu = new DbUtils(); - $query = "DELETE FROM `" . $this->getTable() . "` + } + return ''; + } + + public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) + { + if (in_array($item->getType(), self::getTypes(true))) { + PluginManufacturersimportsModel::showModelForm(get_class($item), $item->getID()); + } + return true; + } + + /** + * Preconfig datas for standard system + * + * @param $type type of standard system : AD + * + * @return nothing + **/ + public function preconfig($type) + { + switch ($type) { + case self::DELL: + case self::HP: + case self::FUJITSU: + case self::LENOVO: + case self::TOSHIBA: + case self::WORTMANN_AG: + $supplierclass = "PluginManufacturersimports" . $type; + $supplier = new $supplierclass(); + $infos = $supplier->getSupplierInfo(); + $this->fields["name"] = $infos["name"]; + $this->fields["supplier_url"] = $infos["supplier_url"]; + + if ($type == self::HP) { + $this->fields["supplier_secret"] = 'abcdefabcdefAaBBBBB'; + } + break; + default: + $this->post_getEmpty(); + break; + } + } + + public function post_addItem() + { + global $DB; + + if ($this->fields["is_recursive"]) { + $dbu = new DbUtils(); + $query = "DELETE FROM `" . $this->getTable() . "` WHERE `name` = '" . $this->fields["name"] . "' AND `id` != '" . $this->fields['id'] . "' " . - $dbu->getEntitiesRestrictRequest('AND', $this->getTable(), - '', - $dbu->getSonsOf("glpi_entities", - $this->fields["entities_id"])); - $DB->query($query); - } - } - - function post_updateItem($history = 1) { - global $DB; - - if ($this->fields["is_recursive"]) { - $dbu = new DbUtils(); - $query = "DELETE FROM `" . $this->getTable() . "` + $dbu->getEntitiesRestrictRequest( + 'AND', + $this->getTable(), + '', + $dbu->getSonsOf( + "glpi_entities", + $this->fields["entities_id"] + ) + ); + $DB->query($query); + } + } + + public function post_updateItem($history = 1) + { + global $DB; + + if ($this->fields["is_recursive"]) { + $dbu = new DbUtils(); + $query = "DELETE FROM `" . $this->getTable() . "` WHERE `name` = '" . $this->fields["name"] . "' AND `id` != '" . $this->fields["id"] . "' " . - $dbu->getEntitiesRestrictRequest('AND', $this->getTable(), - '', - $dbu->getSonsOf("glpi_entities", - $this->fields["entities_id"])); - $DB->query($query); - } - } - - static function dropdownSupplier($name, $options = []) { - $params['value'] = 0; - $params['toadd'] = []; - $params['on_change'] = ''; - - if (is_array($options) && count($options)) { - foreach ($options as $key => $val) { - $params[$key] = $val; - } - } - - $items = []; - if (count($params['toadd']) > 0) { - $items = $params['toadd']; - } - - $items += self::getSuppliers(); - return Dropdown::showFromArray($name, $items, $params); - } - - static function getSuppliers() { - $options[-1] = Dropdown::EMPTY_VALUE; - $options[self::DELL] = self::DELL; - $options[self::HP] = self::HP; - $options[self::FUJITSU] = self::FUJITSU; - $options[self::TOSHIBA] = self::TOSHIBA; - $options[self::LENOVO] = self::LENOVO; - $options[self::WORTMANN_AG] = self::WORTMANN_AG; - return $options; - } - - /** - * Provides search options configuration. Do not rely directly - * on this, @return array a *not indexed* array of search options - * - * @since 9.3 - * - * This should be overloaded in Class - * - * @see CommonDBTM::searchOptions instead. - * - * @see https://glpi-developer-documentation.rtfd.io/en/master/devapi/search.html - **/ - public function rawSearchOptions() { - - $tab = parent::rawSearchOptions(); - - $tab[] = [ - 'id' => '2', - 'table' => 'glpi_manufacturers', - 'field' => 'name', - 'name' => __('Manufacturer'), - 'datatype' => 'dropdown', - 'massiveaction' => false - ]; - - $tab[] = [ - 'id' => '3', - 'table' => $this->getTable(), - 'field' => 'supplier_url', - 'name' => __('Manufacturer web address', 'manufacturersimports'), - 'datatype' => 'weblink', - 'massiveaction' => false - ]; - - $tab[] = [ - 'id' => '4', - 'table' => 'glpi_suppliers', - 'field' => 'name', - 'name' => __('Default supplier attached', 'manufacturersimports'), - 'datatype' => 'dropdown' - ]; - - $tab[] = [ - 'id' => '5', - 'table' => $this->getTable(), - 'field' => 'warranty_duration', - 'name' => __('New warranty attached', 'manufacturersimports'), - 'datatype' => 'integer', - 'massiveaction' => false - ]; - - $tab[] = [ - 'id' => '6', - 'table' => $this->getTable(), - 'field' => 'document_adding', - 'name' => __('Auto add of document', 'manufacturersimports'), - 'datatype' => 'bool', - 'massiveaction' => false - ]; - - $tab[] = [ - 'id' => '7', - 'table' => 'glpi_documentcategories', - 'field' => 'name', - 'name' => __('Document heading'), - 'massiveaction' => false - ]; - - $tab[] = [ - 'id' => '8', - 'table' => $this->getTable(), - 'field' => 'comment_adding', - 'name' => __('Add a comment line', 'manufacturersimports'), - 'datatype' => 'bool' - ]; - - $tab[] = [ - 'id' => '30', - 'table' => $this->getTable(), - 'field' => 'id', - 'name' => __('ID') - ]; - - $tab[] = [ - 'id' => '80', - 'table' => 'glpi_entities', - 'field' => 'completename', - 'name' => __('Entity'), - 'datatype' => 'dropdown' - ]; - - return $tab; - } - - function showForm($ID, $options = []) { - - if (!$this->canView()) { - return false; - } - - if ($ID > 0) { - $this->check($ID, READ); - } else { - $this->check(-1, UPDATE); - $this->getEmpty(); - if (isset($_GET['preconfig'])) { - $this->preconfig($_GET['preconfig']); - } else { - $_GET['preconfig'] = -1; - } - } - - echo ""; - echo ""; - echo ""; - echo ""; - echo "
" . __('Preconfiguration') . " "; - - $opt = ['value' => $_GET['preconfig']]; - $rand = self::dropdownSupplier('supplier', $opt); - $params = ['supplier' => '__VALUE__']; - - Ajax::updateItemOnSelectEvent("dropdown_supplier$rand", - "show_preconfig", - "../ajax/dropdownSuppliers.php", - $params); - echo ""; - echo ""; - echo "
"; - - if ($_GET['preconfig'] == -1 && $ID <= 0) { - $style = "style='display:none;'"; - } else { - $style = "style='display:block;'"; - } - echo "
"; - - $this->initForm($ID, $options); - $this->showFormHeader($options); - - echo ""; - echo "" . __('Name') . ""; - echo ""; - echo $this->fields["name"]; - echo Html::hidden('name', ['value' => $this->fields["name"]]); - echo ""; - - echo "" . __('Manufacturer') . ""; - echo ""; - Dropdown::show('Manufacturer', - ['name' => "manufacturers_id", - 'value' => $this->fields["manufacturers_id"]]); - echo ""; - echo ""; - - echo ""; - echo "" . __('Manufacturer web address', 'manufacturersimports') . ""; - echo ""; - echo Html::input('supplier_url', ['value' => $this->fields["supplier_url"], 'size' => 100]); - echo ""; - echo ""; - - if ($this->fields["name"] == self::DELL) { - echo ""; - echo "" . __('Access token API address', 'manufacturersimports') . ""; - echo ""; - echo Html::input('token_url', ['value' => $this->fields["token_url"], 'size' => 100]); - echo ""; - echo ""; - echo ""; - echo "" . __('Warranty API address', 'manufacturersimports') . ""; - echo ""; - echo Html::input('warranty_url', ['value' => $this->fields["warranty_url"], 'size' => 100]); - echo ""; - echo ""; - } - - echo ""; - echo "" . __('Default supplier attached', 'manufacturersimports') . ""; - echo ""; - Dropdown::show('Supplier', ['name' => "suppliers_id", - 'value' => $this->fields["suppliers_id"]]); - echo ""; - echo ""; - - if ($this->fields["name"] == self::FUJITSU) { - echo ""; - echo "" . - __('New warranty affected by default (Replace if 0)', - 'manufacturersimports') . ""; - echo ""; - Dropdown::showNumber("warranty_duration", ['value' => $this->fields["warranty_duration"], - 'min' => 0, - 'max' => 120]); - echo ""; - echo ""; - } else { - echo Html::hidden('warranty_duration', ['value' => 0]); - } - if ($this->fields["name"] == self::HP) { - - echo ""; - echo "" . __('Manufacturer API key', 'manufacturersimports') . ""; - echo ""; - echo Html::input('supplier_key', ['value' => $this->fields["supplier_key"], 'size' => 100]); - echo ""; - echo ""; - - echo ""; - echo "" . __('Manufacturer API Secret', 'manufacturersimports') . ""; - echo ""; - echo Html::input('supplier_secret', ['value' => $this->fields["supplier_secret"], 'size' => 100]); - echo ""; - echo ""; - - } else if ($this->fields["name"] == self::DELL) { - echo ""; - echo "" . __('Client id', 'manufacturersimports') . ""; - echo ""; - echo Html::input('supplier_key', ['value' => $this->fields["supplier_key"], 'size' => 100]); - echo ""; - echo ""; - echo ""; - echo "" . __('Client secret', 'manufacturersimports') . ""; - echo ""; - echo Html::input('supplier_secret', ['value' => $this->fields["supplier_secret"], 'size' => 100]); - echo ""; - echo ""; - } else if ($this->fields["name"] == self::LENOVO) { - echo ""; - echo "" . __('Client id', 'manufacturersimports') . ""; - echo ""; - echo Html::input('supplier_key', ['value' => $this->fields["supplier_key"], 'size' => 100]); - echo ""; - echo ""; - } else { - echo ""; - echo "" . __('Auto add of document', 'manufacturersimports') . ""; - echo ""; - Dropdown::showYesNo("document_adding", $this->fields["document_adding"]); - echo ""; - echo ""; - - echo ""; - echo "" . __('Section for document records', 'manufacturersimports') . ""; - echo ""; - Dropdown::show('DocumentCategory', ['name' => "documentcategories_id", - 'value' => $this->fields["documentcategories_id"]]); - echo ""; - echo ""; - } - echo ""; - echo "" . __('Add a comment line', 'manufacturersimports') . ""; - echo ""; - Dropdown::showYesNo("comment_adding", $this->fields["comment_adding"]); - echo ""; - echo ""; - - $this->showFormButtons($options); - - echo "
"; - echo ""; - echo __('Back'); - echo ""; - echo "
"; - - return true; - } - - /** - * For other plugins, add a type to the linkable types - * - * @param $type string class name - **@since version 1.5.0 - * - */ - static function registerType($type) { - if (!in_array($type, self::$types)) { - self::$types[] = $type; - } - } - - - /** - * Type than could be linked to a Rack - * - * @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 => $itemtype) { - if (!class_exists($itemtype)) { - continue; - } - - if (!$itemtype::canView()) { - unset($types[$key]); - } - } - return $types; - } - - static function checkManufacturerName($itemtype, $items_id) { - - $item = new $itemtype(); - $name = false; - - if ($item->getFromDB($items_id)) { - $dbu = new DbUtils(); - $configs = $dbu->getAllDataFromTable("glpi_plugin_manufacturersimports_configs"); - if (!empty($configs)) { - foreach ($configs as $config) { - if ($item->fields["manufacturers_id"] == $config['manufacturers_id']) { - $name = $config["name"]; - } + $dbu->getEntitiesRestrictRequest( + 'AND', + $this->getTable(), + '', + $dbu->getSonsOf( + "glpi_entities", + $this->fields["entities_id"] + ) + ); + $DB->query($query); + } + } + + public static function dropdownSupplier($name, $options = []) + { + $params['value'] = 0; + $params['toadd'] = []; + $params['on_change'] = ''; + + if (is_array($options) && count($options)) { + foreach ($options as $key => $val) { + $params[$key] = $val; } - } - } - return $name; - } - - static function checkManufacturerID($itemtype, $items_id) { - - $item = new $itemtype(); - $id = false; - - if ($item->getFromDB($items_id)) { - $dbu = new DbUtils(); - $configs = $dbu->getAllDataFromTable("glpi_plugin_manufacturersimports_configs"); - if (!empty($configs)) { - foreach ($configs as $config) { - if ($item->fields["manufacturers_id"] == $config['manufacturers_id']) { - $id = $config["id"]; - } + } + + $items = []; + if (count($params['toadd']) > 0) { + $items = $params['toadd']; + } + + $items += self::getSuppliers(); + return Dropdown::showFromArray($name, $items, $params); + } + + public static function getSuppliers() + { + $options[-1] = Dropdown::EMPTY_VALUE; + $options[self::DELL] = self::DELL; + $options[self::HP] = self::HP; + $options[self::FUJITSU] = self::FUJITSU; + $options[self::TOSHIBA] = self::TOSHIBA; + $options[self::LENOVO] = self::LENOVO; + $options[self::WORTMANN_AG] = self::WORTMANN_AG; + return $options; + } + + /** + * Provides search options configuration. Do not rely directly + * on this, @return array a *not indexed* array of search options + * + * @since 9.3 + * + * This should be overloaded in Class + * + * @see CommonDBTM::searchOptions instead. + * + * @see https://glpi-developer-documentation.rtfd.io/en/master/devapi/search.html + **/ + public function rawSearchOptions() + { + $tab = parent::rawSearchOptions(); + + $tab[] = [ + 'id' => '2', + 'table' => 'glpi_manufacturers', + 'field' => 'name', + 'name' => __('Manufacturer'), + 'datatype' => 'dropdown', + 'massiveaction' => false + ]; + + $tab[] = [ + 'id' => '3', + 'table' => $this->getTable(), + 'field' => 'supplier_url', + 'name' => __('Manufacturer web address', 'manufacturersimports'), + 'datatype' => 'weblink', + 'massiveaction' => false + ]; + + $tab[] = [ + 'id' => '4', + 'table' => 'glpi_suppliers', + 'field' => 'name', + 'name' => __('Default supplier attached', 'manufacturersimports'), + 'datatype' => 'dropdown' + ]; + + $tab[] = [ + 'id' => '5', + 'table' => $this->getTable(), + 'field' => 'warranty_duration', + 'name' => __('New warranty attached', 'manufacturersimports'), + 'datatype' => 'integer', + 'massiveaction' => false + ]; + + $tab[] = [ + 'id' => '6', + 'table' => $this->getTable(), + 'field' => 'document_adding', + 'name' => __('Auto add of document', 'manufacturersimports'), + 'datatype' => 'bool', + 'massiveaction' => false + ]; + + $tab[] = [ + 'id' => '7', + 'table' => 'glpi_documentcategories', + 'field' => 'name', + 'name' => __('Document heading'), + 'massiveaction' => false + ]; + + $tab[] = [ + 'id' => '8', + 'table' => $this->getTable(), + 'field' => 'comment_adding', + 'name' => __('Add a comment line', 'manufacturersimports'), + 'datatype' => 'bool' + ]; + + $tab[] = [ + 'id' => '30', + 'table' => $this->getTable(), + 'field' => 'id', + 'name' => __('ID') + ]; + + $tab[] = [ + 'id' => '80', + 'table' => 'glpi_entities', + 'field' => 'completename', + 'name' => __('Entity'), + 'datatype' => 'dropdown' + ]; + + return $tab; + } + + public function showForm($ID, $options = []) + { + if (!$this->canView()) { + return false; + } + + if ($ID > 0) { + $this->check($ID, READ); + } else { + $this->check(-1, UPDATE); + $this->getEmpty(); + if (isset($_GET['preconfig'])) { + $this->preconfig($_GET['preconfig']); + } else { + $_GET['preconfig'] = -1; } - } - } - return $id; - } + } + + echo ""; + echo ""; + echo ""; + echo ""; + echo "
" . __('Preconfiguration') . " "; + + $opt = ['value' => $_GET['preconfig']]; + $rand = self::dropdownSupplier('supplier', $opt); + $params = ['supplier' => '__VALUE__']; + + Ajax::updateItemOnSelectEvent( + "dropdown_supplier$rand", + "show_preconfig", + "../ajax/dropdownSuppliers.php", + $params + ); + echo ""; + echo ""; + echo "
"; + + if ($_GET['preconfig'] == -1 && $ID <= 0) { + $style = "style='display:none;'"; + } else { + $style = "style='display:block;'"; + } + echo "
"; + + $this->initForm($ID, $options); + $this->showFormHeader($options); + + echo ""; + echo "" . __('Name') . ""; + echo ""; + echo $this->fields["name"]; + echo Html::hidden('name', ['value' => $this->fields["name"]]); + echo ""; + + echo "" . __('Manufacturer') . ""; + echo ""; + Dropdown::show( + 'Manufacturer', + ['name' => "manufacturers_id", + 'value' => $this->fields["manufacturers_id"]] + ); + echo ""; + echo ""; + + echo ""; + echo "" . __('Manufacturer web address', 'manufacturersimports') . ""; + echo ""; + echo Html::input('supplier_url', ['value' => $this->fields["supplier_url"], 'size' => 100]); + echo ""; + echo ""; + + if ($this->fields["name"] == self::DELL) { + echo ""; + echo "" . __('Access token API address', 'manufacturersimports') . ""; + echo ""; + echo Html::input('token_url', ['value' => $this->fields["token_url"], 'size' => 100]); + echo ""; + echo ""; + echo ""; + echo "" . __('Warranty API address', 'manufacturersimports') . ""; + echo ""; + echo Html::input('warranty_url', ['value' => $this->fields["warranty_url"], 'size' => 100]); + echo ""; + echo ""; + } + + echo ""; + echo "" . __('Default supplier attached', 'manufacturersimports') . ""; + echo ""; + Dropdown::show('Supplier', ['name' => "suppliers_id", + 'value' => $this->fields["suppliers_id"]]); + echo ""; + echo ""; + + if ($this->fields["name"] == self::FUJITSU) { + echo ""; + echo "" . + __( + 'New warranty affected by default (Replace if 0)', + 'manufacturersimports' + ) . ""; + echo ""; + Dropdown::showNumber("warranty_duration", ['value' => $this->fields["warranty_duration"], + 'min' => 0, + 'max' => 120]); + echo ""; + echo ""; + } else { + echo Html::hidden('warranty_duration', ['value' => 0]); + } + if ($this->fields["name"] == self::HP) { + echo ""; + echo "" . __('Manufacturer API key', 'manufacturersimports') . ""; + echo ""; + echo Html::input('supplier_key', ['value' => $this->fields["supplier_key"], 'size' => 100]); + echo ""; + echo ""; + + echo ""; + echo "" . __('Manufacturer API Secret', 'manufacturersimports') . ""; + echo ""; + echo Html::input('supplier_secret', ['value' => $this->fields["supplier_secret"], 'size' => 100]); + echo ""; + echo ""; + } elseif ($this->fields["name"] == self::DELL) { + echo ""; + echo "" . __('Client id', 'manufacturersimports') . ""; + echo ""; + echo Html::input('supplier_key', ['value' => $this->fields["supplier_key"], 'size' => 100]); + echo ""; + echo ""; + echo ""; + echo "" . __('Client secret', 'manufacturersimports') . ""; + echo ""; + echo Html::input('supplier_secret', ['value' => $this->fields["supplier_secret"], 'size' => 100]); + echo ""; + echo ""; + } elseif ($this->fields["name"] == self::LENOVO) { + echo ""; + echo "" . __('Client id', 'manufacturersimports') . ""; + echo ""; + echo Html::input('supplier_key', ['value' => $this->fields["supplier_key"], 'size' => 100]); + echo ""; + echo ""; + } else { + echo ""; + echo "" . __('Auto add of document', 'manufacturersimports') . ""; + echo ""; + Dropdown::showYesNo("document_adding", $this->fields["document_adding"]); + echo ""; + echo ""; + + echo ""; + echo "" . __('Section for document records', 'manufacturersimports') . ""; + echo ""; + Dropdown::show('DocumentCategory', ['name' => "documentcategories_id", + 'value' => $this->fields["documentcategories_id"]]); + echo ""; + echo ""; + } + echo ""; + echo "" . __('Add a comment line', 'manufacturersimports') . ""; + echo ""; + Dropdown::showYesNo("comment_adding", $this->fields["comment_adding"]); + echo ""; + echo ""; + + $this->showFormButtons($options); + + echo "
"; + echo ""; + echo __('Back'); + echo ""; + echo "
"; + + return true; + } + + /** + * For other plugins, add a type to the linkable types + * + * @param $type string class name + **@since version 1.5.0 + * + */ + public static function registerType($type) + { + if (!in_array($type, self::$types)) { + self::$types[] = $type; + } + } + + + /** + * Type than could be linked to a Rack + * + * @param $all boolean, all type, or only allowed ones + * + * @return array of types + **/ + public static function getTypes($all = false) + { + if ($all) { + return self::$types; + } + + // Only allowed types + $types = self::$types; + + foreach ($types as $key => $itemtype) { + if (!class_exists($itemtype)) { + continue; + } + + if (!$itemtype::canView()) { + unset($types[$key]); + } + } + return $types; + } + + public static function checkManufacturerName($itemtype, $items_id) + { + $item = new $itemtype(); + $name = false; + + if ($item->getFromDB($items_id)) { + $dbu = new DbUtils(); + $configs = $dbu->getAllDataFromTable("glpi_plugin_manufacturersimports_configs"); + if (!empty($configs)) { + foreach ($configs as $config) { + if ($item->fields["manufacturers_id"] == $config['manufacturers_id']) { + $name = $config["name"]; + } + } + } + } + return $name; + } + + public static function checkManufacturerID($itemtype, $items_id) + { + $item = new $itemtype(); + $id = false; + + if ($item->getFromDB($items_id)) { + $dbu = new DbUtils(); + $configs = $dbu->getAllDataFromTable("glpi_plugin_manufacturersimports_configs"); + if (!empty($configs)) { + foreach ($configs as $config) { + if ($item->fields["manufacturers_id"] == $config['manufacturers_id']) { + $id = $config["id"]; + } + } + } + } + return $id; + } // } - //Massive action - function getSpecificMassiveActions($checkitem = null) { - $isadmin = static::canUpdate(); - $actions = parent::getSpecificMassiveActions($checkitem); - - if ($isadmin) { - if (Session::haveRight('transfer', READ) - && Session::isMultiEntitiesMode()) { - $actions['Transfert'] = __('Transfer'); - } - } - - return $actions; - } - - function showSpecificMassiveActionsParameters($input = []) { - - switch ($input['action']) { - case "Transfert" : - Dropdown::show('Entity'); - echo Html::submit(_sx('button', 'Post'), ['name' => 'massiveaction', 'class' => 'btn btn-primary']); - return true; - break; - - default : - return parent::showSpecificMassiveActionsParameters($input); - break; - } - return false; - } - - function doSpecificMassiveActions($input = []) { - - $res = ['ok' => 0, 'ko' => 0, 'noright' => 0]; - - switch ($input['action']) { - case "Transfert" : - - if ($input['itemtype'] == 'PluginManufacturersimportsConfig') { - foreach ($input["item"] as $key => $val) { - if ($val == 1) { - - $values["id"] = $key; - $values["entities_id"] = $input['entities_id']; - if ($this->update($values)) { - $res['ok']++; - } else { - $res['ko']++; - } - } - } + //Massive action + public function getSpecificMassiveActions($checkitem = null) + { + $isadmin = static::canUpdate(); + $actions = parent::getSpecificMassiveActions($checkitem); + + if ($isadmin) { + if (Session::haveRight('transfer', READ) + && Session::isMultiEntitiesMode()) { + $actions['Transfert'] = __('Transfer'); + } + } + + return $actions; + } + + public function showSpecificMassiveActionsParameters($input = []) + { + switch ($input['action']) { + case "Transfert": + Dropdown::show('Entity'); + echo Html::submit(_sx('button', 'Post'), ['name' => 'massiveaction', 'class' => 'btn btn-primary']); + return true; + break; + + default: + return parent::showSpecificMassiveActionsParameters($input); + break; + } + return false; + } + + public function doSpecificMassiveActions($input = []) + { + $res = ['ok' => 0, 'ko' => 0, 'noright' => 0]; + + switch ($input['action']) { + case "Transfert": + + if ($input['itemtype'] == 'PluginManufacturersimportsConfig') { + foreach ($input["item"] as $key => $val) { + if ($val == 1) { + $values["id"] = $key; + $values["entities_id"] = $input['entities_id']; + if ($this->update($values)) { + $res['ok']++; + } else { + $res['ko']++; + } + } + } + } + break; + default: + return parent::doSpecificMassiveActions($input); + break; + } + return $res; + } + + /** + * Display the current tag dropdown in form header of items + * + * @param item the CommonDBTM object + * + * @return nothing + */ + public static function showForInfocom($item) + { + if (in_array($item->getType(), self::getTypes(true))) { + $suppliername = PluginManufacturersimportsConfig::checkManufacturerName($item->getType(), $item->getID()); + + $model = new PluginManufacturersimportsModel(); + $otherserial = $model->checkIfModelNeeds($item->getType(), $item->getID()); + + $configID = PluginManufacturersimportsConfig::checkManufacturerID($item->getType(), $item->getID()); + $config = new PluginManufacturersimportsConfig(); + $config->getFromDB($configID); + $supplierkey = (isset($config->fields["supplier_key"])) ? $config->fields["supplier_key"] : false; + $supplierurl = (isset($config->fields["supplier_url"])) ? $config->fields["supplier_url"] : false; + + if ($suppliername == PluginManufacturersimportsConfig::LENOVO) { + $url = PluginManufacturersimportsPreImport::selectSupplier($suppliername, $supplierurl, $item->fields['serial'], $otherserial, $supplierkey, null, true); + } else { + $url = PluginManufacturersimportsPreImport::selectSupplier($suppliername, $supplierurl, $item->fields['serial'], $otherserial, $supplierkey); + } + echo ""; + echo ""; + echo ""; + echo ""; + + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo "
" . PluginManufacturersimportsPreImport::getTypeName(2) . "
"; + echo _n('Link', 'Links', 1); + echo ""; + echo "" . __('Manufacturer information', 'manufacturersimports') . ""; + echo ""; + $target = PluginManufacturersimportsConfig::getFormUrl(true); + Html::showSimpleForm( + $target, + 'retrieve_warranty', + _sx('button', 'Retrieve warranty from manufacturer', 'manufacturersimports'), + ['itemtype' => $item->getType(), + 'items_id' => $item->getID()] + ); + echo "
"; + } + return $item; + } + + public static function retrieveOneWarranty($itemtype, $items_id) + { + $item = new $itemtype(); + if ($item->getFromDB($items_id)) { + $log = new PluginManufacturersimportsLog(); + $log->reinitializeImport($itemtype, $items_id); + + $config = new PluginManufacturersimportsConfig(); + $suppliername = PluginManufacturersimportsConfig::checkManufacturerName($itemtype, $items_id); + $config->getFromDBByCrit(['name' => $suppliername]); + $suppliername = $config->fields["name"]; + $supplierUrl = $config->fields["supplier_url"]; + $supplierkey = $config->fields["supplier_key"]; + + $url = PluginManufacturersimportsPreImport::selectSupplier( + $suppliername, + $supplierUrl, + $item->fields['serial'], + $item->fields['otherserial'], + $supplierkey + ); + + $post = PluginManufacturersimportsPreImport::getSupplierPost( + $suppliername, + $item->fields['serial'], + $item->fields['otherserial'] + ); + + $data = []; + $options = ["url" => $url, + "post" => $post, + "type" => $itemtype, + "ID" => $items_id, + "config" => $config, + "line" => $data, + "display" => false]; + + + $supplierclass = "PluginManufacturersimports" . $suppliername; + $token = $supplierclass::getToken($config); + $warranty_url = $supplierclass::getWarrantyUrl($config, $item->fields['serial']); + $options['token'] = $token; + $options['line']['entities_id'] = $item->fields['entities_id']; + if (isset($warranty_url['url'])) { + $options['url'] = $warranty_url['url']; + } + if ( + isset($_SESSION['glpi_use_mode']) + && ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE) + ) { + Toolbox::loginfo($options); } - break; - default : - return parent::doSpecificMassiveActions($input); - break; - } - return $res; - } - - /** - * Display the current tag dropdown in form header of items - * - * @param item the CommonDBTM object - * - * @return nothing - */ - public static function showForInfocom($item) { - - if (in_array($item->getType(), self::getTypes(true))) { - - $suppliername = PluginManufacturersimportsConfig::checkManufacturerName($item->getType(), $item->getID()); - - $model = new PluginManufacturersimportsModel(); - $otherserial = $model->checkIfModelNeeds($item->getType(), $item->getID()); - - $configID = PluginManufacturersimportsConfig::checkManufacturerID($item->getType(), $item->getID()); - $config = new PluginManufacturersimportsConfig(); - $config->getFromDB($configID); - $supplierkey = (isset($config->fields["supplier_key"])) ? $config->fields["supplier_key"] : false; - $supplierurl = (isset($config->fields["supplier_url"])) ? $config->fields["supplier_url"] : false; - - if ($suppliername == PluginManufacturersimportsConfig::LENOVO) { - $url = PluginManufacturersimportsPreImport::selectSupplier($suppliername, $supplierurl, $item->fields['serial'], $otherserial, $supplierkey, null, true); - } else { - $url = PluginManufacturersimportsPreImport::selectSupplier($suppliername, $supplierurl, $item->fields['serial'], $otherserial, $supplierkey); - } - echo ""; - echo ""; - echo ""; - echo ""; - - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo "
" . PluginManufacturersimportsPreImport::getTypeName(2) . "
"; - echo _n('Link', 'Links', 1); - echo ""; - echo "" . __('Manufacturer information', 'manufacturersimports') . ""; - echo ""; - $target = PluginManufacturersimportsConfig::getFormUrl(true); - Html::showSimpleForm($target, 'retrieve_warranty', - _sx('button', 'Retrieve warranty from manufacturer', 'manufacturersimports'), - ['itemtype' => $item->getType(), - 'items_id' => $item->getID()] - ); - echo "
"; - } - return $item; - } - - static function retrieveOneWarranty($itemtype, $items_id) { - - $item = new $itemtype(); - if ($item->getFromDB($items_id)) { - - $log = new PluginManufacturersimportsLog(); - $log->reinitializeImport($itemtype, $items_id); - - $config = new PluginManufacturersimportsConfig(); - $suppliername = PluginManufacturersimportsConfig::checkManufacturerName($itemtype, $items_id); - $config->getFromDBByCrit(['name' => $suppliername]); - $suppliername = $config->fields["name"]; - $supplierUrl = $config->fields["supplier_url"]; - $supplierkey = $config->fields["supplier_key"]; - - $url = PluginManufacturersimportsPreImport::selectSupplier($suppliername, $supplierUrl, - $item->fields['serial'], $item->fields['otherserial'], $supplierkey); - - $post = PluginManufacturersimportsPreImport::getSupplierPost($suppliername, $item->fields['serial'], - $item->fields['otherserial']); - - $data = []; - $options = ["url" => $url, - "post" => $post, - "type" => $itemtype, - "ID" => $items_id, - "config" => $config, - "line" => $data, - "display" => false]; - - - $supplierclass = "PluginManufacturersimports" . $suppliername; - $token = $supplierclass::getToken($config); - $warranty_url = $supplierclass::getWarrantyUrl($config, $item->fields['serial']); - $options['token'] = $token; - $options['line']['entities_id'] = $item->fields['entities_id']; - if (isset($warranty_url['url'])) { - $options['url'] = $warranty_url['url']; - } - if ( - isset($_SESSION['glpi_use_mode']) - && ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE) - ) { - Toolbox::loginfo($options); - } - PluginManufacturersimportsPostImport::saveImport($options); - } - } - - public static function showItemImport($params) { - $item = $params['item']; - - if ($item && in_array($item->getType(), self::getTypes(true))) { - - $config = new self(); - $log = new PluginManufacturersimportsLog(); - - $suppliername = PluginManufacturersimportsConfig::checkManufacturerName($item->getType(), $item->getID()); - if (!empty($suppliername) && !empty($item->fields['serial'])) { - $NotAlreadyImported = $log->checkIfAlreadyImported($item->getType(), $item->getID()); - if (!$NotAlreadyImported) { - echo "
"; - echo __("You did not import the warranty for this item. Do you want to get it back?", "manufacturersimports"); - $target = PluginManufacturersimportsConfig::getFormUrl(true); - echo " "; - Html::showSimpleForm($target, 'retrieve_warranty', - _sx('button', 'Retrieve warranty from manufacturer', 'manufacturersimports'), - ['itemtype' => $item->getType(), - 'items_id' => $item->getID()], - 'fa-2x fa-cloud-download-alt'); - echo "
"; + PluginManufacturersimportsPostImport::saveImport($options); + } + } + + public static function showItemImport($params) + { + $item = $params['item']; + + if ($item && in_array($item->getType(), self::getTypes(true))) { + $config = new self(); + $log = new PluginManufacturersimportsLog(); + + $suppliername = PluginManufacturersimportsConfig::checkManufacturerName($item->getType(), $item->getID()); + if (!empty($suppliername) && !empty($item->fields['serial'])) { + $NotAlreadyImported = $log->checkIfAlreadyImported($item->getType(), $item->getID()); + if (!$NotAlreadyImported) { + echo "
"; + echo __("You did not import the warranty for this item. Do you want to get it back?", "manufacturersimports"); + $target = PluginManufacturersimportsConfig::getFormUrl(true); + echo " "; + Html::showSimpleForm( + $target, + 'retrieve_warranty', + _sx('button', 'Retrieve warranty from manufacturer', 'manufacturersimports'), + ['itemtype' => $item->getType(), + 'items_id' => $item->getID()], + 'fa-2x fa-cloud-download-alt' + ); + echo "
"; + } } - } - } - } + } + } } diff --git a/inc/dell.class.php b/inc/dell.class.php index f65b5fd..5d6f7c0 100644 --- a/inc/dell.class.php +++ b/inc/dell.class.php @@ -28,7 +28,7 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } ini_set("max_execution_time", "0"); @@ -36,227 +36,244 @@ /** * Class PluginManufacturersimportsDell */ -class PluginManufacturersimportsDell extends PluginManufacturersimportsManufacturer { - - /** - * @see PluginManufacturersimportsManufacturer::showCheckbox() - */ - function showCheckbox($ID, $sel, $otherSerial = false) { - $name = "item[" . $ID . "]"; - return Html::getCheckbox(["name" => $name, "value" => 1, "selected" => $sel]); - } - - /** - * @see PluginManufacturersimportsManufacturer::showItem() - */ - function showItem($output_type, $item_num, $row_num, $otherSerial = false) { - return false; - } - - /** - * @see PluginManufacturersimportsManufacturer::showItemTitle() - */ - function showItemTitle($output_type, $header_num) { - return false; - } - - /** - * @see PluginManufacturersimportsManufacturer::showDocTitle() - */ - function showDocTitle($output_type, $header_num) { - return false; - } - - /** - * @see PluginManufacturersimportsManufacturer::showDocItem() - */ - function showDocItem($output_type, $item_num, $row_num, $doc = null) { - return Search::showEndLine($output_type); - } - - /** - * @see PluginManufacturersimportsManufacturer::getSupplierInfo() - */ - function getSupplierInfo($compSerial = null, $otherSerial = null, $key = null, $apisecret = null, - $supplierUrl = null) { - - if (!$compSerial) { - // by default - $info["name"] = PluginManufacturersimportsConfig::DELL; - $info['supplier_url'] = "https://www.dell.com/support/home/product-support/servicetag/"; - $info['token_url'] = "https://apigtwb2c.us.dell.com/auth/oauth/v2/token"; - $info['warranty_url'] = "https://apigtwb2c.us.dell.com/PROD/sbil/eapi/v5/asset-entitlements?servicetags="; - $info["supplier_key"] = "123456789"; - $info["supplier_secret"] = "987654321"; - return $info; - } - - $info["url"] = $supplierUrl . "$compSerial"; - return $info; - } - - /** - * @return bool - */ - function getSearchField() { - return false; - } - - /** - * @see PluginManufacturersimportsManufacturer::getBuyDate() - */ - function getBuyDate($contents) { - $info = json_decode($contents, true); - // v5 - if (isset($info[0]['shipDate'])) { - $date = new \DateTime($info[0]['shipDate']); - return $date->format('c'); - } - - return false; - } - - /** - * @see PluginManufacturersimportsManufacturer::getStartDate() - */ - function getStartDate($contents) { - $info = json_decode($contents, true); - // v5 - $max_date = false; - if (isset($info[0]['entitlements'])) { - foreach ($info[0]['entitlements'] as $d) { - $date = new \DateTime($d['startDate']); - if ($max_date == false || $date > $max_date) { - $max_date = $date; +class PluginManufacturersimportsDell extends PluginManufacturersimportsManufacturer +{ + /** + * @see PluginManufacturersimportsManufacturer::showCheckbox() + */ + public function showCheckbox($ID, $sel, $otherSerial = false) + { + $name = "item[" . $ID . "]"; + return Html::getCheckbox(["name" => $name, "value" => 1, "selected" => $sel]); + } + + /** + * @see PluginManufacturersimportsManufacturer::showItem() + */ + public function showItem($output_type, $item_num, $row_num, $otherSerial = false) + { + return false; + } + + /** + * @see PluginManufacturersimportsManufacturer::showItemTitle() + */ + public function showItemTitle($output_type, $header_num) + { + return false; + } + + /** + * @see PluginManufacturersimportsManufacturer::showDocTitle() + */ + public function showDocTitle($output_type, $header_num) + { + return false; + } + + /** + * @see PluginManufacturersimportsManufacturer::showDocItem() + */ + public function showDocItem($output_type, $item_num, $row_num, $doc = null) + { + return Search::showEndLine($output_type); + } + + /** + * @see PluginManufacturersimportsManufacturer::getSupplierInfo() + */ + public function getSupplierInfo( + $compSerial = null, + $otherSerial = null, + $key = null, + $apisecret = null, + $supplierUrl = null + ) + { + if (!$compSerial) { + // by default + $info["name"] = PluginManufacturersimportsConfig::DELL; + $info['supplier_url'] = "https://www.dell.com/support/home/product-support/servicetag/"; + $info['token_url'] = "https://apigtwb2c.us.dell.com/auth/oauth/v2/token"; + $info['warranty_url'] = "https://apigtwb2c.us.dell.com/PROD/sbil/eapi/v5/asset-entitlements?servicetags="; + $info["supplier_key"] = "123456789"; + $info["supplier_secret"] = "987654321"; + return $info; + } + + $info["url"] = $supplierUrl . "$compSerial"; + return $info; + } + + /** + * @return bool + */ + public function getSearchField() + { + return false; + } + + /** + * @see PluginManufacturersimportsManufacturer::getBuyDate() + */ + public function getBuyDate($contents) + { + $info = json_decode($contents, true); + // v5 + if (isset($info[0]['shipDate'])) { + $date = new \DateTime($info[0]['shipDate']); + return $date->format('c'); + } + + return false; + } + + /** + * @see PluginManufacturersimportsManufacturer::getStartDate() + */ + public function getStartDate($contents) + { + $info = json_decode($contents, true); + // v5 + $max_date = false; + if (isset($info[0]['entitlements'])) { + foreach ($info[0]['entitlements'] as $d) { + $date = new \DateTime($d['startDate']); + if ($max_date == false || $date > $max_date) { + $max_date = $date; + } } - } - - if ($max_date) { - return $max_date->format('c'); - } - } - - return false; - } - - /** - * @see PluginManufacturersimportsManufacturer::getExpirationDate() - */ - function getExpirationDate($contents) { - $info = json_decode($contents, true); - // v5 - // when several dates are available, will take the last one - $max_date = false; - if (isset($info[0]['entitlements'])) { - foreach ($info[0]['entitlements'] as $d) { - $date = new \DateTime($d['endDate']); - if ($max_date == false || $date > $max_date) { - $max_date = $date; + + if ($max_date) { + return $max_date->format('c'); + } + } + + return false; + } + + /** + * @see PluginManufacturersimportsManufacturer::getExpirationDate() + */ + public function getExpirationDate($contents) + { + $info = json_decode($contents, true); + // v5 + // when several dates are available, will take the last one + $max_date = false; + if (isset($info[0]['entitlements'])) { + foreach ($info[0]['entitlements'] as $d) { + $date = new \DateTime($d['endDate']); + if ($max_date == false || $date > $max_date) { + $max_date = $date; + } + } + + if ($max_date) { + return $max_date->format('c'); } - } - - if ($max_date) { - return $max_date->format('c'); - } - } - - - return false; - } - - /** - * @see PluginManufacturersimportsManufacturer::getWarrantyInfo() - */ - function getWarrantyInfo($contents) { - $info = json_decode($contents, true); - - // v5 - // when several warranties are available, will take the last one - $max_date = false; - $i = false; - if (isset($info[0]['entitlements'])) { - foreach ($info[0]['entitlements'] as $k => $d) { - $date = new \DateTime($d['endDate']); - if ($max_date == false || $date > $max_date) { - $max_date = $date; - $i = $k; + } + + + return false; + } + + /** + * @see PluginManufacturersimportsManufacturer::getWarrantyInfo() + */ + public function getWarrantyInfo($contents) + { + $info = json_decode($contents, true); + + // v5 + // when several warranties are available, will take the last one + $max_date = false; + $i = false; + if (isset($info[0]['entitlements'])) { + foreach ($info[0]['entitlements'] as $k => $d) { + $date = new \DateTime($d['endDate']); + if ($max_date == false || $date > $max_date) { + $max_date = $date; + $i = $k; + } } - } - } - - if ($max_date && $i) { - return $info[0]['entitlements'][$i]['serviceLevelDescription']; - } - - return false; - } - - /** - * @param $name - * - * @return array - */ - static function cronInfo($name) { - - switch ($name) { - case "DataRecoveryDELL" : - return ['description' => PluginManufacturersimportsModel::getTypeName(1) . " - " . __('Data recovery DELL for computer', 'manufacturersimports')]; - } - return []; - } - - - /** - * Run for data recovery DELL - * - * @param $task : object of crontask - * - * @return integer : 0 (nothing to do) - * >0 (endded) - **/ - static function cronDataRecoveryDELL($task) { - - $cron_status = PluginManufacturersimportsImport::importCron($task, PluginManufacturersimportsConfig::DELL); - - return $cron_status; - } - - /** - * Summary of getToken - * - * @param $config - * - * @return mixed - */ - static function getToken($config) { - $token = false; - // must manage token - $options = ["url" => $config->fields["token_url"], - "download" => false, - "file" => false, - "post" => ['client_id' => $config->fields["supplier_key"], - 'client_secret' => $config->fields["supplier_secret"], - 'grant_type' => 'client_credentials'], - "suppliername" => $config->fields["name"]]; - $contents = PluginManufacturersimportsPostImport::cURLData($options); - // must extract from $contents the token bearer - $response = json_decode($contents, true); - if (isset($response['access_token'])) { - $token = $response['access_token']; - } - return $token; - } - - - /** - * Summary of getWarrantyUrl - * - * @param $config - * @param $compSerial - * - * @return string[] - */ - static function getWarrantyUrl($config, $compSerial) { - return ["url" => $config->fields['warranty_url'] . "$compSerial"]; - } + } + + if ($max_date && $i) { + return $info[0]['entitlements'][$i]['serviceLevelDescription']; + } + + return false; + } + + /** + * @param $name + * + * @return array + */ + public static function cronInfo($name) + { + switch ($name) { + case "DataRecoveryDELL": + return ['description' => PluginManufacturersimportsModel::getTypeName(1) . " - " . __('Warranty import for computer (dell, HP)', 'manufacturersimports')]; + } + return []; + } + + + /** + * Run for data recovery DELL + * + * @param $task : object of crontask + * + * @return integer : 0 (nothing to do) + * >0 (endded) + **/ + public static function cronDataRecoveryDELL($task) + { + $cron_status = PluginManufacturersimportsImport::importCron($task, PluginManufacturersimportsConfig::DELL); + + return $cron_status; + } + + /** + * Summary of getToken + * + * @param $config + * + * @return mixed + */ + public static function getToken($config) + { + $token = false; + // must manage token + $options = ["url" => $config->fields["token_url"], + "download" => false, + "file" => false, + "post" => ['client_id' => $config->fields["supplier_key"], + 'client_secret' => $config->fields["supplier_secret"], + 'grant_type' => 'client_credentials'], + "suppliername" => $config->fields["name"]]; + $contents = PluginManufacturersimportsPostImport::cURLData($options); + // must extract from $contents the token bearer + $response = json_decode($contents, true); + if (isset($response['access_token'])) { + $token = $response['access_token']; + } + return $token; + } + + + /** + * Summary of getWarrantyUrl + * + * @param $config + * @param $compSerial + * + * @return string[] + */ + public static function getWarrantyUrl($config, $compSerial) + { + return ["url" => $config->fields['warranty_url'] . "$compSerial"]; + } } diff --git a/inc/fujitsu.class.php b/inc/fujitsu.class.php index a9ffedf..dc7aa78 100644 --- a/inc/fujitsu.class.php +++ b/inc/fujitsu.class.php @@ -28,81 +28,90 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } /** * Class PluginManufacturersimportsFujitsu */ -class PluginManufacturersimportsFujitsu extends PluginManufacturersimportsManufacturer { - - /** - * @see PluginManufacturersimportsManufacturer::showDocTitle() - */ - function showDocTitle($output_type, $header_num) { - return Search::showHeaderItem($output_type, __('File'), $header_num); - } - - function getSearchField() { - return false; - } - - /** - * @see PluginManufacturersimportsManufacturer::getSupplierInfo() - */ - function getSupplierInfo($compSerial = null, $otherSerial = null, $key = null, $apisecret = null, - $supplierUrl = null) { - $info["name"] = PluginManufacturersimportsConfig::FUJITSU; - $info["supplier_url"] = 'https://support.ts.fujitsu.com/ProductCheck/Default.aspx?Lng=en&GotoDiv=Warranty/WarrantyStatus&DivID=indexwarranty&GotoUrl=IndexWarranty&RegionID=1&Token=${$i$M$f$u&Ident='; - $info["url"] = $supplierUrl.$compSerial; - $info["url_web"] = "https://support.ts.fujitsu.com/IndexWarranty.asp?lng=FR"; - return $info; - } - - /** - * Summary of getWarrantyUrl - * - * @param $config - * @param $compSerial - * - * @return string[] - */ - static function getWarrantyUrl($config, $compSerial) { - return ["url" => 'https://support.ts.fujitsu.com/ProductCheck/Default.aspx?Lng=en&GotoDiv=Warranty/WarrantyStatus&DivID=indexwarranty&GotoUrl=IndexWarranty&RegionID=1&Token=${$i$M$f$u&Ident=' . "$compSerial"]; - } - - /** - * @see PluginManufacturersimportsManufacturer::getBuyDate() - */ - function getBuyDate($contents) { - - $matchesarray = []; - preg_match_all("/value=\"(\d{4}\-\d{2}\-\d{2})\" id=\"Firstuse\"/", $contents, $matchesarray); - - $buydate = (isset($matchesarray[1][0])?trim($matchesarray[1][0]):'0000-00-00'); - - return $buydate; - } - - /** - * @see PluginManufacturersimportsManufacturer::getStartDate() - */ - function getStartDate($contents) { - - return self::getBuyDate($contents); - } - - - /** - * @see PluginManufacturersimportsManufacturer::getExpirationDate() - */ - function getExpirationDate($contents) { - - $matchesarray = []; - preg_match_all("/value=\"(\d{4}\-\d{2}\-\d{2})\" id=\"WarrantyEndDate\"/", $contents, $matchesarray); - - $expirationdate = (isset($matchesarray[1][0])?trim($matchesarray[1][0]):'0000-00-00'); - - return $expirationdate; - } +class PluginManufacturersimportsFujitsu extends PluginManufacturersimportsManufacturer +{ + /** + * @see PluginManufacturersimportsManufacturer::showDocTitle() + */ + public function showDocTitle($output_type, $header_num) + { + return Search::showHeaderItem($output_type, __('File'), $header_num); + } + + public function getSearchField() + { + return false; + } + + /** + * @see PluginManufacturersimportsManufacturer::getSupplierInfo() + */ + public function getSupplierInfo( + $compSerial = null, + $otherSerial = null, + $key = null, + $apisecret = null, + $supplierUrl = null + ) + { + $info["name"] = PluginManufacturersimportsConfig::FUJITSU; + $info["supplier_url"] = 'https://support.ts.fujitsu.com/ProductCheck/Default.aspx?Lng=en&GotoDiv=Warranty/WarrantyStatus&DivID=indexwarranty&GotoUrl=IndexWarranty&RegionID=1&Token=${$i$M$f$u&Ident='; + $info["url"] = $supplierUrl.$compSerial; + $info["url_web"] = "https://support.ts.fujitsu.com/IndexWarranty.asp?lng=FR"; + return $info; + } + + /** + * Summary of getWarrantyUrl + * + * @param $config + * @param $compSerial + * + * @return string[] + */ + public static function getWarrantyUrl($config, $compSerial) + { + return ["url" => 'https://support.ts.fujitsu.com/ProductCheck/Default.aspx?Lng=en&GotoDiv=Warranty/WarrantyStatus&DivID=indexwarranty&GotoUrl=IndexWarranty&RegionID=1&Token=${$i$M$f$u&Ident=' . "$compSerial"]; + } + + /** + * @see PluginManufacturersimportsManufacturer::getBuyDate() + */ + public function getBuyDate($contents) + { + $matchesarray = []; + preg_match_all("/value=\"(\d{4}\-\d{2}\-\d{2})\" id=\"Firstuse\"/", $contents, $matchesarray); + + $buydate = (isset($matchesarray[1][0])?trim($matchesarray[1][0]):'0000-00-00'); + + return $buydate; + } + + /** + * @see PluginManufacturersimportsManufacturer::getStartDate() + */ + public function getStartDate($contents) + { + return self::getBuyDate($contents); + } + + + /** + * @see PluginManufacturersimportsManufacturer::getExpirationDate() + */ + public function getExpirationDate($contents) + { + $matchesarray = []; + preg_match_all("/value=\"(\d{4}\-\d{2}\-\d{2})\" id=\"WarrantyEndDate\"/", $contents, $matchesarray); + + $expirationdate = (isset($matchesarray[1][0])?trim($matchesarray[1][0]):'0000-00-00'); + + return $expirationdate; + } } diff --git a/inc/hp.class.php b/inc/hp.class.php index 66b2ebc..cfd688e 100644 --- a/inc/hp.class.php +++ b/inc/hp.class.php @@ -28,96 +28,85 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } /** * Class PluginManufacturersimportsHP */ -class PluginManufacturersimportsHP extends PluginManufacturersimportsManufacturer { - - /** - * @see PluginManufacturersimportsManufacturer::showDocTitle() - */ - function showDocTitle($output_type, $header_num) { - return Search::showHeaderItem($output_type, __('File'), $header_num); - } - - function getSearchField() { - return false; - } - - /** - * @see PluginManufacturersimportsManufacturer::getSupplierInfo() - */ - function getSupplierInfo($compSerial = null, $otherSerial = null, $key = null, $apisecret = null, - $supplierUrl = null) { - $info["name"] = PluginManufacturersimportsConfig::HP; - $info["supplier_url"] = "https://css.api.hp.com/oauth/v1/token"; - - $info["url"] = $supplierUrl; - $info['url_warranty'] = 'https://css.api.hp.com/productWarranty/v1/queries'; - - $info['post'] = ['apiKey' => $key, - 'apiSecret' => $apisecret, - 'grantType' => 'client_credentials', - 'scope' => 'warranty', - 'sn' => rtrim($compSerial), - ]; - - if (!empty($otherSerial)) { - $info['post']['pn'] = $otherSerial; - } - - return $info; - } - - /** - * @see PluginManufacturersimportsManufacturer::getBuyDate() - */ - function getBuyDate($contents) { - $contents = json_decode($contents, true); - $contents = reset($contents); - if (isset($contents['startDate'])) { - return $contents['startDate']; - } - return false; - } - - /** - * @see PluginManufacturersimportsManufacturer::getStartDate() - */ - function getStartDate($contents) { - return self::getBuyDate($contents); - } - - /** - * @see PluginManufacturersimportsManufacturer::getExpirationDate() - */ - function getExpirationDate($contents) { - - $contents = json_decode($contents, true); - $contents = reset($contents); - if (isset($contents['endDate'])) { - return $contents['endDate']; - } - return false; - } - - /** - * @see PluginManufacturersimportsManufacturer::getWarrantyInfo() - */ - function getWarrantyInfo($contents) { - $contents = json_decode($contents, true); - $contents = reset($contents); - - $warrantyInfo = ""; - if (isset($contents['status'])) { - $warrantyInfo .= $contents['status'] . " "; - } - if (isset($contents['type'])) { - $warrantyInfo .= $contents['type'] . " "; - } - return $warrantyInfo; - } +class PluginManufacturersimportsHP extends PluginManufacturersimportsManufacturer +{ + /** + * @see PluginManufacturersimportsManufacturer::showDocTitle() + */ + public function showDocTitle($output_type, $header_num) + { + return Search::showHeaderItem($output_type, __('File'), $header_num); + } + + public function getSearchField() + { + return false; + } + + /** + * @see PluginManufacturersimportsManufacturer::getSupplierInfo() + */ + public function getSupplierInfo( + $compSerial = null, + $otherSerial = null, + $key = null, + $apisecret = null, + $supplierUrl = null + ) { + $info["name"] = PluginManufacturersimportsConfig::HP; + $info["supplier_url"] = "https://warrantyapiproxy.azurewebsites.net/api/HP?serial="; + + $info['url_warranty'] = "https://warrantyapiproxy.azurewebsites.net/api/HP?serial="; + $info["url"] = $supplierUrl.$compSerial; + + return $info; + } + + /** + * @see PluginManufacturersimportsManufacturer::getBuyDate() + */ + public function getBuyDate($contents) + { + $info = json_decode($contents, true); + + if (isset($info['StartDate'])) { + return $info['StartDate']; + } + } + + /** + * @see PluginManufacturersimportsManufacturer::getStartDate() + */ + public function getStartDate($contents) + { + return self::getBuyDate($contents); + } + + /** + * @see PluginManufacturersimportsManufacturer::getExpirationDate() + */ + public function getExpirationDate($contents) + { + $info = json_decode($contents, true); + if (isset($info['EndDate'])) { + return $info['EndDate']; + } + return false; + } + + /** + * @see PluginManufacturersimportsManufacturer::getWarrantyInfo() + */ + public function getWarrantyInfo($contents) + { + $warrantyInfo = ""; + + return $warrantyInfo; + } } diff --git a/inc/ibm.class.php b/inc/ibm.class.php index 8cbb39d..71b2538 100644 --- a/inc/ibm.class.php +++ b/inc/ibm.class.php @@ -28,24 +28,28 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } /** * Class PluginManufacturersimportsIBM */ -class PluginManufacturersimportsIBM extends CommonDBTM { - - /** - * @see PluginManufacturersimportsManufacturer::getSupplierInfo() - */ - function getSupplierInfo($compSerial = null, $otherSerial = null, $key = null, $apisecret = null, - $supplierUrl = null) { - - $info["name"]="IBM"; - $info["supplier_url"] = "http://www-304.ibm.com/jct01004c/systems/support/supportsite.wss/warranty?"; - $info["url"] = $supplierUrl."type=".$otherSerial."&serial=".$compSerial."&brandind=5000008&Submit=Submit&action=warranty"; - return $info; - } - +class PluginManufacturersimportsIBM extends CommonDBTM +{ + /** + * @see PluginManufacturersimportsManufacturer::getSupplierInfo() + */ + public function getSupplierInfo( + $compSerial = null, + $otherSerial = null, + $key = null, + $apisecret = null, + $supplierUrl = null + ) + { + $info["name"]="IBM"; + $info["supplier_url"] = "http://www-304.ibm.com/jct01004c/systems/support/supportsite.wss/warranty?"; + $info["url"] = $supplierUrl."type=".$otherSerial."&serial=".$compSerial."&brandind=5000008&Submit=Submit&action=warranty"; + return $info; + } } diff --git a/inc/import.class.php b/inc/import.class.php index 2af2397..8cd3e1a 100644 --- a/inc/import.class.php +++ b/inc/import.class.php @@ -28,111 +28,121 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } /** * Class PluginManufacturersimportsImport */ -class PluginManufacturersimportsImport extends CommonDBTM { - - /** - * Import via le cron - * - * @param type $supplier - * - * @return int - * @global type $DB - * - */ - static function importCron($task, $supplier) { - global $DB; - - $config = new PluginManufacturersimportsConfig(); - $config->getFromDBByCrit(['name' => $supplier]); - - $log = new PluginManufacturersimportsLog(); - - $suppliername = $config->fields["name"]; - $supplierUrl = $config->fields["supplier_url"]; - $supplierkey = $config->fields["supplier_key"]; - $supplierId = $config->fields["suppliers_id"]; - - $toview = ["name" => 1]; - - $params = []; - $params['manufacturers_id'] = $config->getID(); - $params['imported'] = 1; - $params['sort'] = 1; - $params['order'] = "ASC"; - $params['start'] = 0; +class PluginManufacturersimportsImport extends CommonDBTM +{ + /** + * Import via le cron + * + * @param type $supplier + * + * @return int + * @global type $DB + * + */ + public static function importCron($task, $supplier) + { + global $DB; + + $config = new PluginManufacturersimportsConfig(); + $config->getFromDBByCrit(['name' => $supplier]); + + $log = new PluginManufacturersimportsLog(); + + $suppliername = $config->fields["name"]; + $supplierUrl = $config->fields["supplier_url"]; + $supplierkey = $config->fields["supplier_key"]; + $supplierId = $config->fields["suppliers_id"]; + + $toview = ["name" => 1]; + + $params = []; + $params['manufacturers_id'] = $config->getID(); + $params['imported'] = 1; + $params['sort'] = 1; + $params['order'] = "ASC"; + $params['start'] = 0; // $types = PluginManufacturersimportsConfig::getTypes(); - $nb_import_error = 0; - $msg = ""; + $nb_import_error = 0; + $msg = ""; // foreach ($types as $type) { - $type = "Computer"; - $params['itemtype'] = $type; - $query = PluginManufacturersimportsPreImport::queryImport($params, $config, $toview, true); - - $result = $DB->query($query); - - if ($DB->numrows($result) > 0) { - while ($data = $DB->fetchArray($result)) { - - $log->reinitializeImport($type, $data['id']); - - $compSerial = $data['serial']; - $ID = $data['id']; - - $model = new PluginManufacturersimportsModel(); - $otherSerial = $model->checkIfModelNeeds($type, $ID); - - $url = PluginManufacturersimportsPreImport::selectSupplier($suppliername, $supplierUrl, - $compSerial, $otherSerial, $supplierkey); - $post = PluginManufacturersimportsPreImport::getSupplierPost($suppliername, $compSerial, - $otherSerial); - - $options = ["url" => $url, - "post" => $post, - "type" => $type, - "ID" => $ID, - "config" => $config, - "line" => $data, - "display" => false]; - - if ($suppliername == PluginManufacturersimportsConfig::LENOVO) { - $options['ClientID'] = $supplierkey; - } - - if ($suppliername == PluginManufacturersimportsConfig::DELL) { - $supplierclass = "PluginManufacturersimports" . $suppliername; - $token = $supplierclass::getToken($config); - $warranty_url = $supplierclass::getWarrantyUrl($config, $compSerial); - $options['token'] = $token; - if (isset($warranty_url)) { - $options['url'] = $warranty_url['url']; - } + $type = "Computer"; + $params['itemtype'] = $type; + $query = PluginManufacturersimportsPreImport::queryImport($params, $config, $toview, true); + + $result = $DB->query($query); + + if ($DB->numrows($result) > 0) { + while ($data = $DB->fetchArray($result)) { + $log->reinitializeImport($type, $data['id']); + + $compSerial = $data['serial']; + $ID = $data['id']; + + $model = new PluginManufacturersimportsModel(); + $otherSerial = $model->checkIfModelNeeds($type, $ID); + + $url = PluginManufacturersimportsPreImport::selectSupplier( + $suppliername, + $supplierUrl, + $compSerial, + $otherSerial, + $supplierkey + ); + $post = PluginManufacturersimportsPreImport::getSupplierPost( + $suppliername, + $compSerial, + $otherSerial + ); + + $options = ["url" => $url, + "post" => $post, + "type" => $type, + "ID" => $ID, + "config" => $config, + "line" => $data, + "display" => false]; + + if ($suppliername == PluginManufacturersimportsConfig::DELL) { + $supplierclass = "PluginManufacturersimports" . $suppliername; + $token = $supplierclass::getToken($config); + $warranty_url = $supplierclass::getWarrantyUrl($config, $compSerial); + $options['token'] = $token; + if (isset($warranty_url)) { + $options['url'] = $warranty_url['url']; + } + } + if ($suppliername == PluginManufacturersimportsConfig::HP) { + $supplierclass = "PluginManufacturersimports" . $suppliername; + $warranty_url = $supplierclass::getWarrantyUrl($config, $compSerial); + if (isset($warranty_url)) { + $options['url'] = $warranty_url['url']; + } + } + + if (PluginManufacturersimportsPostImport::saveImport($options)) { + $task->addVolume(1); + } else { + $nb_import_error += 1; + } } - - if (PluginManufacturersimportsPostImport::saveImport($options)) { - $task->addVolume(1); - } else { - $nb_import_error += 1; - } - } - } + } // } - if ($task) { - $task->log(__('Import OK', 'manufacturersimports')); - - $task->addVolume($nb_import_error); - $task->log(__('Import failed', 'manufacturersimports')); - } - return true; - - } + if ($task) { + $task->log(__('Import OK', 'manufacturersimports')); + + $task->addVolume($nb_import_error); + $task->log(__('Import failed', 'manufacturersimports')); + } + return true; + } } diff --git a/inc/lenovo.class.php b/inc/lenovo.class.php index 0bd213a..1dda4f6 100644 --- a/inc/lenovo.class.php +++ b/inc/lenovo.class.php @@ -28,7 +28,7 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } ini_set("max_execution_time", "0"); @@ -38,179 +38,207 @@ */ class PluginManufacturersimportsLenovo extends PluginManufacturersimportsManufacturer { - /** - * @see PluginManufacturersimportsManufacturer::showCheckbox() - */ - function showCheckbox($ID, $sel, $otherSerial = false) { - $name = "item[" . $ID . "]"; - return Html::getCheckbox(["name" => $name, "value" => 1, "selected" => $sel]); - - } - - /** - * @see PluginManufacturersimportsManufacturer::showItemTitle() - */ - function showItemTitle($output_type, $header_num) { - return Search::showHeaderItem($output_type, __('Model number', 'manufacturersimports'), $header_num); - } - - /** - * @see PluginManufacturersimportsManufacturer::showDocTitle() - */ - function showDocTitle($output_type, $header_num) { - return Search::showHeaderItem($output_type, __('File'), $header_num); - } - - /** - * @see PluginManufacturersimportsManufacturer::showItem() - */ - function showItem($output_type, $item_num, $row_num, $otherSerial = false) { - return false; - } - - function getSearchField() { - return false; - } - - /** - * @see PluginManufacturersimportsManufacturer::getSupplierInfo() - */ - function getSupplierInfo($compSerial = null, $otherSerial = null, $key = null, $apisecret = null, - $supplierUrl = null) { - - $info["name"] = PluginManufacturersimportsConfig::LENOVO; - $info["supplier_url"] = "https://SupportAPI.lenovo.com/v2.5/Warranty"; - // $info["url"] = $supplierUrl . $compSerial."?machineType=&btnSubmit"; - $info["url"] = $supplierUrl . "?Serial=".$compSerial; - $info["url_web"] = "https://pcsupport.lenovo.com/products/$compSerial/warranty"; - return $info; - } - - /** - * @see PluginManufacturersimportsManufacturer::getBuyDate() - */ - function getBuyDate($contents) { - - $contents = json_decode($contents, true); - - if (isset($contents['Purchased'])) { - - if(strpos($contents['Purchased'], '0001-01-01') !== false){ - if(strpos($contents['Shipped'], '0001-01-01') !== false) { - if(isset($contents['Warranty']) && !empty($contents['Warranty'])){ - $minStart = 0; - $start = 0; - $n = 0; - foreach ($contents['Warranty'] as $id => $warranty){ - $myDate= trim($warranty['start']); - $dateStart = strtotime($myDate); - if($n === 0){ - $minStart = $dateStart; - $myDate = strtotime(trim($warranty['Start'])); - } - if($dateStart > $minStart){ - $minStart = $dateStart; - $myDate = strtotime(trim($warranty['Start'])); - } - $n++; - } - } - }else{ - $myDate = trim($contents['Shipped']); - } - }else{ - $myDate = trim($contents['Purchased']); - } - // $myDate = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year)); - $myDate = date("Y-m-d", strtotime($myDate)); - - - return PluginManufacturersimportsPostImport::checkDate($myDate); - } - } - - /** - * @see PluginManufacturersimportsManufacturer::getStartDate() - */ - function getStartDate($contents) { - //TODO change to have good start date with new json - $contents = json_decode($contents, true); - if(isset($contents['Warranty']) && !empty($contents['Warranty'])){ - $maxEnd = 0; - $start = 0; - foreach ($contents['Warranty'] as $id => $warranty){ - $myDate = trim($warranty['End']); - $dateEnd = strtotime($myDate); - if($dateEnd > $maxEnd){ - $maxEnd = $dateEnd; - $start = strtotime(trim($warranty['Start'])); - } - } - - } - - if(isset($start)) { - $myDate = date("Y-m-d", $start); - - return PluginManufacturersimportsPostImport::checkDate($myDate); - } - - } - - /** - * @see PluginManufacturersimportsManufacturer::getExpirationDate() - */ - function getExpirationDate($contents) { - $contents = json_decode($contents, true); - //TODO change to have good expiration date with new json - if(isset($contents['Warranty']) && !empty($contents['Warranty'])){ - $maxEnd = 0; - - foreach ($contents['Warranty'] as $id => $warranty){ - $myDate = trim($warranty['End']); - $dateEnd = strtotime($myDate); - if($dateEnd > $maxEnd){ - $maxEnd = $dateEnd; - } - } - - } - - if(isset($maxEnd)) { - $myDate = date("Y-m-d", $maxEnd); - - return PluginManufacturersimportsPostImport::checkDate($myDate); - } - } - - /** - * @see PluginManufacturersimportsManufacturer::getWarrantyInfo() - */ - function getWarrantyInfo($contents) { - $contents = json_decode($contents, true); - - //TODO change to have good information with new json - $warranty_info = false; - if(isset($contents['Warranty']) && !empty($contents['Warranty'])){ - $maxEnd = 0; - - foreach ($contents['Warranty'] as $id => $warranty){ - $myDate = trim($warranty['End']); - $dateEnd = strtotime($myDate); - if($dateEnd > $maxEnd){ - $maxEnd = $dateEnd; - if(isset($warranty["Description"])){ - $warranty_info = $warranty["Description"]; - }else{ - $warranty_info = $warranty["Type"]." - ".$warranty["Name"]; - } - } - } - - } - if (strlen($warranty_info) > 255) { - $warranty_info = substr($warranty_info, 0, 254); - } - return $warranty_info; - } + /** + * @see PluginManufacturersimportsManufacturer::showCheckbox() + */ + function showCheckbox($ID, $sel, $otherSerial = false) { + $name = "item[" . $ID . "]"; + return Html::getCheckbox(["name" => $name, "value" => 1, "selected" => $sel]); + + } + + /** + * @see PluginManufacturersimportsManufacturer::showItemTitle() + */ + function showItemTitle($output_type, $header_num) { + return Search::showHeaderItem($output_type, __('Model number', 'manufacturersimports'), $header_num); + } + + /** + * @see PluginManufacturersimportsManufacturer::showDocTitle() + */ + function showDocTitle($output_type, $header_num) { + return Search::showHeaderItem($output_type, __('File'), $header_num); + } + + /** + * @see PluginManufacturersimportsManufacturer::showItem() + */ + function showItem($output_type, $item_num, $row_num, $otherSerial = false) { + return false; + } + + function getSearchField() { + return false; + } + + /** + * @see PluginManufacturersimportsManufacturer::getSupplierInfo() + */ + function getSupplierInfo($compSerial = null, $otherSerial = null, $key = null, $apisecret = null, + $supplierUrl = null) { + + $info["name"] = PluginManufacturersimportsConfig::LENOVO; + $info["supplier_url"] = "https://pcsupport.lenovo.com/products/$compSerial/warranty"; + // $info["url"] = $supplierUrl . $compSerial."?machineType=&btnSubmit"; + $info["url"] = "https://pcsupport.lenovo.com/products/$compSerial/warranty"; + $info["url_web"] = "https://pcsupport.lenovo.com/products/$compSerial/warranty"; + return $info; + } + + /** + * @see PluginManufacturersimportsManufacturer::getBuyDate() + */ + function getBuyDate($contents) { + +// $contents = json_decode($contents, true); + $field = "POPDate"; + $search = stristr($contents, $field); + $myDate = substr($search, 10, 10); + $myDate = trim($myDate); + $myDate = PluginManufacturersimportsPostImport::checkDate($myDate); + + return $myDate; + +// if (isset($contents['POPDate'])) { +// +// Toolbox::loginfo($contents['POPDate']); +// +// if (strpos($contents['POPDate'], '0001-01-01') !== false) { +// if (strpos($contents['Shipped'], '0001-01-01') !== false) { +// if (isset($contents['Warranty']) && !empty($contents['Warranty'])) { +// $minStart = 0; +// $start = 0; +// $n = 0; +// foreach ($contents['Warranty'] as $id => $warranty) { +// $myDate = trim($warranty['start']); +// $dateStart = strtotime($myDate); +// if ($n === 0) { +// $minStart = $dateStart; +// $myDate = strtotime(trim($warranty['Start'])); +// } +// if ($dateStart > $minStart) { +// $minStart = $dateStart; +// $myDate = strtotime(trim($warranty['Start'])); +// } +// $n++; +// } +// } +// } else { +// $myDate = trim($contents['POPDate']); +// } +// } else { +// $myDate = trim($contents['POPDate']); +// } +// Toolbox::loginfo($myDate); +// // $myDate = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year)); +// $myDate = date("Y-m-d", strtotime($myDate)); +// +// +// return PluginManufacturersimportsPostImport::checkDate($myDate); +// } + } + + /** + * @see PluginManufacturersimportsManufacturer::getStartDate() + */ + function getStartDate($contents) { + + $field = "POPDate"; + $search = stristr($contents, $field); + $myDate = substr($search, 10, 10); + $myDate = trim($myDate); + $myDate = PluginManufacturersimportsPostImport::checkDate($myDate); + + return $myDate; + +// //TODO change to have good start date with new json +// $contents = json_decode($contents, true); +// if (isset($contents['Warranty']) && !empty($contents['Warranty'])) { +// $maxEnd = 0; +// $start = 0; +// foreach ($contents['Warranty'] as $id => $warranty) { +// $myDate = trim($warranty['End']); +// $dateEnd = strtotime($myDate); +// if ($dateEnd > $maxEnd) { +// $maxEnd = $dateEnd; +// $start = strtotime(trim($warranty['Start'])); +// } +// } +// +// } +// +// if (isset($start)) { +// $myDate = date("Y-m-d", $start); +// +// return PluginManufacturersimportsPostImport::checkDate($myDate); +// } + + } + + /** + * @see PluginManufacturersimportsManufacturer::getExpirationDate() + */ + function getExpirationDate($contents) { +// $contents = json_decode($contents, true); +// //TODO change to have good expiration date with new json +// if (isset($contents['Warranty']) && !empty($contents['Warranty'])) { +// $maxEnd = 0; +// +// foreach ($contents['Warranty'] as $id => $warranty) { +// $myDate = trim($warranty['End']); +// $dateEnd = strtotime($myDate); +// if ($dateEnd > $maxEnd) { +// $maxEnd = $dateEnd; +// } +// } +// +// } +// +// if (isset($maxEnd)) { +// $myDate = date("Y-m-d", $maxEnd); +// +// return PluginManufacturersimportsPostImport::checkDate($myDate); +// } + + $field = "BaseUpmaWarranties"; + $search = stristr($contents, $field); + + $myEndDate = substr($search, 29, 10); + + $myEndDate = trim($myEndDate); + $myEndDate = PluginManufacturersimportsPostImport::checkDate($myEndDate); + return $myEndDate; + } + + /** + * @see PluginManufacturersimportsManufacturer::getWarrantyInfo() + */ + function getWarrantyInfo($contents) { +// $contents = json_decode($contents, true); +// +// //TODO change to have good information with new json +// $warranty_info = false; +// if (isset($contents['Warranty']) && !empty($contents['Warranty'])) { +// $maxEnd = 0; +// +// foreach ($contents['Warranty'] as $id => $warranty) { +// $myDate = trim($warranty['End']); +// $dateEnd = strtotime($myDate); +// if ($dateEnd > $maxEnd) { +// $maxEnd = $dateEnd; +// if (isset($warranty["Description"])) { +// $warranty_info = $warranty["Description"]; +// } else { +// $warranty_info = $warranty["Type"] . " - " . $warranty["Name"]; +// } +// } +// } +// +// } +// if (strlen($warranty_info) > 255) { +// $warranty_info = substr($warranty_info, 0, 254); +// } +// return $warranty_info; + } } diff --git a/inc/manufacturer.class.php b/inc/manufacturer.class.php index 5cf7048..925bff7 100644 --- a/inc/manufacturer.class.php +++ b/inc/manufacturer.class.php @@ -28,154 +28,168 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } /** * Class PluginManufacturersimportsManufacturer */ -class PluginManufacturersimportsManufacturer extends CommonDBTM { - - /** - * @param $ID - * @param $sel - * @param bool $otherSerial - * @return string - */ - function showCheckbox($ID, $sel, $otherSerial = false) { - $name = "item[" . $ID . "]"; - return Html::getCheckbox(["name" => $name, "value" => 1, "selected" => $sel]); - } - - /** - * @param $output_type - * @param $header_num - * @return bool - */ - function showItemTitle($output_type, $header_num) { - return false; - } - - /** - * @param $output_type - * @param bool $otherSerial - * @param $item_num - * @param $row_num - * @return bool - */ - function showItem($output_type, $item_num, $row_num, $otherSerial = false) { - return false; - } - - /** - * @param $output_type - * @param $header_num - * @return bool - */ - function showDocTitle($output_type, $header_num) { - return false; - - } - - /** - * @param $output_type - * @param $item_num - * @param $row_num - * @param null $doc - * @return string - */ - function showDocItem($output_type, $item_num, $row_num, $documents_id = null) { - $doc = new document(); - if ($doc->getFromDB($documents_id)) { - return Search::showItem($output_type, - $doc->getDownloadLink(), - $item_num, $row_num); - } - return Search::showItem($output_type, "", $item_num, $row_num); - - } - - /** - * - * @param type $ID - * @param type $supplierWarranty - */ - function showWarrantyItem($ID, $supplierWarranty) { - echo "".__('Automatic'); - $name = "to_warranty_duration".$ID; - echo Html::hidden($name, ['value' => 0]); - echo ""; - } - - /** - * Get supplier information with url - * - * @param null $compSerial - * @param null $otherserial - * @param null $key - * @param null $supplierUrl - * @return mixed - */ - function getSupplierInfo($compSerial = null, $otherSerial = null, $key = null, $apisecret = null, - $supplierUrl = null) { - - } - - /** - * Get buy date of object - * - * @param $contents - */ - function getBuyDate($contents) { - - } - - /** - * Get start date of warranty - * - * @param $contents - * @return mixed - */ - function getStartDate($contents) { - return false; - } - - /** - * Get expiration date of warranty - * - * @param $contents - */ - function getExpirationDate($contents) { - - } - - /** - * Get warranty info - * - * @param $contents - */ - function getWarrantyInfo($contents) { - - } - - /** - * Summary of getToken - * @param $config - * @return mixed - */ - static function getToken($config) { - return false; - } - - - /** - * Summary of getWarrantyUrl - * @param $config - * @param $compSerial - * @return string[]|boolean - */ - static function getWarrantyUrl($config, $compSerial) { - return false; - } - +class PluginManufacturersimportsManufacturer extends CommonDBTM +{ + /** + * @param $ID + * @param $sel + * @param bool $otherSerial + * @return string + */ + public function showCheckbox($ID, $sel, $otherSerial = false) + { + $name = "item[" . $ID . "]"; + return Html::getCheckbox(["name" => $name, "value" => 1, "selected" => $sel]); + } + + /** + * @param $output_type + * @param $header_num + * @return bool + */ + public function showItemTitle($output_type, $header_num) + { + return false; + } + + /** + * @param $output_type + * @param bool $otherSerial + * @param $item_num + * @param $row_num + * @return bool + */ + public function showItem($output_type, $item_num, $row_num, $otherSerial = false) + { + return false; + } + + /** + * @param $output_type + * @param $header_num + * @return bool + */ + public function showDocTitle($output_type, $header_num) + { + return false; + } + + /** + * @param $output_type + * @param $item_num + * @param $row_num + * @param null $doc + * @return string + */ + public function showDocItem($output_type, $item_num, $row_num, $documents_id = null) + { + $doc = new document(); + if ($doc->getFromDB($documents_id)) { + return Search::showItem( + $output_type, + $doc->getDownloadLink(), + $item_num, + $row_num + ); + } + return Search::showItem($output_type, "", $item_num, $row_num); + } + + /** + * + * @param type $ID + * @param type $supplierWarranty + */ + public function showWarrantyItem($ID, $supplierWarranty) + { + echo "".__('Automatic'); + $name = "to_warranty_duration".$ID; + echo Html::hidden($name, ['value' => 0]); + echo ""; + } + + /** + * Get supplier information with url + * + * @param null $compSerial + * @param null $otherserial + * @param null $key + * @param null $supplierUrl + * @return mixed + */ + public function getSupplierInfo( + $compSerial = null, + $otherSerial = null, + $key = null, + $apisecret = null, + $supplierUrl = null + ) + { + } + + /** + * Get buy date of object + * + * @param $contents + */ + public function getBuyDate($contents) + { + } + + /** + * Get start date of warranty + * + * @param $contents + * @return mixed + */ + public function getStartDate($contents) + { + return false; + } + + /** + * Get expiration date of warranty + * + * @param $contents + */ + public function getExpirationDate($contents) + { + } + + /** + * Get warranty info + * + * @param $contents + */ + public function getWarrantyInfo($contents) + { + } + + /** + * Summary of getToken + * @param $config + * @return mixed + */ + public static function getToken($config) + { + return false; + } + + + /** + * Summary of getWarrantyUrl + * @param $config + * @param $compSerial + * @return string[]|boolean + */ + public static function getWarrantyUrl($config, $compSerial) + { + return false; + } } diff --git a/inc/model.class.php b/inc/model.class.php index 20506a4..85443f3 100644 --- a/inc/model.class.php +++ b/inc/model.class.php @@ -28,199 +28,216 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } /** * Class PluginManufacturersimportsModel */ -class PluginManufacturersimportsModel extends CommonDBTM { - - static $rightname = "plugin_manufacturersimports"; - - /** - * Return the localized name of the current Type - * Should be overloaded in each new class - * - * @return string - **/ - static function getTypeName($nb = 0) { - return _n('Suppliers import', 'Suppliers imports', - $nb, 'manufacturersimports'); - } - - - /** - * @param $items_id - * @param $itemtype - * - * @return bool - */ - function getFromDBbyDevice($items_id, $itemtype) { - global $DB; - - $query = "SELECT * FROM `" . $this->getTable() . "` " . - "WHERE `items_id` = '" . $items_id . "' +class PluginManufacturersimportsModel extends CommonDBTM +{ + public static $rightname = "plugin_manufacturersimports"; + + /** + * Return the localized name of the current Type + * Should be overloaded in each new class + * + * @return string + **/ + public static function getTypeName($nb = 0) + { + return _n( + 'Suppliers import', + 'Suppliers imports', + $nb, + 'manufacturersimports' + ); + } + + + /** + * @param $items_id + * @param $itemtype + * + * @return bool + */ + public function getFromDBbyDevice($items_id, $itemtype) + { + global $DB; + + $query = "SELECT * FROM `" . $this->getTable() . "` " . + "WHERE `items_id` = '" . $items_id . "' AND `itemtype` = '" . $itemtype . "' "; - if ($result = $DB->query($query)) { - if ($DB->numrows($result) != 1) { - return false; - } - $this->fields = $DB->fetchAssoc($result); - if (is_array($this->fields) && count($this->fields)) { - return true; - } else { + if ($result = $DB->query($query)) { + if ($DB->numrows($result) != 1) { + return false; + } + $this->fields = $DB->fetchAssoc($result); + if (is_array($this->fields) && count($this->fields)) { + return true; + } else { + return false; + } + } + return false; + } + + /** + * @param $itemtype + * @param $items_id + * + * @return bool + */ + public function checkIfModelNeeds($itemtype, $items_id) + { + if ($this->getFromDBbyDevice($items_id, $itemtype)) { + return $this->fields["model_name"]; + } else { return false; - } - } - return false; - } - - /** - * @param $itemtype - * @param $items_id - * - * @return bool - */ - function checkIfModelNeeds($itemtype, $items_id) { - if ($this->getFromDBbyDevice($items_id, $itemtype)) { - return $this->fields["model_name"]; - } else { - return false; - } - } - - /** - * @param $values - * - * @return bool - */ - function addModel($values) { - $tmp['model_name'] = $values['model_name']; - $tmp['itemtype'] = $values['itemtype']; - $tmp['items_id'] = $values['items_id']; - if ($this->getFromDBbyDevice($values['items_id'], - $values['itemtype'])) { - $tmp['id'] = $this->getID(); - $this->update($tmp); - } else { - $this->add($tmp); - } - return true; - } - - /** - * Prints the model add form (into devices) - * - * @param $device the device ID - * @param $type the device type - * - * @return nothing (print out a table) - * - */ - static function showModelForm($itemtype, $items_id) { - global $DB; - - $canedit = Session::haveRight(static::$rightname, UPDATE); - - $query = "SELECT * + } + } + + /** + * @param $values + * + * @return bool + */ + public function addModel($values) + { + $tmp['model_name'] = $values['model_name']; + $tmp['itemtype'] = $values['itemtype']; + $tmp['items_id'] = $values['items_id']; + if ($this->getFromDBbyDevice( + $values['items_id'], + $values['itemtype'] + )) { + $tmp['id'] = $this->getID(); + $this->update($tmp); + } else { + $this->add($tmp); + } + return true; + } + + /** + * Prints the model add form (into devices) + * + * @param $device the device ID + * @param $type the device type + * + * @return nothing (print out a table) + * + */ + public static function showModelForm($itemtype, $items_id) + { + global $DB; + + $canedit = Session::haveRight(static::$rightname, UPDATE); + + $query = "SELECT * FROM `glpi_plugin_manufacturersimports_models` WHERE `itemtype` = '" . $itemtype . "' AND `items_id` = '" . $items_id . "'"; - $result = $DB->query($query); - $number = $DB->numrows($result); - - $config_url = PluginManufacturersimportsConfig::getFormUrl(true); - echo "
"; - echo "
"; - echo ""; - echo ""; - echo ""; - echo ""; - - if ($number == 1) { - while ($line = $DB->fetchArray($result)) { - $ID = $line["id"]; - echo ""; - echo ""; + $result = $DB->query($query); + $number = $DB->numrows($result); + + $config_url = PluginManufacturersimportsConfig::getFormUrl(true); + echo ""; + echo "
" . PluginManufacturersimportsPreImport::getTypeName(2) . "" . __('Model Number', 'manufacturersimports') . "
"; - echo Html::input('model_name', ['value' => $line["model_name"], 'size' => 30]); - echo "
"; + echo ""; + echo ""; + echo ""; + echo ""; + + if ($number == 1) { + while ($line = $DB->fetchArray($result)) { + $ID = $line["id"]; + echo ""; + echo ""; // if ($canedit) { - echo ""; + echo ""; // } else { // echo ""; // } - echo ""; - } - } else if ($canedit) { - echo ""; - echo ""; - echo ""; - } - - echo "
" . PluginManufacturersimportsPreImport::getTypeName(2) . "" . __('Model Number', 'manufacturersimports') . "
"; + echo Html::input('model_name', ['value' => $line["model_name"], 'size' => 30]); + echo ""; - if ($ID > 0) { - Html::showSimpleForm($config_url, 'delete_model', - _x('button', 'Delete permanently'), - ['id' => $ID]); - } - echo ""; + if ($ID > 0) { + Html::showSimpleForm( + $config_url, + 'delete_model', + _x('button', 'Delete permanently'), + ['id' => $ID] + ); + } + echo ""; // echo "
"; - echo Html::input('model_name', ['size' => 30]); - echo Html::hidden('items_id', ['value' => $items_id]); - echo Html::hidden('itemtype', ['value' => $itemtype]); - echo ""; - echo Html::submit(_sx('button', 'Save'), ['name' => 'update_model', 'class' => 'btn btn-primary']); - echo "
"; - Html::closeForm(); - } - - /** - * Class-specific method used to show the fields to specify the massive action - * - * @param $ma the current massive action object - * - * @return false if parameters displayed ? - **@since version 0.85 - * - */ - static function showMassiveActionsSubForm(MassiveAction $ma) { - - switch ($ma->getAction()) { - case "add_model" : - echo " "; - echo Html::submit(_sx('button', 'Post'), ['name' => 'massiveaction', 'class' => 'btn btn-primary']); - return true; - break; - } - return parent::showMassiveActionsSubForm($ma); - } - - /** - * @since version 0.85 - * - * @see CommonDBTM::processMassiveActionsForOneItemtype() - **/ - static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, - array $ids) { - - switch ($ma->getAction()) { - case "add_model": - $model = new PluginManufacturersimportsModel(); - $input = $ma->getInput(); - foreach ($ma->items as $itemtype => $myitem) { - foreach ($myitem as $key => $value) { - $input = ['model_name' => $ma->POST['model_name'], - 'items_id' => $key, - 'itemtype' => $itemtype]; - if ($model->addModel($input)) { - $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_OK); - } else { - $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_KO); - } - } + echo ""; } - break; - } - } + } elseif ($canedit) { + echo ""; + echo ""; + echo Html::input('model_name', ['size' => 30]); + echo Html::hidden('items_id', ['value' => $items_id]); + echo Html::hidden('itemtype', ['value' => $itemtype]); + echo ""; + echo ""; + echo Html::submit(_sx('button', 'Save'), ['name' => 'update_model', 'class' => 'btn btn-primary']); + echo ""; + } + + echo "
"; + Html::closeForm(); + } + + /** + * Class-specific method used to show the fields to specify the massive action + * + * @param $ma the current massive action object + * + * @return false if parameters displayed ? + **@since version 0.85 + * + */ + public static function showMassiveActionsSubForm(MassiveAction $ma) + { + switch ($ma->getAction()) { + case "add_model": + echo " "; + echo Html::submit(_sx('button', 'Post'), ['name' => 'massiveaction', 'class' => 'btn btn-primary']); + return true; + break; + } + return parent::showMassiveActionsSubForm($ma); + } + + /** + * @since version 0.85 + * + * @see CommonDBTM::processMassiveActionsForOneItemtype() + **/ + public static function processMassiveActionsForOneItemtype( + MassiveAction $ma, + CommonDBTM $item, + array $ids + ) + { + switch ($ma->getAction()) { + case "add_model": + $model = new PluginManufacturersimportsModel(); + $input = $ma->getInput(); + foreach ($ma->items as $itemtype => $myitem) { + foreach ($myitem as $key => $value) { + $input = ['model_name' => $ma->POST['model_name'], + 'items_id' => $key, + 'itemtype' => $itemtype]; + if ($model->addModel($input)) { + $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_OK); + } else { + $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_KO); + } + } + } + break; + } + } } diff --git a/inc/postimport.class.php b/inc/postimport.class.php index 35c6b1d..85f57c8 100644 --- a/inc/postimport.class.php +++ b/inc/postimport.class.php @@ -28,300 +28,307 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } /** * Class PluginManufacturersimportsPostImport */ -class PluginManufacturersimportsPostImport extends CommonDBTM { - - /** - * @param $field - * @param bool $reverse - * - * @return string - */ - static function checkDate($field, $reverse = false) { - - // Date is already "reformat" according to getDateFormat() - - if ($reverse) { - $pattern = "/^([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})"; - $pattern .= "([0-5][0-9]:[0-5]?[0-9]:[_][01][0-9]|2[0-3])?/"; - } else { - $pattern = "/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})"; - $pattern .= "([_][01][0-9]|2[0-3]:[0-5][0-9]:[0-5]?[0-9])?/"; - } - preg_match($pattern, $field, $regs); - if (empty($regs)) { - return "0000-00-00"; - } - return $field; - } - - - /** - * @param $options - * - * @return mixed|string - */ - static function cURLData($options) { - global $CFG_GLPI; - - if (!function_exists('curl_init')) { - return __('Curl PHP package not installed', 'manufacturersimports') . "\n"; - } - $data = ''; - $timeout = 30; - $proxy_host = !empty($CFG_GLPI["proxy_name"]) ? ($CFG_GLPI["proxy_name"] . ":" . $CFG_GLPI["proxy_port"]) : false; // host:port - $proxy_ident = !empty($CFG_GLPI["proxy_user"]) ? ($CFG_GLPI["proxy_user"] . ":" . - (new GLPIKey())->decrypt($CFG_GLPI["proxy_passwd"])) : false; // username:password - - $url = $options["url"]; - - $ch = curl_init(); - - curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); - curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); - - if (preg_match('`^https://`i', $options["url"])) { - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); - } - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_HEADER, 0); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile"); - curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile"); // SAME cookiefile - - if (!empty($options['token'])) { - curl_setopt($ch, CURLOPT_HTTPHEADER, [ - "Authorization: Bearer " . $options['token'] - ]); - } - - if (!empty($options['ClientID'])) { - curl_setopt($ch, CURLOPT_HTTPHEADER, [ - "ClientID: " . $options['ClientID'] - ]); - } - - //Do we have post field to send? - if (!empty($options["post"])) { - //curl_setopt($ch, CURLOPT_POST,true); - $post = ''; - foreach ($options['post'] as $key => $value) { - $post .= $key . '=' . $value . '&'; - } - $post = rtrim($post, '&'); - curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type:application/x-www-form-urlencoded"]); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTREDIR, 2); - curl_setopt($ch, CURLOPT_POSTFIELDS, $post); - - // ADDED FOR HP curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json')); - if ($options['suppliername'] == PluginManufacturersimportsConfig::HP) { - - if (isset($options['access_token'])) { - curl_setopt($ch, CURLOPT_HTTPHEADER, [ - 'accept: application/json', - 'Content-Type: application/json', - 'Authorization: Bearer ' . $options['access_token'] - ]); - - curl_setopt($ch, CURLOPT_POSTFIELDS, "[" . json_encode($options['post']) . "]"); - - } else { - curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']); +class PluginManufacturersimportsPostImport extends CommonDBTM +{ + /** + * @param $field + * @param bool $reverse + * + * @return string + */ + public static function checkDate($field, $reverse = false) + { + // Date is already "reformat" according to getDateFormat() + + if ($reverse) { + $pattern = "/^([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})"; + $pattern .= "([0-5][0-9]:[0-5]?[0-9]:[_][01][0-9]|2[0-3])?/"; + } else { + $pattern = "/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})"; + $pattern .= "([_][01][0-9]|2[0-3]:[0-5][0-9]:[0-5]?[0-9])?/"; + } + preg_match($pattern, $field, $regs); + if (empty($regs)) { + return "0000-00-00"; + } + return $field; + } + + + /** + * @param $options + * + * @return mixed|string + */ + public static function cURLData($options) + { + global $CFG_GLPI; + + if (!function_exists('curl_init')) { + return __('Curl PHP package not installed', 'manufacturersimports') . "\n"; + } + $data = ''; + $timeout = 30; + $proxy_host = !empty($CFG_GLPI["proxy_name"]) ? ($CFG_GLPI["proxy_name"] . ":" . $CFG_GLPI["proxy_port"]) : false; // host:port + $proxy_ident = !empty($CFG_GLPI["proxy_user"]) ? ($CFG_GLPI["proxy_user"] . ":" . + (new GLPIKey())->decrypt($CFG_GLPI["proxy_passwd"])) : false; // username:password + + $url = $options["url"]; + + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); + + if (preg_match('`^https://`i', $options["url"])) { + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); + } + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_COOKIESESSION, true); + curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile"); + curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile"); // SAME cookiefile + + if (!empty($options['token'])) { + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "Authorization: Bearer " . $options['token'] + ]); + } + + if (!empty($options['ClientID'])) { + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + "ClientID: " . $options['ClientID'] + ]); + } + + //Do we have post field to send? + if (!empty($options["post"])) { + //curl_setopt($ch, CURLOPT_POST,true); + $post = ''; + foreach ($options['post'] as $key => $value) { + $post .= $key . '=' . $value . '&'; + } + $post = rtrim($post, '&'); + curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type:application/x-www-form-urlencoded"]); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTREDIR, 2); + curl_setopt($ch, CURLOPT_POSTFIELDS, $post); + + // ADDED FOR HP curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json')); +// if ($options['suppliername'] == PluginManufacturersimportsConfig::HP) { +// if (isset($options['access_token'])) { +// curl_setopt($ch, CURLOPT_HTTPHEADER, [ +// 'accept: application/json', +// 'Content-Type: application/json', +// 'Authorization: Bearer ' . $options['access_token'] +// ]); +// +// curl_setopt($ch, CURLOPT_POSTFIELDS, "[" . json_encode($options['post']) . "]"); +// } else { +// curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: application/json']); +// } +// } + } + + if (!$options["download"]) { + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); + } + + // Activation de l'utilisation d'un serveur proxy + if (!empty($CFG_GLPI["proxy_name"])) { + // Définition de l'adresse du proxy + curl_setopt($ch, CURLOPT_PROXY, $proxy_host); + + // Définition des identifiants si le proxy requiert une identification + if ($proxy_ident) { + curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_ident); } - } - } - - if (!$options["download"]) { - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); - } - - // Activation de l'utilisation d'un serveur proxy - if (!empty($CFG_GLPI["proxy_name"])) { - // Définition de l'adresse du proxy - curl_setopt($ch, CURLOPT_PROXY, $proxy_host); - - // Définition des identifiants si le proxy requiert une identification - if ($proxy_ident) { - curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_ident); - } - } - if ( - isset($_SESSION['glpi_use_mode']) - && ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE) - ) { - $info = curl_getinfo($ch); - Toolbox::logInfo($info); - } - if ($options["download"]) { - $fp = fopen($options["file"], "w"); - curl_setopt($ch, CURLOPT_FILE, $fp); - curl_exec($ch); - } else { - $data = curl_exec($ch); - } - if ( - isset($_SESSION['glpi_use_mode']) - && ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE) - ) { - $errors = curl_error($ch); - $response = curl_getinfo($ch, CURLINFO_HTTP_CODE); - Toolbox::logInfo($errors); - Toolbox::logInfo($response); - } - if (!$options["download"] && !$data) { - $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); // make sure we closeany current curl sessions - //die($http_code.' Unable to connect to server. Please come back later.'); - } else { - curl_close($ch); - } - - if ($options["download"]) { - fclose($fp); - } - if (!$options["download"] && $data) { - return $data; - } - } - - /** - * @param $values - */ - static function massiveimport($values) { - - $config = new PluginManufacturersimportsConfig(); - $log = new PluginManufacturersimportsLog(); - - $_SESSION["glpi_plugin_manufacturersimports_total"] = 0; - - Html::createProgressBar(__('Launching of imports', 'manufacturersimports')); - - echo ""; - echo ""; - echo ""; - echo ""; - - $config->getFromDB($values["manufacturers_id"]); - $suppliername = $config->fields["name"]; - - echo ""; - echo ""; - echo ""; - echo ""; - - $pos = 0; - foreach ($values["item"] as $key => $val) { - if ($val == 1) { - $NotAlreadyImported = $log->checkIfAlreadyImported($values["itemtype"], $key); - if (!$NotAlreadyImported) { - self::seePostImport($values["itemtype"], - $key, - $values["to_suppliers_id$key"], - $values["to_warranty_duration$key"], - $values["manufacturers_id"]); + } + if ( + isset($_SESSION['glpi_use_mode']) + && ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE) + ) { + $info = curl_getinfo($ch); + Toolbox::logInfo($info); + } + if ($options["download"]) { + $fp = fopen($options["file"], "w"); + curl_setopt($ch, CURLOPT_FILE, $fp); + curl_exec($ch); + } else { + $data = curl_exec($ch); + } + if ( + isset($_SESSION['glpi_use_mode']) + && ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE) + ) { + $errors = curl_error($ch); + $response = curl_getinfo($ch, CURLINFO_HTTP_CODE); + Toolbox::logInfo($errors); + Toolbox::logInfo($response); + } + if (!$options["download"] && !$data) { + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); // make sure we closeany current curl sessions + //die($http_code.' Unable to connect to server. Please come back later.'); + } else { + curl_close($ch); + } + + if ($options["download"]) { + fclose($fp); + } + if (!$options["download"] && $data) { + return $data; + } + } + + /** + * @param $values + */ + public static function massiveimport($values) + { + $config = new PluginManufacturersimportsConfig(); + $log = new PluginManufacturersimportsLog(); + + $_SESSION["glpi_plugin_manufacturersimports_total"] = 0; + + Html::createProgressBar(__('Launching of imports', 'manufacturersimports')); + + echo "
" . __('Post import', 'manufacturersimports') . "
" . __('Name') . "" . __('Serial number') . "" . _n('Link', 'Links', 1) . "" . __('Result', 'manufacturersimports') . "" . __('Details', 'manufacturersimports') . "
"; + echo ""; + echo ""; + echo ""; + + $config->getFromDB($values["manufacturers_id"]); + $suppliername = $config->fields["name"]; + + echo ""; + echo ""; + echo ""; + echo ""; + + $pos = 0; + foreach ($values["item"] as $key => $val) { + if ($val == 1) { + $NotAlreadyImported = $log->checkIfAlreadyImported($values["itemtype"], $key); + if (!$NotAlreadyImported) { + self::seePostImport( + $values["itemtype"], + $key, + $values["to_suppliers_id$key"], + $values["to_warranty_duration$key"], + $values["manufacturers_id"] + ); + } } - } - $pos += 1; - Html::changeProgressBarPosition($pos, - count($values["item"]), - __('Import in progress', 'manufacturersimports') . " (" . $values['itemtype']::getTypeName(2) . " : " . $suppliername . ")"); - } - Html::changeProgressBarPosition($pos, - count($values["item"]), - __('Done')); - - echo ""; - echo "
" . __('Post import', 'manufacturersimports') . "
" . __('Name') . "" . __('Serial number') . "" . _n('Link', 'Links', 1) . "" . __('Result', 'manufacturersimports') . "" . __('Details', 'manufacturersimports') . "
"; - $total = $_SESSION["glpi_plugin_manufacturersimports_total"]; - echo sprintf(__('Total number of devices imported %s', 'manufacturersimports'), $total); - echo "
"; - echo "
"; - echo ""; - echo __('Back'); - echo ""; - echo "
"; - } - - /** - * Fonction to select the search field on the website of the supplier - * - * @param $suppliername the suppliername - * - * @return $field for date and warranty searching - * - */ - static function selectSupplierField($suppliername) { - - $field = ''; - if (!empty($suppliername)) { - $supplierclass = "PluginManufacturersimports" . $suppliername; - $supplier = new $supplierclass(); - $field = $supplier->getSearchField(); - } - - return $field; - } - - /** - * @param $suppliername - * @param $contents - * - * @return mixed - */ - static function importDate($suppliername, $contents) { - - $supplierclass = "PluginManufacturersimports" . $suppliername; - $supplier = new $supplierclass(); - $importDate = $supplier->getBuyDate($contents); - - return $importDate; - } - - /** - * @param $suppliername - * @param $contents - * - * @return mixed - */ - static function importStartDate($suppliername, $contents) { - - $supplierclass = "PluginManufacturersimports" . $suppliername; - $supplier = new $supplierclass(); - $importStartDate = $supplier->getStartDate($contents); - - return $importStartDate; - } - - /** - * @param $suppliername - * @param $contents - * - * @return mixed - */ - static function importWarrantyInfo($suppliername, $contents) { - $supplierclass = "PluginManufacturersimports" . $suppliername; - $supplier = new $supplierclass(); - $importWarrantyInfo = $supplier->getWarrantyInfo($contents); - - return $importWarrantyInfo; - } - - //static function importWarranty($suppliername, $maDate, $contents, $warranty) { - // if ($warranty==0) { + $pos += 1; + Html::changeProgressBarPosition( + $pos, + count($values["item"]), + __('Import in progress', 'manufacturersimports') . " (" . $values['itemtype']::getTypeName(2) . " : " . $suppliername . ")" + ); + } + Html::changeProgressBarPosition( + $pos, + count($values["item"]), + __('Done') + ); + + echo ""; + $total = $_SESSION["glpi_plugin_manufacturersimports_total"]; + echo sprintf(__('Total number of devices imported %s', 'manufacturersimports'), $total); + echo ""; + echo ""; + echo "
"; + echo ""; + echo __('Back'); + echo ""; + echo "
"; + } + + /** + * Fonction to select the search field on the website of the supplier + * + * @param $suppliername the suppliername + * + * @return $field for date and warranty searching + * + */ + public static function selectSupplierField($suppliername) + { + $field = ''; + if (!empty($suppliername)) { + $supplierclass = "PluginManufacturersimports" . $suppliername; + $supplier = new $supplierclass(); + $field = $supplier->getSearchField(); + } + + return $field; + } + + /** + * @param $suppliername + * @param $contents + * + * @return mixed + */ + public static function importDate($suppliername, $contents) + { + $supplierclass = "PluginManufacturersimports" . $suppliername; + $supplier = new $supplierclass(); + $importDate = $supplier->getBuyDate($contents); + + return $importDate; + } + + /** + * @param $suppliername + * @param $contents + * + * @return mixed + */ + public static function importStartDate($suppliername, $contents) + { + $supplierclass = "PluginManufacturersimports" . $suppliername; + $supplier = new $supplierclass(); + $importStartDate = $supplier->getStartDate($contents); + + return $importStartDate; + } + + /** + * @param $suppliername + * @param $contents + * + * @return mixed + */ + public static function importWarrantyInfo($suppliername, $contents) + { + $supplierclass = "PluginManufacturersimports" . $suppliername; + $supplier = new $supplierclass(); + $importWarrantyInfo = $supplier->getWarrantyInfo($contents); + + return $importWarrantyInfo; + } + + //static function importWarranty($suppliername, $maDate, $contents, $warranty) { + // if ($warranty==0) { // if ($suppliername == PluginManufacturersimportsConfig::DELL) { // $maDateFin = PluginManufacturersimportsDellSoap::getDates($contents, "EndDate"); // } else { @@ -341,58 +348,59 @@ static function importWarrantyInfo($suppliername, $contents) { // } // $warranty += $mfin; // } - // } - // return $warranty; - //} - - /** - * @param $suppliername - * @param $contents - * - * @return mixed - */ - static function importDateFin($suppliername, $contents) { - - $supplierclass = "PluginManufacturersimports" . $suppliername; - $supplier = new $supplierclass(); - $maDateFin = $supplier->getExpirationDate($contents); - - return $maDateFin; - } - - /** - * Prints display post import - * - * @param $type the type of device - * @param $ID the ID of device - * @param $fromsupplier selection on pre import - * @param $fromwarranty selection on pre import - * @param $configID ID of supplier plugin config - * - * @return results of data import - * - */ - static function seePostImport($type, $ID, $fromsupplier, $fromwarranty, $configID) { - global $DB; - - $config = new PluginManufacturersimportsConfig(); - $config->getFromDB($configID); - $manufacturerId = $config->fields["manufacturers_id"]; - - if ($fromsupplier) { - $supplierId = $fromsupplier; - } else { - $supplierId = $config->fields["suppliers_id"]; - } - $suppliername = $config->fields["name"]; - $supplierUrl = $config->fields["supplier_url"]; - $supplierkey = $config->fields["supplier_key"]; - $supplierSecret = $config->fields["supplier_secret"]; - - $dbu = new DbUtils(); - $itemtable = $dbu->getTableForItemType($type); - - $query = "SELECT `" . $itemtable . "`.`id`, + // } + // return $warranty; + //} + + /** + * @param $suppliername + * @param $contents + * + * @return mixed + */ + public static function importDateFin($suppliername, $contents) + { + $supplierclass = "PluginManufacturersimports" . $suppliername; + $supplier = new $supplierclass(); + $maDateFin = $supplier->getExpirationDate($contents); + + return $maDateFin; + } + + /** + * Prints display post import + * + * @param $type the type of device + * @param $ID the ID of device + * @param $fromsupplier selection on pre import + * @param $fromwarranty selection on pre import + * @param $configID ID of supplier plugin config + * + * @return results of data import + * + */ + public static function seePostImport($type, $ID, $fromsupplier, $fromwarranty, $configID) + { + global $DB; + + $config = new PluginManufacturersimportsConfig(); + $config->getFromDB($configID); + $manufacturerId = $config->fields["manufacturers_id"]; + + if ($fromsupplier) { + $supplierId = $fromsupplier; + } else { + $supplierId = $config->fields["suppliers_id"]; + } + $suppliername = $config->fields["name"]; + $supplierUrl = $config->fields["supplier_url"]; + $supplierkey = $config->fields["supplier_key"]; + $supplierSecret = $config->fields["supplier_secret"]; + + $dbu = new DbUtils(); + $itemtable = $dbu->getTableForItemType($type); + + $query = "SELECT `" . $itemtable . "`.`id`, `" . $itemtable . "`.`name`, `" . $itemtable . "`.`entities_id`, `" . $itemtable . "`.`serial` @@ -403,449 +411,454 @@ static function seePostImport($type, $ID, $fromsupplier, $fromwarranty, $configI AND `glpi_manufacturers`.`id` = '" . $manufacturerId . "' AND `" . $itemtable . "`.`serial` != '' AND `" . $itemtable . "`.`id` = '" . $ID . "' "; - $query .= " ORDER BY `" . $itemtable . "`.`name`"; - $result = $DB->query($query); - - $supplierclass = "PluginManufacturersimports" . $suppliername; - $token = $supplierclass::getToken($config); - - while ($line = $DB->fetchArray($result)) { - - $compSerial = $line['serial']; - $ID = $line['id']; - echo ""; - $link = Toolbox::getItemTypeFormURL($type); - $dID = ""; - $model = new PluginManufacturersimportsModel(); - $otherSerial = $model->checkIfModelNeeds($type, $ID); - - if ($_SESSION["glpiis_ids_visible"] || empty($line["name"])) { - $dID .= " (" . $line["id"] . ")"; - } - echo "" . $line["name"] . $dID . "
" . $otherSerial . ""; - - $url = PluginManufacturersimportsPreImport::selectSupplier($suppliername, $supplierUrl, - $compSerial, $otherSerial, - $supplierkey, $supplierSecret); - $post = PluginManufacturersimportsPreImport::getSupplierPost($suppliername, $compSerial, - $otherSerial, $supplierkey, - $supplierSecret); - $warranty_url = $supplierclass::getWarrantyUrl($config, $compSerial); - - //On complete l url du support du fournisseur avec le serial - echo "" . $compSerial . ""; - echo ""; - echo "" . _n('Manufacturer', 'Manufacturers', 1) . ""; - echo ""; - - $options = ["url" => isset($warranty_url['url']) ? $warranty_url['url'] : $url, - "post" => $post, - "type" => $type, - "ID" => $ID, - "config" => $config, - "line" => $line, - "fromsupplier" => $fromsupplier, - "fromwarranty" => $fromwarranty, - "display" => true, - "token" => $token]; - - if ($suppliername == PluginManufacturersimportsConfig::HP) { - $options['url_warranty'] = PluginManufacturersimportsPreImport::selectSupplierWarranty( - $suppliername, $supplierUrl, - $compSerial, $otherSerial, - $supplierkey, $supplierSecret); - } - if ($suppliername == PluginManufacturersimportsConfig::LENOVO) { - $options['ClientID'] = $supplierkey; - } - - self::saveImport($options); - - echo "\n"; - } - } - - /** - * @param array $params - * - * @return bool - */ - static function saveImport($params = []) { - - $default_values = []; - $default_values['url'] = ""; - $default_values['url_warranty'] = ""; - $default_values['post'] = ""; - $default_values['display'] = false; - $default_values['type'] = ""; - $default_values['ID'] = 0; - $default_values['fromsupplier'] = 0; - $default_values['fromwarranty'] = 0; - $default_values['line'] = []; - $default_values['config'] = new PluginManufacturersimportsConfig(); - $default_values['token'] = false; - - $values = []; - foreach ($default_values as $key => $val) { - if (isset($params[$key])) { - $values[$key] = $params[$key]; - } else { - $values[$key] = $val; - } - } - - $config = $values['config']; - - if ($values['fromsupplier']) { - $supplierId = $values['fromsupplier']; - } else { - $supplierId = $config->fields["suppliers_id"]; - } - $suppliername = $config->fields["name"]; - $adddoc = $config->fields["document_adding"]; - $rubrique = $config->fields["documentcategories_id"]; - $addcomments = $config->fields["comment_adding"]; - $url_warranty = $config->fields["supplier_url"]; - $supplier_key = $config->fields["supplier_key"]; - - if (isset($params['fromwarranty']) && $params['fromwarranty']) { - $warranty = $values['fromwarranty']; - } else { - $warranty = $config->fields["warranty_duration"]; - } - - $contents = ""; - //$msgerr = ""; - - $options = ["url" => $values['url'], - "download" => false, - "file" => false, - "post" => $values['post'], - "suppliername" => $suppliername, - "token" => $values['token'] - ]; - - if ($suppliername == PluginManufacturersimportsConfig::LENOVO - && $supplier_key != null) { - - $options["ClientID"] = $supplier_key; - $contents = self::cURLData($options); - - } else if ($suppliername == PluginManufacturersimportsConfig::HP) { - $json = json_decode($contents); - if (isset($json->access_token)) { - $options['access_token'] = $json->access_token; - $options['url'] = $url_warranty; - $contents = self::cURLData($options); // Getting Warranty Data - } - } else { - $contents = self::cURLData($options); - } - if ( - isset($_SESSION['glpi_use_mode']) - && ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE) - ) { - Toolbox::loginfo($contents); - } - // On extrait la date de garantie de la variable contents. - $field = self::selectSupplierfield($suppliername); - - if ($field != false) { - $contents = stristr($contents, $field); - } - - if (!$contents === false) { - $maBuyDate = self::importDate($suppliername, $contents); - $maDate = self::importStartDate($suppliername, $contents); - $maDateFin = self::importDateFin($suppliername, $contents); - $warrantyinfo = self::importWarrantyInfo($suppliername, $contents); - - } - - if (isset($maDate) - && $maDate != "0000-00-00" - && $maDate != false - && isset($maDateFin) - && $maDateFin != "0000-00-00" - && $maDateFin != false) { - - list ($adebut, $mdebut, $jdebut) = explode('-', $maDate); - list ($afin, $mfin, $jfin) = explode('-', $maDateFin); - $warranty = 0; - $warranty = 12 - $mdebut; - for ($year = $adebut + 1; $year < $afin; $year++) { - $warranty += 12; - } - $warranty += $mfin; - } - - if (isset($maDate) - && $maDate != "0000-00-00" - && $maDate != false) { - //warranty for life - if ($warranty > 120) { - $warranty = -1; - } - - $date = date("Y-m-d"); - $options = ["itemtype" => $values['type'], - "ID" => $values['ID'], - "date" => $date, - "supplierId" => $supplierId, - "warranty" => $warranty, - "suppliername" => $suppliername, - "addcomments" => $addcomments, - "maDate" => $maDate, - "buyDate" => $maBuyDate, - "warranty_info" => $warrantyinfo]; - self::saveInfocoms($options, $values['display']); - - // on cree un doc dans GLPI qu'on va lier au materiel - if ($adddoc != 0 - && $suppliername != PluginManufacturersimportsConfig::DELL) { - $options = ["itemtype" => $values['type'], - "ID" => $values['ID'], - "url" => $values['url'], - "entities_id" => $values['line']["entities_id"], - "rubrique" => $rubrique, - "suppliername" => $suppliername]; - $values["documents_id"] = self::addDocument($options); - } - - //insert base locale - $values["import_status"] = 1; - $values["items_id"] = $values['ID']; - $values["itemtype"] = $values['type']; - $values["date_import"] = $date; - $log = new PluginManufacturersimportsLog(); - $log->add($values); - - // cleanup Log - $log_clean = new PluginManufacturersimportsLog(); - $log_clean->deleteByCriteria([ - 'items_id' => $values['ID'], - 'itemtype' => $values['type'], - 'import_status' => 2, - 'LIMIT' => 1 - ] - ); - if (isset($_SESSION["glpi_plugin_manufacturersimports_total"])) { - $_SESSION["glpi_plugin_manufacturersimports_total"] += 1; - } - - return true; - - } else { // Failed check contents - if ($values['display']) { - self::isInError($suppliername, $values['type'], $values['ID'], $contents); - } else { - self::isInError($suppliername, $values['type'], $values['ID'], null, $values['display']); - return false; - } - } - return false; - - } - - /** - * Adding infocoms date of purchase and warranty - * - * @param $options - * @param bool $display - */ - static function saveInfocoms($options, $display = false) { - - //Original values - $warranty_date = ""; - $buy_date = ""; - $warranty_duration = ""; - $warranty_info = ""; - $suppliers_id = ""; - $ic_comments = ""; - - //New values - $input_infocom = []; - if ($options["supplierId"] != 0) { - $input_infocom["suppliers_id"] = $options["supplierId"]; - } - $input_infocom["warranty_date"] = date("Y-m-d",strtotime($options["maDate"])); - $input_infocom["warranty_duration"] = $options["warranty"]; - $input_infocom["warranty_info"] = $options["warranty_info"]; - $input_infocom["buy_date"] = date("Y-m-d",strtotime($options["buyDate"])); - $input_infocom["items_id"] = $options["ID"]; - $input_infocom["itemtype"] = $options["itemtype"]; - - //add new infocoms - $ic = new Infocom(); - if ($ic->getfromDBforDevice($options["itemtype"], $options["ID"])) { - - //Original values - $warranty_date = Html::convdate($ic->fields["warranty_date"]); - $warranty_duration = $ic->fields["warranty_duration"]; - $warranty_info = $ic->fields["warranty_info"]; - $buy_date = $ic->fields["buy_date"]; - $suppliers_id = Dropdown::getDropdownName("glpi_suppliers", $ic->fields["suppliers_id"]); - $ic_comment = $ic->fields["comment"]; - - //New values - $input_infocom["id"] = $ic->fields["id"]; - - if ($options["addcomments"]) { - $input_infocom["comment"] = $ic_comment . "\n" . - __('Imported from web site', 'manufacturersimports') . " " . $options["suppliername"] . " " . - __('With the manufacturersimports plugin', 'manufacturersimports') . " (" . Html::convdate($options["date"]) . ")"; - } - $infocom = new Infocom(); - $infocom->update($input_infocom); - - } else { - - if ($options["addcomments"]) { - $input_infocom["comment"] = __('Imported from web site', 'manufacturersimports') . - " " . $options["suppliername"] . " " . __('With the manufacturersimports plugin', 'manufacturersimports') . - " (" . Html::convdate($options["date"]) . ")"; - } - $infocom = new Infocom(); - $infocom->add($input_infocom); - - } - - if ($display) { - //post message - echo ""; - echo __('Import OK', 'manufacturersimports') . " (" . Html::convdate($options["date"]) . ")"; - echo ""; - echo ""; - echo _n('Supplier', 'Suppliers', 1) . ": "; - echo $suppliers_id . "->" . Dropdown::getDropdownName("glpi_suppliers", $options["supplierId"]) . "
"; - echo __('Date of purchase') . ": "; - echo Html::convdate($buy_date) . "->" . Html::convdate($options["buyDate"]) . "
"; - echo __('Start date of warranty') . ": "; - echo $warranty_date . "->" . Html::convdate($options["maDate"]) . "
"; - if ($warranty_duration == -1) { - $warranty_duration = __('Lifelong'); - $warranty = __('Lifelong'); - } else { - $warranty = $options["warranty"]; - } - echo __('Warranty duration') . ": " . $warranty_duration . "->" . $warranty . "
"; - echo ""; - } - } - - /** - * @param $options - * - * @return int - */ - static function addDocument($options) { - - //configure adding doc - - $time_file = date("Y-m-d-H-i"); - $name = ""; - switch ($options["itemtype"]) { - case 'Computer': - $name = "computer"; - break; - case 'NetworkEquipment': - $name = "networking"; - break; - case 'Peripheral': - $name = "peripheral"; - break; - case 'Monitor': - $name = "monitor"; - break; - case 'Printer': - $name = "printer"; - break; - case 'PluginRacksRack': - $name = "rack"; - break; - } - - $filename = - "infocoms_" . $options["suppliername"] - . "_" . $name . "_" . $options["ID"] . ".html"; - - //on enregistre - $path = GLPI_DOC_DIR . "/_uploads/"; - $filepath = $path . $filename; - $datas = ["url" => $options["url"], - "download" => true, - "file" => $filepath, - "suppliername" => $options["suppliername"]]; - self::cURLData($datas); - - $doc = new document(); - - $input = []; - $input["entities_id"] = $options["entities_id"]; - $input["name"] = - addslashes("infocoms_" . $options["suppliername"] . "_" . $name . "_" . $options["ID"]); - $input["upload_file"] = $filename; - $input["documentcategories_id"] = $options["rubrique"]; - $input["mime"] = "text/html"; - $input["date_mod"] = date("Y-m-d H:i:s"); - $input["users_id"] = Session::getLoginUserID(); - - $newdoc = $doc->add($input); - $docitem = new Document_Item(); - $docitem->add(['documents_id' => $newdoc, - 'itemtype' => $options["itemtype"], - 'items_id' => $options["ID"], - 'entities_id' => $input["entities_id"]]); - - $temp = new PluginManufacturersimportsLog(); - $temp->deleteByCriteria(['itemtype' => $options["itemtype"], - 'items_id' => $options["ID"]]); - - return $newdoc; - } - - - /** - * @param $type - * @param $ID - * @param null $contents - */ - static function isInError($suppliername, $type, $ID, $contents = null, $display = true) { - - $msgerr = ""; - $date = date("Y-m-d"); - if ($display) { - echo ""; - echo ""; - echo __('Import failed', 'manufacturersimports') . " ("; - echo Html::convdate($date) . ")"; - } - - $temp = new PluginManufacturersimportsLog(); - $temp->deleteByCriteria(['itemtype' => $type, - 'items_id' => $ID]); - - //insert base locale - $values["import_status"] = 2; - $values["items_id"] = $ID; - $values["itemtype"] = $type; - $values["date_import"] = $date; - $log = new PluginManufacturersimportsLog(); - $log->add($values); - - if ($display) { - if (!empty($contents)) { - switch ($suppliername) { - case PluginManufacturersimportsConfig::HP : - case PluginManufacturersimportsConfig::LENOVO : - $msgerr = self::importWarrantyInfo($suppliername, $contents); - break; - default: - $msgerr = __('Connection failed/data download from manufacturer web site', 'manufacturersimports'); + $query .= " ORDER BY `" . $itemtable . "`.`name`"; + $result = $DB->query($query); + + $supplierclass = "PluginManufacturersimports" . $suppliername; + $token = $supplierclass::getToken($config); + + while ($line = $DB->fetchArray($result)) { + $compSerial = $line['serial']; + $ID = $line['id']; + echo ""; + $link = Toolbox::getItemTypeFormURL($type); + $dID = ""; + $model = new PluginManufacturersimportsModel(); + $otherSerial = $model->checkIfModelNeeds($type, $ID); + + if ($_SESSION["glpiis_ids_visible"] || empty($line["name"])) { + $dID .= " (" . $line["id"] . ")"; + } + echo "" . $line["name"] . $dID . "
" . $otherSerial . ""; + + $url = PluginManufacturersimportsPreImport::selectSupplier( + $suppliername, + $supplierUrl, + $compSerial, + $otherSerial, + $supplierkey, + $supplierSecret + ); + $post = PluginManufacturersimportsPreImport::getSupplierPost( + $suppliername, + $compSerial, + $otherSerial, + $supplierkey, + $supplierSecret + ); + $warranty_url = $supplierclass::getWarrantyUrl($config, $compSerial); + + //On complete l url du support du fournisseur avec le serial + echo "" . $compSerial . ""; + echo ""; + echo "" . _n('Manufacturer', 'Manufacturers', 1) . ""; + echo ""; + + $options = ["url" => isset($warranty_url['url']) ? $warranty_url['url'] : $url, + "post" => $post, + "type" => $type, + "ID" => $ID, + "config" => $config, + "line" => $line, + "fromsupplier" => $fromsupplier, + "fromwarranty" => $fromwarranty, + "display" => true, + "token" => $token]; + +// if ($suppliername == PluginManufacturersimportsConfig::HP) { +// $options['url_warranty'] = PluginManufacturersimportsPreImport::selectSupplierWarranty( +// $suppliername, +// $supplierUrl, +// $compSerial, +// $otherSerial, +// $supplierkey, +// $supplierSecret +// ); +// } + if ($suppliername == PluginManufacturersimportsConfig::LENOVO) { + $options['ClientID'] = $supplierkey; + } + + self::saveImport($options); + + echo "\n"; + } + } + + /** + * @param array $params + * + * @return bool + */ + public static function saveImport($params = []) + { + $default_values = []; + $default_values['url'] = ""; + $default_values['url_warranty'] = ""; + $default_values['post'] = ""; + $default_values['display'] = false; + $default_values['type'] = ""; + $default_values['ID'] = 0; + $default_values['fromsupplier'] = 0; + $default_values['fromwarranty'] = 0; + $default_values['line'] = []; + $default_values['config'] = new PluginManufacturersimportsConfig(); + $default_values['token'] = false; + + $values = []; + foreach ($default_values as $key => $val) { + if (isset($params[$key])) { + $values[$key] = $params[$key]; + } else { + $values[$key] = $val; + } + } + + $config = $values['config']; + + if ($values['fromsupplier']) { + $supplierId = $values['fromsupplier']; + } else { + $supplierId = $config->fields["suppliers_id"]; + } + $suppliername = $config->fields["name"]; + $adddoc = $config->fields["document_adding"]; + $rubrique = $config->fields["documentcategories_id"]; + $addcomments = $config->fields["comment_adding"]; + $url_warranty = $config->fields["supplier_url"]; + $supplier_key = $config->fields["supplier_key"]; + + if (isset($params['fromwarranty']) && $params['fromwarranty']) { + $warranty = $values['fromwarranty']; + } else { + $warranty = $config->fields["warranty_duration"]; + } + + $contents = ""; + //$msgerr = ""; + + $options = ["url" => $values['url'], + "download" => false, + "file" => false, + "post" => $values['post'], + "suppliername" => $suppliername, + "token" => $values['token'] + ]; + + if ($suppliername == PluginManufacturersimportsConfig::LENOVO + && $supplier_key != null) { + $options["ClientID"] = $supplier_key; + $contents = self::cURLData($options); + } +// elseif ($suppliername == PluginManufacturersimportsConfig::HP) { +// $json = json_decode($contents); +// if (isset($json->access_token)) { +// $options['access_token'] = $json->access_token; +// $options['url'] = $url_warranty; +// $contents = self::cURLData($options); // Getting Warranty Data +// } +// } + else { + $contents = self::cURLData($options); + } + if ( + isset($_SESSION['glpi_use_mode']) + && ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE) + ) { + Toolbox::loginfo($contents); + } + // On extrait la date de garantie de la variable contents. + $field = self::selectSupplierfield($suppliername); + + if ($field != false) { + $contents = stristr($contents, $field); + } + + if (!$contents === false) { + $maBuyDate = self::importDate($suppliername, $contents); + $maDate = self::importStartDate($suppliername, $contents); + $maDateFin = self::importDateFin($suppliername, $contents); + $warrantyinfo = self::importWarrantyInfo($suppliername, $contents); + } + + if (isset($maDate) + && $maDate != "0000-00-00" + && $maDate != false + && isset($maDateFin) + && $maDateFin != "0000-00-00" + && $maDateFin != false) { + list($adebut, $mdebut, $jdebut) = explode('-', $maDate); + list($afin, $mfin, $jfin) = explode('-', $maDateFin); + $warranty = 0; + $warranty = 12 - $mdebut; + for ($year = $adebut + 1; $year < $afin; $year++) { + $warranty += 12; + } + $warranty += $mfin; + } + + if (isset($maDate) + && $maDate != "0000-00-00" + && $maDate != false) { + //warranty for life + if ($warranty > 120) { + $warranty = -1; + } + + $date = date("Y-m-d"); + $options = ["itemtype" => $values['type'], + "ID" => $values['ID'], + "date" => $date, + "supplierId" => $supplierId, + "warranty" => $warranty, + "suppliername" => $suppliername, + "addcomments" => $addcomments, + "maDate" => $maDate, + "buyDate" => $maBuyDate, + "warranty_info" => $warrantyinfo]; + self::saveInfocoms($options, $values['display']); + + // on cree un doc dans GLPI qu'on va lier au materiel + if ($adddoc != 0 + && $suppliername != PluginManufacturersimportsConfig::DELL) { + $options = ["itemtype" => $values['type'], + "ID" => $values['ID'], + "url" => $values['url'], + "entities_id" => $values['line']["entities_id"], + "rubrique" => $rubrique, + "suppliername" => $suppliername]; + $values["documents_id"] = self::addDocument($options); } - } - echo "$msgerr"; - } - } + //insert base locale + $values["import_status"] = 1; + $values["items_id"] = $values['ID']; + $values["itemtype"] = $values['type']; + $values["date_import"] = $date; + $log = new PluginManufacturersimportsLog(); + $log->add($values); + + // cleanup Log + $log_clean = new PluginManufacturersimportsLog(); + $log_clean->deleteByCriteria( + [ + 'items_id' => $values['ID'], + 'itemtype' => $values['type'], + 'import_status' => 2, + 'LIMIT' => 1 + ] + ); + if (isset($_SESSION["glpi_plugin_manufacturersimports_total"])) { + $_SESSION["glpi_plugin_manufacturersimports_total"] += 1; + } + + return true; + } else { // Failed check contents + if ($values['display']) { + self::isInError($suppliername, $values['type'], $values['ID'], $contents); + } else { + self::isInError($suppliername, $values['type'], $values['ID'], null, $values['display']); + return false; + } + } + return false; + } + + /** + * Adding infocoms date of purchase and warranty + * + * @param $options + * @param bool $display + */ + public static function saveInfocoms($options, $display = false) + { + //Original values + $warranty_date = ""; + $buy_date = ""; + $warranty_duration = ""; + $warranty_info = ""; + $suppliers_id = ""; + $ic_comments = ""; + + //New values + $input_infocom = []; + if ($options["supplierId"] != 0) { + $input_infocom["suppliers_id"] = $options["supplierId"]; + } + $input_infocom["warranty_date"] = date("Y-m-d", strtotime($options["maDate"])); + $input_infocom["warranty_duration"] = $options["warranty"]; + $input_infocom["warranty_info"] = $options["warranty_info"]; + $input_infocom["buy_date"] = date("Y-m-d", strtotime($options["buyDate"])); + $input_infocom["items_id"] = $options["ID"]; + $input_infocom["itemtype"] = $options["itemtype"]; + + //add new infocoms + $ic = new Infocom(); + if ($ic->getfromDBforDevice($options["itemtype"], $options["ID"])) { + //Original values + $warranty_date = Html::convdate($ic->fields["warranty_date"]); + $warranty_duration = $ic->fields["warranty_duration"]; + $warranty_info = $ic->fields["warranty_info"]; + $buy_date = $ic->fields["buy_date"]; + $suppliers_id = Dropdown::getDropdownName("glpi_suppliers", $ic->fields["suppliers_id"]); + $ic_comment = $ic->fields["comment"]; + + //New values + $input_infocom["id"] = $ic->fields["id"]; + + if ($options["addcomments"]) { + $input_infocom["comment"] = $ic_comment . "\n" . + __('Imported from web site', 'manufacturersimports') . " " . $options["suppliername"] . " " . + __('With the manufacturersimports plugin', 'manufacturersimports') . " (" . Html::convdate($options["date"]) . ")"; + } + $infocom = new Infocom(); + $infocom->update($input_infocom); + } else { + if ($options["addcomments"]) { + $input_infocom["comment"] = __('Imported from web site', 'manufacturersimports') . + " " . $options["suppliername"] . " " . __('With the manufacturersimports plugin', 'manufacturersimports') . + " (" . Html::convdate($options["date"]) . ")"; + } + $infocom = new Infocom(); + $infocom->add($input_infocom); + } + + if ($display) { + //post message + echo ""; + echo __('Import OK', 'manufacturersimports') . " (" . Html::convdate($options["date"]) . ")"; + echo ""; + echo ""; + echo _n('Supplier', 'Suppliers', 1) . ": "; + if ($options["supplierId"] != 0) { + echo $suppliers_id . "->" . Dropdown::getDropdownName("glpi_suppliers", $options["supplierId"]) . "
"; + } + echo __('Date of purchase') . ": "; + echo Html::convdate($buy_date) . "->" . Html::convdate($options["buyDate"]) . "
"; + echo __('Start date of warranty') . ": "; + echo $warranty_date . "->" . Html::convdate($options["maDate"]) . "
"; + if ($warranty_duration == -1) { + $warranty_duration = __('Lifelong'); + $warranty = __('Lifelong'); + } else { + $warranty = $options["warranty"]; + } + echo __('Warranty duration') . ": " . $warranty_duration . "->" . $warranty . "
"; + echo ""; + } + } + + /** + * @param $options + * + * @return int + */ + public static function addDocument($options) + { + //configure adding doc + + $time_file = date("Y-m-d-H-i"); + $name = ""; + switch ($options["itemtype"]) { + case 'Computer': + $name = "computer"; + break; + case 'NetworkEquipment': + $name = "networking"; + break; + case 'Peripheral': + $name = "peripheral"; + break; + case 'Monitor': + $name = "monitor"; + break; + case 'Printer': + $name = "printer"; + break; + case 'PluginRacksRack': + $name = "rack"; + break; + } + + $filename = + "infocoms_" . $options["suppliername"] + . "_" . $name . "_" . $options["ID"] . ".html"; + + //on enregistre + $path = GLPI_DOC_DIR . "/_uploads/"; + $filepath = $path . $filename; + $datas = ["url" => $options["url"], + "download" => true, + "file" => $filepath, + "suppliername" => $options["suppliername"]]; + self::cURLData($datas); + + $doc = new document(); + + $input = []; + $input["entities_id"] = $options["entities_id"]; + $input["name"] = + addslashes("infocoms_" . $options["suppliername"] . "_" . $name . "_" . $options["ID"]); + $input["upload_file"] = $filename; + $input["documentcategories_id"] = $options["rubrique"]; + $input["mime"] = "text/html"; + $input["date_mod"] = date("Y-m-d H:i:s"); + $input["users_id"] = Session::getLoginUserID(); + + $newdoc = $doc->add($input); + $docitem = new Document_Item(); + $docitem->add(['documents_id' => $newdoc, + 'itemtype' => $options["itemtype"], + 'items_id' => $options["ID"], + 'entities_id' => $input["entities_id"]]); + + $temp = new PluginManufacturersimportsLog(); + $temp->deleteByCriteria(['itemtype' => $options["itemtype"], + 'items_id' => $options["ID"]]); + + return $newdoc; + } + + + /** + * @param $type + * @param $ID + * @param null $contents + */ + public static function isInError($suppliername, $type, $ID, $contents = null, $display = true) + { + $msgerr = ""; + $date = date("Y-m-d"); + if ($display) { + echo ""; + echo ""; + echo __('Import failed', 'manufacturersimports') . " ("; + echo Html::convdate($date) . ")"; + } + + $temp = new PluginManufacturersimportsLog(); + $temp->deleteByCriteria(['itemtype' => $type, + 'items_id' => $ID]); + + //insert base locale + $values["import_status"] = 2; + $values["items_id"] = $ID; + $values["itemtype"] = $type; + $values["date_import"] = $date; + $log = new PluginManufacturersimportsLog(); + $log->add($values); + + if ($display) { + if (!empty($contents)) { + switch ($suppliername) { + case PluginManufacturersimportsConfig::LENOVO : + $msgerr = self::importWarrantyInfo($suppliername, $contents); + break; + default: + $msgerr = __('Connection failed/data download from manufacturer web site', 'manufacturersimports'); + } + } + echo "$msgerr"; + } + } } diff --git a/inc/preimport.class.php b/inc/preimport.class.php index 9a31ee0..0954580 100644 --- a/inc/preimport.class.php +++ b/inc/preimport.class.php @@ -27,456 +27,510 @@ -------------------------------------------------------------------------- */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } /** * Class PluginManufacturersimportsPreImport */ -class PluginManufacturersimportsPreImport extends CommonDBTM { - - static $rightname = "plugin_manufacturersimports"; - - const IMPORTED = 2; - const NOT_IMPORTED = 1; - - /** - * Return the localized name of the current Type - * Should be overloaded in each new class - * - * @return string - **/ - static function getTypeName($nb = 0) { - return _n('Suppliers import', 'Suppliers imports', $nb, 'manufacturersimports'); - } - - /** - * @param $myname - * @param int $value_type - * @param int $value - * @param int $entity_restrict - * @param string $types - */ - static function showAllItems($myname, $value_type = 0, $value = 0, $entity_restrict = -1, $types = '') { - - if (!is_array($types)) { - $types = PluginManufacturersimportsConfig::getTypes(); - } - - $rand = mt_rand(); - $options = []; - - foreach ($types as $type) { - $item = new $type(); - $options[$type] = $item::getTypeName(); - } - asort($options); - if (count($options)) { - $id = "item_type$rand"; - Dropdown::showFromArray($myname, $options, ['value' => $value, - 'id' => $id, - 'display_emptychoice' => true]); - } - } - - /** - * Fonction to use the supplier url - * - * @param $suppliername the suppliername - * @param $supplierUrl the supplierUrl (in plugin config) - * @param $compSerial the serial of the device - * @param $otherSerial the otherSerial (model) of the device - * @param null $supplierkey the supplierkey - * @param null $supplierSecret the supplierSecret - * - * @return $url of the supplier - * - */ - static function selectSupplier($suppliername, $supplierUrl, $compSerial, $otherserial = null, - $supplierkey = null, $supplierSecret = null, $second_url = false) { - $url = ""; - if (!empty($suppliername)) { - $supplierclass = "PluginManufacturersimports" . $suppliername; - $supplier = new $supplierclass(); - $infos = $supplier->getSupplierInfo($compSerial, $otherserial, $supplierkey, - $supplierSecret, $supplierUrl); - if (!$second_url) { - $url = $infos['url']; - } else { - $url = $infos['url_web']; - } - - } - return $url; - } - - /** - * Fonction to use the supplier url - * - * @param $suppliername the suppliername - * @param $supplierUrl the supplierUrl (in plugin config) - * @param $compSerial the serial of the device - * @param $otherSerial the otherSerial (model) of the device - * @param null $supplierkey the supplierkey - * @param null $supplierSecret the supplierSecret - * - * @return $url of the supplier - * - */ - static function selectSupplierWarranty($suppliername, $supplierUrl, $compSerial, $otherserial = null, - $supplierkey = null, $supplierSecret = null) { - $url_warranty = ""; - if (!empty($suppliername)) { - $supplierclass = "PluginManufacturersimports" . $suppliername; - $supplier = new $supplierclass(); - $infos = $supplier->getSupplierInfo($compSerial, $otherserial, $supplierkey, - $supplierSecret, $supplierUrl); - $url_warranty = $infos['url_warranty']; - } - return $url_warranty; - } - - /** - * @param $suppliername - * @param $supplierUrl - * @param $compSerial - * @param null $otherserial - * @param null $supplierkey - * - * @return string - */ - static function getMoreInfosSupplier($suppliername, $supplierUrl, $compSerial, $otherserial = null, $supplierkey = null) { - - $url = ""; - if (!empty($suppliername)) { - $supplierclass = "PluginManufacturersimports" . $suppliername; - $supplier = new $supplierclass(); - if (method_exists($supplier, "getSupplierMoreInfo")) { - $url = $supplier->getSupplierMoreInfo($compSerial, $otherserial, $supplierkey, $supplierUrl); - } - } - return $url; - } - - /** - * @param $suppliername - * @param $supplierUrl - * @param $compSerial - * @param null $otherserial - * @param null $supplierkey - * - * @return string - */ - static function getJSSupplier($suppliername, $supplierUrl, $compSerial, $otherserial = null, $supplierkey = null) { - - $js = ""; - if (!empty($suppliername)) { - $supplierclass = "PluginManufacturersimports" . $suppliername; - $supplier = new $supplierclass(); - if (method_exists($supplier, "getJSSupplier")) { - $js = $supplier->getJSSupplier($compSerial, $otherserial, $supplierkey, $supplierUrl); - } - } - return $js; - } - - /** - * @param $suppliername - * @param $compSerial - * @param null $otherserial - * - * @return string - */ - static function getSupplierPost($suppliername, $compSerial, $otherserial = null, - $supplierkey = null, $supplierSecret = null) { - - $post = ""; - if (!empty($suppliername)) { - $supplierclass = "PluginManufacturersimports" . $suppliername; - $supplier = new $supplierclass(); - $infos = $supplier->getSupplierInfo($compSerial, $otherserial, $supplierkey, $supplierSecret); - if (isset($infos['post'])) { - $post = $infos['post']; - } - } - return $post; - } - - /** - * @param $row_num - * @param $item_num - * @param $line - * @param $output_type - * @param $configID - * @param $status - * @param $imported - */ - static function showImport($row_num, $item_num, $line, $output_type, - $configID, $status, $imported) { - - $infocom = new Infocom(); - $canedit = Session::haveRight(static::$rightname, UPDATE) && $infocom->canUpdate(); - $config = new PluginManufacturersimportsConfig(); - $config->getFromDB($configID); - - $suppliername = $config->fields["name"]; - $supplierUrl = $config->fields["supplier_url"]; - $supplierId = $config->fields["suppliers_id"]; - $supplierWarranty = $config->fields["warranty_duration"]; - $supplierkey = $config->fields["supplier_key"]; - $supplierkeysecret = $config->fields["supplier_secret"]; - $supplierclass = "PluginManufacturersimports" . $suppliername; - $supplier = new $supplierclass(); - - $row_num++; - - if ($suppliername) { - - $model = new PluginManufacturersimportsModel(); - $otherSerial = $model->checkIfModelNeeds($line["itemtype"], $line["id"]); - - echo Search::showNewLine($output_type, $row_num % 2); - $ic = new Infocom; - $output_check = ""; - if ($canedit - && $output_type == Search::HTML_OUTPUT) { - $sel = ""; - if (isset($_GET["select"]) - && $_GET["select"] == "all") { - $sel = "checked"; - } - $output_check = $supplier->showCheckbox($line["id"], $sel, $otherSerial); - } - - echo Search::showItem($output_type, $output_check, $item_num, $row_num); - $link = Toolbox::getItemTypeFormURL($line["itemtype"]); - $ID = ""; - if ($_SESSION["glpiis_ids_visible"] - || empty($line["name"])) { - $ID .= " (" . $line["id"] . ")"; - } - $output_link = "" . - $line["name"] . $ID . "
" . $line["model_name"]; - echo Search::showItem($output_type, $output_link, $item_num, $row_num); - if (Session::isMultiEntitiesMode()) { - echo Search::showItem($output_type, - Dropdown::getDropdownName("glpi_entities", - $line['entities_id']), - $item_num, - $row_num); - } - - $url = self::selectSupplier($suppliername, $supplierUrl, $line["serial"], $otherSerial, - $supplierkey, $supplierkeysecret); - //serial - echo Search::showItem($output_type, $line["serial"], $item_num, $row_num); - //otherserial - echo Search::showItem($output_type, $otherSerial, $item_num, $row_num); - - //display infocoms - $output_ic = ""; - if ($ic->getfromDBforDevice($line["itemtype"], $line["id"])) { - $output_ic .= _n('Supplier', 'Suppliers', 1) . ":" . - Dropdown::getDropdownName("glpi_suppliers", $ic->fields["suppliers_id"]) . "
"; - $output_ic .= __('Date of purchase') . " : " . Html::convdate($ic->fields["buy_date"]) . "
"; - $output_ic .= __('Start date of warranty') . " : " . Html::convdate($ic->fields["warranty_date"]) . "
"; - if ($ic->fields["warranty_duration"] == -1) { - $output_ic .= __('Warranty duration') . " : " . __('Lifelong') . "
"; +class PluginManufacturersimportsPreImport extends CommonDBTM +{ + public static $rightname = "plugin_manufacturersimports"; + + const IMPORTED = 2; + const NOT_IMPORTED = 1; + + /** + * Return the localized name of the current Type + * Should be overloaded in each new class + * + * @return string + **/ + public static function getTypeName($nb = 0) + { + return _n('Suppliers import', 'Suppliers imports', $nb, 'manufacturersimports'); + } + + /** + * @param $myname + * @param int $value_type + * @param int $value + * @param int $entity_restrict + * @param string $types + */ + public static function showAllItems($myname, $value_type = 0, $value = 0, $entity_restrict = -1, $types = '') + { + if (!is_array($types)) { + $types = PluginManufacturersimportsConfig::getTypes(); + } + + $rand = mt_rand(); + $options = []; + + foreach ($types as $type) { + $item = new $type(); + $options[$type] = $item::getTypeName(); + } + asort($options); + if (count($options)) { + $id = "item_type$rand"; + Dropdown::showFromArray($myname, $options, ['value' => $value, + 'id' => $id, + 'display_emptychoice' => true]); + } + } + + /** + * Fonction to use the supplier url + * + * @param $suppliername the suppliername + * @param $supplierUrl the supplierUrl (in plugin config) + * @param $compSerial the serial of the device + * @param $otherSerial the otherSerial (model) of the device + * @param null $supplierkey the supplierkey + * @param null $supplierSecret the supplierSecret + * + * @return $url of the supplier + * + */ + public static function selectSupplier( + $suppliername, + $supplierUrl, + $compSerial, + $otherserial = null, + $supplierkey = null, + $supplierSecret = null, + $second_url = false + ) + { + $url = ""; + if (!empty($suppliername)) { + $supplierclass = "PluginManufacturersimports" . $suppliername; + $supplier = new $supplierclass(); + $infos = $supplier->getSupplierInfo( + $compSerial, + $otherserial, + $supplierkey, + $supplierSecret, + $supplierUrl + ); + if (!$second_url) { + $url = $infos['url']; } else { - $output_ic .= __('Warranty duration') . " : " . $ic->fields["warranty_duration"] . " " . __('month') . "
"; + $url = $infos['url_web']; } - $tmpdat = Infocom::getWarrantyExpir($ic->fields["warranty_date"], $ic->fields["warranty_duration"]); - $output_ic .= sprintf(__('Valid to %s'), $tmpdat); - } else { - $output_ic .= ""; - } - echo Search::showItem($output_type, $output_ic, $item_num, $row_num); - - if ($imported != self::IMPORTED) { - //display enterprise and warranty selection - echo ""; - if (Session::isMultiEntitiesMode() && $supplierId) { - $item = new Supplier(); - $item->getFromDB($supplierId); - if ($item->fields["is_recursive"] - || $item->fields["entities_id"] == $line['entities_id']) { - Dropdown::show('Supplier', ['name' => "to_suppliers_id" . $line["id"], - 'value' => $supplierId, - 'comments' => 0, - 'entity' => $line['entities_id']]); - } else { - echo ""; - echo __('The choosen supplier is not recursive', 'manufacturersimports') . ""; - $name = "to_suppliers_id" . $line["id"]; - echo Html::hidden($name, ['value' => -1]); - } - } else { - Dropdown::show('Supplier', ['name' => "to_suppliers_id" . $line["id"], - 'value' => $supplierId, - 'comments' => 0, - 'entity' => $line['entities_id']]); + } + return $url; + } + + /** + * Fonction to use the supplier url + * + * @param $suppliername the suppliername + * @param $supplierUrl the supplierUrl (in plugin config) + * @param $compSerial the serial of the device + * @param $otherSerial the otherSerial (model) of the device + * @param null $supplierkey the supplierkey + * @param null $supplierSecret the supplierSecret + * + * @return $url of the supplier + * + */ + public static function selectSupplierWarranty( + $suppliername, + $supplierUrl, + $compSerial, + $otherserial = null, + $supplierkey = null, + $supplierSecret = null + ) + { + $url_warranty = ""; + if (!empty($suppliername)) { + $supplierclass = "PluginManufacturersimports" . $suppliername; + $supplier = new $supplierclass(); + $infos = $supplier->getSupplierInfo( + $compSerial, + $otherserial, + $supplierkey, + $supplierSecret, + $supplierUrl + ); + $url_warranty = $infos['url_warranty']; + } + return $url_warranty; + } + + /** + * @param $suppliername + * @param $supplierUrl + * @param $compSerial + * @param null $otherserial + * @param null $supplierkey + * + * @return string + */ + public static function getMoreInfosSupplier($suppliername, $supplierUrl, $compSerial, $otherserial = null, $supplierkey = null) + { + $url = ""; + if (!empty($suppliername)) { + $supplierclass = "PluginManufacturersimports" . $suppliername; + $supplier = new $supplierclass(); + if (method_exists($supplier, "getSupplierMoreInfo")) { + $url = $supplier->getSupplierMoreInfo($compSerial, $otherserial, $supplierkey, $supplierUrl); + } + } + return $url; + } + + /** + * @param $suppliername + * @param $supplierUrl + * @param $compSerial + * @param null $otherserial + * @param null $supplierkey + * + * @return string + */ + public static function getJSSupplier($suppliername, $supplierUrl, $compSerial, $otherserial = null, $supplierkey = null) + { + $js = ""; + if (!empty($suppliername)) { + $supplierclass = "PluginManufacturersimports" . $suppliername; + $supplier = new $supplierclass(); + if (method_exists($supplier, "getJSSupplier")) { + $js = $supplier->getJSSupplier($compSerial, $otherserial, $supplierkey, $supplierUrl); + } + } + return $js; + } + + /** + * @param $suppliername + * @param $compSerial + * @param null $otherserial + * + * @return string + */ + public static function getSupplierPost( + $suppliername, + $compSerial, + $otherserial = null, + $supplierkey = null, + $supplierSecret = null + ) + { + $post = ""; + if (!empty($suppliername)) { + $supplierclass = "PluginManufacturersimports" . $suppliername; + $supplier = new $supplierclass(); + $infos = $supplier->getSupplierInfo($compSerial, $otherserial, $supplierkey, $supplierSecret); + if (isset($infos['post'])) { + $post = $infos['post']; + } + } + return $post; + } + + /** + * @param $row_num + * @param $item_num + * @param $line + * @param $output_type + * @param $configID + * @param $status + * @param $imported + */ + public static function showImport( + $row_num, + $item_num, + $line, + $output_type, + $configID, + $status, + $imported + ) + { + $infocom = new Infocom(); + $canedit = Session::haveRight(static::$rightname, UPDATE) && $infocom->canUpdate(); + $config = new PluginManufacturersimportsConfig(); + $config->getFromDB($configID); + + $suppliername = $config->fields["name"]; + $supplierUrl = $config->fields["supplier_url"]; + $supplierId = $config->fields["suppliers_id"]; + $supplierWarranty = $config->fields["warranty_duration"]; + $supplierkey = $config->fields["supplier_key"]; + $supplierkeysecret = $config->fields["supplier_secret"]; + $supplierclass = "PluginManufacturersimports" . $suppliername; + $supplier = new $supplierclass(); + + $row_num++; + + if ($suppliername) { + $model = new PluginManufacturersimportsModel(); + $otherSerial = $model->checkIfModelNeeds($line["itemtype"], $line["id"]); + + echo Search::showNewLine($output_type, $row_num % 2); + $ic = new Infocom; + $output_check = ""; + if ($canedit + && $output_type == Search::HTML_OUTPUT) { + $sel = ""; + if (isset($_GET["select"]) + && $_GET["select"] == "all") { + $sel = "checked"; + } + $output_check = $supplier->showCheckbox($line["id"], $sel, $otherSerial); } - echo ""; - $supplier->showWarrantyItem($line["id"], $supplierWarranty); + echo Search::showItem($output_type, $output_check, $item_num, $row_num); + $link = Toolbox::getItemTypeFormURL($line["itemtype"]); + $ID = ""; + if ($_SESSION["glpiis_ids_visible"] + || empty($line["name"])) { + $ID .= " (" . $line["id"] . ")"; + } + $output_link = "" . + $line["name"] . $ID . "
" . $line["model_name"]; + echo Search::showItem($output_type, $output_link, $item_num, $row_num); + if (Session::isMultiEntitiesMode()) { + echo Search::showItem( + $output_type, + Dropdown::getDropdownName( + "glpi_entities", + $line['entities_id'] + ), + $item_num, + $row_num + ); + } - } else { - //display enterprise and warranty selection - echo "" . Dropdown::getDropdownName("glpi_suppliers", - $ic->fields["suppliers_id"]) . ""; - if ($ic->fields["warranty_duration"] == -1) { - echo "" . __('Lifelong') . ""; + $url = self::selectSupplier( + $suppliername, + $supplierUrl, + $line["serial"], + $otherSerial, + $supplierkey, + $supplierkeysecret + ); + //serial + echo Search::showItem($output_type, $line["serial"], $item_num, $row_num); + //otherserial + echo Search::showItem($output_type, $otherSerial, $item_num, $row_num); + + //display infocoms + $output_ic = ""; + if ($ic->getfromDBforDevice($line["itemtype"], $line["id"])) { + $output_ic .= _n('Supplier', 'Suppliers', 1) . ":" . + Dropdown::getDropdownName("glpi_suppliers", $ic->fields["suppliers_id"]) . "
"; + $output_ic .= __('Date of purchase') . " : " . Html::convdate($ic->fields["buy_date"]) . "
"; + $output_ic .= __('Start date of warranty') . " : " . Html::convdate($ic->fields["warranty_date"]) . "
"; + if ($ic->fields["warranty_duration"] == -1) { + $output_ic .= __('Warranty duration') . " : " . __('Lifelong') . "
"; + } else { + $output_ic .= __('Warranty duration') . " : " . $ic->fields["warranty_duration"] . " " . __('month') . "
"; + } + $tmpdat = Infocom::getWarrantyExpir($ic->fields["warranty_date"], $ic->fields["warranty_duration"]); + $output_ic .= sprintf(__('Valid to %s'), $tmpdat); } else { - echo "" . $ic->fields["warranty_duration"] . ""; + $output_ic .= ""; + } + echo Search::showItem($output_type, $output_ic, $item_num, $row_num); + + if ($imported != self::IMPORTED) { + //display enterprise and warranty selection + echo ""; + if (Session::isMultiEntitiesMode() && $supplierId) { + $item = new Supplier(); + $item->getFromDB($supplierId); + if ($item->fields["is_recursive"] + || $item->fields["entities_id"] == $line['entities_id']) { + Dropdown::show('Supplier', ['name' => "to_suppliers_id" . $line["id"], + 'value' => $supplierId, + 'comments' => 0, + 'entity' => $line['entities_id']]); + } else { + echo ""; + echo __('The choosen supplier is not recursive', 'manufacturersimports') . ""; + $name = "to_suppliers_id" . $line["id"]; + echo Html::hidden($name, ['value' => -1]); + } + } else { + Dropdown::show('Supplier', ['name' => "to_suppliers_id" . $line["id"], + 'value' => $supplierId, + 'comments' => 0, + 'entity' => $line['entities_id']]); + } + echo ""; + + $supplier->showWarrantyItem($line["id"], $supplierWarranty); + } else { + //display enterprise and warranty selection + echo "" . Dropdown::getDropdownName( + "glpi_suppliers", + $ic->fields["suppliers_id"] + ) . ""; + if ($ic->fields["warranty_duration"] == -1) { + echo "" . __('Lifelong') . ""; + } else { + echo "" . $ic->fields["warranty_duration"] . ""; + } } - } - - //supplier url - //url to supplier - $output_url = "" . - __('Manufacturer information', 'manufacturersimports') . ""; + //supplier url + //url to supplier + $output_url = "" . + __('Manufacturer information', 'manufacturersimports') . ""; // if ($suppliername == PluginManufacturersimportsConfig::HP) { // $output_url = ""; // $output_url = "" . // __('Manufacturer information', 'manufacturersimports') . ""; // } - if ($suppliername == PluginManufacturersimportsConfig::LENOVO) { - $url = self::selectSupplier($suppliername, $supplierUrl, $line["serial"], $otherSerial, - $supplierkey, $supplierkeysecret, true); - $output_url = "" . - __('Manufacturer information', 'manufacturersimports') . ""; - - } - echo Search::showItem($output_type, $output_url, $item_num, $row_num); - - //status - if ($imported != self::IMPORTED) { - if ($status != 2) { - $output_doc = __('Not yet imported', 'manufacturersimports'); - } else { - $output_doc = "" . - __('Problem during the importation', 'manufacturersimports'); - if (!empty($data["date_import"])) { - $output_doc .= " (" . Html::convdate($data["date_import"]) . ")"; - } - $output_doc .= ""; + if ($suppliername == PluginManufacturersimportsConfig::LENOVO) { + $url = self::selectSupplier( + $suppliername, + $supplierUrl, + $line["serial"], + $otherSerial, + $supplierkey, + $supplierkeysecret, + true + ); + $output_url = "" . + __('Manufacturer information', 'manufacturersimports') . ""; } - } else { - $output_doc = "" . - __('Already imported', 'manufacturersimports'); - if (!empty($line["date_import"])) { - $output_doc .= " (" . Html::convdate($line["date_import"]) . ")"; + echo Search::showItem($output_type, $output_url, $item_num, $row_num); + + //status + if ($imported != self::IMPORTED) { + if ($status != 2) { + $output_doc = __('Not yet imported', 'manufacturersimports'); + } else { + $output_doc = "" . + __('Problem during the importation', 'manufacturersimports'); + if (!empty($data["date_import"])) { + $output_doc .= " (" . Html::convdate($data["date_import"]) . ")"; + } + $output_doc .= ""; + } + } else { + $output_doc = "" . + __('Already imported', 'manufacturersimports'); + if (!empty($line["date_import"])) { + $output_doc .= " (" . Html::convdate($line["date_import"]) . ")"; + } + $output_doc .= ""; } - $output_doc .= ""; - } - echo Search::showItem($output_type, $output_doc, $item_num, $row_num); - //no associated doc - echo $supplier->showDocItem($output_type, $item_num, $row_num, $line["documents_id"]); - - } - } - - /** - * Prints search form - * - * @param $manufacturer the supplier choice - * @param $type the device type - * - * @return nothing (print out a table) - * - */ - static function searchForm($params) { - global $DB, $CFG_GLPI; - - // Default values of parameters - $p['itemtype'] = ''; - $p['manufacturers_id'] = ''; - $p['imported'] = ''; - - foreach ($params as $key => $val) { - $p[$key] = $val; - } - - $dbu = new DbUtils(); - - echo ""; - echo "
"; - echo ""; - echo ""; - echo ""; + echo ""; + + echo "
" . __('Choose inventory type and manufacturer', 'manufacturersimports') . "
"; - - //Manufacturer choice - $query = "SELECT `glpi_plugin_manufacturersimports_configs`.* + echo Search::showItem($output_type, $output_doc, $item_num, $row_num); + //no associated doc + echo $supplier->showDocItem($output_type, $item_num, $row_num, $line["documents_id"]); + } + } + + /** + * Prints search form + * + * @param $manufacturer the supplier choice + * @param $type the device type + * + * @return nothing (print out a table) + * + */ + public static function searchForm($params) + { + global $DB; + + // Default values of parameters + $p['itemtype'] = ''; + $p['manufacturers_id'] = ''; + $p['imported'] = ''; + + foreach ($params as $key => $val) { + $p[$key] = $val; + } + + $dbu = new DbUtils(); + + echo ""; + echo "
"; + echo ""; + echo ""; + echo ""; - echo ""; - - echo "
" . __('Choose inventory type and manufacturer', 'manufacturersimports') . "
"; + + //Manufacturer choice + $query = "SELECT `glpi_plugin_manufacturersimports_configs`.* FROM `glpi_plugin_manufacturersimports_configs` "; - $query .= " LEFT JOIN `glpi_entities` ON (`glpi_entities`.`id` = `glpi_plugin_manufacturersimports_configs`.`entities_id`)" - . " WHERE `glpi_plugin_manufacturersimports_configs`.`manufacturers_id` != '0' - AND `glpi_plugin_manufacturersimports_configs`.`name` != 'HP'"; - $query .= "" . $dbu->getEntitiesRestrictRequest(" AND", "glpi_plugin_manufacturersimports_configs", '', '', true); - $query .= " ORDER BY `glpi_plugin_manufacturersimports_configs`.`entities_id`, + $query .= " LEFT JOIN `glpi_entities` ON (`glpi_entities`.`id` = `glpi_plugin_manufacturersimports_configs`.`entities_id`)" + . " WHERE `glpi_plugin_manufacturersimports_configs`.`manufacturers_id` != '0'"; + $query .= "" . $dbu->getEntitiesRestrictRequest(" AND", "glpi_plugin_manufacturersimports_configs", '', '', true); + $query .= " ORDER BY `glpi_plugin_manufacturersimports_configs`.`entities_id`, `glpi_plugin_manufacturersimports_configs`.`name`"; - $iterator = $DB->request($query); - $number = $iterator->numrows(); - - if ($number > 0) { - self::showAllItems("itemtype", 0, $p['itemtype'], -1, - PluginManufacturersimportsConfig::getTypes()); - echo ""; - foreach ($iterator as $data) { + $iterator = $DB->request($query); + $number = $iterator->numrows(); + + if ($number > 0) { + self::showAllItems( + "itemtype", + 0, + $p['itemtype'], + -1, + PluginManufacturersimportsConfig::getTypes() + ); + echo ""; + foreach ($iterator as $data) { // if (Session::isMultiEntitiesMode()) { // $name = Dropdown::getDropdownName("glpi_entities", $data["entities_id"]) . " > "; // } - $name = $data["name"]; - if (empty($data["name"]) || $_SESSION["glpiis_ids_visible"]) { - $name .= " ("; - $name .= $data["id"] . ")"; + $name = $data["name"]; + if (empty($data["name"]) || $_SESSION["glpiis_ids_visible"]) { + $name .= " ("; + $name .= $data["id"] . ")"; + } + $opt[$data["id"]] = $name; } - $opt[$data["id"]] = $name; - } - - Dropdown::showFromArray('manufacturers_id', $opt, ['value' => $p['manufacturers_id']]); - - echo ""; - - $options[self::NOT_IMPORTED] = __('Devices not imported', 'manufacturersimports'); - $options[self::IMPORTED] = __('Devices already imported', 'manufacturersimports'); - Dropdown::showFromArray('imported', $options, ['value' => $p['imported']]); - - - } else { - if (Session::haveRight('config', UPDATE)) { - //Please configure a supplier - echo ""; - echo __('No manufacturer available. Please configure at least one manufacturer', 'manufacturersimports'); - echo ""; - } else { - echo "
"; - echo __('No manufacturer available. Please configure at least one manufacturer', 'manufacturersimports'); - echo "
"; - } - } - echo "
"; - if ($number > 0) { - echo Html::submit(_sx('button', 'Post'), ['name' => 'typechoice', 'class' => 'btn btn-primary']); - } - echo "
"; - Html::closeForm(); - } - - /** - * Prints display Search Header - * - * @param $fixed if fixed cells - * @param $output_type the output_type - * - * @return nothing (print out a table) - * - */ + + Dropdown::showFromArray('manufacturers_id', $opt, ['value' => $p['manufacturers_id']]); + + echo "
"; + + $options[self::NOT_IMPORTED] = __('Devices not imported', 'manufacturersimports'); + $options[self::IMPORTED] = __('Devices already imported', 'manufacturersimports'); + Dropdown::showFromArray('imported', $options, ['value' => $p['imported']]); + } else { + if (Session::haveRight('config', UPDATE)) { + //Please configure a supplier + echo ""; + echo __('No manufacturer available. Please configure at least one manufacturer', 'manufacturersimports'); + echo ""; + } else { + echo "
"; + echo __('No manufacturer available. Please configure at least one manufacturer', 'manufacturersimports'); + echo "
"; + } + } + echo "
"; + if ($number > 0) { + echo Html::submit(_sx('button', 'Post'), ['name' => 'typechoice', 'class' => 'btn btn-primary']); + } + echo "
"; + Html::closeForm(); + } + + /** + * Prints display Search Header + * + * @param $fixed if fixed cells + * @param $output_type the output_type + * + * @return nothing (print out a table) + * + */ // static function displaySearchHeader($output_type, $fixed = 0) { // $out = ""; // switch ($output_type) { @@ -491,307 +545,340 @@ static function searchForm($params) { // return $out; // } - /** - * Prints display pre import - * - * @param $type the type of device - * @param $configID the ID of the supplier config - * @param $start for pager display - * @param $complete to see all device (already imported and not) - * - * @return nothing (print out a table) - * - */ - static function seePreImport($params) { - global $DB, $CFG_GLPI; - - // Default values of parameters - $p['link'] = []; - $p['field'] = []; - $p['contains'] = []; - $p['searchtype'] = []; - $p['sort'] = '1'; - $p['order'] = 'ASC'; - $p['start'] = 0; - $p['export_all'] = 0; - $p['link2'] = ''; - $p['contains2'] = ''; - $p['field2'] = ''; - $p['itemtype2'] = ''; - $p['searchtype2'] = ''; - $p['itemtype'] = ''; - $p['manufacturers_id'] = ''; - $p['imported'] = ''; - - foreach ($params as $key => $val) { - $p[$key] = $val; - } - - $target = PLUGIN_MANUFACTURERSIMPORTS_WEBDIR . "/front/import.php"; - - if ($p['itemtype'] && $p['manufacturers_id']) { - - $config = new PluginManufacturersimportsConfig(); - $config->getFromDB($p['manufacturers_id']); - $suppliername = $config->fields["name"]; - $supplierclass = "PluginManufacturersimports" . $suppliername; - $supplier = new $supplierclass(); - - $infocom = new Infocom(); - $canedit = Session::haveRight(static::$rightname, UPDATE) && $infocom->canUpdate(); - - if (!$p['start']) { - $p['start'] = 0; - } - - $toview = ["name" => 1]; - - $query = self::queryImport($p, $config, $toview); - - $result = $DB->query($query); - $numrows = $DB->numrows($result); - - if ($p['start'] < $numrows) { - - // Set display type for export if define - $output_type = Search::HTML_OUTPUT; - if (isset($_GET["display_type"])) { - $output_type = $_GET["display_type"]; - } - $parameters = "itemtype=" . $p['itemtype'] . - "&manufacturers_id=" . $p['manufacturers_id'] . - "&imported=" . $p['imported']; - $total = 0; - - if ($output_type == Search::HTML_OUTPUT) { - self::printPager($p['start'], $numrows, - $target, $parameters, $p['itemtype']); - } - - - if (Session::isMultiEntitiesMode()) { - $colsup = 1; - } else { - $colsup = 0; - } - //////////////////////HEADER/////////////// - if ($output_type == Search::HTML_OUTPUT) { - echo ""; - } - - //echo Search::displaySearchHeader($output_type,0); //table + div - if ($canedit) { - $nbcols = 11 + $colsup; - } else { - $nbcols = 10 + $colsup; - } - $LIST_LIMIT = $_SESSION['glpilist_limit']; - $begin_display = $p['start']; - $end_display = $p['start'] + $LIST_LIMIT; - - echo Search::showHeader($output_type, $end_display - $begin_display + 1, $nbcols); - echo Search::showNewLine($output_type); - $header_num = 1; - - echo Search::showHeaderItem($output_type, "", $header_num); - echo Search::showHeaderItem($output_type, __('Name'), - $header_num, '', $p['sort'] == $val, $p['order']); - if (Session::isMultiEntitiesMode()) { - echo Search::showHeaderItem($output_type, __('Entity'), $header_num); - } - echo Search::showHeaderItem($output_type, __('Serial number'), $header_num); - echo Search::showHeaderItem($output_type, __('Model Number', 'manufacturersimports'), $header_num); - echo Search::showHeaderItem($output_type, - __('Financial and administrative information'), - $header_num); - echo Search::showHeaderItem($output_type, - __('Supplier attached', 'manufacturersimports'), - $header_num); - echo Search::showHeaderItem($output_type, - __('New warranty attached', 'manufacturersimports'), - $header_num); - echo Search::showHeaderItem($output_type, - _n('Link', 'Links', 1), - $header_num); - echo Search::showHeaderItem($output_type, - _n('Status', 'Statuses', 1), - $header_num); - echo $supplier->showDocTitle($output_type, $header_num); - - // End Line for column headers - echo Search::showEndLine($output_type); - - $i = $p['start']; - if (isset($_GET['export_all'])) { - $i = 0; - } - if ($i > 0) { - $DB->dataSeek($result, $i); - } - - $row_num = 1; - - while ($i < $numrows && $i < $end_display) { - $i++; - - $item_num = 1; - $line = $DB->fetchArray($result); - $compSerial = $line['serial']; - $compId = $line['id']; - $model = $line["model_name"]; - if (!$line["itemtype"]) { - $line["itemtype"] = $p['itemtype']; - } - - self::showImport($row_num, $item_num, $line, $output_type, - $p['manufacturers_id'], - $line["import_status"], - $p['imported']); - //1.show already imported items && import_status not failed - if ($p['imported'] == 1) { - $total += 1; - } - - } - echo ""; - echo sprintf(__('Total number of devices to import %s', - 'manufacturersimports'), $total); - echo ""; - - // Close Table - $title = ""; - // Create title - if ($output_type == Search::PDF_OUTPUT_PORTRAIT - || $output_type == Search::PDF_OUTPUT_LANDSCAPE) { - $title .= - PluginManufacturersimportsPreImport::getTypeName(2) - . " " . $suppliername; + /** + * Prints display pre import + * + * @param $type the type of device + * @param $configID the ID of the supplier config + * @param $start for pager display + * @param $complete to see all device (already imported and not) + * + * @return nothing (print out a table) + * + */ + public static function seePreImport($params) + { + global $DB, $CFG_GLPI; + + // Default values of parameters + $p['link'] = []; + $p['field'] = []; + $p['contains'] = []; + $p['searchtype'] = []; + $p['sort'] = '1'; + $p['order'] = 'ASC'; + $p['start'] = 0; + $p['export_all'] = 0; + $p['link2'] = ''; + $p['contains2'] = ''; + $p['field2'] = ''; + $p['itemtype2'] = ''; + $p['searchtype2'] = ''; + $p['itemtype'] = ''; + $p['manufacturers_id'] = ''; + $p['imported'] = ''; + + foreach ($params as $key => $val) { + $p[$key] = $val; + } + + $target = PLUGIN_MANUFACTURERSIMPORTS_WEBDIR . "/front/import.php"; + + if ($p['itemtype'] && $p['manufacturers_id']) { + $config = new PluginManufacturersimportsConfig(); + $config->getFromDB($p['manufacturers_id']); + $suppliername = $config->fields["name"]; + $supplierclass = "PluginManufacturersimports" . $suppliername; + $supplier = new $supplierclass(); + + $infocom = new Infocom(); + $canedit = Session::haveRight(static::$rightname, UPDATE) && $infocom->canUpdate(); + + if (!$p['start']) { + $p['start'] = 0; } - echo Search::showFooter($output_type, $title); - - //massive action - if ($canedit && $output_type == Search::HTML_OUTPUT) { - if ($_SESSION['glpilist_limit'] < Toolbox::get_max_input_vars()) { - self::openArrowMassives("massiveaction_form", false); - self::dropdownMassiveAction($compId, $p['itemtype'], - $p['manufacturers_id'], - $p['start'], $p['imported']); - self::closeArrowMassives([]); - } else { - echo "" . - "
"; - echo __('Selection too large, massive action disabled.') . ""; - if ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE) { - echo "
" . __('To increase the limit: change max_input_vars or suhosin.post.max_vars in php configuration.'); - } - echo "
"; - } - Html::closeForm(); + $toview = ["name" => 1]; + + $query = self::queryImport($p, $config, $toview); + + $result = $DB->query($query); + $numrows = $DB->numrows($result); + + if ($p['start'] < $numrows) { + // Set display type for export if define + $output_type = Search::HTML_OUTPUT; + if (isset($_GET["display_type"])) { + $output_type = $_GET["display_type"]; + } + $parameters = "itemtype=" . $p['itemtype'] . + "&manufacturers_id=" . $p['manufacturers_id'] . + "&imported=" . $p['imported']; + $total = 0; + + if ($output_type == Search::HTML_OUTPUT) { + self::printPager( + $p['start'], + $numrows, + $target, + $parameters, + $p['itemtype'] + ); + } + + + if (Session::isMultiEntitiesMode()) { + $colsup = 1; + } else { + $colsup = 0; + } + //////////////////////HEADER/////////////// + if ($output_type == Search::HTML_OUTPUT) { + echo ""; + } + + //echo Search::displaySearchHeader($output_type,0); //table + div + if ($canedit) { + $nbcols = 11 + $colsup; + } else { + $nbcols = 10 + $colsup; + } + $LIST_LIMIT = $_SESSION['glpilist_limit']; + $begin_display = $p['start']; + $end_display = $p['start'] + $LIST_LIMIT; + + echo Search::showHeader($output_type, $end_display - $begin_display + 1, $nbcols); + echo Search::showNewLine($output_type); + $header_num = 1; + + echo Search::showHeaderItem($output_type, "", $header_num); + echo Search::showHeaderItem( + $output_type, + __('Name'), + $header_num, + '', + $p['sort'] == $val, + $p['order'] + ); + if (Session::isMultiEntitiesMode()) { + echo Search::showHeaderItem($output_type, __('Entity'), $header_num); + } + echo Search::showHeaderItem($output_type, __('Serial number'), $header_num); + echo Search::showHeaderItem($output_type, __('Model Number', 'manufacturersimports'), $header_num); + echo Search::showHeaderItem( + $output_type, + __('Financial and administrative information'), + $header_num + ); + echo Search::showHeaderItem( + $output_type, + __('Supplier attached', 'manufacturersimports'), + $header_num + ); + echo Search::showHeaderItem( + $output_type, + __('New warranty attached', 'manufacturersimports'), + $header_num + ); + echo Search::showHeaderItem( + $output_type, + _n('Link', 'Links', 1), + $header_num + ); + echo Search::showHeaderItem( + $output_type, + _n('Status', 'Statuses', 1), + $header_num + ); + echo $supplier->showDocTitle($output_type, $header_num); + + // End Line for column headers + echo Search::showEndLine($output_type); + + $i = $p['start']; + if (isset($_GET['export_all'])) { + $i = 0; + } + if ($i > 0) { + $DB->dataSeek($result, $i); + } + + $row_num = 1; + + while ($i < $numrows && $i < $end_display) { + $i++; + + $item_num = 1; + $line = $DB->fetchArray($result); + $compSerial = $line['serial']; + $compId = $line['id']; + $model = $line["model_name"]; + if (!$line["itemtype"]) { + $line["itemtype"] = $p['itemtype']; + } + + self::showImport( + $row_num, + $item_num, + $line, + $output_type, + $p['manufacturers_id'], + $line["import_status"], + $p['imported'] + ); + //1.show already imported items && import_status not failed + if ($p['imported'] == 1) { + $total += 1; + } + } + echo ""; + echo sprintf(__( + 'Total number of devices to import %s', + 'manufacturersimports' + ), $total); + echo ""; + + // Close Table + $title = ""; + // Create title + if ($output_type == Search::PDF_OUTPUT_PORTRAIT + || $output_type == Search::PDF_OUTPUT_LANDSCAPE) { + $title .= + PluginManufacturersimportsPreImport::getTypeName(2) + . " " . $suppliername; + } + + echo Search::showFooter($output_type, $title); + + //massive action + if ($canedit && $output_type == Search::HTML_OUTPUT) { + if ($_SESSION['glpilist_limit'] < Toolbox::get_max_input_vars()) { + self::openArrowMassives("massiveaction_form", false); + self::dropdownMassiveAction( + $compId, + $p['itemtype'], + $p['manufacturers_id'], + $p['start'], + $p['imported'] + ); + self::closeArrowMassives([]); + } else { + echo "" . + "
"; + echo __('Selection too large, massive action disabled.') . ""; + if ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE) { + echo "
" . __('To increase the limit: change max_input_vars or suhosin.post.max_vars in php configuration.'); + } + echo "
"; + } + Html::closeForm(); + } else { + echo ""; + echo "
"; + } + + echo "
"; + if ($output_type == Search::HTML_OUTPUT) { + self::printPager( + $p['start'], + $numrows, + $target, + $parameters, + $p['itemtype'] + ); + } } else { - echo ""; - echo ""; + echo "
" . + __('No device finded', 'manufacturersimports') . "
"; } - - echo "
"; - if ($output_type == Search::HTML_OUTPUT) { - self::printPager($p['start'], $numrows, - $target, $parameters, $p['itemtype']); - } - - } else { - echo "
" . - __('No device finded', 'manufacturersimports') . "
"; - } - } - } - - /** - * show arrow for massives actions : opening - * - * @param $formname string - * @param $fixed boolean used tab_cadre_fixe in both tables (false by default) - * @param $ontop display on top of the list (false by default) - * @param $onright display on right of the list (false by default) - * - **/ - static function openArrowMassives($formname, $fixed = false, $ontop = false, $onright = false) { - global $CFG_GLPI; - - if ($fixed) { - echo ""; - } else { - echo "
"; - } - - echo ""; - if (!$onright) { - echo ""; - } else { - echo ""; - } - echo ""; + echo "
"; - echo ""; + } else { + echo ""; + } + + echo ""; + if (!$onright) { + echo ""; + } else { + echo ""; + } + echo ""; - echo ""; - echo ""; + echo ""; - if ($onright) { - echo ""; - echo "
"; + echo "" . __('Check all') . "/"; - echo "/"; + echo "" . __('Uncheck all') . ""; - } else { - echo ""; - } - - } - - - /** - * show arrow for massives actions : closing - * - * @param $actions array of action : $name -> $label - * @param $confirm array of confirmation string (optional) - * - **/ - static function closeArrowMassives($actions, $confirm = []) { - - if (count($actions)) { - foreach ($actions as $name => $label) { - if (!empty($name)) { - - echo " "; + if ($onright) { + echo ""; + } else { + echo ""; + } + } + + + /** + * show arrow for massives actions : closing + * + * @param $actions array of action : $name -> $label + * @param $confirm array of confirmation string (optional) + * + **/ + public static function closeArrowMassives($actions, $confirm = []) + { + if (count($actions)) { + foreach ($actions as $name => $label) { + if (!empty($name)) { + echo " "; + } } - } - } - echo "
"; - } - - /** - * Request - * - * @param $p - * @param $config - * @param $toview - * - * @return string - */ - static function queryImport($p, $config, $toview, $isCron = false) { - - $dbu = new DbUtils(); - - $modeltable = $dbu->getTableForItemType($p['itemtype'] . "Model"); - $modelfield = $dbu->getForeignKeyFieldForTable($dbu->getTableForItemType($p['itemtype'] . "Model")); - $item = new $p['itemtype'](); - $itemtable = $dbu->getTableForItemType($p['itemtype']); - - $query = "SELECT `" . $itemtable . "`.`id`, + } + echo "
"; + } + + /** + * Request + * + * @param $p + * @param $config + * @param $toview + * + * @return string + */ + public static function queryImport($p, $config, $toview, $isCron = false) + { + $dbu = new DbUtils(); + + $modeltable = $dbu->getTableForItemType($p['itemtype'] . "Model"); + $modelfield = $dbu->getForeignKeyFieldForTable($dbu->getTableForItemType($p['itemtype'] . "Model")); + $item = new $p['itemtype'](); + $itemtable = $dbu->getTableForItemType($p['itemtype']); + + $query = "SELECT `" . $itemtable . "`.`id`, `" . $itemtable . "`.`name`, `" . $itemtable . "`.`serial`, `" . $itemtable . "`.`entities_id`, @@ -805,234 +892,237 @@ static function queryImport($p, $config, $toview, $isCron = false) { `$modeltable`.`name` AS model_name FROM `" . $itemtable . "` "; - //model device left join - $query .= "LEFT JOIN `$modeltable` ON (`$modeltable`.`id` = `" . $itemtable . "`.`" . $modelfield . "`) "; - $query .= " LEFT JOIN `glpi_entities` ON (`glpi_entities`.`id` = `" . $itemtable . "`.`entities_id`)"; - $query .= " LEFT JOIN `glpi_plugin_manufacturersimports_configs` + //model device left join + $query .= "LEFT JOIN `$modeltable` ON (`$modeltable`.`id` = `" . $itemtable . "`.`" . $modelfield . "`) "; + $query .= " LEFT JOIN `glpi_entities` ON (`glpi_entities`.`id` = `" . $itemtable . "`.`entities_id`)"; + $query .= " LEFT JOIN `glpi_plugin_manufacturersimports_configs` ON (`glpi_plugin_manufacturersimports_configs`.`manufacturers_id` = `" . $itemtable . "`.`manufacturers_id`)"; - $query .= " LEFT JOIN `glpi_plugin_manufacturersimports_logs` + $query .= " LEFT JOIN `glpi_plugin_manufacturersimports_logs` ON (`glpi_plugin_manufacturersimports_logs`.`items_id` = `" . $itemtable . "`.`id` AND `glpi_plugin_manufacturersimports_logs`.`itemtype` = '" . $p['itemtype'] . "')"; - $query .= " LEFT JOIN `glpi_plugin_manufacturersimports_models` + $query .= " LEFT JOIN `glpi_plugin_manufacturersimports_models` ON (`glpi_plugin_manufacturersimports_models`.`items_id` = `" . $itemtable . "`.`id` AND `glpi_plugin_manufacturersimports_models`.`itemtype` = '" . $p['itemtype'] . "')"; - //serial must be not empty - $query .= " WHERE `" . $itemtable . "`.`is_deleted` = '0' + //serial must be not empty + $query .= " WHERE `" . $itemtable . "`.`is_deleted` = '0' AND `" . $itemtable . "`.`is_template` = '0' AND `glpi_plugin_manufacturersimports_configs`.`id` = '" . $p['manufacturers_id'] . "' AND `" . $itemtable . "`.`serial` != '' "; - //already imported - if ($p['imported'] == self::IMPORTED) { - $query .= " AND `import_status` != " . self::IMPORTED . ""; - //not imported - } else if ($p['imported'] == self::NOT_IMPORTED) { - $query .= " AND (`date_import` IS NULL OR `import_status` = " . self::IMPORTED . " "; - $query .= ") "; - } - $entities = ""; - if ($config->isRecursive()) { - $entities = $dbu->getSonsOf('glpi_entities', $config->getEntityID()); - } else { - $entities = $config->getEntityID(); - } - if (!$isCron) { - $query .= "" . $dbu->getEntitiesRestrictRequest(" AND", $itemtable, '', '', $item->maybeRecursive()); - } - //// 4 - ORDER - $ORDER = " ORDER BY `entities_id`,`" . $itemtable . "`.`name` "; - - foreach ($toview as $key => $val) { - if ($p['sort'] == $val) { - $ORDER = self::addOrderBy($p['itemtype'], $p['sort'], $p['order'], $key); - } - } - $query .= $ORDER; - - return $query; - - } - - /** - * Generic Function to add ORDER BY to a request - * - * @param $itemtype ID of the device type - * @param $ID field to add - * @param $order order define - * @param $key item number - * - * @return select string - * - **/ - static function addOrderBy($itemtype, $ID, $order, $key = 0) { - global $CFG_GLPI; - - // Security test for order - if ($order != "ASC") { - $order = "DESC"; - } - $searchopt = &Search::getOptions($itemtype); - - $table = $searchopt[$ID]["table"]; - $field = $searchopt[$ID]["field"]; - - $addtable = ''; - $dbu = new DbUtils(); - - if ($table != $dbu->getTableForItemType($itemtype) - && $searchopt[$ID]["linkfield"] != $dbu->getForeignKeyFieldForTable($table)) { - $addtable .= "_" . $searchopt[$ID]["linkfield"]; - } - - if (isset($searchopt[$ID]['joinparams'])) { - $complexjoin = Search::computeComplexJoinID($searchopt[$ID]['joinparams']); - - if (!empty($complexjoin)) { - $addtable .= "_" . $complexjoin; - } - } - - if (isset($CFG_GLPI["union_search_type"][$itemtype])) { - return " ORDER BY ITEM_$key $order "; - } - - return " ORDER BY $table.$field $order "; - - } - - /** - * @param $start - * @param $numrows - * @param $target - * @param $parameters - * @param int $item_type_output - * @param int $item_type_output_param - */ - static function printPager($start, $numrows, $target, $parameters, $item_type_output = 0, $item_type_output_param = 0) { - global $CFG_GLPI; - - $list_limit = $_SESSION['glpilist_limit']; - // Forward is the next step forward - $forward = $start + $list_limit; - - // This is the end, my friend - $end = $numrows - $list_limit; - - // Human readable count starts here - $current_start = $start + 1; - - // And the human is viewing from start to end - $current_end = $current_start + $list_limit - 1; - if ($current_end > $numrows) { - $current_end = $numrows; - } - - // Backward browsing - if ($current_start - $list_limit <= 0) { - $back = 0; - } else { - $back = $start - $list_limit; - } - - // Print it - - echo "\n"; - echo "\n"; - - // Back and fast backward button - if (!$start == 0) { - echo ""; - echo ""; - } - - // Print the "where am I?" - echo "\n"; - - echo "\n"; - - // Forward and fast forward button - if ($forward < $numrows) { - echo "\n"; - - echo "\n"; - } - - // End pager - echo "\n"; - echo "
"; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - Html::printPagerForm("$target?$parameters&start=$start"); - echo ""; - //TRANS: %1$d, %2$d, %3$d are page numbers - printf(__('From %1$d to %2$d on %3$d'), $current_start, $current_end, $numrows); - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo "

\n"; - - } - - /** - * @param $ID - * @param $type - * @param $manufacturer - * @param $start - * @param $imported - */ - static function dropdownMassiveAction($ID, $type, $manufacturer, $start, $imported) { - global $CFG_GLPI; - - echo " "; - - $params = ['action' => '__VALUE__', - 'manufacturers_id' => $manufacturer, - 'itemtype' => $type, - 'start' => $start, - 'imported' => $imported, - 'id' => $ID, - ]; - - Ajax::updateItemOnSelectEvent("massiveaction", "show_massiveaction", - PLUGIN_MANUFACTURERSIMPORTS_WEBDIR . "/ajax/dropdownMassiveAction.php", $params); - - echo " \n"; - - } - - /** - * @param $name - * @param $array - * - * @return string - */ - static function getArrayUrlLink($name, $array) { - $out = ""; - if (is_array($array) && count($array) > 0) { - foreach ($array as $key => $val) { - $out .= "&" . $name . "[$key]=" . urlencode(stripslashes($val)); - } - } - return $out; - } - + //already imported + if ($p['imported'] == self::IMPORTED) { + $query .= " AND `import_status` != " . self::IMPORTED . ""; + //not imported + } elseif ($p['imported'] == self::NOT_IMPORTED) { + $query .= " AND (`date_import` IS NULL OR `import_status` = " . self::IMPORTED . " "; + $query .= ") "; + } + $entities = ""; + if ($config->isRecursive()) { + $entities = $dbu->getSonsOf('glpi_entities', $config->getEntityID()); + } else { + $entities = $config->getEntityID(); + } + if (!$isCron) { + $query .= "" . $dbu->getEntitiesRestrictRequest(" AND", $itemtable, '', '', $item->maybeRecursive()); + } + //// 4 - ORDER + $ORDER = " ORDER BY `entities_id`,`" . $itemtable . "`.`name` "; + + foreach ($toview as $key => $val) { + if ($p['sort'] == $val) { + $ORDER = self::addOrderBy($p['itemtype'], $p['sort'], $p['order'], $key); + } + } + $query .= $ORDER; + + return $query; + } + + /** + * Generic Function to add ORDER BY to a request + * + * @param $itemtype ID of the device type + * @param $ID field to add + * @param $order order define + * @param $key item number + * + * @return select string + * + **/ + public static function addOrderBy($itemtype, $ID, $order, $key = 0) + { + global $CFG_GLPI; + + // Security test for order + if ($order != "ASC") { + $order = "DESC"; + } + $searchopt = &Search::getOptions($itemtype); + + $table = $searchopt[$ID]["table"]; + $field = $searchopt[$ID]["field"]; + + $addtable = ''; + $dbu = new DbUtils(); + + if ($table != $dbu->getTableForItemType($itemtype) + && $searchopt[$ID]["linkfield"] != $dbu->getForeignKeyFieldForTable($table)) { + $addtable .= "_" . $searchopt[$ID]["linkfield"]; + } + + if (isset($searchopt[$ID]['joinparams'])) { + $complexjoin = Search::computeComplexJoinID($searchopt[$ID]['joinparams']); + + if (!empty($complexjoin)) { + $addtable .= "_" . $complexjoin; + } + } + + if (isset($CFG_GLPI["union_search_type"][$itemtype])) { + return " ORDER BY ITEM_$key $order "; + } + + return " ORDER BY $table.$field $order "; + } + + /** + * @param $start + * @param $numrows + * @param $target + * @param $parameters + * @param int $item_type_output + * @param int $item_type_output_param + */ + public static function printPager($start, $numrows, $target, $parameters, $item_type_output = 0, $item_type_output_param = 0) + { + global $CFG_GLPI; + + $list_limit = $_SESSION['glpilist_limit']; + // Forward is the next step forward + $forward = $start + $list_limit; + + // This is the end, my friend + $end = $numrows - $list_limit; + + // Human readable count starts here + $current_start = $start + 1; + + // And the human is viewing from start to end + $current_end = $current_start + $list_limit - 1; + if ($current_end > $numrows) { + $current_end = $numrows; + } + + // Backward browsing + if ($current_start - $list_limit <= 0) { + $back = 0; + } else { + $back = $start - $list_limit; + } + + // Print it + + echo "\n"; + echo "\n"; + + // Back and fast backward button + if (!$start == 0) { + echo ""; + echo ""; + } + + // Print the "where am I?" + echo "\n"; + + echo "\n"; + + // Forward and fast forward button + if ($forward < $numrows) { + echo "\n"; + + echo "\n"; + } + + // End pager + echo "\n"; + echo "
"; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + Html::printPagerForm("$target?$parameters&start=$start"); + echo ""; + //TRANS: %1$d, %2$d, %3$d are page numbers + printf(__('From %1$d to %2$d on %3$d'), $current_start, $current_end, $numrows); + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo "

\n"; + } + + /** + * @param $ID + * @param $type + * @param $manufacturer + * @param $start + * @param $imported + */ + public static function dropdownMassiveAction($ID, $type, $manufacturer, $start, $imported) + { + global $CFG_GLPI; + + echo " "; + + $params = ['action' => '__VALUE__', + 'manufacturers_id' => $manufacturer, + 'itemtype' => $type, + 'start' => $start, + 'imported' => $imported, + 'id' => $ID, + ]; + + Ajax::updateItemOnSelectEvent( + "massiveaction", + "show_massiveaction", + PLUGIN_MANUFACTURERSIMPORTS_WEBDIR . "/ajax/dropdownMassiveAction.php", + $params + ); + + echo " \n"; + } + + /** + * @param $name + * @param $array + * + * @return string + */ + public static function getArrayUrlLink($name, $array) + { + $out = ""; + if (is_array($array) && count($array) > 0) { + foreach ($array as $key => $val) { + $out .= "&" . $name . "[$key]=" . urlencode(stripslashes($val)); + } + } + return $out; + } } diff --git a/inc/toshiba.class.php b/inc/toshiba.class.php index 6c1ce49..4a23294 100644 --- a/inc/toshiba.class.php +++ b/inc/toshiba.class.php @@ -28,84 +28,96 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } /** * Class PluginManufacturersimportsToshiba */ -class PluginManufacturersimportsToshiba extends PluginManufacturersimportsManufacturer { - - /** - * @see PluginManufacturersimportsManufacturer::showDocTitle() - */ - function showDocTitle($output_type, $header_num) { - return Search::showHeaderItem($output_type, __('File'), $header_num); - } - - function getSearchField() { - return false; - } - - /** - * @see PluginManufacturersimportsManufacturer::getSupplierInfo() - */ - function getSupplierInfo($compSerial = null, $otherSerial = null, $key = null, $apisecret = null, - $supplierUrl = null) { - $info["name"] = PluginManufacturersimportsConfig::TOSHIBA; - $info["supplier_url"] = "https://support.dynabook.com/support/warrantyResults?"; - $info["url"] = $supplierUrl . - "sno=" . $compSerial; - return $info; - } - - /** - * Summary of getWarrantyUrl - * - * @param $config - * @param $compSerial - * - * @return string[] - */ - static function getWarrantyUrl($config, $compSerial) { - return ["url" => "https://support.dynabook.com/support/warrantyResults?sno=" . "$compSerial"]; - } - - /** - * @see PluginManufacturersimportsManufacturer::getBuyDate() - */ - function getBuyDate($contents) { - $field = "shipDate"; - $search = stristr($contents, $field); - $myDate = substr($search, 11, 21); - $myDate = trim($myDate); - $myDate = PluginManufacturersimportsPostImport::checkDate($myDate); - - return $myDate; - } - - /** - * @see PluginManufacturersimportsManufacturer::getStartDate() - */ - function getStartDate($contents) { - $field = "customerPurchaseDate"; - $search = stristr($contents, $field); - $myDate = substr($search, 23, 21); - $myDate = trim($myDate); - $myDate = PluginManufacturersimportsPostImport::checkDate($myDate); - - return $myDate; - } - - /** - * @see PluginManufacturersimportsManufacturer::getExpirationDate() - */ - function getExpirationDate($contents) { - $field = "warrantyExpiryDate"; - $search = stristr($contents, $field); - $myEndDate = substr($search, 21, 21); - $myEndDate = trim($myEndDate); - $myEndDate = PluginManufacturersimportsPostImport::checkDate($myEndDate); - return $myEndDate; - } +class PluginManufacturersimportsToshiba extends PluginManufacturersimportsManufacturer +{ + /** + * @see PluginManufacturersimportsManufacturer::showDocTitle() + */ + public function showDocTitle($output_type, $header_num) + { + return Search::showHeaderItem($output_type, __('File'), $header_num); + } + + public function getSearchField() + { + return false; + } + + /** + * @see PluginManufacturersimportsManufacturer::getSupplierInfo() + */ + public function getSupplierInfo( + $compSerial = null, + $otherSerial = null, + $key = null, + $apisecret = null, + $supplierUrl = null + ) + { + $info["name"] = PluginManufacturersimportsConfig::TOSHIBA; + $info["supplier_url"] = "https://support.dynabook.com/support/warrantyResults?"; + $info["url"] = $supplierUrl . + "sno=" . $compSerial; + return $info; + } + + /** + * Summary of getWarrantyUrl + * + * @param $config + * @param $compSerial + * + * @return string[] + */ + public static function getWarrantyUrl($config, $compSerial) + { + return ["url" => "https://support.dynabook.com/support/warrantyResults?sno=" . "$compSerial"]; + } + + /** + * @see PluginManufacturersimportsManufacturer::getBuyDate() + */ + public function getBuyDate($contents) + { + $field = "shipDate"; + $search = stristr($contents, $field); + $myDate = substr($search, 11, 21); + $myDate = trim($myDate); + $myDate = PluginManufacturersimportsPostImport::checkDate($myDate); + + return $myDate; + } + + /** + * @see PluginManufacturersimportsManufacturer::getStartDate() + */ + public function getStartDate($contents) + { + $field = "customerPurchaseDate"; + $search = stristr($contents, $field); + $myDate = substr($search, 23, 21); + $myDate = trim($myDate); + $myDate = PluginManufacturersimportsPostImport::checkDate($myDate); + + return $myDate; + } + + /** + * @see PluginManufacturersimportsManufacturer::getExpirationDate() + */ + public function getExpirationDate($contents) + { + $field = "warrantyExpiryDate"; + $search = stristr($contents, $field); + $myEndDate = substr($search, 21, 21); + $myEndDate = trim($myEndDate); + $myEndDate = PluginManufacturersimportsPostImport::checkDate($myEndDate); + return $myEndDate; + } } diff --git a/inc/wortmann_ag.class.php b/inc/wortmann_ag.class.php index f11d7df..02080f2 100644 --- a/inc/wortmann_ag.class.php +++ b/inc/wortmann_ag.class.php @@ -28,82 +28,92 @@ */ if (!defined('GLPI_ROOT')) { - die("Sorry. You can't access directly to this file"); + die("Sorry. You can't access directly to this file"); } /** * Class PluginManufacturersimportsWortmann_ag */ -class PluginManufacturersimportsWortmann_ag extends PluginManufacturersimportsManufacturer { - - /** - * @see PluginManufacturersimportsManufacturer::showDocTitle() - */ - function showDocTitle($output_type, $header_num) { - return false; - } - - function getSearchField() { - return "search"; - } - - /** - * @see PluginManufacturersimportsManufacturer::getSupplierInfo() - */ - function getSupplierInfo($compSerial = null, $otherSerial = null, $key = null, $apisecret = null, - $supplierUrl = null) { - $info["name"] = PluginManufacturersimportsConfig::WORTMANN_AG; - $info["supplier_url"] = "https://www.wortmann.de/fr-fr/profile/snsearch.aspx?SN="; - $info["url"] = $supplierUrl.$compSerial; - return $info; - } - - /** - * @see PluginManufacturersimportsManufacturer::getBuyDate() - */ - function getBuyDate($contents) { - $field = "but de la garantie"; - $searchstart = stristr($contents, $field); - $myDate = substr($searchstart, 27, 10); - - $myDate = trim($myDate); - $myDate = str_replace('/', '-', $myDate); - - $myDate = PluginManufacturersimportsPostImport::checkDate($myDate, true); - - if ($myDate != "0000-00-00") { - list($day, $month, $year) = explode('-', $myDate); - $myDate = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year)); - } - - return $myDate; - } - - /** - * @see PluginManufacturersimportsManufacturer::getStartDate() - */ - function getStartDate($contents) { - - return self::getBuyDate($contents); - } - - /** - * @see PluginManufacturersimportsManufacturer::getExpirationDate() - */ - function getExpirationDate($contents) { - $field = "Fin de garantie"; - $searchstart = stristr($contents, $field); - $myDate = substr($searchstart, 24, 10); - - $myDate = trim($myDate); - $myDate = str_replace('/', '-', $myDate); - - $myDate = PluginManufacturersimportsPostImport::checkDate($myDate, true); - - if ($myDate != "0000-00-00") { - list($day, $month, $year) = explode('-', $myDate); - $myDate = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year)); - } - return $myDate; - } +class PluginManufacturersimportsWortmann_ag extends PluginManufacturersimportsManufacturer +{ + /** + * @see PluginManufacturersimportsManufacturer::showDocTitle() + */ + public function showDocTitle($output_type, $header_num) + { + return false; + } + + public function getSearchField() + { + return "search"; + } + + /** + * @see PluginManufacturersimportsManufacturer::getSupplierInfo() + */ + public function getSupplierInfo( + $compSerial = null, + $otherSerial = null, + $key = null, + $apisecret = null, + $supplierUrl = null + ) + { + $info["name"] = PluginManufacturersimportsConfig::WORTMANN_AG; + $info["supplier_url"] = "https://www.wortmann.de/fr-fr/profile/snsearch.aspx?SN="; + $info["url"] = $supplierUrl.$compSerial; + return $info; + } + + /** + * @see PluginManufacturersimportsManufacturer::getBuyDate() + */ + public function getBuyDate($contents) + { + $field = "but de la garantie"; + $searchstart = stristr($contents, $field); + $myDate = substr($searchstart, 27, 10); + + $myDate = trim($myDate); + $myDate = str_replace('/', '-', $myDate); + + $myDate = PluginManufacturersimportsPostImport::checkDate($myDate, true); + + if ($myDate != "0000-00-00") { + list($day, $month, $year) = explode('-', $myDate); + $myDate = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year)); + } + + return $myDate; + } + + /** + * @see PluginManufacturersimportsManufacturer::getStartDate() + */ + public function getStartDate($contents) + { + return self::getBuyDate($contents); + } + + /** + * @see PluginManufacturersimportsManufacturer::getExpirationDate() + */ + public function getExpirationDate($contents) + { + $field = "Fin de garantie"; + $searchstart = stristr($contents, $field); + $myDate = substr($searchstart, 24, 10); + + $myDate = trim($myDate); + $myDate = str_replace('/', '-', $myDate); + + $myDate = PluginManufacturersimportsPostImport::checkDate($myDate, true); + + if ($myDate != "0000-00-00") { + list($day, $month, $year) = explode('-', $myDate); + $myDate = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year)); + } + return $myDate; + } } diff --git a/manufacturersimports.xml b/manufacturersimports.xml index 2853ba3..72e6964 100644 --- a/manufacturersimports.xml +++ b/manufacturersimports.xml @@ -41,6 +41,11 @@ Bazile Lebeau + + 3.0.3 + ~10.0 + https://github.com/InfotelGLPI/manufacturersimports/releases/download/3.0.3/glpi-manufacturersimports-3.0.3.tar.bz2 + 3.0.2 ~10.0 diff --git a/setup.php b/setup.php index bedc64e..b0d36d1 100644 --- a/setup.php +++ b/setup.php @@ -27,73 +27,80 @@ -------------------------------------------------------------------------- */ -define('PLUGIN_MANUFACTURERSIMPORTS_VERSION', '3.0.2'); +define('PLUGIN_MANUFACTURERSIMPORTS_VERSION', '3.0.3'); if (!defined("PLUGIN_MANUFACTURERSIMPORTS_DIR")) { - define("PLUGIN_MANUFACTURERSIMPORTS_DIR", Plugin::getPhpDir("manufacturersimports")); - define("PLUGIN_MANUFACTURERSIMPORTS_NOTFULL_DIR", Plugin::getPhpDir("manufacturersimports",false)); - define("PLUGIN_MANUFACTURERSIMPORTS_WEBDIR", Plugin::getWebDir("manufacturersimports")); + define("PLUGIN_MANUFACTURERSIMPORTS_DIR", Plugin::getPhpDir("manufacturersimports")); + define("PLUGIN_MANUFACTURERSIMPORTS_NOTFULL_DIR", Plugin::getPhpDir("manufacturersimports", false)); + define("PLUGIN_MANUFACTURERSIMPORTS_WEBDIR", Plugin::getWebDir("manufacturersimports")); } // Init the hooks of the plugins -Needed -function plugin_init_manufacturersimports() { - global $PLUGIN_HOOKS, $CFG_GLPI; - - $PLUGIN_HOOKS['csrf_compliant']['manufacturersimports'] = true; - - if (Plugin::isPluginActive('manufacturersimports') - && Session::getLoginUserID()) { - Plugin::registerClass('PluginManufacturersimportsProfile', - ['addtabon' => 'Profile']); - - //Display menu entry only if user has right to see it ! - if (Session::haveRight('plugin_manufacturersimports', READ)) { - $PLUGIN_HOOKS["menu_toadd"]['manufacturersimports'] - = ['tools' => 'PluginManufacturersimportsMenu']; - } - - if (Session::haveRight('config', UPDATE)) { - $PLUGIN_HOOKS['config_page']['manufacturersimports'] - = 'front/config.php'; - $PLUGIN_HOOKS['use_massive_action']['manufacturersimports'] = 1; - } - - // End init, when all types are registered - $PLUGIN_HOOKS['post_init']['manufacturersimports'] - = 'plugin_manufacturersimports_postinit'; - - $PLUGIN_HOOKS['infocom']['manufacturersimports'] = ['PluginManufacturersimportsConfig', 'showForInfocom']; - $PLUGIN_HOOKS['pre_show_item']['manufacturersimports'] = ['PluginManufacturersimportsConfig', 'showItemImport']; - } - - if (isset($_SESSION['glpiactiveprofile']['interface']) - && $_SESSION['glpiactiveprofile']['interface'] == 'central') { - // Add specific files to add to the header : javascript or css - $PLUGIN_HOOKS['add_css']['manufacturersimports'] = [ - "manufacturersimports.css", - ]; - } - +function plugin_init_manufacturersimports() +{ + global $PLUGIN_HOOKS, $CFG_GLPI; + + $PLUGIN_HOOKS['csrf_compliant']['manufacturersimports'] = true; + + if (Plugin::isPluginActive('manufacturersimports') + && Session::getLoginUserID()) { + Plugin::registerClass( + 'PluginManufacturersimportsProfile', + ['addtabon' => 'Profile'] + ); + + //Display menu entry only if user has right to see it ! + if (Session::haveRight('plugin_manufacturersimports', READ)) { + $PLUGIN_HOOKS["menu_toadd"]['manufacturersimports'] + = ['tools' => 'PluginManufacturersimportsMenu']; + } + + if (Session::haveRight('config', UPDATE)) { + $PLUGIN_HOOKS['config_page']['manufacturersimports'] + = 'front/config.php'; + $PLUGIN_HOOKS['use_massive_action']['manufacturersimports'] = 1; + } + + // End init, when all types are registered + $PLUGIN_HOOKS['post_init']['manufacturersimports'] + = 'plugin_manufacturersimports_postinit'; + + $PLUGIN_HOOKS['infocom']['manufacturersimports'] = ['PluginManufacturersimportsConfig', 'showForInfocom']; + $PLUGIN_HOOKS['pre_show_item']['manufacturersimports'] = ['PluginManufacturersimportsConfig', 'showItemImport']; + } + + if (isset($_SESSION['glpiactiveprofile']['interface']) + && $_SESSION['glpiactiveprofile']['interface'] == 'central') { + // Add specific files to add to the header : javascript or css + $PLUGIN_HOOKS['add_css']['manufacturersimports'] = [ + "manufacturersimports.css", + ]; + } } // Get the name and the version of the plugin - Needed -function plugin_version_manufacturersimports() { - return ['name' => _n('Suppliers import', 'Suppliers imports', 2, - 'manufacturersimports'), - 'oldname' => 'suppliertag', - 'version' => PLUGIN_MANUFACTURERSIMPORTS_VERSION, - 'license' => 'GPLv2+', - 'author' => "Infotel", - 'homepage' => 'https://github.com/InfotelGLPI/manufacturersimports/', - 'requirements' => [ - 'glpi' => [ - 'min' => '10.0', - 'max' => '11.0', - 'dev' => false - ], - 'php' => [ - 'exts' => ['soap', 'curl', 'json'], - ] - ] - ]; +function plugin_version_manufacturersimports() +{ + return ['name' => _n( + 'Suppliers import', + 'Suppliers imports', + 2, + 'manufacturersimports' + ), + 'oldname' => 'suppliertag', + 'version' => PLUGIN_MANUFACTURERSIMPORTS_VERSION, + 'license' => 'GPLv2+', + 'author' => "Infotel", + 'homepage' => 'https://github.com/InfotelGLPI/manufacturersimports/', + 'requirements' => [ + 'glpi' => [ + 'min' => '10.0', + 'max' => '11.0', + 'dev' => false + ], + 'php' => [ + 'exts' => ['soap', 'curl', 'json'], + ] + ] + ]; }