Skip to content

Commit 34c5f31

Browse files
committed
refactor / fix some code
1 parent 0c7c2f4 commit 34c5f31

File tree

6 files changed

+54
-99
lines changed

6 files changed

+54
-99
lines changed

front/config.form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
$PluginSccmConfig->update($_POST);
5050
$sccmDB->testConfiguration($_POST['id']);
51-
Html::redirect(PluginSccmConfig::searchUrl());
51+
Html::back();
5252
} else if (isset($_POST["add"])) {
5353
if ($PluginSccmConfig->add($_POST)) {
5454
if ($_SESSION['glpibackcreated']) {

inc/config.class.php

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ static function canView() {
5050
return Session::haveRight('config', UPDATE);
5151
}
5252

53+
static function canPurge() {
54+
return Session::haveRight('config', UPDATE);
55+
}
56+
5357
static function getTypeName($nb = 0) {
5458
return __("SCCM", "sccm");
5559
}
@@ -130,29 +134,15 @@ function getAllConfigurations() {
130134
return getAllDataFromTable(self::getTable());
131135
}
132136

133-
function loadFirstConfiguration() {
134-
$configurations = $this->getAllConfigurations();
135-
if (empty($configurations)) {
136-
return false;
137-
}
138-
$this->getFromDB(array_values($configurations)[0]['id']);
139-
140-
return true;
137+
public function prepareInputForUpdate($input) {
138+
return self::handleInput($input);
141139
}
142140

143-
function prepareInputForUpdate($input) {
144-
if (isset($input["sccmdb_password"]) AND !empty($input["sccmdb_password"])) {
145-
$input["sccmdb_password"] = (new GLPIKey())->encrypt($input["sccmdb_password"]);
146-
}
147-
148-
if (array_key_exists('inventory_server_url', $input) && !empty($input['inventory_server_url'])) {
149-
$input['inventory_server_url'] = trim($input['inventory_server_url'], '/ ');
150-
}
151-
152-
return $input;
141+
public function prepareInputForAdd($input) {
142+
return self::handleInput($input);
153143
}
154144

155-
function prepareInputForAdd($input) {
145+
public static function handleInput($input) {
156146
if (isset($input["sccmdb_password"]) AND !empty($input["sccmdb_password"])) {
157147
$input["sccmdb_password"] = (new GLPIKey())->encrypt($input["sccmdb_password"]);
158148
}
@@ -167,10 +157,8 @@ function prepareInputForAdd($input) {
167157
static function isIdAutoIncrement()
168158
{
169159
global $DB;
170-
171160
$columns = $DB->query("SHOW COLUMNS FROM glpi_plugin_sccm_configs WHERE FIELD = 'id'");
172161
$data = $columns->fetch_assoc();
173-
Toolbox::logInFile('sccm', "Auto increment ... " . $data["Extra"] . " \n", true);
174162
return str_contains($data["Extra"], "auto_increment");
175163
}
176164

@@ -181,12 +169,12 @@ static function install(Migration $migration) {
181169
$default_collation = DBConnection::getDefaultCollation();
182170
$default_key_sign = DBConnection::getDefaultPrimaryKeySignOption();
183171

184-
$table = 'glpi_plugin_sccm_configs';
185-
Toolbox::logInFile('sccm', "Installing ...\n", true);
172+
$table = self::getTable();
186173

187174
if (!$DB->tableExists($table)) {
188175

189-
Toolbox::logInFile('sccm', "Table not exists, creating ...\n", true);
176+
$migration->displayMessage("Installing SCCM plugin ...");
177+
$migration->displayMessage("Table not exists, creating ...");
190178

191179
$query = "CREATE TABLE `". $table."`(
192180
`id` int {$default_key_sign} NOT NULL AUTO_INCREMENT,
@@ -210,34 +198,32 @@ static function install(Migration $migration) {
210198
PRIMARY KEY (`id`)
211199
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
212200

213-
$DB->queryOrDie($query, __("Error when using glpi_plugin_sccm_configs table.", "sccm")
214-
. "<br />".$DB->error());
215-
216-
$query = "INSERT INTO `$table`
217-
(date_mod, sccmdb_host, sccmdb_dbname,
218-
sccmdb_user, sccmdb_password, inventory_server_url)
219-
VALUES (NOW(), 'srv_sccm','bdd_sccm','user_sccm','',
220-
NULL)";
201+
$DB->queryOrDie($query, $DB->error());
221202

222-
$DB->queryOrDie($query, __("Error when using glpi_plugin_sccm_configs table.", "sccm")
223-
. "<br />" . $DB->error());
203+
// Update display preferences
204+
$migration->updateDisplayPrefs([PluginSccmConfig::class => [1, 2, 3, 4, 5, 6]]);
224205

225206
} else {
226-
if (!self::isIdAutoIncrement())
227-
{
228-
Toolbox::logInFile('sccm', "Changing to Auto increment ... \n", true);
207+
208+
$migration->displayMessage("Updating SCCM plugin ...");
209+
210+
// Need to move ID column to auto increment.
211+
if (!self::isIdAutoIncrement()) {
229212
$migration->changeField("glpi_plugin_sccm_configs", "id", "id", "autoincrement");
230213
$migration->migrationOneTable('glpi_plugin_sccm_configs');
214+
// Update display preferences
215+
$migration->updateDisplayPrefs([PluginSccmConfig::class => [1, 2, 3, 4, 5, 6]]);
231216
}
217+
232218
if (!$DB->fieldExists($table, 'sccm_config_name')) {
233219
$migration->addField("glpi_plugin_sccm_configs", "sccm_config_name", "VARCHAR(255)");
234220
$migration->migrationOneTable('glpi_plugin_sccm_configs');
235-
}
221+
}
236222

237223
if (!$DB->fieldExists($table, 'sccm_collection_name')) {
238224
$migration->addField("glpi_plugin_sccm_configs", "sccm_collection_name", "VARCHAR(255)");
239225
$migration->migrationOneTable('glpi_plugin_sccm_configs');
240-
}
226+
}
241227

242228
if (!$DB->fieldExists($table, 'verify_ssl_cert')) {
243229
$migration->addField("glpi_plugin_sccm_configs", "verify_ssl_cert", "tinyint NOT NULL default '0'");
@@ -327,21 +313,23 @@ static function install(Migration $migration) {
327313
]
328314
);
329315
}
316+
330317
}
331318

332319
return true;
333320
}
334321

335-
336322
static function uninstall() {
323+
/** @var \DBmysql $DB */
337324
global $DB;
338-
339-
if ($DB->tableExists('glpi_plugin_sccm_configs')) {
340-
341-
$query = "DROP TABLE `glpi_plugin_sccm_configs`";
342-
$DB->queryOrDie($query, $DB->error());
325+
$table = self::getTable();
326+
if ($DB->tableExists($table)) {
327+
$DB->queryOrDie("DROP TABLE IF EXISTS `" . self::getTable() . "`") or die($DB->error());
328+
$displayPref = new DisplayPreference();
329+
foreach ($displayPref->find(['itemtype' => PluginSccmConfig::class]) as $pref) {
330+
$displayPref->delete($pref);
331+
}
343332
}
344-
return true;
345333
}
346334

347335
static function searchUrl() {
@@ -363,7 +351,7 @@ function showForm($ID, $options = []) {
363351

364352
if (!$this->getFromDB($ID)) {
365353
$this->getEmpty();
366-
}
354+
}
367355

368356
$this->initForm($ID, $options);
369357
$this->showFormHeader($options);
@@ -451,13 +439,10 @@ function showForm($ID, $options = []) {
451439
echo "</td></tr>\n";
452440

453441
$this->showFormButtons($options);
454-
//$this->showFormButtons(['candel'=>false]);
455-
456442
return false;
457443
}
458444

459-
static function canPurge() {
460-
return Session::haveRight('config', UPDATE);
445+
public static function getIcon() {
446+
return "fa-solid fa-dice-d20";
461447
}
462-
463448
}

inc/menu.class.php

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,20 @@ static function getTypeName($nb = 0) {
3535
return __('Menu', 'sccm');
3636
}
3737

38-
static function getSearchURL($full = true)
39-
{
40-
$url = Plugin::getWebDir('sccm', false);
41-
return $url . '/front/config.php';
42-
}
43-
44-
static function getNewURL($full = true)
45-
{
46-
$url = Plugin::getWebDir('sccm', false);
47-
return $url . '/front/config.form.php';
48-
}
49-
5038
static function getMenuName() {
5139
return __('SCCM', 'sccm');
5240
}
5341

54-
public static function getIcon() {
55-
return "fa-solid fa-dice-d20";
56-
}
57-
5842
static function getMenuContent() {
5943
$menu = [
60-
'title' => self::getMenuName(),
61-
'page' => self::getSearchURL(false),
62-
'icon' => self::getIcon(),
44+
'title' => PluginSccmConfig::getMenuName(),
45+
'page' => PluginSccmConfig::getSearchURL(false),
46+
'icon' => PluginSccmConfig::getIcon(),
6347
'options' => [],
6448
];
6549

66-
$menu['links']['add'] = self::getNewURL();
67-
return $menu;
50+
$menu['links']['add'] = PluginSccmConfig::getFormURL(false);
51+
return $menu;
6852
}
6953

70-
}
54+
}

inc/sccm.class.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ function getDatas($type, $deviceid, $limit = 99999999) {
8282

8383
$PluginSccmSccmdb = $this->sccmdb;
8484

85-
$datas = [];
86-
8785
switch ($type) {
8886
case 'processors' :
8987
$fields = ['Manufacturer00','Name00','NormSpeed00','AddressWidth00','CPUKey00','NumberOfCores00', 'NumberOfLogicalProcessors00'];
@@ -420,7 +418,6 @@ static function cronInfo($name) {
420418
if ($name == "SCCMPush") {
421419
return ['description' => __("Interface - SCCMPush", "sccm")];
422420
}
423-
424421
}
425422

426423
static function executeCollect($task) {
@@ -447,10 +444,10 @@ static function executeCollect($task) {
447444

448445
if (!is_dir($REP_XML)) {
449446
mkdir($REP_XML);
450-
}
447+
}
451448

452449
if ($PluginSccmConfig->getField('active_sync') == 1) {
453-
$PluginSccmSccmdb = new PluginSccmSccmdb();
450+
$PluginSccmSccmdb = new PluginSccmSccmdb();
454451
if (!$PluginSccmSccmdb->connect($config['id'])) {
455452
Toolbox::logInFile('sccm', "Error connecting to database on config ". $config['sccm_config_name'] ." \n", true);
456453
continue;
@@ -678,7 +675,7 @@ static function executePush($task) {
678675
foreach(libxml_get_errors() as $error) {
679676
$errors = $errors . $error->message . "\n";
680677
}
681-
Toolbox::logInFile('sccm', "Can't load the file with the path : ".$REP_XML."\n\n".$errors."\n", true);
678+
Toolbox::logInFile('sccm', "Can't load the file with the path : ".$REP_XML."\n\n".$errors."\n", true);
682679
}
683680
}
684681
Toolbox::logInFile('sccm', "Push completed on ".$config['sccm_config_name']."\n", true);

inc/sccmdb.class.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ class PluginSccmSccmdb {
3838
var $dbconn;
3939

4040
function testConfiguration($id) {
41-
if ($this->connect($id)) {
41+
if ($this->connect($id)) {
4242
Session::addMessageAfterRedirect(__("Connection successfull!", 'sccm'), false, INFO, false);
4343
$this->disconnect();
44-
} else {
44+
} else {
4545
Session::addMessageAfterRedirect(__("Connection failed!", 'sccm'), false, ERROR, false);
46-
}
46+
}
4747
}
4848

4949
function connect($id) {
@@ -74,34 +74,26 @@ function connect($id) {
7474
return true;
7575
}
7676

77-
function disconnect() {
78-
77+
public function disconnect() {
7978
sqlsrv_close($this->dbconn);
80-
8179
}
8280

83-
function exec_query($query) {
84-
81+
public function exec_query($query) {
8582
$result = sqlsrv_query($this->dbconn, $query) or die('Query error : ' . print_r(sqlsrv_errors(), true));
8683
if ($result == false) {
8784
die( FormatErrors( sqlsrv_errors()));
8885
}
8986
return $result;
90-
9187
}
9288

93-
function FormatErrors($errors) {
94-
89+
public function FormatErrors($errors) {
9590
foreach ($errors as $error) {
96-
$debug = "";
9791
$state = "SQLSTATE: ".$error['SQLSTATE'];
9892
$code = "Code: ".$error['code'];
9993
$message = "Message: ".$error['message'];
100-
101-
echo $state."</br>".$code."<br>".$message."<br>";
94+
echo $state . "</br>" . $code . "<br>" . $message . "<br>";
10295
Toolbox::logInFile("sccm", $state.PHP_EOL.$code.PHP_EOL.$message.PHP_EOL);
10396
}
104-
10597
}
10698

10799
}

inc/sccmxml.class.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,6 @@ function setOS() {
140140
$OPERATINGSYSTEM->addChild('SERVICE_PACK', $this->data['OSD-CSDVersion']);
141141
}
142142

143-
144-
145-
146143
function setBios() {
147144
$CONTENT = $this->sxml->CONTENT[0];
148145
$CONTENT->addChild('BIOS');

0 commit comments

Comments
 (0)