-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_function.php
336 lines (293 loc) · 12.1 KB
/
common_function.php
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
***********************************************************************************************
* Common functions for the Admidio plugin InventoryManager
*
* @see https://github.com/MightyMCoder/InventoryManager/ The InventoryManager GitHub project
* @author MightyMCoder
* @copyright 2024 - today MightyMCoder
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 only
*
*
* Methods:
* defineConstantsPIM() : Define necessary constants if not already defined
* isUserAuthorizedForPIM($scriptName) : Check if the user is authorized to access the plugin
* isUserAuthorizedForPreferencesPIM() : Check if the user is authorized to access the Preferences module
* isUserAuthorizedForAddinPIM() : Check if the user is authorized to see the
* Inventory Manager Addin on the profile page
* isKeeperAuthorizedToEdit($keeper) : Check if the keeper is authorized to edit spezific item data
* getMenuIdByScriptNamePIM($scriptName) : Get menu ID by script name
* convlanguagePIM($field_name) : Translate field name according to naming conventions
* getNewNameInternPIM($name, $index) : Generate a new internal name
* genNewSequencePIM() : Generate a new sequence number
* umlautePIM($tmptext) : Replace umlauts in the text
* getPreferencePanelPIM($group, $id, $title, $icon, $body) : Generate HTML for a preference panel
* getSqlOrganizationsUsersCompletePIM() : Get all users with their id, name, and address
* getSqlOrganizationsUsersShortPIM() : Get all users with their id and name
***********************************************************************************************
*/
require_once(__DIR__ . '/../../adm_program/system/common.php');
// Define necessary constants if not already defined
defineConstantsPIM();
/**
* Define necessary constants if not already defined
*
* @return void
*/
function defineConstantsPIM() : void
{
if (!defined('PLUGIN_FOLDER_IM')) {
define('PLUGIN_FOLDER_IM', '/' . basename(__DIR__));
}
if (!defined('TBL_INVENTORY_MANAGER_FIELDS')) {
define('TBL_INVENTORY_MANAGER_FIELDS', TABLE_PREFIX . '_inventory_manager_fields');
}
if (!defined('TBL_INVENTORY_MANAGER_DATA')) {
define('TBL_INVENTORY_MANAGER_DATA', TABLE_PREFIX . '_inventory_manager_data');
}
if (!defined('TBL_INVENTORY_MANAGER_ITEMS')) {
define('TBL_INVENTORY_MANAGER_ITEMS', TABLE_PREFIX . '_inventory_manager_items');
}
if (!defined('TBL_INVENTORY_MANAGER_LOG')) {
define('TBL_INVENTORY_MANAGER_LOG', TABLE_PREFIX . '_inventory_manager_log');
}
}
/**
* Check if the user is authorized to access the plugin
*
* @param string $scriptName The script name of the plugin
* @return bool true if the user is authorized
*/
function isUserAuthorizedForPIM($scriptName) : bool
{
global $gMessage, $gL10n, $gDb, $gCurrentUser;
$gCurrentUser = $GLOBALS['gCurrentUser'];
$userIsAuthorized = false;
$menId = getMenuIdByScriptNamePIM($scriptName);
if ($menId === null) {
$gMessage->show($gL10n->get('PLG_INVENTORY_MANAGER_MENU_URL_ERROR', [$scriptName]), $gL10n->get('SYS_ERROR'));
} else {
$sql = 'SELECT men_id, men_com_id, com_name_intern FROM ' . TBL_MENU . '
LEFT JOIN ' . TBL_COMPONENTS . ' ON com_id = men_com_id
WHERE men_id = ? ORDER BY men_men_id_parent DESC, men_order;';
$menuStatement = $gDb->queryPrepared($sql, array($menId));
while ($row = $menuStatement->fetch()) {
if ((int)$row['men_com_id'] === 0 || Component::isVisible($row['com_name_intern'])) {
$displayMenu = new RolesRights($gDb, 'menu_view', $row['men_id']);
$rolesDisplayRight = $displayMenu->getRolesIds();
if (count($rolesDisplayRight) === 0 || $displayMenu->hasRight($gCurrentUser->getRoleMemberships())) {
$userIsAuthorized = true;
}
}
}
}
return $userIsAuthorized;
}
/**
* Check if the user is authorized to access the Preferences module
*
* @return bool true if the user is authorized
*/
function isUserAuthorizedForPreferencesPIM() : bool
{
global $pPreferences, $gCurrentUser;
$gCurrentUser = $GLOBALS['gCurrentUser'];
if ($gCurrentUser->isAdministrator()) {
return true;
}
foreach ($pPreferences->config['access']['preferences'] as $roleId) {
if ($gCurrentUser->isMemberOfRole((int)$roleId)) {
return true;
}
}
return false;
}
/**
* Check if the user is authorized to see the Inventory Manager Addin on the profile page
*
* @return bool true if the user is authorized
*/
function isUserAuthorizedForAddinPIM() : bool
{
global $gDb;
$sql = 'SELECT men_id, men_name, men_name_intern
FROM ' . TBL_MENU . '
WHERE men_men_id_parent IS NULL
ORDER BY men_order';
$mainNodesStatement = $gDb->queryPrepared($sql);
while ($mainNodes = $mainNodesStatement->fetch()) {
$menuNodes = new MenuNode($mainNodes['men_name_intern'], $mainNodes['men_name']);
$nodeId = $mainNodes['men_id'];
$sql = 'SELECT men_id, men_com_id, men_name_intern, men_name, men_description, men_url, men_icon, com_name_intern
FROM ' . TBL_MENU . '
LEFT JOIN ' . TBL_COMPONENTS . ' ON com_id = men_com_id
WHERE men_men_id_parent = ?
ORDER BY men_men_id_parent DESC, men_order';
$nodesStatement = $gDb->queryPrepared($sql, array($nodeId));
while ($node = $nodesStatement->fetch(PDO::FETCH_ASSOC)) {
if ((int) $node['men_com_id'] === 0 || Component::isVisible($node['com_name_intern'])) {
if ($node['men_url'] === "/adm_plugins/InventoryManager/inventory_manager.php" && $menuNodes->menuItemIsVisible($node['men_id'])) {
return true;
}
}
}
}
return false;
}
/**
* Check if the keeper is authorized to edit spezific item data
*
* @param int|null $keeper The user ID of the keeper
* @return bool true if the keeper is authorized
*/
function isKeeperAuthorizedToEdit(int $keeper = null) : bool
{
global $pPreferences, $gCurrentUser;
$gCurrentUser = $GLOBALS['gCurrentUser'];
if ($pPreferences->config['Optionen']['allow_keeper_edit'] === 1) {
if (isset($keeper) && $keeper === $gCurrentUser->getValue('usr_id')) {
return true;
}
}
return false;
}
/**
* Get menu ID by script name
*
* @param string $scriptName The script name of the plugin
* @return int|null The menu ID or null if not found
*/
function getMenuIdByScriptNamePIM($scriptName) : ?int
{
global $gDb;
$sql = 'SELECT men_id FROM ' . TBL_MENU . ' WHERE men_url = ?;';
$menuStatement = $gDb->queryPrepared($sql, array($scriptName));
if ($menuStatement->rowCount() === 1) {
return (int)$menuStatement->fetch()['men_id'];
}
return null;
}
/**
* Translate field name according to naming conventions
*
* @param string $field_name field name to translate
* @return string translated field name
*/
function convlanguagePIM($field_name) : string
{
global $gL10n;
return (substr($field_name, 3, 1) === '_') ? $gL10n->get($field_name) : $field_name;
}
/**
* Generate a new internal name
*
* @param string $name name to generate internal name from
* @param int $index index to append to the internal name
* @return string new internal name
*/
function getNewNameInternPIM($name, $index) : string
{
global $gDb;
$name = umlautePIM($name);
$newNameIntern = strtoupper(str_replace(' ', '_', $name));
if ($index > 1) {
$newNameIntern .= '_' . $index;
}
$sql = 'SELECT imf_id FROM ' . TBL_INVENTORY_MANAGER_FIELDS . ' WHERE imf_name_intern = ?;';
$userFieldsStatement = $gDb->queryPrepared($sql, array($newNameIntern));
if ($userFieldsStatement->rowCount() > 0) {
return getNewNameInternPIM($name, ++$index);
}
return $newNameIntern;
}
/**
* Generate a new sequence number
*
* @return int new sequence number
*/
function genNewSequencePIM() : int
{
global $gDb, $gCurrentOrgId;
$sql = 'SELECT max(imf_sequence) as max_sequence FROM ' . TBL_INVENTORY_MANAGER_FIELDS . ' WHERE (imf_org_id = ? OR imf_org_id IS NULL);';
$statement = $gDb->queryPrepared($sql, array($gCurrentOrgId));
$row = $statement->fetch();
return $row['max_sequence'] + 1;
}
/**
* Replace umlauts in the text
*
* @param string $tmptext text to replace umlauts in
* @return string text with replaced umlauts
*/
function umlautePIM($tmptext) : string
{
$replacements = [
'ü' => 'ue',
'ä' => 'ae',
'ö' => 'oe',
'ß' => 'ss',
'Ü' => 'Ue',
'Ä' => 'Ae',
'Ö' => 'Oe',
'.' => '',
',' => '',
'/' => ''
];
return str_replace(array_keys($replacements), array_values($replacements), htmlentities($tmptext));
}
/**
* Generate HTML for a preference panel
*
* @param string $group group the preference panel belongs to
* @param string $id unique ID of the preference panel
* @param string $title title of the preference panel
* @param string $icon icon of the preference panel
* @param string $body body of the preference panel
* @return string HTML for the preference panel
*/
function getPreferencePanelPIM($group, $id, $title, $icon, $body) : string
{
return '
<div class="card" id="' . $group . '_panel_' . $id . '">
<div class="card-header" data-toggle="collapse" data-target="#collapse_' . $id . '">
<i class="' . $icon . ' fa-fw"></i>' . $title . '
</div>
<div id="collapse_' . $id . '" class="collapse" aria-labelledby="headingOne" data-parent="#accordion_preferences">
<div class="card-body">
' . $body . '
</div>
</div>
</div>
';
}
/**
* Get all users with their id, name, and address
*
* @return string SQL query to get all users with their ID and name
*/
function getSqlOrganizationsUsersCompletePIM() : string
{
global $gProfileFields, $gCurrentOrgId;
return 'SELECT usr_id, CONCAT(last_name.usd_value, \', \', first_name.usd_value, IFNULL(CONCAT(\', \', postcode.usd_value),\'\'), IFNULL(CONCAT(\' \', city.usd_value),\'\'), IFNULL(CONCAT(\', \', street.usd_value),\'\') ) as name
FROM ' . TBL_USERS . '
JOIN ' . TBL_USER_DATA . ' as last_name ON last_name.usd_usr_id = usr_id AND last_name.usd_usf_id = ' . $gProfileFields->getProperty('LAST_NAME', 'usf_id') . '
JOIN ' . TBL_USER_DATA . ' as first_name ON first_name.usd_usr_id = usr_id AND first_name.usd_usf_id = ' . $gProfileFields->getProperty('FIRST_NAME', 'usf_id') . '
LEFT JOIN ' . TBL_USER_DATA . ' as postcode ON postcode.usd_usr_id = usr_id AND postcode.usd_usf_id = ' . $gProfileFields->getProperty('POSTCODE', 'usf_id') . '
LEFT JOIN ' . TBL_USER_DATA . ' as city ON city.usd_usr_id = usr_id AND city.usd_usf_id = ' . $gProfileFields->getProperty('CITY', 'usf_id') . '
LEFT JOIN ' . TBL_USER_DATA . ' as street ON street.usd_usr_id = usr_id AND street.usd_usf_id = ' . $gProfileFields->getProperty('ADDRESS', 'usf_id') . '
WHERE usr_valid = 1 AND EXISTS (SELECT 1 FROM ' . TBL_MEMBERS . ', ' . TBL_ROLES . ', ' . TBL_CATEGORIES . ' WHERE mem_usr_id = usr_id AND mem_rol_id = rol_id AND mem_begin <= \'' . DATE_NOW . '\' AND mem_end > \'' . DATE_NOW . '\' AND rol_valid = 1 AND rol_cat_id = cat_id AND (cat_org_id = ' . $gCurrentOrgId . ' OR cat_org_id IS NULL)) ORDER BY last_name.usd_value, first_name.usd_value;';
}
/**
* Get all users with their id and name
*
* @return string SQL query to get all users with their ID and name
*/
function getSqlOrganizationsUsersShortPIM() : string
{
global $gProfileFields, $gCurrentOrgId;
return 'SELECT usr_id, CONCAT(last_name.usd_value, \', \', first_name.usd_value) as name
FROM ' . TBL_USERS . '
JOIN ' . TBL_USER_DATA . ' as last_name ON last_name.usd_usr_id = usr_id AND last_name.usd_usf_id = ' . $gProfileFields->getProperty('LAST_NAME', 'usf_id') . '
JOIN ' . TBL_USER_DATA . ' as first_name ON first_name.usd_usr_id = usr_id AND first_name.usd_usf_id = ' . $gProfileFields->getProperty('FIRST_NAME', 'usf_id') . '
WHERE usr_valid = 1 AND EXISTS (SELECT 1 FROM ' . TBL_MEMBERS . ', ' . TBL_ROLES . ', ' . TBL_CATEGORIES . ' WHERE mem_usr_id = usr_id AND mem_rol_id = rol_id AND mem_begin <= \'' . DATE_NOW . '\' AND mem_end > \'' . DATE_NOW . '\' AND rol_valid = 1 AND rol_cat_id = cat_id AND (cat_org_id = ' . $gCurrentOrgId . ' OR cat_org_id IS NULL)) ORDER BY last_name.usd_value, first_name.usd_value;';
}