forked from mateusznitka/protocolsmanager
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathhook.php
More file actions
201 lines (188 loc) · 7.18 KB
/
hook.php
File metadata and controls
201 lines (188 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* Install the plugin
*/
function plugin_protocolsmanager_install(): bool
{
global $DB;
$version = plugin_version_protocolsmanager();
$migration = new Migration($version['version']);
// Helper: create table if not exists
$createTable = function (string $name, string $schema, array $inserts = []) use ($DB) {
if (!$DB->tableExists($name)) {
if (!$DB->query($schema)) {
Toolbox::logInFile('php-errors', "Error creating table $name: " . $DB->error() . "\n");
return;
}
foreach ($inserts as $insert) {
if (!$DB->query($insert)) {
Toolbox::logInFile('php-errors', "Error inserting defaults for $name: " . $DB->error() . "\n");
}
}
}
};
// Profiles table
$createTable(
'glpi_plugin_protocolsmanager_profiles',
"CREATE TABLE glpi_plugin_protocolsmanager_profiles (
id INT(11) NOT NULL AUTO_INCREMENT,
profile_id INT(11),
plugin_conf CHAR(1) COLLATE utf8_unicode_ci DEFAULT NULL,
tab_access CHAR(1) COLLATE utf8_unicode_ci DEFAULT NULL,
make_access CHAR(1) COLLATE utf8_unicode_ci DEFAULT NULL,
delete_access CHAR(1) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8_unicode_ci",
[
sprintf(
"INSERT INTO glpi_plugin_protocolsmanager_profiles (profile_id, plugin_conf, tab_access, make_access, delete_access)
VALUES (%d, 'w', 'w', 'w', 'w')",
$_SESSION['glpiactiveprofile']['id'] ?? 0
)
]
);
// Config table
$createTable(
'glpi_plugin_protocolsmanager_config',
"CREATE TABLE glpi_plugin_protocolsmanager_config (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(255),
title VARCHAR(255),
font VARCHAR(255),
fontsize VARCHAR(255),
logo VARCHAR(255),
logo_width INT(11) DEFAULT NULL,
logo_height INT(11) DEFAULT NULL,
content TEXT,
footer TEXT,
city VARCHAR(255),
serial_mode INT(2),
column1 VARCHAR(255),
column2 VARCHAR(255),
orientation VARCHAR(10),
breakword INT(2),
email_mode INT(2),
upper_content TEXT,
email_template INT(2),
author_name VARCHAR(255),
author_state INT(2),
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8_unicode_ci",
[
"INSERT INTO glpi_plugin_protocolsmanager_config
(name, title, font, fontsize, content, footer, city, serial_mode, orientation, breakword, email_mode, author_name, author_state)
VALUES
('Equipment report',
'Certificate of delivery of {owner}',
'Roboto',
'9',
'User: \\n I have read the terms of use of IT equipment in the Example Company.',
'Example Company \\n Example Street 21 \\n 01-234 Example City',
'Example city',
1,
'Portrait',
1,
2,
'Test Division',
1)",
"INSERT INTO glpi_plugin_protocolsmanager_config
(name, title, font, fontsize, content, footer, city, serial_mode, orientation, breakword, email_mode, author_name, author_state)
VALUES
('Equipment report 2',
'Certificate of delivery of {owner}',
'Roboto',
'9',
'User: \\n I have read the terms of use of IT equipment in the Example Company.',
'Example Company \\n Example Street 21 \\n 01-234 Example City',
'Example city',
1,
'Portrait',
1,
2,
'Test Division',
1)"
]
);
// Email config table
$createTable(
'glpi_plugin_protocolsmanager_emailconfig',
"CREATE TABLE glpi_plugin_protocolsmanager_emailconfig (
id INT(11) NOT NULL AUTO_INCREMENT,
tname VARCHAR(255),
send_user INT(2),
email_content TEXT,
email_subject VARCHAR(255),
email_footer VARCHAR(255),
recipients VARCHAR(255),
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8_unicode_ci",
[
"INSERT INTO glpi_plugin_protocolsmanager_emailconfig
(tname, send_user, email_content, email_subject, recipients)
VALUES
('Email default', 2, 'Testmail', 'Testmail', 'Testmail')"
]
);
// Protocols table
$createTable(
'glpi_plugin_protocolsmanager_protocols',
"CREATE TABLE glpi_plugin_protocolsmanager_protocols (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(255),
user_id INT(11),
gen_date DATETIME,
author VARCHAR(255),
document_id INT(11),
document_type VARCHAR(255),
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8_unicode_ci"
);
// Update config table fields if upgrading from older versions
$fieldsToAdd = [
'author_name' => "ALTER TABLE glpi_plugin_protocolsmanager_config ADD author_name VARCHAR(255) AFTER email_template",
'author_state' => "ALTER TABLE glpi_plugin_protocolsmanager_config ADD author_state INT(2) AFTER author_name",
'title' => "ALTER TABLE glpi_plugin_protocolsmanager_config ADD title VARCHAR(255) AFTER name",
'logo_width' => "ALTER TABLE glpi_plugin_protocolsmanager_config ADD logo_width INT(11) DEFAULT NULL AFTER logo",
'logo_height' => "ALTER TABLE glpi_plugin_protocolsmanager_config ADD logo_height INT(11) DEFAULT NULL AFTER logo_width"
];
foreach ($fieldsToAdd as $field => $sql) {
if (!$DB->fieldExists('glpi_plugin_protocolsmanager_config', $field)) {
if (!$DB->query($sql)) {
Toolbox::logInFile('php-errors', "Error adding field $field: " . $DB->error() . "\n");
}
}
}
$migration->executeMigration();
return true;
}
/**
* Uninstall the plugin
*/
function plugin_protocolsmanager_uninstall(): bool
{
global $DB;
$tables = [
'glpi_plugin_protocolsmanager_protocols',
'glpi_plugin_protocolsmanager_config',
'glpi_plugin_protocolsmanager_profiles',
'glpi_plugin_protocolsmanager_emailconfig'
];
foreach ($tables as $table) {
$DB->query("DROP TABLE IF EXISTS `$table`");
}
return true;
}
/**
* Safe loader to prevent early table access
*/
function plugin_protocolsmanager_getRights(?int $profile_id = null)
{
global $DB;
if (!$DB->tableExists('glpi_plugin_protocolsmanager_profiles')) {
return []; // Avoid query before installation
}
return $DB->request([
'FROM' => 'glpi_plugin_protocolsmanager_profiles',
'WHERE' => ['profile_id' => $profile_id ?? 0]
])->current();
}