Newer
Older
<?php
/*********************************************************************************
** The contents of this file are subject to the vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
* All Rights Reserved.
*
********************************************************************************/
require_once('modules/Users/Users.php');
require_once ('modules/Settings/MailConverter/handlers/MailScannerBodyRule.php');
/**
* Mail Scanner Action
*/
class Vtiger_MailScannerAction {
// actionid for this instance
var $actionid = false;
// scanner to which this action is associated
var $scannerid = false;
// type of mailscanner action
var $actiontype= false;
// text representation of action
var $actiontext= false;
// target module for action
// lookup information while taking action
var $lookup = false;
// Storage folder to use
var $STORAGE_FOLDER = 'storage/mailscanner/';
/** DEBUG functionality */
var $debug = false;
function log($message) {
global $log;
if($log && $this->debug) {
$log->debug($message);
}
else if($this->debug)
echo "$message\n";
}
/**
* Constructor.
*/
function __construct($foractionid) {
$this->initialize($foractionid);
}
/**
* Initialize this instance.
*/
function initialize($foractionid) {
global $adb;
$result = $adb->pquery("SELECT * FROM vtiger_mailscanner_actions WHERE actionid=? ORDER BY sequence", Array($foractionid));
if($adb->num_rows($result)) {
$this->actionid = $adb->query_result($result, 0, 'actionid');
$this->scannerid = $adb->query_result($result, 0, 'scannerid');
$this->actiontype = $adb->query_result($result, 0, 'actiontype');
$this->actionModule = $adb->query_result($result, 0, 'module');
$this->lookup = $adb->query_result($result, 0, 'lookup');
$this->actiontext = "$this->actiontype,$this->actionModule,$this->lookup";
}
}
/**
* Create/Update the information of Action into database.
*/
function update($ruleid, $actiontext) {
global $adb;
$inputparts = explode(',', $actiontext);
$this->actiontype = $inputparts[0]; // LINK, CREATE
$this->lookup = $inputparts[2]; // FROM, TO
$this->actiontext = $actiontext;
if($this->actionid) {
$adb->pquery("UPDATE vtiger_mailscanner_actions SET scannerid=?, actiontype=?, module=?, lookup=? WHERE actionid=?",
Array($this->scannerid, $this->actiontype, $this->actionModule, $this->lookup, $this->actionid));
} else {
$this->sequence = $this->__nextsequence();
$adb->pquery("INSERT INTO vtiger_mailscanner_actions(scannerid, actiontype, module, lookup, sequence) VALUES(?,?,?,?,?)",
Array($this->scannerid, $this->actiontype, $this->actionModule, $this->lookup, $this->sequence));
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
$this->actionid = $adb->database->Insert_ID();
}
$checkmapping = $adb->pquery("SELECT COUNT(*) AS ruleaction_count FROM vtiger_mailscanner_ruleactions
WHERE ruleid=? AND actionid=?", Array($ruleid, $this->actionid));
if($adb->num_rows($checkmapping) && !$adb->query_result($checkmapping, 0, 'ruleaction_count')) {
$adb->pquery("INSERT INTO vtiger_mailscanner_ruleactions(ruleid, actionid) VALUES(?,?)",
Array($ruleid, $this->actionid));
}
}
/**
* Delete the actions from tables.
*/
function delete() {
global $adb;
if($this->actionid) {
$adb->pquery("DELETE FROM vtiger_mailscanner_actions WHERE actionid=?", Array($this->actionid));
$adb->pquery("DELETE FROM vtiger_mailscanner_ruleactions WHERE actionid=?", Array($this->actionid));
}
}
/**
* Get next sequence of Action to use.
*/
function __nextsequence() {
global $adb;
$seqres = $adb->pquery("SELECT max(sequence) AS max_sequence FROM vtiger_mailscanner_actions", Array());
$maxsequence = 0;
if($adb->num_rows($seqres)) {
$maxsequence = $adb->query_result($seqres, 0, 'max_sequence');
}
++$maxsequence;
return $maxsequence;
}
/**
* Apply the action on the mail record.
*/
function apply($mailscanner, $mailrecord, $mailscannerrule, $matchresult) {
$returnid = false;
if($this->actiontype == 'CREATE') {
if($this->actionModule == 'HelpDesk' || $this->actionModule == 'HelpDeskNoContact') {
$returnid = $this->__CreateTicket($mailscanner, $mailrecord, $mailscannerrule);
} else if ($this->actionModule == 'Contacts') {
$returnid = $this->__CreateContact($mailscanner, $mailrecord, $mailscannerrule);
} else if ($this->actionModule == 'Leads') {
$returnid = $this->__CreateLead($mailscanner, $mailrecord, $mailscannerrule);
} else if ($this->actionModule == 'Accounts') {
$returnid = $this->__CreateAccount($mailscanner, $mailrecord, $mailscannerrule);
} else if ($this->actionModule == 'Potentials' || $this->actionModule == 'PotentialsNoContact') {
$returnid = $this->__CreatePotential($mailscanner, $mailrecord, $mailscannerrule);
$focus = $this->getLinkFocusByRegexMatch($mailscanner, $mailrecord, $mailscannerrule->hasRegexMatch($matchresult));
if(!is_bool($focus)) {
$returnid = $this->__LinkToRecord($mailscanner, $mailrecord, $focus);
} else if($focus) {
$returnid = $this->__LinkToRecord($mailscanner, $mailrecord);
}
if ($this->actionModule == 'HelpDesk') {
$returnid = $this->__UpdateTicket($mailscanner, $mailrecord, $mailscannerrule->hasRegexMatch($matchresult), $mailscannerrule);
/*
* Function to get Ticket focus for Link Mail Action matching
* "Regex" expression or "Has Ticket Number" condition.
function getLinkFocusByRegexMatch($mailscanner, $mailrecord, $regexMatchInfo) {
if($this->actionModule == 'HelpDesk' && is_array($regexMatchInfo)) {
$record = $regexMatchInfo['matches'];
if($this->lookup == 'FROM')
$useemail = $mailrecord->_from;
else if($this->lookup == 'TO')
$useemail = $mailrecord->_to;
$focus = $mailscanner->GetTicketRecord($record);
foreach($useemail as $email) {
if($focus->column_fields['email'] == $email) {
return $focus;
}
}
return false;
}
return true;
}
/**
* Update ticket action.
*/
function __UpdateTicket($mailscanner, $mailrecord, $regexMatchInfo, $mailscannerrule) {
global $adb;
$returnid = false;
$usesubject = false;
if($this->lookup == 'SUBJECT') {
// If regex match was performed on subject use the matched group
// to lookup the ticket record
if($regexMatchInfo) $usesubject = $regexMatchInfo['matches'];
else $usesubject = $mailrecord->_subject;
// Get the ticket record that was created by SENDER earlier
$fromemail = $mailrecord->_from[0];
$linkfocus = $mailscanner->GetTicketRecord($usesubject, $fromemail);
$commentedBy = $mailscanner->LookupContact($fromemail);
if(!$commentedBy) {
$commentedBy = $mailscanner->LookupAccount($fromemail);
}
// If matching ticket is found, update comment, attach email
if($linkfocus) {
$recordModel = CRMEntity::getInstance('ModComments');
$recordModel->column_fields['commentcontent'] = $mailrecord->getParsedBody();
$recordModel->column_fields['related_to'] = $linkfocus->id;
$recordModel->column_fields['assigned_user_id'] = $linkfocus->column_fields['assigned_user_id'];
$recordModel->column_fields['from_mailconverter'] = true;
$recordModel->column_fields['from_portal'] = true;
if($commentedBy) {
$recordModel->column_fields['customer'] = $commentedBy;
$recordModel->column_fields['customer_email'] = $fromemail;
$recordModel->column_fields['userid'] = $linkfocus->column_fields['assigned_user_id'];
} else {
$recordModel->column_fields['customer_email'] = $fromemail;
$recordModel->column_fields['userid'] = $linkfocus->column_fields['assigned_user_id'];
}
$recordModel->save('ModComments');
// Set the ticket status to Open if its Closed
$adb->pquery("UPDATE vtiger_troubletickets set status=? WHERE ticketid=? AND status='Closed'", Array('Open', $linkfocus->id));
$returnid = $this->__CreateNewEmail($mailrecord, $this->actionModule, $linkfocus);
/**
* Create ticket action.
*/
function __CreateContact($mailscanner, $mailrecord, $mailscannerrule) {
if($mailscanner->LookupContact($mailrecord->_from[0]) && !Vtiger_MailScannerBodyRule::hasBodyRule($mailscannerrule->scannerid, $mailscannerrule->ruleid)) {
$this->lookup = 'FROM';
return $this->__LinkToRecord($mailscanner, $mailrecord);
}
$name = $this->getName($mailrecord);
$email = $mailrecord->_from[0];
$description = $mailrecord->getParsedBody();
$contact = CRMEntity::getInstance('Contacts');
$this->setDefaultValue('Contacts', $contact);
if(Vtiger_MailScannerBodyRule::hasBodyRule($mailscannerrule->scannerid, $mailscannerrule->ruleid) && $this->actionModule == 'Contacts') {
$this->applyBodyRule($mailscannerrule, $mailrecord, $contact);
} else {
$contact->column_fields['firstname'] = $name[0];
$contact->column_fields['lastname'] = $name[1];
$contact->column_fields['email'] = $email;
$contact->column_fields['description'] = $description;
}
$contact->column_fields['assigned_user_id'] = $mailscannerrule->assigned_to;
$contact->column_fields['source'] = "Mail Converter";
$contact->save('Contacts');
if($this->actionModule == 'Contacts') {
$this->linkMailToRecord($mailscanner, $mailrecord, $contact);
}
return $contact->id;
}
/**
* Create Lead action.
*/
function __CreateLead($mailscanner, $mailrecord, $mailscannerrule) {
if($mailscanner->LookupLead($mailrecord->_from[0]) && !Vtiger_MailScannerBodyRule::hasBodyRule($mailscannerrule->scannerid, $mailscannerrule->ruleid)) {
$this->lookup = 'FROM';
return $this->__LinkToRecord($mailscanner, $mailrecord);
}
$name = $this->getName($mailrecord);
$email = $mailrecord->_from[0];
$description = $mailrecord->getParsedBody();
$lead = CRMEntity::getInstance('Leads');
$this->setDefaultValue('Leads', $lead);
if(Vtiger_MailScannerBodyRule::hasBodyRule($mailscannerrule->scannerid, $mailscannerrule->ruleid)) {
$this->applyBodyRule($mailscannerrule, $mailrecord, $lead);
} else {
$lead->column_fields['firstname'] = $name[0];
$lead->column_fields['lastname'] = $name[1];
$lead->column_fields['email'] = $email;
$lead->column_fields['description'] = $description;
}
$lead->column_fields['assigned_user_id'] = $mailscannerrule->assigned_to;
$lead->column_fields['source'] = 'Mail Converter';
$lead->save('Leads');
}
/**
* Create Account action.
*/
function __CreateAccount($mailscanner, $mailrecord, $mailscannerrule) {
if($mailscanner->LookupAccount($mailrecord->_from[0]) && !Vtiger_MailScannerBodyRule::hasBodyRule($mailscannerrule->scannerid, $mailscannerrule->ruleid)) {
$this->lookup = 'FROM';
return $this->__LinkToRecord($mailscanner, $mailrecord);
}
$name = $this->getName($mailrecord);
$email = $mailrecord->_from[0];
$description = $mailrecord->getParsedBody();
$account = CRMEntity::getInstance('Accounts');
$this->setDefaultValue('Accounts', $account);
if(Vtiger_MailScannerBodyRule::hasBodyRule($mailscannerrule->scannerid, $mailscannerrule->ruleid)) {
$this->applyBodyRule($mailscannerrule, $mailrecord, $account);
} else {
$account->column_fields['accountname'] = $name[0].' '.$name[1];
$account->column_fields['email1'] = $email;
$account->column_fields['description'] = $description;
}
$account->column_fields['assigned_user_id'] = $mailscannerrule->assigned_to;
$account->column_fields['source'] = 'Mail Converter';
$account->save('Accounts');
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
/**
* Create Potential action
*/
function __CreatePotential($mailscanner, $mailrecord, $mailscannerrule) {
$email = $mailrecord->_from[0];
$contactLinktoid = $mailscanner->LookupContact($email);
if(!$contactLinktoid && $this->actionModule == 'Potentials') {
$contactLinktoid = $this-> __CreateContact($mailscanner, $mailrecord, $mailscannerrule);
}
if ($contactLinktoid)
$linktoid = $mailscanner->getAccountId($contactLinktoid);
else
$linktoid = $mailscanner->LookupAccount($email);
$potential = CRMEntity::getInstance('Potentials');
$this->setDefaultValue('Potentials', $potential);
$potential->column_fields['closingdate'] = date('Y-m-d', strtotime('+1 day'));
if(Vtiger_MailScannerBodyRule::hasBodyRule($mailscannerrule->scannerid, $mailscannerrule->ruleid)) {
$this->applyBodyRule($mailscannerrule, $mailrecord, $potential);
} else {
$potential->column_fields['potentialname'] = $mailrecord->_subject;
$potential->column_fields['email'] = $email;
$potential->column_fields['description'] = $mailrecord->getParsedBody();
}
$potential->column_fields['assigned_user_id'] = $mailscannerrule->assigned_to;
$potential->column_fields['contact_id'] = $contactLinktoid;
$potential->column_fields['related_to'] = $linktoid;
$potential->column_fields['source'] = 'Mail Converter';
$potential->save('Potentials');
$this->linkMailToRecord($mailscanner, $mailrecord, $potential);
return $potential->id;
}
/**
* Create ticket action.
*/
function __CreateTicket($mailscanner, $mailrecord, $mailscannerrule) {
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
388
389
390
391
392
393
394
395
396
397
// Prepare data to create trouble ticket
$usetitle = $mailrecord->_subject;
$description = $mailrecord->getParsedBody();
// There will be only on FROM address to email, so pick the first one
$fromemail = $mailrecord->_from[0];
$contactLinktoid = $mailscanner->LookupContact($fromemail);
if(!$contactLinktoid && $this->actionModule == 'HelpDesk') {
$contactLinktoid = $this-> __CreateContact($mailscanner, $mailrecord, $mailscannerrule);
}
if ($contactLinktoid)
$linktoid = $mailscanner->getAccountId($contactLinktoid);
if(!$linktoid)
$linktoid = $mailscanner->LookupAccount($fromemail);
// Create trouble ticket record
$ticket = CRMEntity::getInstance('HelpDesk');
$this->setDefaultValue('HelpDesk', $ticket);
if(Vtiger_MailScannerBodyRule::hasBodyRule($mailscannerrule->scannerid, $mailscannerrule->ruleid)) {
$this->applyBodyRule($mailscannerrule, $mailrecord, $ticket);
} else {
$ticket->column_fields['ticket_title'] = $usetitle;
$ticket->column_fields['description'] = $description;
$ticket->column_fields['email'] = $fromemail;
}
$ticket->column_fields['assigned_user_id'] = $mailscannerrule->assigned_to;
if ($contactLinktoid)
$ticket->column_fields['contact_id'] = $contactLinktoid;
if ($linktoid)
$ticket->column_fields['parent_id'] = $linktoid;
$ticket->column_fields['source'] = 'Mail Converter';
$ticket->save('HelpDesk');
$this->linkMailToRecord($mailscanner, $mailrecord, $ticket);
return $ticket->id;
/**
* Function to Link Mail to Records after creation of record through Mail Converter
function linkMailToRecord($mailscanner, $mailrecord, $focus) {
$this->lookup = 'FROM';
return $this->__LinkToRecord($mailscanner, $mailrecord, $focus);
}
/**
* Add email to CRM record like Contacts/Accounts
*/
function __LinkToRecord($mailscanner, $mailrecord, $linkfocus = false) {
$useemail = false;
if($this->lookup == 'FROM') $useemail = $mailrecord->_from;
else if($this->lookup == 'TO') $useemail = $mailrecord->_to;
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
if($this->actionModule == 'PotentialsNoContact') {
$module = 'Potentials';
} else if($this->actionModule == 'HelpDeskNoContact') {
$module = 'HelpDesk';
} else {
$module = $this->actionModule;
}
if(!$linkfocus) {
if ($module == 'Contacts') {
foreach ($useemail as $email) {
$linkfocus = $mailscanner->GetContactRecord($email);
if ($linkfocus)
break;
}
} else if ($module == 'Accounts') {
foreach ($useemail as $email) {
$linkfocus = $mailscanner->GetAccountRecord($email);
if ($linkfocus)
break;
}
} else if ($module == 'Leads') {
foreach ($useemail as $email) {
$linkfocus = $mailscanner->GetLeadRecord($email);
if ($linkfocus)
break;
}
} else if ($module == 'Potentials') {
foreach ($useemail as $email) {
$linkfocus = $mailscanner->GetPotentialRecord($email);
if ($linkfocus)
break;
}
} else if ($module == 'HelpDesk') {
foreach ($useemail as $email) {
$linkfocus = $mailscanner->GetTicketFocus($email);
if ($linkfocus)
break;
}
}
}
$returnid = false;
if($linkfocus) {
$returnid = $this->__CreateNewEmail($mailrecord, $module, $linkfocus);
}
return $returnid;
}
/**
* Create new Email record (and link to given record) including attachements
*/
function __CreateNewEmail($mailrecord, $module, $linkfocus) {
global $current_user, $adb;
//get mail scanner info to save mail box email id
$scannerId = $this->scannerid;
include_once 'modules/Settings/MailConverter/handlers/MailScannerInfo.php';
include_once 'modules/Settings/MailConverter/handlers/MailBox.php';
$scannerName = Settings_MailConverter_Module_Model::getScannerName($scannerId);
$scannerInfo = new Vtiger_MailScannerInfo($scannerName);
if(!$current_user) {
$current_user = Users::getActiveAdminUser();
}
$assignedToId = $linkfocus->column_fields['assigned_user_id'];
if(vtws_getOwnerType($assignedToId) == 'Groups') {
$assignedToId = Users::getActiveAdminId();
}
$focus = CRMEntity::getInstance('Emails');
$focus->column_fields['parent_type'] = $module;
$focus->column_fields['activitytype'] = 'Emails';
$focus->column_fields['parent_id'] = "$linkfocus->id@-1|";
$focus->column_fields['subject'] = $mailrecord->_subject;
$focus->column_fields['description'] = $mailrecord->getBodyHTML();
$focus->column_fields['assigned_user_id'] = $assignedToId;
$focus->column_fields["date_start"] = date('Y-m-d', $mailrecord->_date);
$focus->column_fields["time_start"] = date('H:i:s', $mailrecord->_date);
$focus->column_fields["email_flag"] = 'MAILSCANNER';
$from=$mailrecord->_from[0];
$to = $mailrecord->_to[0];
$cc = (!empty($mailrecord->_cc))? implode(',', $mailrecord->_cc) : '';
$bcc= (!empty($mailrecord->_bcc))? implode(',', $mailrecord->_bcc) : '';
//emails field were restructured and to,bcc and cc field are JSON arrays
$focus->column_fields['from_email'] = $from;
$focus->column_fields['saved_toid'] = $to;
$focus->column_fields['ccmail'] = $cc;
$focus->column_fields['bccmail'] = $bcc;
$focus->column_fields['mailboxemail'] = $scannerInfo->username;
$focus->save('Emails');
$emailid = $focus->id;
$this->log("Created [$focus->id]: $mailrecord->_subject linked it to " . $linkfocus->id);
// TODO: Handle attachments of the mail (inline/file)
$this->__SaveAttachements($mailrecord, 'Emails', $focus);
// To add entry in ModTracker for email relation
relateEntities($linkfocus, $module, $linkfocus->id, 'Emails', $emailid);
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
return $emailid;
}
/**
* Save attachments from the email and add it to the module record.
*/
function __SaveAttachements($mailrecord, $basemodule, $basefocus) {
global $adb;
// If there is no attachments return
if(!$mailrecord->_attachments) return;
$userid = $basefocus->column_fields['assigned_user_id'];
$setype = "$basemodule Attachment";
$date_var = $adb->formatDate(date('YmdHis'), true);
foreach($mailrecord->_attachments as $filename=>$filecontent) {
$attachid = $adb->getUniqueId('vtiger_crmentity');
$description = $filename;
$usetime = $adb->formatDate($date_var, true);
$adb->pquery("INSERT INTO vtiger_crmentity(crmid, smcreatorid, smownerid,
modifiedby, setype, description, createdtime, modifiedtime, presence, deleted)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
Array($attachid, $userid, $userid, $userid, $setype, $description, $usetime, $usetime, 1, 0));
$issaved = $this->__SaveAttachmentFile($attachid, $filename, $filecontent);
if($issaved) {
// Create document record
$document->column_fields['notes_title'] = $filename;
$document->column_fields['filename'] = $filename;
$document->column_fields['filesize'] = mb_strlen($filecontent, '8bit');
$document->column_fields['filestatus'] = 1;
$document->column_fields['filelocationtype'] = 'I';
$document->column_fields['folderid'] = 1; // Default Folder
$document->column_fields['assigned_user_id'] = $userid;
$document->save('Documents');
// Link file attached to document
$adb->pquery("INSERT INTO vtiger_seattachmentsrel(crmid, attachmentsid) VALUES(?,?)", Array($document->id, $attachid));
$adb->pquery("INSERT INTO vtiger_senotesrel(crmid, notesid) VALUES(?,?)", Array($basefocus->id, $document->id));
// Link document to Parent entity - Account/Contact/...
list($eid,$junk)=explode('@',$basefocus->column_fields['parent_id']);
$adb->pquery("INSERT INTO vtiger_senotesrel(crmid, notesid) VALUES(?,?)", Array($eid, $document->id));
}
}
}
/**
* Save the attachment to the file
*/
function __SaveAttachmentFile($attachid, $filename, $filecontent) {
global $adb;
$dirname = $this->STORAGE_FOLDER;
//To get group permissions given to config.inc.php file
$permissions = Vtiger_Functions::getGroupPermissionsFromConfigFile();
if(!is_dir($dirname)){
mkdir($dirname);
// Giving group permissions to newly created directory
exec("chown -R $permissions $dirname");
}
$description = $filename;
$filename = str_replace(' ', '-', $filename);
$saveasfile = "$dirname$attachid" . "_$filename";
if(!file_exists($saveasfile)) {
$this->log("Saved attachement as $saveasfile\n");
$fh = fopen($saveasfile, 'wb');
fwrite($fh, $filecontent);
fclose($fh);
// Giving group permissions to newly created directory
exec("chown -R $permissions $saveasfile");
}
$mimetype = MailAttachmentMIME::detect($saveasfile);
$adb->pquery("INSERT INTO vtiger_attachments SET attachmentsid=?, name=?, description=?, type=?, path=?",
Array($attachid, $filename, $description, $mimetype, $dirname));
return true;
}
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
function setDefaultValue($module, $moduleObj) {
$moduleInstance = Vtiger_Module_Model::getInstance($module);
$fieldInstances = Vtiger_Field_Model::getAllForModule($moduleInstance);
foreach($fieldInstances as $blockInstance) {
foreach($blockInstance as $fieldInstance) {
$fieldName = $fieldInstance->getName();
$defaultValue = $fieldInstance->getDefaultFieldValue();
if($defaultValue) {
$moduleObj->column_fields[$fieldName] = decode_html($defaultValue);
}
if($fieldInstance->isMandatory() && !$defaultValue) {
if($fieldInstance->getFieldDataType() == 'picklist' || $fieldInstance->getFieldDataType() == 'multipicklist') {
$picklistValues = $fieldInstance->getPicklistValues();
$randomValue = reset($picklistValues);
if($randomValue)
$moduleObj->column_fields[$fieldName] = $randomValue;
} else {
$moduleObj->column_fields[$fieldName] = Vtiger_Util_Helper::getDefaultMandatoryValue($fieldInstance->getFieldDataType());
}
}
}
}
}
/**
* Function to get Mail Sender's Name
* @param <Vtiger_MailRecord Object> $mailrecord
* @return <Array> containing First Name and Last Name
*/
function getName($mailrecord) {
$name = $mailrecord->_fromname;
if(!empty($name)) {
$nameParts = explode(' ', $name);
if(count($nameParts) > 1) {
$firstName = $nameParts[0];
unset($nameParts[0]);
$lastName = implode(' ', $nameParts);
} else {
$firstName = '';
$lastName = $nameParts[0];
}
} else {
$firstName = '';
$lastName = $mailrecord->_from[0];
}
return array($firstName, $lastName);
}
function applyBodyRule($mailscannerrule, $mailrecord, $focus) {
$bodyRule = new Vtiger_MailScannerBodyRule($mailscannerrule->scannerid, $mailscannerrule->ruleid);
if($bodyRule->ruleId) {
$bodyRule->fromemail = $mailrecord->_from[0];
$bodyRule->subject = $mailrecord->_subject;
$name = $this->getName($mailrecord);
$bodyRule->fromname = implode(' ', $name);
$bodyFields = $bodyRule->getFieldValues($mailrecord->getParsedBody());
foreach($bodyFields as $fieldName => $fieldValue) {
$focus->column_fields[$fieldName] = $fieldValue;
}
}
}