Skip to content

Commit

Permalink
Manual fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Nov 10, 2023
1 parent 718303f commit 85747ee
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 291 deletions.
6 changes: 0 additions & 6 deletions front/entity.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,13 @@
* -------------------------------------------------------------------------
*/

/** @file
* @brief
*/

use Glpi\Event;

include('../../../inc/includes.php');

Session::haveRight("entity", UPDATE);

$Entity = new Entity();
$PluginCreditEntity = new PluginCreditEntity();
$PluginCreditType = new PluginCreditType();

if (isset($_POST["add"])) {
$PluginCreditEntity->check(-1, CREATE, $_POST);
Expand Down
6 changes: 0 additions & 6 deletions front/ticket.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@
* -------------------------------------------------------------------------
*/

/** @file
* @brief
*/

use Glpi\Event;

include('../../../inc/includes.php');

Session::haveRight("ticket", UPDATE);
Expand Down
4 changes: 0 additions & 4 deletions front/ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
* -------------------------------------------------------------------------
*/

/** @file
* @brief
*/

include('../../../inc/includes.php');

Html::popHeader(__('Setup'), $_SERVER['PHP_SELF'], true);
Expand Down
30 changes: 15 additions & 15 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@
*/
function plugin_credit_install()
{

$migration = new Migration(PLUGIN_CREDIT_VERSION);

// Parse inc directory
// Parse inc directory
foreach (glob(dirname(__FILE__) . '/inc/*') as $filepath) {
// Load *.class.php files and get the class name
// Load *.class.php files and get the class name
if (preg_match("/inc.(.+)\.class.php/", $filepath, $matches)) {
$classname = 'PluginCredit' . ucfirst($matches[1]);
include_once($filepath);
// If the install method exists, load it
// If the install method exists, load it
if (method_exists($classname, 'install')) {
$classname::install($migration);
}
Expand Down Expand Up @@ -80,16 +79,15 @@ function plugin_credit_install()
*/
function plugin_credit_uninstall()
{

$migration = new Migration(PLUGIN_CREDIT_VERSION);

// Parse inc directory
// Parse inc directory
foreach (glob(dirname(__FILE__) . '/inc/*') as $filepath) {
// Load *.class.php files and get the class name
// Load *.class.php files and get the class name
if (preg_match("/inc.(.+)\.class.php/", $filepath, $matches)) {
$classname = 'PluginCredit' . ucfirst($matches[1]);
include_once($filepath);
// If the install method exists, load it
// If the install method exists, load it
if (method_exists($classname, 'uninstall')) {
$classname::uninstall($migration);
}
Expand Down Expand Up @@ -123,13 +121,15 @@ function plugin_credit_get_datas(NotificationTargetTicket $target)
$ticket->getFromDB($id);
$entity_id = $ticket->fields['entities_id'];

$query = "SELECT
`glpi_plugin_credit_entities`.`name`,
`glpi_plugin_credit_entities`.`quantity`,
(SELECT SUM(`glpi_plugin_credit_tickets`.`consumed`) FROM `glpi_plugin_credit_tickets` WHERE `glpi_plugin_credit_tickets`.`plugin_credit_entities_id` = `glpi_plugin_credit_entities`.`id` AND `glpi_plugin_credit_tickets`.`tickets_id` = {$id}) AS `consumed_on_ticket`,
(SELECT SUM(`glpi_plugin_credit_tickets`.`consumed`) FROM `glpi_plugin_credit_tickets` WHERE `glpi_plugin_credit_tickets`.`plugin_credit_entities_id` = `glpi_plugin_credit_entities`.`id`) AS `consumed_total`
FROM `glpi_plugin_credit_entities`
WHERE `is_active`=1 and `entities_id`={$entity_id}";
$query = <<<SQL
SELECT
`glpi_plugin_credit_entities`.`name`,
`glpi_plugin_credit_entities`.`quantity`,
(SELECT SUM(`glpi_plugin_credit_tickets`.`consumed`) FROM `glpi_plugin_credit_tickets` WHERE `glpi_plugin_credit_tickets`.`plugin_credit_entities_id` = `glpi_plugin_credit_entities`.`id` AND `glpi_plugin_credit_tickets`.`tickets_id` = {$id}) AS `consumed_on_ticket`,
(SELECT SUM(`glpi_plugin_credit_tickets`.`consumed`) FROM `glpi_plugin_credit_tickets` WHERE `glpi_plugin_credit_tickets`.`plugin_credit_entities_id` = `glpi_plugin_credit_entities`.`id`) AS `consumed_total`
FROM `glpi_plugin_credit_entities`
WHERE `is_active`=1 and `entities_id`={$entity_id}
SQL;

foreach ($DB->request($query) as $credit) {
$target->data["credit.ticket"][] = [
Expand Down
112 changes: 53 additions & 59 deletions inc/entity.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
* -------------------------------------------------------------------------
*/

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

class PluginCreditEntity extends CommonDBTM
{
public static $rightname = 'entity';
Expand Down Expand Up @@ -67,9 +63,9 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $
return true;
}

/**
* @param $item CommonDBTM object
**/
/**
* @param $item CommonDBTM object
*/
public static function countForItem(CommonDBTM $item)
{
return countElementsInTable(
Expand Down Expand Up @@ -114,13 +110,13 @@ public function post_purgeItem()
]);
}

/**
* Get all credit vouchers for entity.
*
* @param $ID integer entities ID
* @param $sqlfilter array to add a SQL filter (default [])
* @return array of vouchers
*/
/**
* Get all credit vouchers for entity.
*
* @param $ID integer entities ID
* @param $sqlfilter array to add a SQL filter (default [])
* @return array of vouchers
*/
public static function getAllForEntity($ID, $sqlfilter = []): array
{
/** @var DBmysql $DB */
Expand All @@ -141,12 +137,12 @@ public static function getAllForEntity($ID, $sqlfilter = []): array
return $vouchers;
}

/**
* Show credit vouchers of an entity
*
* @param $entity object Entity
* @param $itemtype string Entity or Ticket
**/
/**
* Show credit vouchers of an entity
*
* @param $entity object Entity
* @param $itemtype string Entity or Ticket
*/
public static function showForItemtype(Entity $entity, $itemtype = 'Entity')
{
$ID = $entity->getField('id');
Expand Down Expand Up @@ -236,7 +232,7 @@ public static function showForItemtype(Entity $entity, $itemtype = 'Entity')
];
MassiveAction::getAddTransferList($specific_actions);

// Remove icons
// Remove icons
$specific_actions = array_map(function ($action) {
return strip_tags($action);
}, $specific_actions);
Expand Down Expand Up @@ -322,9 +318,7 @@ public static function showForItemtype(Entity $entity, $itemtype = 'Entity')
$out .= "<td class='center'>";
$out .= Ajax::createIframeModalWindow(
'displaycreditconsumed_' . $data["id"],
Plugin::getWebDir('credit') .
"/front/ticket.php?plugcreditentity=" .
$data["id"],
Plugin::getWebDir('credit') . "/front/ticket.php?plugcreditentity=" . $data["id"],
['title' => __('Consumed details', 'credit'),
'reloadonclose' => false,
'display' => false
Expand Down Expand Up @@ -372,7 +366,6 @@ public static function showForItemtype(Entity $entity, $itemtype = 'Entity')

public function rawSearchOptions()
{

$tab = parent::rawSearchOptions();

$tab[] = [
Expand Down Expand Up @@ -519,11 +512,11 @@ public static function cronCreditExpired($task)
return 1;
}

/**
* Install all necessary tables for the plugin
*
* @return boolean True if success
*/
/**
* Install all necessary tables for the plugin
*
* @return boolean True if success
*/
public static function install(Migration $migration)
{
/** @var DBmysql $DB */
Expand All @@ -536,47 +529,49 @@ public static function install(Migration $migration)
$table = self::getTable();

if (!$DB->tableExists($table)) {
$query = "CREATE TABLE IF NOT EXISTS `$table` (
`id` int {$default_key_sign} NOT NULL auto_increment,
`name` varchar(255) DEFAULT NULL,
`entities_id` int {$default_key_sign} NOT NULL DEFAULT '0',
`is_recursive` tinyint NOT NULL DEFAULT '0',
`is_active` tinyint NOT NULL DEFAULT '0',
`plugin_credit_types_id` tinyint {$default_key_sign} NOT NULL DEFAULT '0',
`begin_date` timestamp NULL DEFAULT NULL,
`end_date` timestamp NULL DEFAULT NULL,
`quantity` int NOT NULL DEFAULT '0',
`overconsumption_allowed` tinyint NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `entities_id` (`entities_id`),
KEY `is_recursive` (`is_recursive`),
KEY `is_active` (`is_active`),
KEY `plugin_credit_types_id` (`plugin_credit_types_id`),
KEY `begin_date` (`begin_date`),
KEY `end_date` (`end_date`)
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
$query = <<<SQL
CREATE TABLE IF NOT EXISTS `$table` (
`id` int {$default_key_sign} NOT NULL auto_increment,
`name` varchar(255) DEFAULT NULL,
`entities_id` int {$default_key_sign} NOT NULL DEFAULT '0',
`is_recursive` tinyint NOT NULL DEFAULT '0',
`is_active` tinyint NOT NULL DEFAULT '0',
`plugin_credit_types_id` tinyint {$default_key_sign} NOT NULL DEFAULT '0',
`begin_date` timestamp NULL DEFAULT NULL,
`end_date` timestamp NULL DEFAULT NULL,
`quantity` int NOT NULL DEFAULT '0',
`overconsumption_allowed` tinyint NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `entities_id` (`entities_id`),
KEY `is_recursive` (`is_recursive`),
KEY `is_active` (`is_active`),
KEY `plugin_credit_types_id` (`plugin_credit_types_id`),
KEY `begin_date` (`begin_date`),
KEY `end_date` (`end_date`)
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;
SQL;
$DB->query($query) or die($DB->error());
} else {
// 1.5.0
// 1.5.0
$migration->addField($table, 'overconsumption_allowed', 'bool', ['update' => "1"]);

// 1.9.0
// 1.9.0
$migration->addField($table, 'is_recursive', 'bool');
$migration->addKey($table, 'is_recursive');

// 1.10.0
// 1.10.0
$migration->dropField($table, 'is_default'); // Was created during dev phase of 1.10.0, then removed
}

return true;
}

/**
* Uninstall previously installed table of the plugin
*
* @return boolean True if success
*/
/**
* Uninstall previously installed table of the plugin
*
* @return boolean True if success
*/
public static function uninstall(Migration $migration)
{
$table = self::getTable();
Expand All @@ -585,7 +580,6 @@ public static function uninstall(Migration $migration)
return true;
}


public static function getActiveFilter()
{
/** @var DBmysql $DB */
Expand Down
Loading

0 comments on commit 85747ee

Please sign in to comment.