-
Notifications
You must be signed in to change notification settings - Fork 35
/
Ticket_model.php
368 lines (305 loc) · 13.2 KB
/
Ticket_model.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
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
<?PHP
require __DIR__."/constants.php";
class Ticket_model extends BaseMySQL_model {
public function __construct()
{
parent::__construct(TABLE_TICKETS);
$this->load->model("core/Session_model", "Session");
$this->load->model("user/User_model", "User");
$this->load->model("notification/Email_model", "Email");
}
public function test()
{
$threadId = 2;
$from = "test_user";
$to = "test_admin";
//status 1 = open, 0 = closed
$data = array(
"threadid" => $threadId,
"from" => $from,
"to" => $to,
"subject" => "start subject thread",
"body" => 'hello, test body',
"seen_status" => 0,
);
$tId = $this->create($data);
$data2 = array(
"threadid" => $threadId,
"from" => $to,
"to" => $from,
"subject" => "start subject thread",
"body" => 'hello, test body',
"seen_status" => 0,
);
$tId2 = $this->create($data2);
$data3 = array(
"threadid" => $threadId,
"from" => $from,
"to" => $to,
"subject" => "start subject thread",
"body" => 'hello, test body',
"seen_status" => 0,
);
$tId3 = $this->create($data3);
if($tId) {
echo "Ticket create() ---> successfull";
} else {
echo "Ticket create() ---> ERROR!";
}
$updateData = array(
"seen_status" => 1
);
$updateRes = $this->update($tId, $updateData);
if($updateRes) {
echo "Ticket update() ---> Successfull";
} else {
echo "Ticket update() ---> Error";
}
$ticket = $this->get(array('id' => $tId));
if(count($ticket) > 0) {
echo "Ticket --> get() --> Successfull!";
} else {
echo "Ticket --> get() --> ERROR!";
}
if($ticket[0]['seen_status'] == $updateData['seen_status']) {
echo "Ticket --> update() --> Successfull!";
} else {
echo "Ticket --> update() --> ERROR";
}
$this->deleteById($tId);
}
public function getByThread($tId)
{
$query = array('threadid' => $tId);
return parent::getBy("*", $query);
}
public function getTicketNoFromID($id) {
if(!$id)
return "-";
return CLIENT_TICKET_PREFIX . $this->pad($id, CLIENT_TICKET_ID_LENGTH);
}
public function pad($num, $size) {
$s = (string)(1000000 + (int)$num);
return substr($s, strlen($s) - $size);
}
public function create($data)
{
$info = getValuesOfKeys($data, array('owner','purpose', 'subject', 'message', 'assign_to', 'assign_on', 'severity', 'priority', 'category', 'cc', 'data'));
$attachments = $info['data'];
$info['data'] = json_encode($info['data']);
if(!$info['owner'])
$info['owner'] = $this->Session->getLoggedDetails()['username'];
if (!empty($info['subject']) && !empty($info['message'])) {
$info = array_merge($info, array('ticket_no' => NULL, 'created' => time(), 'status' => TICKET_STATUS_OPEN));
$res = parent::add($info);
if($res){
$ticket_no = $this->getTicketNoFromID($res);
$result = parent::setByID($res, array('ticket_no' => $ticket_no));
// add attachment reference
$this->addAttachmentRef($attachments['attachments'], $ticket_no);
// send update Email
$usernames = array($info['owner']);
array_merge($usernames, explode(';', $info['cc']));
//if($result)
// $this->sendUpdateEmail($usernames,'[#'.$ticket_no.'] '.$info['subject'], $info['message']);
return $ticket_no;
}
}
else $res = -1;
return $res;
}
public function addAttachmentRef($attachments, $ref){
if($attachments && !empty($attachments)){
$attach_ref = array();
foreach ($attachments as $attachment) {
array_push($attach_ref, array('ref'=> $ref, 'name'=>$attachment['file_name'], 'path'=> $attachment['path'], 'uploaded_by'=> $this->Session->getLoggedDetails()['username']));
}
return $this->db->insert_batch('attachments', $attach_ref);
}
}
public function updateTicket($data, $meta){
if(!empty($data['id'])){
$info = array_merge($data, array('updated'=> time()));
$res = parent::setByID($data['id'], $data);
$info = parent::getBy(array('owner', 'cc', 'ticket_no', 'subject'), array('id'=>$data['id']));
// send update Email
// if(!empty($info) && $res){
// $info= $info[0];
// $usernames = array($info['owner']);
// $usernames = array_merge(explode(';', $info['cc']), $usernames);
// $this->sendUpdateEmail($usernames,'[#'.$info["ticket_no"].'] '.$info['subject'], 'From @'.$info['owner'].': <br>'.$meta['plain_txt_message']);
// }
}
else $res = -1;
return $res;
}
public function getAllCategories(){
return TICKET_CATEGORIES;
}
public function getAllPriorities(){
return TICKET_PRIORITIES;
}
public function getAllSeverities(){
return TICKET_SEVERITIES;
}
public function getAllStatus(){
return TICKET_STATUS;
}
public function get($data)
{
return parent::getBy("*", $data);
}
public function getAllTickets()
{
return parent::getAll("*");
}
public function deleteById($tId)
{
$query = array('id' => $tId);
return parent::deleteBy($query);
}
public function add_thread($data, $sendEmail=FALSE)
{
$res=$this->db->insert(TABLE_MESSAGES, $data);
$info = parent::getBy(array('owner', 'cc', 'ticket_no', 'subject'), array('ticket_no'=>$data['ticket']));
// send update Email
// if(!empty($info) && $res){
// $info= $info[0];
// $usernames = array($info['owner'], $data['owner']);
// $usernames = array_merge(explode(';', $info['cc']), $usernames);
// $this->sendUpdateEmail($usernames,'[#'.$info["ticket_no"].'] '.$info['subject'], 'From @'.$data['owner'].': <br>'.$data['message']);
// }
return $res;
}
/**
* Process mails from IMAP endpoint, process the content and accordingly create reply messages or new tickets
*/
public function processIMAPEmail($data){
$subject = $data['subject'];
$owner = $this->User->getBy(array('username'), array('email'=> $data['from'][0]['address']));
if(empty($owner))
$owner = $data['from'][0]['address']; // user's email address
else
$owner = $owner[0]['username'];
if(substr($subject, 0, 2) === '[#' || substr($subject, 0, 6) === 'Re: [#' || substr($subject, 0, 6) === 'RE: [#') //is a reply to existing mail thread
{
print_r('Replying to thread based on ticket number in to subject line');
$message_thread = array('created'=>time());
preg_match_all("/\[([^\]]*)\]/", $subject, $matches);
$message_thread['ticket'] = substr($matches[1][0], 1, strlen($matches[1][0]));
$message_thread['message'] = $this->Email->getCurrentEmailContent($data['text']);
$message_thread['owner'] = $owner;
$message_thread['type'] = 1; //message type
$message_thread['to'] = array();
if(!empty($data['cc'])){
foreach ($data['cc'] as $cc) {
array_push($message_thread['to'], $cc['address']);
}
}
$message_thread['to'] = implode(';', $message_thread['to']);
// $message_thread['data']['attachment'] = TODO attachments download and attach with this message
print_r($message_thread);
return $this->add_thread($message_thread);
}
else{
$to_addresses = array();
if(!empty($data['to'])){ // create new ticket if mail came to specific helpdesk email
foreach ($data['to'] as $to) {
array_push($to_addresses, $to['address']);
}
if(in_array(CLIENT_HELPDESK_EMAIL, $to_addresses)){
print_r('Creating a new ticket via'. CLIENT_HELPDESK_EMAIL);
$ticket = array('created'=>time(), 'subject'=> $data['subject'], 'message'=> $this->Email->getCurrentEmailContent($data['text']),
'owner'=>$owner);
$ticket['cc'] = array();
if(!empty($data['cc'])){
foreach ($data['cc'] as $cc) {
array_push($ticket['cc'], $cc['address']);
}
}
$ticket['cc'] = implode(';', $ticket['cc']);
// $ticket['data']['attachment'] = TODO attachments download and attach with this message
print_r($ticket);
return $this->create($ticket);
}
else{
foreach ($to_addresses as $to) {
$username = explode('@', $to)[0];
// check if username is ticket id ?
$ticket = parent::getBy(array('ticket_no'), array('ticket_no'=> $username));
if(!empty($ticket))
{
// This is redundant, need to arrange this code.
print_r('Replying to thread based on ticket number in to field');
$message_thread = array('created'=>time());
$message_thread['ticket'] = $ticket[0]['ticket_no'];
$message_thread['message'] = $this->Email->getCurrentEmailContent($data['text']);
$message_thread['owner'] = $owner;
$message_thread['type'] = 1; //message type
$message_thread['to'] = array();
if(!empty($data['cc'])){
foreach ($data['cc'] as $cc) {
array_push($message_thread['to'], $cc['address']);
}
}
$message_thread['to'] = implode(';', $message_thread['to']);
// $message_thread['data']['attachment'] = TODO attachments download and attach with this message
print_r($message_thread);
return $this->add_thread($message_thread);
}
}
}
}
}
}
//TODO: should this be in User_model? but this is Ticket specific only
public function getEmailFromUsername($username){
if(!$this->checkIfEmail($username))
return $username.'@'.CLIENT_DOMAIN;
else return $username;
}
public function getUsernameFromEmail($email){
if(!$this->checkIfEmail($email)){
$split = explode('@', $email);
$domain = $split[1];
if($domain == CLIENT_DOMAIN) return $split[0];
}
return $email;
}
public function sendUpdateEmail($users, $subject, $message){
$to_emails = array();
foreach ($users as $user) {
array_push($to_emails, $this->getEmailFromUsername($user));
}
return $this->Email->send($to_emails, CLIENT_FROM_EMAIL, $subject, $message.CLIENT_MAIL_FOOTER, CLIENT_REPLYTO_EMAIL);
}
// checks if string is email.
function checkIfEmail($str) {
if(filter_var($str, FILTER_VALIDATE_EMAIL))
return TRUE;
else
return FALSE;
}
public function makeDatatableQuery($table, $context, $db, $arguments = null)
{
return array(
'columns' => array(
'id' => TABLE_TICKETS . ".id",
'ticket_no' => TABLE_TICKETS . ".ticket_no",
'owner' => TABLE_TICKETS . '.owner',
'severity' => TABLE_TICKETS . '.severity',
'priority' => TABLE_TICKETS . '.priority',
'category' => TABLE_TICKETS . '.category',
'created' => TABLE_TICKETS . '.created',
'purpose' => TABLE_TICKETS . '.purpose',
'subject' => TABLE_TICKETS . '.subject',
'message' => TABLE_TICKETS . '.message',
'status' => TABLE_TICKETS . '.status',
'assign_to' => TABLE_TICKETS . '.assign_to',
'assign_on' => TABLE_TICKETS . '.assign_on',
'progress' => TABLE_TICKETS . '.progress',
'updated' => TABLE_TICKETS . '.updated'
),
);
}
}