Skip to content

Commit

Permalink
Config request source
Browse files Browse the repository at this point in the history
  • Loading branch information
xacobofg committed Jan 10, 2024
1 parent 56edb4b commit 1b792bf
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 0 deletions.
45 changes: 45 additions & 0 deletions front/config.form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/*
-------------------------------------------------------------------------
GappEssentials plugin for GLPI
Copyright (C) 2019 by the TICgal
https://tic.gal
https://github.com/pluginsGLPI/gappessentials
-------------------------------------------------------------------------
LICENSE
This file is part of GappEssentials.
GappEssentials is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GappEssentials is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GappEssentials. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/

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

$plugin = new Plugin();
if (!$plugin->isInstalled('gappessentials') || !$plugin->isActivated('gappessentials')) {
Html::displayNotFoundError();
}

Session::checkRight('config', UPDATE);

$config = new PluginGappessentialsConfig();
if (isset($_POST["update"])) {
$config->check($_POST['id'], UPDATE);
$config->update($_POST);
Html::back();
}

Html::redirect($CFG_GLPI["root_doc"] . "/front/config.form.php?forcetab=" . urlencode('PluginGappessentialsConfig$1'));
14 changes: 14 additions & 0 deletions inc/apirest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,9 @@ public function call()
case 'location':
return $this->returnResponse($this->location($this->parameters));
break;
case 'defaultConfig':
return $this->returnResponse($this->defaultConfig($this->parameters));
break;
default:
$this->messageLostError();
break;
Expand Down Expand Up @@ -672,4 +675,15 @@ protected function location($params = [])

return $info;
}

protected function defaultConfig($params = [])
{

$config = new PluginGappessentialsConfig();
$config->getFromDB(1);

$data = $config->fields;

return $data;
}
}
148 changes: 148 additions & 0 deletions inc/config.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
/*
-------------------------------------------------------------------------
GappEssentials plugin for GLPI
Copyright (C) 2019 by the TICgal
https://tic.gal
https://github.com/pluginsGLPI/gappessentials
-------------------------------------------------------------------------
LICENSE
This file is part of GappEssentials.
GappEssentials is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GappEssentials is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GappEssentials. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------
*/

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

class PluginGappessentialsConfig extends CommonDBTM
{
static private $_instance = null;

public function __construct()
{
global $DB;
if ($DB->tableExists($this->getTable())) {
$this->getFromDB(1);
}
}

static function canCreate()
{
return Session::haveRight('config', UPDATE);
}

static function canView()
{
return Session::haveRight('config', READ);
}

static function canUpdate()
{
return Session::haveRight('config', UPDATE);
}

static function getTypeName($nb = 0)
{
return __("Gapp configuration", "gappessentials");
}

static function getInstance()
{
if (!isset(self::$_instance)) {
self::$_instance = new self();
if (!self::$_instance->getFromDB(1)) {
self::$_instance->getEmpty();
}
}
return self::$_instance;
}

static function getConfig($update = false)
{
static $config = null;
if (is_null(self::$config)) {
$config = new self();
}
if ($update) {
$config->getFromDB(1);
}
return $config;
}

static function showConfigForm()
{

$config = new self();
$config->getFromDB(1);

$config->showFormHeader(['colspan' => 1]);
echo "<tr class='tab_bg_1'>";
echo "<td>" . __("Request source") . "</td><td>";

$condition = ['is_active' => 1, 'is_ticketheader' => 1];
RequestType::dropdown(['value' => $config->fields["requesttypes_id"], 'condition' => $condition]);
echo "</td>";
echo "</tr>";

$config->showFormButtons(['candel' => false]);

return false;
}

function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
{
if ($item->getType() == 'Config') {
return self::getTypeName();
}
return '';
}

static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
{
if ($item->getType() == 'Config') {
self::showConfigForm($item);
}
return true;
}

static function install(Migration $migration)
{
global $DB;

$default_charset = DBConnection::getDefaultCharset();
$default_collation = DBConnection::getDefaultCollation();
$default_key_sign = DBConnection::getDefaultPrimaryKeySignOption();

$table = self::getTable();
$config = new self();
if (!$DB->tableExists($table)) {
$migration->displayMessage("Installing $table");
$query = "CREATE TABLE IF NOT EXISTS $table (
`id` int {$default_key_sign} NOT NULL auto_increment,
`requesttypes_id` int {$default_key_sign} NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
$DB->query($query) or die($DB->error());
$config->add([
'id' => 1,
'requesttypes_id' => 0,
]);
}
}
}
5 changes: 5 additions & 0 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ function plugin_init_gappessentials()
global $PLUGIN_HOOKS;

$PLUGIN_HOOKS['csrf_compliant']['gappessentials'] = true;
$plugin = new Plugin();
if ($plugin->isActivated('gappessentials')) {
Plugin::registerClass('PluginGappessentialsConfig', ['addtabon' => 'Config']);
$PLUGIN_HOOKS['config_page']['gappessentials'] = 'front/config.form.php';
}
}

/**
Expand Down

0 comments on commit 1b792bf

Please sign in to comment.