Newer
Older
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
<?php
/*********************************************************************
ajax.thread.php
AJAX interface for thread
Peter Rotich <peter@osticket.com>
Copyright (c) 2015 osTicket
http://www.osticket.com
Released under the GNU General Public License WITHOUT ANY WARRANTY.
See LICENSE.TXT for details.
vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/
if(!defined('INCLUDE_DIR')) die('403');
include_once(INCLUDE_DIR.'class.ticket.php');
require_once(INCLUDE_DIR.'class.ajax.php');
require_once(INCLUDE_DIR.'class.note.php');
include_once INCLUDE_DIR . 'class.thread_actions.php';
class ThreadAjaxAPI extends AjaxController {
function lookup() {
global $thisstaff;
if(!is_numeric($_REQUEST['q']))
return self::lookupByEmail();
$limit = isset($_REQUEST['limit']) ? (int) $_REQUEST['limit']:25;
$tickets=array();
$visibility = Q::any(array(
'staff_id' => $thisstaff->getId(),
'team_id__in' => $thisstaff->teams->values_flat('team_id'),
));
if (!$thisstaff->showAssignedOnly() && ($depts=$thisstaff->getDepts())) {
$visibility->add(array('dept_id__in' => $depts));
}
$hits = TicketModel::objects()
->filter(Q::any(array(
'number__startswith' => $_REQUEST['q'],
)))
->filter($visibility)
->values('number', 'user__emails__address')
->annotate(array('tickets' => SqlAggregate::COUNT('ticket_id')))
->order_by('-created')
->limit($limit);
foreach ($hits as $T) {
$tickets[] = array('id'=>$T['number'], 'value'=>$T['number'],
'info'=>"{$T['number']} — {$T['user__emails__address']}",
'matches'=>$_REQUEST['q']);
}
if (!$tickets)
return self::lookupByEmail();
return $this->json_encode($tickets);
}
function addRemoteCollaborator($tid, $bk, $id) {
global $thisstaff;
if (!($thread=Thread::lookup($tid))
|| !($object=$thread->getObject())
|| !$object->checkStaffPerm($thisstaff))
Http::response(404, 'No such thread');
elseif (!$bk || !$id)
Http::response(422, 'Backend and user id required');
elseif (!($backend = StaffAuthenticationBackend::getBackend($bk)))
Http::response(404, 'User not found');
$user_info = $backend->lookup($id);
$form = UserForm::getUserForm()->getForm($user_info);
$info = array();
if (!$user_info)
$info['error'] = __('Unable to find user in directory');
return self::_addcollaborator($thread, null, $form, $info);
}
//Collaborators utils
function addCollaborator($tid, $uid=0) {
global $thisstaff;
if (!($thread=Thread::lookup($tid))
|| !($object=$thread->getObject())
|| !$object->checkStaffPerm($thisstaff))
Http::response(404, __('No such thread'));
$user = $uid? User::lookup($uid) : null;
//If not a post then assume new collaborator form
if(!$_POST)
return self::_addcollaborator($thread, $user);
$user = $form = null;
if (isset($_POST['id']) && $_POST['id']) { //Existing user/
$user = User::lookup($_POST['id']);
} else { //We're creating a new user!
$form = UserForm::getUserForm()->getForm($_POST);
$user = User::fromForm($form);
}
$errors = $info = array();
if ($user) {
// FIXME: Refuse to add ticket owner??
if (($c=$thread->addCollaborator($user,
array('isactive'=>1), $errors))) {
$info = array('msg' => sprintf(__('%s added as a collaborator'),
Format::htmlchars($c->getName())));
return self::_collaborators($thread, $info);
}
}
if($errors && $errors['err']) {
$info +=array('error' => $errors['err']);
} else {
Michael
committed
$info +=array('error' =>__('Unable to add collaborator.').' '.__('Internal error occurred'));
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
}
return self::_addcollaborator($thread, $user, $form, $info);
}
function updateCollaborator($tid, $cid) {
global $thisstaff;
if (!($thread=Thread::lookup($tid))
|| !($object=$thread->getObject())
|| !$object->checkStaffPerm($thisstaff))
Http::response(405, 'No such thread');
if (!($c=Collaborator::lookup(array(
'id' => $cid,
'thread_id' => $thread->getId())))
|| !($user=$c->getUser()))
Http::response(406, 'Unknown collaborator');
$errors = array();
if(!$user->updateInfo($_POST, $errors))
return self::_collaborator($c ,$user->getForms($_POST), $errors);
$info = array('msg' => sprintf('%s updated successfully',
Format::htmlchars($c->getName())));
return self::_collaborators($thread, $info);
}
function viewCollaborator($tid, $cid) {
global $thisstaff;
if (!($thread=Thread::lookup($tid))
|| !($object=$thread->getObject())
|| !$object->checkStaffPerm($thisstaff))
Http::response(404, 'No such thread');
if (!($collaborator=Collaborator::lookup(array(
'id' => $cid,
'thread_id' => $thread->getId()))))
Http::response(404, 'Unknown collaborator');
return self::_collaborator($collaborator);
}
function showCollaborators($tid) {
global $thisstaff;
if(!($thread=Thread::lookup($tid))
|| !($object=$thread->getObject())
|| !$object->checkStaffPerm($thisstaff))
Http::response(404, 'No such thread');
if ($thread->getCollaborators())
return self::_collaborators($thread);
return self::_addcollaborator($thread);
}
function previewCollaborators($tid) {
global $thisstaff;
if (!($thread=Thread::lookup($tid))
|| !($object=$thread->getObject())
|| !$object->checkStaffPerm($thisstaff))
Http::response(404, 'No such thread');
ob_start();
include STAFFINC_DIR . 'templates/collaborators-preview.tmpl.php';
$resp = ob_get_contents();
ob_end_clean();
return $resp;
}
function _addcollaborator($thread, $user=null, $form=null, $info=array()) {
global $thisstaff;
$info += array(
'title' => __('Add a collaborator'),
'action' => sprintf('#thread/%d/add-collaborator',
$thread->getId()),
'onselect' => sprintf('ajax.php/thread/%d/add-collaborator/',
$thread->getId()),
);
ob_start();
include STAFFINC_DIR . 'templates/user-lookup.tmpl.php';
$resp = ob_get_contents();
ob_end_clean();
return $resp;
}
function updateCollaborators($tid) {
global $thisstaff;
if (!($thread=Thread::lookup($tid))
|| !($object=$thread->getObject())
|| !$object->checkStaffPerm($thisstaff))
Http::response(404, 'No such thread');
$errors = $info = array();
if ($thread->updateCollaborators($_POST, $errors))
Http::response(201, $this->json_encode(array(
'id' => $thread->getId(),
'text' => sprintf('Recipients (%d of %d)',
$thread->getNumActiveCollaborators(),
$thread->getNumCollaborators())
)
));
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
if($errors && $errors['err'])
$info +=array('error' => $errors['err']);
return self::_collaborators($thread, $info);
}
function _collaborator($collaborator, $form=null, $info=array()) {
global $thisstaff;
$info += array('action' => sprintf('#thread/%d/collaborators/%d',
$collaborator->thread_id, $collaborator->getId()));
$user = $collaborator->getUser();
ob_start();
include(STAFFINC_DIR . 'templates/user.tmpl.php');
$resp = ob_get_contents();
ob_end_clean();
return $resp;
}
function _collaborators($thread, $info=array()) {
ob_start();
include(STAFFINC_DIR . 'templates/collaborators.tmpl.php');
$resp = ob_get_contents();
ob_end_clean();
return $resp;
}
function triggerThreadAction($ticket_id, $thread_id, $action) {
$thread = ThreadEntry::lookup($thread_id);
if (!$thread)
Http::response(404, 'No such ticket thread entry');
if ($thread->getThread()->getObjectId() != $ticket_id)
Http::response(404, 'No such ticket thread entry');
$valid = false;
foreach ($thread->getActions() as $group=>$list) {
foreach ($list as $name=>$A) {
if ($A->getId() == $action) {
$valid = true; break;
}
}
}
if (!$valid)
Http::response(400, 'Not a valid action for this thread');
$thread->triggerAction($action);
}
}
?>