[ Index ] |
PHP Cross Reference of vtigercrm-6.1.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /********************************************************************************* 3 ** The contents of this file are subject to the vtiger CRM Public License Version 1.0 4 * ("License"); You may not use this file except in compliance with the License 5 * The Original Code is: vtiger CRM Open Source 6 * The Initial Developer of the Original Code is vtiger. 7 * Portions created by vtiger are Copyright (C) vtiger. 8 * All Rights Reserved. 9 * 10 ********************************************************************************/ 11 12 require_once ('modules/Emails/Emails.php'); 13 require_once ('modules/HelpDesk/HelpDesk.php'); 14 require_once('modules/ModComments/ModComments.php'); 15 require_once ('modules/Users/Users.php'); 16 require_once ('modules/Documents/Documents.php'); 17 require_once ('modules/Leads/Leads.php'); 18 require_once ('modules/Contacts/Contacts.php'); 19 require_once ('modules/Accounts/Accounts.php'); 20 21 /** 22 * Mail Scanner Action 23 */ 24 class Vtiger_MailScannerAction { 25 // actionid for this instance 26 var $actionid = false; 27 // scanner to which this action is associated 28 var $scannerid = false; 29 // type of mailscanner action 30 var $actiontype= false; 31 // text representation of action 32 var $actiontext= false; 33 // target module for action 34 var $module = false; 35 // lookup information while taking action 36 var $lookup = false; 37 38 // Storage folder to use 39 var $STORAGE_FOLDER = 'storage/mailscanner/'; 40 41 /** DEBUG functionality */ 42 var $debug = false; 43 function log($message) { 44 global $log; 45 if($log && $this->debug) { $log->debug($message); } 46 else if($this->debug) echo "$message\n"; 47 } 48 49 /** 50 * Constructor. 51 */ 52 function __construct($foractionid) { 53 $this->initialize($foractionid); 54 } 55 56 /** 57 * Initialize this instance. 58 */ 59 function initialize($foractionid) { 60 global $adb; 61 $result = $adb->pquery("SELECT * FROM vtiger_mailscanner_actions WHERE actionid=? ORDER BY sequence", Array($foractionid)); 62 63 if($adb->num_rows($result)) { 64 $this->actionid = $adb->query_result($result, 0, 'actionid'); 65 $this->scannerid = $adb->query_result($result, 0, 'scannerid'); 66 $this->actiontype = $adb->query_result($result, 0, 'actiontype'); 67 $this->module = $adb->query_result($result, 0, 'module'); 68 $this->lookup = $adb->query_result($result, 0, 'lookup'); 69 $this->actiontext = "$this->actiontype,$this->module,$this->lookup"; 70 } 71 } 72 73 /** 74 * Create/Update the information of Action into database. 75 */ 76 function update($ruleid, $actiontext) { 77 global $adb; 78 79 $inputparts = explode(',', $actiontext); 80 $this->actiontype = $inputparts[0]; // LINK, CREATE 81 $this->module = $inputparts[1]; // Module name 82 $this->lookup = $inputparts[2]; // FROM, TO 83 84 $this->actiontext = $actiontext; 85 86 if($this->actionid) { 87 $adb->pquery("UPDATE vtiger_mailscanner_actions SET scannerid=?, actiontype=?, module=?, lookup=? WHERE actionid=?", 88 Array($this->scannerid, $this->actiontype, $this->module, $this->lookup, $this->actionid)); 89 } else { 90 $this->sequence = $this->__nextsequence(); 91 $adb->pquery("INSERT INTO vtiger_mailscanner_actions(scannerid, actiontype, module, lookup, sequence) VALUES(?,?,?,?,?)", 92 Array($this->scannerid, $this->actiontype, $this->module, $this->lookup, $this->sequence)); 93 $this->actionid = $adb->database->Insert_ID(); 94 } 95 $checkmapping = $adb->pquery("SELECT COUNT(*) AS ruleaction_count FROM vtiger_mailscanner_ruleactions 96 WHERE ruleid=? AND actionid=?", Array($ruleid, $this->actionid)); 97 if($adb->num_rows($checkmapping) && !$adb->query_result($checkmapping, 0, 'ruleaction_count')) { 98 $adb->pquery("INSERT INTO vtiger_mailscanner_ruleactions(ruleid, actionid) VALUES(?,?)", 99 Array($ruleid, $this->actionid)); 100 } 101 } 102 103 /** 104 * Delete the actions from tables. 105 */ 106 function delete() { 107 global $adb; 108 if($this->actionid) { 109 $adb->pquery("DELETE FROM vtiger_mailscanner_actions WHERE actionid=?", Array($this->actionid)); 110 $adb->pquery("DELETE FROM vtiger_mailscanner_ruleactions WHERE actionid=?", Array($this->actionid)); 111 } 112 } 113 114 /** 115 * Get next sequence of Action to use. 116 */ 117 function __nextsequence() { 118 global $adb; 119 $seqres = $adb->pquery("SELECT max(sequence) AS max_sequence FROM vtiger_mailscanner_actions", Array()); 120 $maxsequence = 0; 121 if($adb->num_rows($seqres)) { 122 $maxsequence = $adb->query_result($seqres, 0, 'max_sequence'); 123 } 124 ++$maxsequence; 125 return $maxsequence; 126 } 127 128 /** 129 * Apply the action on the mail record. 130 */ 131 function apply($mailscanner, $mailrecord, $mailscannerrule, $matchresult) { 132 $returnid = false; 133 if($this->actiontype == 'CREATE') { 134 if($this->module == 'HelpDesk') { 135 $returnid = $this->__CreateTicket($mailscanner, $mailrecord); 136 } else if ($this->module == 'Contacts') { 137 $returnid = $this->__CreateContact($mailscanner, $mailrecord); 138 } else if ($this->module == 'Leads') { 139 $returnid = $this->__CreateLead($mailscanner, $mailrecord); 140 } else if ($this->module == 'Accounts') { 141 $returnid = $this->__CreateAccount($mailscanner, $mailrecord); 142 } 143 } else if($this->actiontype == 'LINK') { 144 $returnid = $this->__LinkToRecord($mailscanner, $mailrecord); 145 } else if ($this->actiontype == 'UPDATE') { 146 if ($this->module == 'HelpDesk') { 147 $returnid = $this->__UpdateTicket($mailscanner, $mailrecord, $mailscannerrule->hasRegexMatch($matchresult)); 148 } 149 } 150 return $returnid; 151 } 152 153 /** 154 * Update ticket action. 155 */ 156 function __UpdateTicket($mailscanner, $mailrecord, $regexMatchInfo) { 157 global $adb; 158 $returnid = false; 159 160 $usesubject = false; 161 if($this->lookup == 'SUBJECT') { 162 // If regex match was performed on subject use the matched group 163 // to lookup the ticket record 164 if($regexMatchInfo) $usesubject = $regexMatchInfo['matches']; 165 else $usesubject = $mailrecord->_subject; 166 167 // Get the ticket record that was created by SENDER earlier 168 $fromemail = $mailrecord->_from[0]; 169 170 $linkfocus = $mailscanner->GetTicketRecord($usesubject, $fromemail); 171 172 $commentedBy = $mailscanner->LookupContact($fromemail); 173 if(!$commentedBy) { 174 $commentedBy = $mailscanner->LookupAccount($fromemail); 175 } 176 177 // If matching ticket is found, update comment, attach email 178 if($linkfocus) { 179 $commentFocus = new ModComments(); 180 $commentFocus->column_fields['commentcontent'] = $mailrecord->getBodyText(); 181 $commentFocus->column_fields['related_to'] = $linkfocus->id; 182 $commentFocus->column_fields['assigned_user_id'] = $mailscanner->_scannerinfo->rules[0]->assigned_to; 183 if($commentedBy) { 184 $commentFocus->column_fields['customer'] = $commentedBy; 185 $commentFocus->column_fields['from_mailconverter'] = 1; 186 } else { 187 $commentFocus->column_fields['userid'] = $mailscanner->_scannerinfo->rules[0]->assigned_to; 188 } 189 $commentFocus->saveentity('ModComments'); 190 191 // Set the ticket status to Open if its Closed 192 $adb->pquery("UPDATE vtiger_troubletickets set status=? WHERE ticketid=? AND status='Closed'", Array('Open', $linkfocus->id)); 193 194 $returnid = $this->__CreateNewEmail($mailrecord, $this->module, $linkfocus); 195 196 } else { 197 // TODO If matching ticket was not found, create ticket? 198 // $returnid = $this->__CreateTicket($mailscanner, $mailrecord); 199 } 200 } 201 return $returnid; 202 } 203 204 /** 205 * Create ticket action. 206 */ 207 function __CreateContact($mailscanner, $mailrecord) { 208 if($mailscanner->LookupContact($mailrecord->_from[0])) { 209 $this->lookup = 'FROM'; 210 return $this->__LinkToRecord($mailscanner, $mailrecord); 211 } 212 $lastname = $mailrecord->_subject; 213 if ($lastname == '') 214 $lastname = $mailrecord->_from[0]; 215 $email = $mailrecord->_from[0]; 216 $description = $mailrecord->getBodyText(); 217 218 $contact = new Contacts(); 219 $this->setDefaultValue('Contacts', $contact); 220 $contact->column_fields['lastname'] = $lastname; 221 $contact->column_fields['email'] = $email; 222 $contact->column_fields['assigned_user_id'] = $mailscanner->_scannerinfo->rules[0]->assigned_to; 223 $contact->column_fields['description'] = $description; 224 $contact->save('Contacts'); 225 226 $this->__SaveAttachements($mailrecord, 'Contacts', $contact); 227 228 return $contact->id; 229 } 230 231 /** 232 * Create Lead action. 233 */ 234 function __CreateLead($mailscanner, $mailrecord) { 235 if($mailscanner->LookupLead($mailrecord->_from[0])) { 236 $this->lookup = 'FROM'; 237 return $this->__LinkToRecord($mailscanner, $mailrecord); 238 } 239 $lastname = $mailrecord->_subject; 240 if ($lastname == '') 241 $lastname = $mailrecord->_from[0]; 242 $email = $mailrecord->_from[0]; 243 $description = $mailrecord->getBodyText(); 244 245 $lead = new Leads(); 246 $this->setDefaultValue('Leads', $lead); 247 $lead->column_fields['lastname'] = $lastname; 248 $lead->column_fields['email'] = $email; 249 $lead->column_fields['assigned_user_id'] = $mailscanner->_scannerinfo->rules[0]->assigned_to; 250 $lead->column_fields['description'] = $description; 251 $lead->save('Leads'); 252 253 $this->__SaveAttachements($mailrecord, 'Leads', $lead); 254 255 return $lead->id; 256 } 257 258 /** 259 * Create Account action. 260 */ 261 function __CreateAccount($mailscanner, $mailrecord) { 262 if($mailscanner->LookupAccount($mailrecord->_from[0])) { 263 $this->lookup = 'FROM'; 264 return $this->__LinkToRecord($mailscanner, $mailrecord); 265 } 266 $accountname = $mailrecord->_subject; 267 if ($accountname == '') 268 $accountname = $mailrecord->_from[0]; 269 $email = $mailrecord->_from[0]; 270 $description = $mailrecord->getBodyText(); 271 272 $account = new Accounts(); 273 $this->setDefaultValue('Accounts', $account); 274 $account->column_fields['accountname'] = $accountname; 275 $account->column_fields['email1'] = $email; 276 $account->column_fields['assigned_user_id'] = $mailscanner->_scannerinfo->rules[0]->assigned_to; 277 $account->column_fields['description'] = $description; 278 $account->save('Accounts'); 279 280 $this->__SaveAttachements($mailrecord, 'Accounts', $account); 281 282 return $account->id; 283 } 284 285 /** 286 * Create ticket action. 287 */ 288 function __CreateTicket($mailscanner, $mailrecord) { 289 // Prepare data to create trouble ticket 290 $usetitle = $mailrecord->_subject; 291 $description = $mailrecord->getBodyText(); 292 293 // There will be only on FROM address to email, so pick the first one 294 $fromemail = $mailrecord->_from[0]; 295 $contactLinktoid = $mailscanner->LookupContact($fromemail); 296 if(!$contactLinktoid) { 297 $contactLinktoid = $this-> __CreateContact($mailscanner, $mailrecord); 298 } 299 if ($contactLinktoid) 300 $linktoid = $mailscanner->getAccountId($contactLinktoid); 301 if(!$linktoid) 302 $linktoid = $mailscanner->LookupAccount($fromemail); 303 304 // Create trouble ticket record 305 $ticket = new HelpDesk(); 306 $this->setDefaultValue('HelpDesk', $ticket); 307 if(empty($ticket->column_fields['ticketstatus']) || $ticket->column_fields['ticketstatus'] == '?????') 308 $ticket->column_fields['ticketstatus'] = 'Open'; 309 $ticket->column_fields['ticket_title'] = $usetitle; 310 $ticket->column_fields['description'] = $description; 311 $ticket->column_fields['assigned_user_id'] = $mailscanner->_scannerinfo->rules[0]->assigned_to; 312 if ($contactLinktoid) 313 $ticket->column_fields['contact_id'] = $contactLinktoid; 314 if ($linktoid) 315 $ticket->column_fields['parent_id'] = $linktoid; 316 $ticket->save('HelpDesk'); 317 318 // Associate any attachement of the email to ticket 319 $this->__SaveAttachements($mailrecord, 'HelpDesk', $ticket); 320 321 if($contactLinktoid) 322 $relatedTo = $contactLinktoid; 323 else 324 $relatedTo = $linktoid; 325 $this->linkMail($mailscanner, $mailrecord, $relatedTo); 326 327 return $ticket->id; 328 } 329 330 /** 331 * Function to link email record to contact/account/lead 332 * record if exists with same email id 333 * @param type $mailscanner 334 * @param type $mailrecord 335 */ 336 function linkMail($mailscanner, $mailrecord, $relatedTo) { 337 $fromemail = $mailrecord->_from[0]; 338 339 $linkfocus = $mailscanner->GetContactRecord($fromemail, $relatedTo); 340 $module = 'Contacts'; 341 if(!$linkfocus) { 342 $linkfocus = $mailscanner->GetAccountRecord($fromemail, $relatedTo); 343 $module = 'Accounts'; 344 } 345 346 if($linkfocus) { 347 $this->__CreateNewEmail($mailrecord, $module, $linkfocus); 348 } 349 } 350 351 /** 352 * Add email to CRM record like Contacts/Accounts 353 */ 354 function __LinkToRecord($mailscanner, $mailrecord) { 355 $linkfocus = false; 356 357 $useemail = false; 358 if($this->lookup == 'FROM') $useemail = $mailrecord->_from; 359 else if($this->lookup == 'TO') $useemail = $mailrecord->_to; 360 361 if ($this->module == 'Contacts') { 362 foreach ($useemail as $email) { 363 $linkfocus = $mailscanner->GetContactRecord($email); 364 if ($linkfocus) 365 break; 366 } 367 } else if ($this->module == 'Accounts') { 368 foreach ($useemail as $email) { 369 $linkfocus = $mailscanner->GetAccountRecord($email); 370 if ($linkfocus) 371 break; 372 } 373 } else if ($this->module == 'Leads') { 374 foreach ($useemail as $email) { 375 $linkfocus = $mailscanner->GetLeadRecord($email); 376 if ($linkfocus) 377 break; 378 } 379 } 380 381 $returnid = false; 382 if($linkfocus) { 383 $returnid = $this->__CreateNewEmail($mailrecord, $this->module, $linkfocus); 384 } 385 return $returnid; 386 } 387 388 /** 389 * Create new Email record (and link to given record) including attachements 390 */ 391 function __CreateNewEmail($mailrecord, $module, $linkfocus) { 392 global $current_user, $adb; 393 if(!$current_user) { 394 $current_user = Users::getActiveAdminUser(); 395 } 396 397 $focus = new Emails(); 398 $focus->column_fields['parent_type'] = $module; 399 $focus->column_fields['activitytype'] = 'Emails'; 400 $focus->column_fields['parent_id'] = "$linkfocus->id@-1|"; 401 $focus->column_fields['subject'] = $mailrecord->_subject; 402 403 $focus->column_fields['description'] = $mailrecord->getBodyHTML(); 404 $focus->column_fields['assigned_user_id'] = $linkfocus->column_fields['assigned_user_id']; 405 $focus->column_fields["date_start"] = date('Y-m-d', $mailrecord->_date); 406 $focus->column_fields["time_start"] = gmdate("H:i:s"); 407 $focus->column_fields["email_flag"] = 'MAILSCANNER'; 408 409 $from=$mailrecord->_from[0]; 410 $to = $mailrecord->_to[0]; 411 $cc = (!empty($mailrecord->_cc))? implode(',', $mailrecord->_cc) : ''; 412 $bcc= (!empty($mailrecord->_bcc))? implode(',', $mailrecord->_bcc) : ''; 413 $flag=''; // 'SENT'/'SAVED' 414 //emails field were restructured and to,bcc and cc field are JSON arrays 415 $focus->column_fields['from_email'] = $from; 416 $focus->column_fields['saved_toid'] = $to; 417 $focus->column_fields['ccmail'] = $cc; 418 $focus->column_fields['bccmail'] = $bcc; 419 $focus->save('Emails'); 420 421 $emailid = $focus->id; 422 $this->log("Created [$focus->id]: $mailrecord->_subject linked it to " . $linkfocus->id); 423 424 // TODO: Handle attachments of the mail (inline/file) 425 $this->__SaveAttachements($mailrecord, 'Emails', $focus); 426 427 return $emailid; 428 } 429 430 /** 431 * Save attachments from the email and add it to the module record. 432 */ 433 function __SaveAttachements($mailrecord, $basemodule, $basefocus) { 434 global $adb; 435 436 // If there is no attachments return 437 if(!$mailrecord->_attachments) return; 438 439 $userid = $basefocus->column_fields['assigned_user_id']; 440 $setype = "$basemodule Attachment"; 441 442 $date_var = $adb->formatDate(date('YmdHis'), true); 443 444 foreach($mailrecord->_attachments as $filename=>$filecontent) { 445 $attachid = $adb->getUniqueId('vtiger_crmentity'); 446 $description = $filename; 447 $usetime = $adb->formatDate($date_var, true); 448 449 $adb->pquery("INSERT INTO vtiger_crmentity(crmid, smcreatorid, smownerid, 450 modifiedby, setype, description, createdtime, modifiedtime, presence, deleted) 451 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 452 Array($attachid, $userid, $userid, $userid, $setype, $description, $usetime, $usetime, 1, 0)); 453 454 $issaved = $this->__SaveAttachmentFile($attachid, $filename, $filecontent); 455 if($issaved) { 456 // Create document record 457 $document = new Documents(); 458 $document->column_fields['notes_title'] = $filename; 459 $document->column_fields['filename'] = $filename; 460 $document->column_fields['filesize'] = mb_strlen($filecontent, '8bit'); 461 $document->column_fields['filestatus'] = 1; 462 $document->column_fields['filelocationtype'] = 'I'; 463 $document->column_fields['folderid'] = 1; // Default Folder 464 $document->column_fields['assigned_user_id'] = $userid; 465 $document->save('Documents'); 466 467 // Link file attached to document 468 $adb->pquery("INSERT INTO vtiger_seattachmentsrel(crmid, attachmentsid) VALUES(?,?)", 469 Array($document->id, $attachid)); 470 471 // Link document to base record 472 $adb->pquery("INSERT INTO vtiger_senotesrel(crmid, notesid) VALUES(?,?)", 473 Array($basefocus->id, $document->id)); 474 475 // Link document to Parent entity - Account/Contact/... 476 list($eid,$junk)=explode('@',$basefocus->column_fields['parent_id']); 477 $adb->pquery("INSERT INTO vtiger_senotesrel(crmid, notesid) VALUES(?,?)", 478 Array($eid, $document->id)); 479 480 // Link Attachement to the Email 481 $adb->pquery("INSERT INTO vtiger_seattachmentsrel(crmid, attachmentsid) VALUES(?,?)", 482 Array($basefocus->id, $attachid)); 483 } 484 } 485 } 486 487 /** 488 * Save the attachment to the file 489 */ 490 function __SaveAttachmentFile($attachid, $filename, $filecontent) { 491 global $adb; 492 493 $dirname = $this->STORAGE_FOLDER; 494 if(!is_dir($dirname)) mkdir($dirname); 495 496 $description = $filename; 497 $filename = str_replace(' ', '-', $filename); 498 $saveasfile = "$dirname$attachid" . "_$filename"; 499 if(!file_exists($saveasfile)) { 500 501 $this->log("Saved attachement as $saveasfile\n"); 502 503 $fh = fopen($saveasfile, 'wb'); 504 fwrite($fh, $filecontent); 505 fclose($fh); 506 } 507 508 $mimetype = MailAttachmentMIME::detect($saveasfile); 509 510 $adb->pquery("INSERT INTO vtiger_attachments SET attachmentsid=?, name=?, description=?, type=?, path=?", 511 Array($attachid, $filename, $description, $mimetype, $dirname)); 512 513 return true; 514 } 515 516 function setDefaultValue($module, $moduleObj) { 517 $moduleInstance = Vtiger_Module_Model::getInstance($module); 518 519 $fieldInstances = Vtiger_Field_Model::getAllForModule($moduleInstance); 520 foreach($fieldInstances as $blockInstance) { 521 foreach($blockInstance as $fieldInstance) { 522 $fieldName = $fieldInstance->getName(); 523 $defaultValue = $fieldInstance->getDefaultFieldValue(); 524 if($defaultValue) { 525 $moduleObj->column_fields[$fieldName] = decode_html($defaultValue); 526 } 527 if($fieldInstance->isMandatory() && !$defaultValue) { 528 $moduleObj->column_fields[$fieldName] = Vtiger_Util_Helper::getDefaultMandatoryValue($fieldInstance->getFieldDataType()); 529 } 530 } 531 } 532 } 533 } 534 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:08:37 2014 | Cross-referenced by PHPXref 0.7.1 |