From 403b4a94954e315fe675f9fbd082d53e432627d8 Mon Sep 17 00:00:00 2001 From: Alexandre Delaunay Date: Wed, 30 Aug 2017 11:53:56 +0200 Subject: [PATCH 1/3] clean and port to 9.2 --- front/entity.form.php | 4 +- front/ticket.php | 3 +- hook.php | 9 +- inc/entity.class.php | 253 ++++++++++++++++++++---------------------- inc/ticket.class.php | 191 +++++++++++++++---------------- inc/type.class.php | 8 +- plugin.xml | 4 + setup.php | 34 ++---- 8 files changed, 231 insertions(+), 275 deletions(-) diff --git a/front/entity.form.php b/front/entity.form.php index caba055..54bcb87 100644 --- a/front/entity.form.php +++ b/front/entity.form.php @@ -34,19 +34,17 @@ Session::haveRight("entity", UPDATE); -$Entity = new Entity(); +$Entity = new Entity(); $PluginCreditEntity = new PluginCreditEntity(); $PluginCreditType = new PluginCreditType(); if (isset($_POST["add"])) { - $PluginCreditEntity->check(-1, CREATE, $_POST); if ($PluginCreditEntity->add($_POST)) { Event::log($_POST["plugin_credit_types_id"], "entity", 4, "setup", sprintf(__('%s adds a vouchers to an entity'), $_SESSION["glpiname"])); } Html::back(); - } Html::displayErrorAndDie("lost"); diff --git a/front/ticket.php b/front/ticket.php index 4fef26c..9c61f55 100644 --- a/front/ticket.php +++ b/front/ticket.php @@ -37,11 +37,10 @@ if (!isset($_GET["plugcreditentity"])) { throw new \RuntimeException('Invalid params provided!', 'credit'); } else { - $_GET['plugcreditentity'] = intval($_GET['plugcreditentity']); + $_GET['plugcreditentity'] = (int) $_GET['plugcreditentity']; } Session::checkLoginUser(); - Session::checkRightsOr('ticket', [Ticket::STEAL, Ticket::OWN]); PluginCreditTicket::displayConsumed($_GET['plugcreditentity']); diff --git a/hook.php b/hook.php index 6e54e71..e9024b4 100644 --- a/hook.php +++ b/hook.php @@ -78,10 +78,7 @@ function plugin_credit_uninstall() { * Define Dropdown tables to be manage in GLPI : */ function plugin_credit_getDropdown() { - - $pluginDropdowns = array('PluginCreditType' => _n('Credit voucher type', - 'Credit vouchers types', - Session::getPluralNumber(), - 'credit')); - return $pluginDropdowns; + return ['PluginCreditType' => _n('Credit voucher type', 'Credit vouchers types', + Session::getPluralNumber(), + 'credit')]; } diff --git a/inc/entity.class.php b/inc/entity.class.php index 0fb5dfb..ba5d51c 100644 --- a/inc/entity.class.php +++ b/inc/entity.class.php @@ -34,33 +34,24 @@ class PluginCreditEntity extends CommonDBTM { public static $rightname = 'entity'; - static function getTypeName($nb=0) { + static function getTypeName($nb = 0) { return _n('Credit voucher', 'Credit vouchers', $nb, 'credit'); } - /** - * @see CommonGLPI::getTabNameForItem() - **/ - function getTabNameForItem(CommonGLPI $item, $withtemplate=0) { - + function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { + $nb = self::countForItem($item); switch ($item->getType()) { case 'Entity' : if ($_SESSION['glpishow_count_on_tabs']) { - return self::createTabEntry(self::getTypeName(), self::countForItem($item)); + return self::createTabEntry(self::getTypeName($nb), $nb); } - break; default : - return self::getTypeName(); - break; + return self::getTypeName($nb); } return ''; } - /** - * @see CommonGLPI::displayTabContentForItem() - **/ - static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) { - + static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { switch ($item->getType()) { case 'Entity' : self::showForItemtype($item); @@ -73,21 +64,13 @@ static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtempl * @param $item CommonDBTM object **/ public static function countForItem(CommonDBTM $item) { - return countElementsInTable(getTableForItemType(__CLASS__), - "`entities_id` = '".$item->getID()."'"); + return countElementsInTable(self::getTable(), "`entities_id` = '".$item->getID()."'"); } - /** - * Actions done after the PURGE of the item in the database - * - * @return nothing - **/ public function post_purgeItem() { - global $DB; - - $table = getTableForItemType('PluginCreditTicket'); - $query = "DELETE FROM `$table` WHERE `plugin_credit_entities_id` = {$this->getID()};"; - $DB->query($query); + PluginCreditTicket::deleteByCriteria([ + 'plugin_credit_entities_id' => $this->getID() + ]); } /** @@ -99,11 +82,11 @@ public function post_purgeItem() { * @param $sqlfilter string to add an SQL filter (default '') * @return array of vouchers **/ - static function getAllForEntity($ID, $start=0, $limit=0, $sqlfilter='') { + static function getAllForEntity($ID, $start = 0, $limit = 0, $sqlfilter = '') { global $DB; $query = "SELECT * - FROM `" . getTableForItemType(__CLASS__) . "` + FROM `".self::getTable()."` WHERE `entities_id` = '$ID'"; if ($sqlfilter) { $query .= "AND ($sqlfilter) "; @@ -111,10 +94,10 @@ static function getAllForEntity($ID, $start=0, $limit=0, $sqlfilter='') { $query .= "ORDER BY `id` DESC"; if ($limit) { - $query .= " LIMIT ".intval($start)."," . intval($limit); + $query .= " LIMIT ".(int) $start.",".(int) $limit; } - $vouchers = array(); + $vouchers = []; foreach ($DB->request($query) as $data) { $vouchers[$data['id']] = $data; } @@ -128,7 +111,7 @@ static function getAllForEntity($ID, $start=0, $limit=0, $sqlfilter='') { * @param $entity object Entity * @param $itemtype string Entity or Ticket **/ - static function showForItemtype(Entity $entity, $itemtype='Entity') { + static function showForItemtype(Entity $entity, $itemtype = 'Entity') { global $DB, $CFG_GLPI; $ID = $entity->getField('id'); @@ -137,51 +120,49 @@ static function showForItemtype(Entity $entity, $itemtype='Entity') { } $out = ""; - $canedit = ($itemtype=='Ticket') ? false : $entity->canEdit($ID); + $canedit = $itemtype == 'Ticket' + ? false + : $entity->canEdit($ID); if ($canedit) { $rand = mt_rand(); $out .= "
"; $out .= "
"; + $out .= self::getFormUrl()."'>"; $out .= ""; $out .= ""; $out .= ""; $out .= ""; $out .= ""; - $out .= ""; - $out .= ""; + $out .= ""; $out .= ""; $out .= ""; $out .= ""; $out .= ""; $out .= ""; $out .= "
"; $out .= __('Add an credit voucher', 'credit')."
". __('Name')."" . Html::input("name", array('size' => 50)) . "". __('Type').""; - echo $out; $out = ""; - PluginCreditType::dropdown(array('name' => 'plugin_credit_types_id')); + $out .= "".Html::input("name", ['size' => 50])."".__('Type').""; + $out .= PluginCreditType::dropdown(['name' => 'plugin_credit_types_id', + 'display' => false]); $out .= "".__('Start date').""; - echo $out; $out = ""; - Html::showDateField("begin_date", array('value' => '')); + $out .= Html::showDateField("begin_date", ['value' => '', 'display' => false]); $out .= "
".__('Active').""; - echo $out; $out = ""; - Dropdown::showYesNo("is_active", ''); + $out .= Dropdown::showYesNo("is_active", 0, -1, ['display' => false]); $out .= ""; $out .= __('Quantity sold', 'credit').""; - echo $out; $out = ""; - Dropdown::showNumber("quantity", ['value' => '', - 'min' => 1, - 'max' => 200, - 'step' => 1, - 'toadd' => [0 => __('Unlimited')]]); + $out .= Dropdown::showNumber("quantity", ['value' => '', + 'min' => 1, + 'max' => 200, + 'step' => 1, + 'toadd' => [0 => __('Unlimited')], + 'display' => false]); $out .= "".__('End date').""; - echo $out; $out = ""; - Html::showDateField("end_date", array('value' => '')); + $out .= Html::showDateField("end_date", ['value' => '', 'display' => false]); $out .= ""; - $out .= ""; + $out .= ""; $out .= "
"; - echo $out; $out = ""; - Html::closeForm(); + $out .= Html::closeForm(false); $out .= "
"; } @@ -191,18 +172,18 @@ static function showForItemtype(Entity $entity, $itemtype='Entity') { if ($canedit) { $rand = mt_rand(); - echo $out; $out = ""; - Html::openMassiveActionsForm('mass'.__CLASS__.$rand); - $massiveactionparams - = array('num_displayed' - => $number, - 'container' - => 'mass'.__CLASS__.$rand, - 'rand' => $rand, - 'specific_actions' - => array('update' => _x('button', 'Update'), - 'purge' => _x('button', 'Delete permanently'))); - Html::showMassiveActions($massiveactionparams); + $out .= Html::getOpenMassiveActionsForm('mass'.__CLASS__.$rand); + $massiveactionparams = [ + 'num_displayed' => $number, + 'container' => 'mass'.__CLASS__.$rand, + 'rand' => $rand, + 'display' => false, + 'specific_actions' => [ + 'update' => _x('button', 'Update'), + 'purge' => _x('button', 'Delete permanently') + ] + ]; + $out .= Html::showMassiveActions($massiveactionparams); } $out .= ""; @@ -224,7 +205,7 @@ static function showForItemtype(Entity $entity, $itemtype='Entity') { $header_end .= ""; $header_end .= ""; $header_end .= ""; - $header_end .= "\n"; + $header_end .= ""; $out .= $header_begin.$header_top.$header_end; $sqlfilter = ""; @@ -237,20 +218,19 @@ static function showForItemtype(Entity $entity, $itemtype='Entity') { $out .= ""; if ($canedit) { $out .= ""; } $out .= ""; $out .= ""; $out .= ""; - Ajax::createIframeModalWindow('displaycreditconsumed_' . $data["id"], - $CFG_GLPI["root_doc"] - . "/plugins/credit/front/ticket.php?plugcreditentity=" - . $data["id"], - ['title' => __('Consumed details', 'credit'), - 'reloadonclose' => false]); - $quantity_consumed = PluginCreditTicket::getConsumedForCreditEntity($data['id']); $out .= ""; $out .= ""; } $out .= $header_begin.$header_bottom.$header_end; - $out .= "
".__('Quantity sold', 'credit')."".__('Quantity consumed', 'credit')."".__('Quantity remaining', 'credit')."
"; - echo $out; $out = ""; - Html::showMassiveActionCheckBox(__CLASS__, $data["id"]); + $out .= Html::getMassiveActionCheckBox(__CLASS__, $data["id"]); $out .= ""; - $out .= ""; + $out .= ""; $out .= $data['name']; $out .= ""; $out .= ""; - $out .= Dropdown::getDropdownName(getTableForItemType('PluginCreditType'), - $data['plugin_credit_types_id']); + $out .= Dropdown::getDropdownName(PluginCreditType::getTable(), + $data['plugin_credit_types_id']); $out .= ""; $out .= ($data["is_active"]) ? __('Yes') : __('No'); @@ -265,81 +245,93 @@ static function showForItemtype(Entity $entity, $itemtype='Entity') { $out .= ($data['quantity']==0) ? __('Unlimited') : $data['quantity'];; $out .= ""; - $out .= ""; + $out .= Ajax::createIframeModalWindow('displaycreditconsumed_'.$data["id"], + $CFG_GLPI["root_doc"]. + "/plugins/credit/front/ticket.php?plugcreditentity=". + $data["id"], + ['title' => __('Consumed details', 'credit'), + 'reloadonclose' => false, + 'display' => false]); + + $out .= ""; $out .= $quantity_consumed; $out .= ""; - $out .= ($data['quantity']==0) ? __('Unlimited') : $data['quantity'] - $quantity_consumed; + $out .= ($data['quantity']==0) + ? __('Unlimited') + : $data['quantity'] - $quantity_consumed; $out .= "
\n"; + $out .= ""; if ($canedit) { $massiveactionparams['ontop'] = false; - echo $out; $out = ""; - Html::showMassiveActions($massiveactionparams); - Html::closeForm(); + $out .= Html::showMassiveActions($massiveactionparams); + $out .= Html::closeForm(false); } } else { $out .= "

".__('No credit voucher', 'credit')."

"; } - $out .= "\n"; + $out .= ""; echo $out; } - /** - * Get search function for the class - * - * @return array of search option - **/ - function getSearchOptions() { - - $tab = parent::getSearchOptions(); - - $tab[991]['table'] = $this->getTable(); - $tab[991]['field'] = 'is_active'; - $tab[991]['name'] = __('Active'); - $tab[991]['datatype'] = 'bool'; - - $tab[992]['table'] = $this->getTable(); - $tab[992]['field'] = 'begin_date'; - $tab[992]['name'] = __('Start date'); - $tab[992]['datatype'] = 'date'; - - $tab[993]['table'] = $this->getTable(); - $tab[993]['field'] = 'end_date'; - $tab[993]['name'] = __('End date'); - $tab[993]['datatype'] = 'date'; - - $tab[994]['table'] = $this->getTable(); - $tab[994]['field'] = 'quantity'; - $tab[994]['name'] = __('Quantity sold', 'credit'); - $tab[994]['datatype'] = 'number'; - $tab[994]['min'] = 1; - $tab[994]['max'] = 200; - $tab[994]['step'] = 1; - $tab[994]['toadd'] = array(0 => __('Unlimited')); - - $tab[995]['table'] = getTableForItemType('PluginCreditType'); - $tab[995]['field'] = 'name'; - $tab[995]['name'] = __('Credit voucher type', 'credit'); - $tab[995]['datatype'] = 'dropdown'; + function getSearchOptionsNew() { + + $tab = parent::getSearchOptionsNew(); + + $tab[] = [ + 'id' => 991, + 'table' => self::getTable(), + 'field' => 'is_active', + 'name' => __('Active'), + 'datatype' => 'bool', + ]; + + $tab[] = [ + 'id' => 992, + 'table' => self::getTable(), + 'field' => 'begin_date', + 'name' => __('Start date'), + 'datatype' => 'date', + ]; + + $tab[] = [ + 'id' => 993, + 'table' => self::getTable(), + 'field' => 'end_date', + 'name' => __('End date'), + 'datatype' => 'date', + ]; + + $tab[] = [ + 'id' => 994, + 'table' => self::getTable(), + 'field' => 'quantity', + 'name' => __('Quantity sold', 'credit'), + 'datatype' => 'number', + 'min' => 1, + 'max' => 200, + 'step' => 1, + 'toadd' => [0 => __('Unlimited')], + ]; + + $tab[] = [ + 'id' => 995, + 'table' => PluginCreditType::getTable(), + 'field' => 'name', + 'name' => __('Credit voucher type', 'credit'), + 'datatype' => 'dropdown', + ]; return $tab; } @@ -352,7 +344,7 @@ function getSearchOptions() { static function install(Migration $migration) { global $DB; - $table = getTableForItemType(__CLASS__); + $table = self::getTable(); if (!TableExists($table)) { $migration->displayMessage("Installing $table"); @@ -384,11 +376,8 @@ static function install(Migration $migration) { * @return boolean True if success */ static function uninstall(Migration $migration) { - - $table = getTableForItemType(__CLASS__); - + $table = self::getTable(); $migration->displayMessage("Uninstalling $table"); - $migration->dropTable($table); } diff --git a/inc/ticket.class.php b/inc/ticket.class.php index 47f3e17..b432351 100644 --- a/inc/ticket.class.php +++ b/inc/ticket.class.php @@ -34,35 +34,26 @@ class PluginCreditTicket extends CommonDBTM { public static $rightname = 'ticket'; - static function getTypeName($nb=0) { + static function getTypeName($nb = 0) { return _n('Credit voucher', 'Credit vouchers', $nb, 'credit'); } - /** - * @see CommonGLPI::getTabNameForItem() - **/ - function getTabNameForItem(CommonGLPI $item, $withtemplate=0) { - + function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) { + $nb = self::countForItem($item); switch ($item->getType()) { case 'Ticket' : if ($_SESSION['glpishow_count_on_tabs']) { - return self::createTabEntry(self::getTypeName(2), self::countForItem($item)); + return self::createTabEntry(self::getTypeName($nb), $nb); } else { - return self::getTypeName(2); + return self::getTypeName($nb); } - break; default : - return self::getTypeName(2); - break; + return self::getTypeName($nb); } return ''; } - /** - * @see CommonGLPI::displayTabContentForItem() - **/ - static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtemplate=0) { - + static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) { switch ($item->getType()) { case 'Ticket' : self::showForTicket($item); @@ -75,8 +66,7 @@ static function displayTabContentForItem(CommonGLPI $item, $tabnum=1, $withtempl * @param $item CommonDBTM object **/ public static function countForItem(CommonDBTM $item) { - return countElementsInTable(getTableForItemType(__CLASS__), - "`tickets_id` = '".$item->getID()."'"); + return countElementsInTable(self::getTable(), "`tickets_id` = '".$item->getID()."'"); } /** @@ -88,11 +78,11 @@ public static function countForItem(CommonDBTM $item) { * @param $sqlfilter string to add an SQL filter (default '') * @return array of vouchers **/ - static function getAllForTicket($ID, $start=0, $limit=0, $sqlfilter='') { + static function getAllForTicket($ID, $start = 0, $limit = 0, $sqlfilter = '') { global $DB; $query = "SELECT * - FROM `" . getTableForItemType(__CLASS__) . "` + FROM `".self::getTable()."` WHERE `tickets_id` = '$ID'"; if ($sqlfilter) { $query .= "AND ($sqlfilter) "; @@ -100,10 +90,10 @@ static function getAllForTicket($ID, $start=0, $limit=0, $sqlfilter='') { $query .= "ORDER BY `id` DESC"; if ($limit) { - $query .= " LIMIT ".intval($start)."," . intval($limit); + $query .= " LIMIT ".(int) $start.",".(int) $limit; } - $vouchers = array(); + $vouchers = []; foreach ($DB->request($query) as $data) { $vouchers[$data['id']] = $data; } @@ -121,11 +111,11 @@ static function getAllForTicket($ID, $start=0, $limit=0, $sqlfilter='') { * @param $sqlfilter string to add an SQL filter (default '') * @return array of vouchers **/ - static function getAllForCreditEntity($ID, $start=0, $limit=0, $sqlfilter='') { + static function getAllForCreditEntity($ID, $start = 0, $limit = 0, $sqlfilter = '') { global $DB; $query = "SELECT * - FROM `" . getTableForItemType(__CLASS__) . "` + FROM `".self::getTable()."` WHERE `plugin_credit_entities_id` = '$ID'"; if ($sqlfilter) { $query .= "AND ($sqlfilter) "; @@ -133,10 +123,10 @@ static function getAllForCreditEntity($ID, $start=0, $limit=0, $sqlfilter='') { $query .= " ORDER BY `id` DESC"; if ($limit) { - $query .= " LIMIT ".intval($start)."," . intval($limit); + $query .= " LIMIT ".(int) $start."," .(int) $limit; } - $tickets = array(); + $tickets = []; foreach ($DB->request($query) as $data) { $tickets[$data['id']] = $data; } @@ -153,10 +143,8 @@ static function getConsumedForCreditEntity($ID) { global $DB; $tot = 0; - $table = getTableForItemType(__CLASS__); - $query = "SELECT SUM(`consumed`) - FROM $table + FROM `".self::getTable()."` WHERE `plugin_credit_entities_id` = '".$ID."'"; if ($result = $DB->query($query)) { @@ -202,18 +190,18 @@ static function showForTicket(Ticket $ticket) { $out .= "
"; if ($canedit) { - echo $out; $out = ""; - Html::openMassiveActionsForm('mass'.__CLASS__.$rand); - $massiveactionparams - = array('num_displayed' - => $number, - 'container' - => 'mass'.__CLASS__.$rand, - 'rand' => $rand, - 'specific_actions' - => array('update' => _x('button', 'Update'), - 'purge' => _x('button', 'Delete permanently'))); - Html::showMassiveActions($massiveactionparams); + $out .= Html::getOpenMassiveActionsForm('mass'.__CLASS__.$rand); + $massiveactionparams = [ + 'num_displayed' => $number, + 'container' => 'mass'.__CLASS__.$rand, + 'rand' => $rand, + 'display' => false, + 'specific_actions' => [ + 'update' => _x('button', 'Update'), + 'purge' => _x('button', 'Delete permanently') + ] + ]; + $out .= Html::showMassiveActions($massiveactionparams); } $out .= ""; @@ -232,7 +220,7 @@ static function showForTicket(Ticket $ticket) { $header_end .= ""; $header_end .= ""; $header_end .= ""; - $header_end .= "\n"; + $header_end .= ""; $out.= $header_begin.$header_top.$header_end; foreach (self::getAllForTicket($ID) as $data) { @@ -240,8 +228,7 @@ static function showForTicket(Ticket $ticket) { $out .= ""; if ($canedit) { $out .= ""; } @@ -252,8 +239,8 @@ static function showForTicket(Ticket $ticket) { $out .= $PluginCreditEntity->getName(); $out .= ""; $out .= ""; $out .= "
".__('Date consumed', 'credit')."".__('User consumed', 'credit')."".__('Quantity consumed', 'credit')."
"; - echo $out; $out = ""; - Html::showMassiveActionCheckBox(__CLASS__, $data["id"]); + $out .= Html::getMassiveActionCheckBox(__CLASS__, $data["id"]); $out .= ""; - $out .= Dropdown::getDropdownName(getTableForItemType('PluginCreditType'), - $PluginCreditEntity->getField('plugin_credit_types_id')); + $out .= Dropdown::getDropdownName(PluginCreditType::getTable(), + $PluginCreditEntity->getField('plugin_credit_types_id')); $out .= ""; $out .= Html::convDate($data["date_creation"]); @@ -273,19 +260,18 @@ static function showForTicket(Ticket $ticket) { } $out .= $header_begin.$header_bottom.$header_end; - $out .= "
\n"; + $out .= ""; if ($canedit) { $massiveactionparams['ontop'] = false; - echo $out; $out = ""; - Html::showMassiveActions($massiveactionparams); - Html::closeForm(); + $out .= Html::showMassiveActions($massiveactionparams); + $out .= Html::closeForm(false); } } else { $out .= "

".__('No credit was recorded', 'credit')."

"; } - $out .= "
\n"; + $out .= ""; $out .= "
"; $out .= ""; @@ -310,13 +296,12 @@ static function showForTicket(Ticket $ticket) { static public function postSolutionForm($params) { global $CFG_GLPI; - $item = $params['item']; - $options = $params['options']; - + $item = $params['item']; + $options = $params['options']; $showForm = false; $callers = debug_backtrace(); foreach ($callers as $call) { - if ($call['function']=='showSolutionForm') { + if ($call['function'] == 'showSolutionForm') { $showForm = true; break; } @@ -351,29 +336,28 @@ static function showMinimalForm(Ticket $ticket) { $out .= __('Save and consumed a voucher ?', 'credit'); $out .= ""; $out .= ""; $out .= ""; $out .= ""; } @@ -412,7 +396,7 @@ static function displayConsumed($ID) { $header_end .= ""; $header_end .= ""; $header_end .= ""; - $header_end .= "\n"; + $header_end .= ""; $out .= $header_begin.$header_top.$header_end; foreach (self::getAllForCreditEntity($ID) as $data) { @@ -460,7 +444,7 @@ static function displayConsumed($ID) { } $out .= $header_begin.$header_bottom.$header_end; - $out .= "
"; - echo $out; $out = ""; - Dropdown::showYesNo('plugin_credit_consumed_voucher'); + $out .= Dropdown::showYesNo('plugin_credit_consumed_voucher', 0, -1, ['display' => false]); $out .= "
"; $out .= ""; $out .= ""; - echo $out; $out = ""; - PluginCreditEntity::dropdown(['name' => 'plugin_credit_entities_id', - 'entity' => $ticket->getEntityID(), - 'condition' => "`is_active`='1'"]); + $out .= PluginCreditEntity::dropdown(['name' => 'plugin_credit_entities_id', + 'entity' => $ticket->getEntityID(), + 'display' => false, + 'condition' => "`is_active`='1'"]); $out .= "
"; $out .= ""; $out .= ""; - echo $out; $out = ""; - Dropdown::showNumber("plugin_credit_quantity", ['value' => '', - 'min' => 1, - 'max' => 200, - 'step' => 1]); + $out .= Dropdown::showNumber("plugin_credit_quantity", ['value' => '', + 'min' => 1, + 'max' => 200, + 'step' => 1, + 'display' => false]); $out .= "
".__('Date consumed', 'credit')."".__('User consumed', 'credit')."".__('Quantity consumed', 'credit')."
\n"; + $out .= ""; } echo $out; @@ -480,7 +464,7 @@ static function beforeUpdate(Ticket $ticket) { } if (!is_numeric(Session::getLoginUserID(false)) - || !Session::haveRightsOr('ticket', array(Ticket::STEAL, Ticket::OWN))) { + || !Session::haveRightsOr('ticket', [Ticket::STEAL, Ticket::OWN])) { return false; } @@ -495,10 +479,10 @@ static function beforeUpdate(Ticket $ticket) { 'credit'), true, ERROR); } else { $PluginCreditTicket = new self(); - $input = ['tickets_id' => $ticket->getID(), + $input = ['tickets_id' => $ticket->getID(), 'plugin_credit_entities_id' => $ticket->input['plugin_credit_entities_id'], - 'consumed' => $ticket->input['plugin_credit_quantity'], - 'users_id' => Session::getLoginUserID()]; + 'consumed' => $ticket->input['plugin_credit_quantity'], + 'users_id' => Session::getLoginUserID()]; if ($PluginCreditTicket->add($input)) { Session::addMessageAfterRedirect(__('Credit voucher successfully added.', 'credit'), true, INFO); @@ -508,33 +492,36 @@ static function beforeUpdate(Ticket $ticket) { } - /** - * Get search function for the class - * - * @return array of search option - **/ - function getSearchOptions() { - - $tab = parent::getSearchOptions(); - - $tab[881]['table'] = $this->getTable(); - $tab[881]['field'] = 'date_creation'; - $tab[881]['name'] = __('Date consumed', 'credit'); - $tab[881]['datatype'] = 'date'; - - $tab[882]['table'] = $this->getTable(); - $tab[882]['field'] = 'consumed'; - $tab[882]['name'] = __('Quantity consumed', 'credit'); - $tab[882]['datatype'] = 'number'; - $tab[882]['min'] = 1; - $tab[882]['max'] = 200; - $tab[882]['step'] = 1; - $tab[882]['toadd'] = array(0 => __('Unlimited')); - - $tab[883]['table'] = getTableForItemType('PluginCreditEntity'); - $tab[883]['field'] = 'name'; - $tab[883]['name'] = __('Credit vouchers', 'credit'); - $tab[883]['datatype'] = 'dropdown'; + function getSearchOptionsNew() { + $tab = parent::getSearchOptionsNew(); + + $tab[] = [ + 'id' => 881, + 'table' => self::getTable(), + 'field' => 'date_creation', + 'name' => __('Date consumed', 'credit'), + 'datatype' => 'date', + ]; + + $tab[] = [ + 'id' => 882, + 'table' => self::getTable(), + 'field' => 'consumed', + 'name' => __('Quantity consumed', 'credit'), + 'datatype' => 'number', + 'min' => 1, + 'max' => 200, + 'step' => 1, + 'toadd' => [0 => __('Unlimited')], + ]; + + $tab[] = [ + 'id' => 883, + 'table' => PluginCreditEntity::getTable(), + 'field' => 'name', + 'name' => __('Credit vouchers', 'credit'), + 'datatype' => 'dropdown', + ]; return $tab; } @@ -547,7 +534,7 @@ function getSearchOptions() { static function install(Migration $migration) { global $DB; - $table = getTableForItemType(__CLASS__); + $table = self::getTable(); if (!TableExists($table)) { $migration->displayMessage("Installing $table"); @@ -584,10 +571,8 @@ static function install(Migration $migration) { */ static function uninstall(Migration $migration) { - $table = getTableForItemType(__CLASS__); - + $table = self::getTable(); $migration->displayMessage("Uninstalling $table"); - $migration->dropTable($table); } diff --git a/inc/type.class.php b/inc/type.class.php index 5c64a9c..189e5f1 100644 --- a/inc/type.class.php +++ b/inc/type.class.php @@ -36,7 +36,7 @@ class PluginCreditType extends CommonTreeDropdown { public $dohistory = true; public $can_be_translated = true; - static function getTypeName($nb=0) { + static function getTypeName($nb = 0) { return _n('Credit voucher type', 'Credit vouchers types', $nb, 'credit'); } @@ -48,7 +48,7 @@ static function getTypeName($nb=0) { static function install(Migration $migration) { global $DB; - $table = getTableForItemType(__CLASS__); + $table = self::getTable(); if (!TableExists($table)) { $migration->displayMessage("Installing $table"); @@ -85,10 +85,8 @@ static function install(Migration $migration) { */ static function uninstall(Migration $migration) { - $table = getTableForItemType(__CLASS__); - + $table = self::getTable(); $migration->displayMessage("Uninstalling $table"); - $migration->dropTable($table); } diff --git a/plugin.xml b/plugin.xml index 32e29d7..73004bf 100644 --- a/plugin.xml +++ b/plugin.xml @@ -22,6 +22,10 @@ TECLIB' + + 1.1.0 + 9.2 + 1.0.1 9.1.2 diff --git a/setup.php b/setup.php index 5432a54..bdeaaff 100644 --- a/setup.php +++ b/setup.php @@ -26,7 +26,7 @@ -------------------------------------------------------------------------- */ -define('PLUGIN_CREDIT_VERSION', '1.0.1'); +define('PLUGIN_CREDIT_VERSION', '1.1.0'); /** * Init hooks of the plugin. @@ -46,16 +46,13 @@ function plugin_init_credit() { $PLUGIN_HOOKS['csrf_compliant']['credit'] = true; - Plugin::registerClass('PluginCreditType'); - if (Session::getLoginUserID() && $plugin->isActivated('credit')) { if (Session::haveRight('entity', UPDATE)) { Plugin::registerClass('PluginCreditEntity', ['addtabon' => 'Entity']); } - if (Session::haveRightsOr('ticket', array(Ticket::STEAL, Ticket::OWN))) { - + if (Session::haveRightsOr('ticket', [Ticket::STEAL, Ticket::OWN])) { Plugin::registerClass('PluginCreditTicket', ['addtabon' => 'Ticket']); $PLUGIN_HOOKS['post_item_form']['credit'] = @@ -81,26 +78,22 @@ function plugin_version_credit() { 'author' => 'Teclib\'', 'license' => 'GPLv3', 'homepage' => 'https://github.com/pluginsGLPI/credit', - 'minGlpiVersion' => '9.1.2' + 'requirements' => [ + 'glpi' => [ + 'min' => '9.2', + 'max' => '9.3', + 'dev' => true + ] + ] ]; } /** * Check pre-requisites before install - * OPTIONNAL, but recommanded * * @return boolean */ function plugin_credit_check_prerequisites() { - // Strict version check (could be less strict, or could allow various version) - if (version_compare(GLPI_VERSION, '9.1.2', 'lt')) { - if (method_exists('Plugin', 'messageIncompatible')) { - echo Plugin::messageIncompatible('core', '9.1.2'); - } else { - echo "This plugin requires GLPI >= 9.1.2"; - } - return false; - } return true; } @@ -112,12 +105,5 @@ function plugin_credit_check_prerequisites() { * @return boolean */ function plugin_credit_check_config($verbose = false) { - if (true) { // Your configuration check - return true; - } - - if ($verbose) { - _e('Installed / not configured', 'credit'); - } - return false; + return true; } From 6a4ed720a3eadedec0ef2ce17a54846f7223ac52 Mon Sep 17 00:00:00 2001 From: Alexandre Delaunay Date: Wed, 30 Aug 2017 16:11:13 +0200 Subject: [PATCH 2/3] deleteByCriteria is not static --- inc/entity.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inc/entity.class.php b/inc/entity.class.php index ba5d51c..7bff61f 100644 --- a/inc/entity.class.php +++ b/inc/entity.class.php @@ -68,7 +68,8 @@ public static function countForItem(CommonDBTM $item) { } public function post_purgeItem() { - PluginCreditTicket::deleteByCriteria([ + $pc_ticket = new PluginCreditTicket; + $pc_ticket->deleteByCriteria([ 'plugin_credit_entities_id' => $this->getID() ]); } From 2f1c908bbf2038914dd80ff74ebeca1c983428bf Mon Sep 17 00:00:00 2001 From: Alexandre Delaunay Date: Wed, 30 Aug 2017 17:18:52 +0200 Subject: [PATCH 3/3] TableExists changes --- inc/entity.class.php | 2 +- inc/ticket.class.php | 2 +- inc/type.class.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/inc/entity.class.php b/inc/entity.class.php index 7bff61f..33be55b 100644 --- a/inc/entity.class.php +++ b/inc/entity.class.php @@ -347,7 +347,7 @@ static function install(Migration $migration) { $table = self::getTable(); - if (!TableExists($table)) { + if (!$DB::tableExists($table)) { $migration->displayMessage("Installing $table"); $query = "CREATE TABLE IF NOT EXISTS `$table` ( diff --git a/inc/ticket.class.php b/inc/ticket.class.php index b432351..b2eddfc 100644 --- a/inc/ticket.class.php +++ b/inc/ticket.class.php @@ -536,7 +536,7 @@ static function install(Migration $migration) { $table = self::getTable(); - if (!TableExists($table)) { + if (!$DB::tableExists($table)) { $migration->displayMessage("Installing $table"); $query = "CREATE TABLE IF NOT EXISTS `$table` ( diff --git a/inc/type.class.php b/inc/type.class.php index 189e5f1..31df8df 100644 --- a/inc/type.class.php +++ b/inc/type.class.php @@ -50,7 +50,7 @@ static function install(Migration $migration) { $table = self::getTable(); - if (!TableExists($table)) { + if (!$DB::tableExists($table)) { $migration->displayMessage("Installing $table"); $query = "CREATE TABLE IF NOT EXISTS `$table` (