Newer
Older
<?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();
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
->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, 'addcc', $info);
function addCollaborator($tid, $type=null, $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, null, $type);
$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,
$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'));
return self::_addcollaborator($thread, $user, $form, $type, $info);
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
}
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, $type=null, $info=array()) {
global $thisstaff;
$info += array(
'title' => __('Add a collaborator'),
'action' => sprintf('#thread/%d/add-collaborator/%s',
$thread->getId(), $type),
'onselect' => sprintf('ajax.php/thread/%d/add-collaborator/%s/',
$thread->getId(), $type),
);
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))
$users = array();
foreach ($thread->getCollaborators() as $c)
$users[] = array('id' => $c->getUserId(),
'name' => $c->getName(),
'email' => $c->getEmail());
Http::response(201, $this->json_encode(array(
'id' => $thread->getId(),
$thread->getNumCollaborators())
)
));
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
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);
}
}
?>