forked from SimpleMachines/SimpleDesk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleDesk-SSI.php
More file actions
387 lines (344 loc) · 14.4 KB
/
SimpleDesk-SSI.php
File metadata and controls
387 lines (344 loc) · 14.4 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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/**************************************************************
* Simple Desk Project - www.simpledesk.net *
***************************************************************
* An advanced help desk modification built on SMF *
***************************************************************
* *
* * Copyright 2025 - SimpleDesk.net *
* *
* This file and its contents are subject to the license *
* included with this distribution, license.txt, which *
* states that this software is New BSD Licensed. *
* Any questions, please contact SimpleDesk.net *
* *
***************************************************************
* SimpleDesk Version: 2.1.0 *
* File Info: SimpleDesk-SSI.php *
**************************************************************/
/**
* This file handles data gathering primarily for SSI.php purposes. It expects Subs-SimpleDesk.php to have been required as this
* will still be needed for helpdesk permissions checks and base functions.
*
* The function names imply who is expected to be the target, ssi_user functions are those intended for users, ssi_staff for staff
* members, such as ssi_userTickets() is primarily for displaying the tickets started by a given user, ssi_staffAssignedTickets()
* for all the tickets assigned to a given member of staff.
*
* No support for displaying these through SHTML is provided, nor is any planned.
*
* Unlike other SimpleDesk functions, these use a SSI style camel casing.
*
* @package source
* @since 2.0
*/
if (!defined('SMF'))
die('Hacking attempt...');
/**
* Makes SMF hook system happy that it has a function to call for SSI.
* @since 2.1
*/
function ssi_shd_called()
{
return true;
}
/**
* Gets a list of the tickets currently open that are a given user's (subject to ticket visibility).
*
* @param int $started_by The user id whose tickets you want to examine, defaults to the current user.
* @param array|int $dept An array of department ids whose tickets you want to examine, defaults to all departments.
* @param int $limit The number of tickets to limit to, default 10.
* @param string $output_method Set to 'echo' for displaying content, set to 'array' to simply return data.
* @return null|array An array of data, more details under the underlying function {@link ssi_getSDTickets()}
* @since 2.0
*/
function ssi_userTickets($started_by = 0, $dept = array(), $limit = 10, $output_method = 'echo')
{
global $user_info;
if (empty($started_by))
$started_by = $user_info['id'];
$limit = (int) $limit;
if (empty($limit))
return;
$query_where = 'hdt.id_member_started = {int:started}';
$query_where_params = array(
'started' => $started_by,
);
$dept_ids = array();
if (!empty($dept))
{
// Did they perhaps just put an integer of some sort?
if (!is_array($dept))
$dept = array(0 => $dept);
foreach ($dept as $id)
$dept_ids[] = (int) $id;
if (!empty($dept_ids))
{
$query_where .= ' AND hdt.dept IN ({array_int:dept})';
$query_where_params['dept'] = $dept_ids;
}
}
return ssi_getSDTickets($query_where, $query_where_params, $limit, 'hdt.id_ticket ASC', $output_method);
}
/**
* Gets a list of the tickets currently open that are assigned to the current user (presumably staff, subject to ticket visibility).
*
* @param int $assignee The user id whose tickets whose assigned tickets you want to examine, defaults to the current user.
* @param int|array $dept An array of department ids whose tickets you want to examine, defaults to all departments.
* @param int $limit The number of tickets to limit to, default 10.
* @param string $output_method Set to 'echo' for displaying content, set to 'array' to simply return data.
* @return null|array An array of data, more details under the underlying function {@link ssi_getSDTickets()}
* @since 2.0
*/
function ssi_staffAssignedTickets($assignee = 0, $dept = array(), $limit = 10, $output_method = 'echo')
{
global $user_info;
if (empty($assignee))
$assignee = $user_info['id'];
$limit = (int) $limit;
if (empty($limit))
return;
$query_where = 'hdt.id_member_assigned = {int:assigned}';
$query_where_params = array(
'assigned' => $assignee,
);
$dept_ids = array();
if (!empty($dept))
{
// Did they perhaps just put an integer of some sort?
if (!is_array($dept))
$dept = array(0 => $dept);
foreach ($dept as $id)
$dept_ids[] = (int) $id;
if (!empty($dept_ids))
{
$query_where .= ' AND hdt.dept IN ({array_int:dept})';
$query_where_params['dept'] = $dept_ids;
}
}
return ssi_getSDTickets($query_where, $query_where_params, $limit, 'hdt.id_ticket ASC', $output_method);
}
/**
* Gets a list of all tickets based on urgency criteria given (subject to ticket visibility)
*
* @param int $urgency The urgency of tickets you want to get.
* @param int|array $dept An array of department ids whose tickets you want to examine, defaults to all departments.
* @param int $limit The number of tickets to limit to, default 10.
* @param string $output_method Set to 'echo' for displaying content, set to 'array' to simply return data.
* @return null|array An array of data, more details under the underlying function {@link ssi_getSDTickets()}
* @since 2.0
*/
function ssi_staffTicketsUrgency($urgency, $dept = array(), $limit = 10, $output_method = 'echo')
{
$query_where = 'hdt.urgency = {int:urgency}';
$query_where_params = array(
'urgency' => $urgency,
);
$dept_ids = array();
if (!empty($dept))
{
// Did they perhaps just put an integer of some sort?
if (!is_array($dept))
$dept = array(0 => $dept);
foreach ($dept as $id)
$dept_ids[] = (int) $id;
if (!empty($dept_ids))
{
$query_where .= ' AND hdt.dept IN ({array_int:dept})';
$query_where_params['dept'] = $dept_ids;
}
}
return ssi_getSDTickets($query_where, $query_where_params, $limit, 'hdt.id_ticket ASC', $output_method);
}
/**
* Gets tickets based on supplied criteria; this is a helper function not really intended to be called directly.
*
* @todo Finish writing and documenting this function.
* @param string $query_where SQL clauses to be supplied to the query in addition to {query_see_ticket} - note 'AND' is not required at the start.
* @param array $query_where_params Key/value associative array to be injected into the query, related to $query_where.
* @param int $query_limit Number of items to limit the query to.
* @param string $query_order The clause to order tickets by, defaults to tickets by order of creation.
* @return null|array An array of arrays, each primary item containing the following:
* <ul>
* <li>id: Main ticket id</li>
* <li>display_id: Formatted ticket id in [0000x] format</li>
* <li>subject: Ticket subject</li>
* <li>short_subject: Shortened version of ticket subject</li>
* <li>href: Ticket href</li>
* <li>opener: array of details about the ticket starter:
* <ul>
* <li>id: user id of the person who opened the ticket</li>
* <li>name: username of the person who opened the ticket</li>
* <li>link: link to the profile of the person who opened the ticket</li>
* </ul>
* </li>
* <li>replier: array of details about the last person to reply to the ticket:
* <ul>
* <li>id: user id of the last person to reply to the ticket</li>
* <li>name: username of the last person to reply to the ticket</li>
* <li>link: link to the profile of the last person to reply to the ticket</li>
* </ul>
* </li>
* <li>assigned: array of details about the person who the ticket is assigned to:
* <ul>
* <li>id: user id of the person who the ticket is assigned to</li>
* <li>name: username of the person who the ticket is assigned to or 'Unassigned' otherwise</li>
* <li>link: link to the profile of the person who the ticket is assigned to or 'Unassigned' otherwise</li>
* </ul>
* </li>
* <li>num_replies: Number of replies in the ticket</li>
* <li>start_time: Formatted string of time the ticket was opened</li>
* <li>start_timestamp: Raw timestamp (adjusted for timezones) of ticket being opened</li>
* <li>last_time: Formatted string of time the ticket was last replied to</li>
* <li>last_timestamp: Raw timestamp (adjusted for timezones) of ticket's last reply</li>
* <li>private: Whether the ticket is private or not</li>
* <li>urgency_id: Number representing ticket urgency</li>
* <li>urgency_string: String representing ticket urgency</li>
* <li>status_id: Number representing ticket status</li>
* <li>status_text: String representing ticket status</li>
* <li>department: Number representing ticket department ID</li>
* </ul>
* @since 2.0
*/
function ssi_getSDTickets($query_where, $query_where_params = array(), $query_limit = 0, $query_order = 'hdt.id_ticket ASC', $output_method = 'echo')
{
global $smcFunc, $scripturl, $txt, $modSettings;
$query_limit = (int) $query_limit;
$query = shd_db_query('', '
SELECT hdt.id_ticket, hdt.subject, hdt.num_replies, hdt.private, hdt.urgency, hdt.status, hdt.dept,
hdtr_first.poster_time AS start_time, hdt.last_updated AS last_time,
COALESCE(mem.real_name, hdtr_first.poster_name) AS starter_name, COALESCE(mem.id_member, 0) AS starter_id,
COALESCE(ma.real_name, 0) AS assigned_name, COALESCE(ma.id_member, 0) AS assigned_id,
COALESCE(mm.real_name, hdtr_last.modified_name) AS modified_name, COALESCE(mm.id_member, 0) AS modified_id
FROM {db_prefix}helpdesk_tickets AS hdt
INNER JOIN {db_prefix}helpdesk_ticket_replies AS hdtr_first ON (hdt.id_first_msg = hdtr_first.id_msg)
INNER JOIN {db_prefix}helpdesk_ticket_replies AS hdtr_last ON (hdt.id_last_msg = hdtr_last.id_msg)
LEFT JOIN {db_prefix}members AS mem ON (hdt.id_member_started = mem.id_member)
LEFT JOIN {db_prefix}members AS ma ON (hdt.id_member_assigned = ma.id_member)
LEFT JOIN {db_prefix}members AS mm ON (hdt.id_member_updated = mm.id_member)
WHERE {query_see_ticket} AND ' . $query_where . '
ORDER BY ' . $query_order . '
' . ($query_limit == 0 ? '' : 'LIMIT ' . $query_limit),
array_merge($query_where_params, array(
))
);
$tickets = array();
while ($row = $smcFunc['db_fetch_assoc']($query))
{
censorText($row['subject']);
$tickets[] = array(
'id' => $row['id_ticket'],
'display_id' => str_pad($row['id_ticket'], $modSettings['shd_zerofill'], '0', STR_PAD_LEFT),
'subject' => $row['subject'],
'short_subject' => shorten_subject($row['subject'], 25),
'href' => $scripturl . '?action=helpdesk;sa=ticket;ticket=' . $row['id_ticket'],
'opener' => array(
'id' => $row['starter_id'],
'name' => $row['starter_name'],
'link' => shd_profile_link($row['starter_name'], $row['starter_id']),
),
'replier' => array(
'id' => $row['modified_id'],
'name' => $row['modified_name'],
'link' => shd_profile_link($row['modified_name'], $row['modified_id']),
),
'assigned' => array(
'id' => $row['assigned_id'],
'name' => empty($row['assigned_name']) ? $txt['shd_unassigned'] : $row['assigned_name'],
'link' => empty($row['assigned_name']) ? '<span class="error">' . $txt['shd_unassigned'] . '</span>' : shd_profile_link($row['assigned_name'], $row['assigned_id']),
),
'start_time' => timeformat($row['start_time']),
'start_timestamp' => forum_time(true, $row['start_time']),
'last_time' => timeformat($row['last_time']),
'last_timestamp' => forum_time(true, $row['last_time']),
'num_replies' => $row['num_replies'],
'private' => !empty($row['private']),
'urgency_id' => $row['urgency'],
'urgency_string' => $txt['shd_urgency_' . $row['urgency']],
'status_id' => $row['status'],
'status_text' => $txt['shd_status_' . $row['status']],
'department' => $row['dept'],
);
}
$smcFunc['db_free_result']($query);
if (empty($tickets) || $output_method != 'echo')
return $tickets;
// output this stuff
echo '
<table border="0" class="ssi_table">';
foreach ($tickets as $ticket)
echo '
<tr>
<td align="right" valign="top" nowrap="nowrap">
[', $ticket['status_text'], ']
</td>
<td valign="top">
<a href="', $ticket['href'], '">', $ticket['subject'], '</a>
', $txt['by'], ' ', $ticket['replier']['link'], '
</td>
<td align="right" nowrap="nowrap">
', $ticket['last_time'], '
</td>
</tr>';
echo '
</table>';
}
/**
* Gets a list of all staff members within the helpdesk.
*
* @param boolean $honour_admin_setting Within the administration panel is the option to exclude forum admins from being considered staff (so can't assign tickets to them). If true (default), assume the outcome of that should be applied here too.
* @param string $output_method Leave as default or explicitly set to 'echo' for this function to output a list of helpdesk staff members, set to 'array' to block output, and have the standard contents back.
* @return array The return is always an array of members that are staff; contains many details about members since SMF's member context is loaded (including avatar, personal text and so on)
* @since 2.0
*/
function ssi_staffMembers($honour_admin_setting = true, $output_method = 'echo')
{
global $modSettings, $smcFunc, $memberContext;
$staff = shd_members_allowed_to('shd_staff');
if ($honour_admin_setting && !empty($modSettings['shd_admins_not_assignable']))
{
$admins = array();
$query = $smcFunc['db_query']('', '
SELECT id_member
FROM {db_prefix}members
WHERE id_group = {int:id_group}
OR FIND_IN_SET({int:id_group}, additional_groups)',
array(
'id_group' => 1,
)
);
while ($row = $smcFunc['db_fetch_row']($query))
$admins[] = $row[0];
$staff = array_diff($staff, $admins);
}
if (empty($staff))
return array();
loadMemberData($staff);
if ($output_method == 'echo')
echo '
<table border="0" class="ssi_table">';
$query_members = array();
foreach ($staff as $member)
{
// Load their context data.
if (!loadMemberContext($member))
continue;
// Store this member's information.
$query_members[$member] = $memberContext[$member];
// Only do something if we're echo'ing.
if ($output_method == 'echo')
echo '
<tr>
<td align="right" valign="top" nowrap="nowrap">
', $query_members[$member]['link'], '
<br>', $query_members[$member]['blurb'], '
<br>', $query_members[$member]['avatar']['image'], '
</td>
</tr>';
}
// End the table if appropriate.
if ($output_method == 'echo')
echo '
</table>';
// Send back the data.
return $query_members;
}