[ 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 class Emails_Record_Model extends Vtiger_Record_Model { 12 13 /** 14 * Function to get the Detail View url for the record 15 * @return <String> - Record Detail View Url 16 */ 17 public function getDetailViewUrl() { 18 list($parentId, $status) = explode('@', reset(array_filter(explode('|', $this->get('parent_id'))))); 19 return 'Javascript:Vtiger_Index_Js.showEmailPreview("'.$this->getId().'","'.$parentId.'")'; 20 } 21 22 /** 23 * Function to save an Email 24 */ 25 public function save() { 26 //Opensource fix for MailManager data mail attachment 27 if($this->get('email_flag')!="MailManager"){ 28 $this->set('date_start', date('Y-m-d')); 29 $this->set('time_start', date('H:i')); 30 } 31 $this->set('activitytype', 'Emails'); 32 33 //$currentUserModel = Users_Record_Model::getCurrentUserModel(); 34 //$this->set('assigned_user_id', $currentUserModel->getId()); 35 $this->getModule()->saveRecord($this); 36 $documentIds = $this->get('documentids'); 37 if (!empty ($documentIds)) { 38 $this->deleteDocumentLink(); 39 $this->saveDocumentDetails(); 40 } 41 } 42 43 /** 44 * Function sends mail 45 */ 46 public function send() { 47 $currentUserModel = Users_Record_Model::getCurrentUserModel(); 48 $rootDirectory = vglobal('root_directory'); 49 50 $mailer = Emails_Mailer_Model::getInstance(); 51 $mailer->IsHTML(true); 52 53 $fromEmail = $this->getFromEmailAddress(); 54 $replyTo = $currentUserModel->get('email1'); 55 $userName = $currentUserModel->getName(); 56 57 // To eliminate the empty value of an array 58 $toEmailInfo = array_filter($this->get('toemailinfo')); 59 $toMailNamesList = array_filter($this->get('toMailNamesList')); 60 foreach($toMailNamesList as $id => $emailData){ 61 foreach($emailData as $key => $email){ 62 if($toEmailInfo[$id]){ 63 array_push($toEmailInfo[$id], $email['value']); 64 } 65 } 66 } 67 $emailsInfo = array(); 68 foreach ($toEmailInfo as $id => $emails) { 69 foreach($emails as $key => $value){ 70 array_push($emailsInfo, $value); 71 } 72 } 73 74 $toFieldData = array_diff(explode(',', $this->get('saved_toid')), $emailsInfo); 75 $toEmailsData = array(); 76 $i = 1; 77 foreach ($toFieldData as $value) { 78 $toEmailInfo['to'.$i++] = array($value); 79 } 80 $attachments = $this->getAttachmentDetails(); 81 $status = false; 82 83 // Merge Users module merge tags based on current user. 84 $mergedDescription = getMergedDescription($this->get('description'), $currentUserModel->getId(), 'Users'); 85 86 foreach($toEmailInfo as $id => $emails) { 87 $mailer->reinitialize(); 88 $mailer->ConfigSenderInfo($fromEmail, $userName, $replyTo); 89 $old_mod_strings = vglobal('mod_strings'); 90 $description = $this->get('description'); 91 92 $parentModule = $this->getEntityType($id); 93 if ($parentModule) { 94 $currentLanguage = Vtiger_Language_Handler::getLanguage(); 95 $moduleLanguageStrings = Vtiger_Language_Handler::getModuleStringsFromFile($currentLanguage,$parentModule); 96 vglobal('mod_strings', $moduleLanguageStrings['languageStrings']); 97 98 if ($parentModule != 'Users') { 99 // Apply merge for non-Users module merge tags. 100 $description = getMergedDescription($mergedDescription, $id, $parentModule); 101 } else { 102 // Re-merge the description for user tags based on actual user. 103 $description = getMergedDescription($description, $id, 'Users'); 104 vglobal('mod_strings', $old_mod_strings); 105 } 106 } 107 108 if (strpos($description, '$logo$')) { 109 $description = str_replace('$logo$',"<img src='cid:logo' />", $description); 110 $logo = true; 111 } 112 113 foreach($emails as $email) { 114 $mailer->Body = ''; 115 if ($parentModule) { 116 $mailer->Body = $this->getTrackImageDetails($id, $this->isEmailTrackEnabled()); 117 } 118 $mailer->Body .= $description; 119 $mailer->Signature = str_replace(array('\r\n', '\n'),'<br>',$currentUserModel->get('signature')); 120 if($mailer->Signature != '') { 121 $mailer->Body.= '<br><br>'.decode_html($mailer->Signature); 122 } 123 $mailer->Subject = $this->get('subject'); 124 $mailer->AddAddress($email); 125 126 //Adding attachments to mail 127 if(is_array($attachments)) { 128 foreach($attachments as $attachment) { 129 $fileNameWithPath = $rootDirectory.$attachment['path'].$attachment['fileid']."_".$attachment['attachment']; 130 if(is_file($fileNameWithPath)) { 131 $mailer->AddAttachment($fileNameWithPath, $attachment['attachment']); 132 } 133 } 134 } 135 if ($logo) { 136 //While sending email template and which has '$logo$' then it should replace with company logo 137 $mailer->AddEmbeddedImage(dirname(__FILE__).'/../../../layouts/vlayout/skins/images/logo_mail.jpg', 'logo', 'logo.jpg', 'base64', 'image/jpg'); 138 } 139 140 $ccs = array_filter(explode(',',$this->get('ccmail'))); 141 $bccs = array_filter(explode(',',$this->get('bccmail'))); 142 143 if(!empty($ccs)) { 144 foreach($ccs as $cc) $mailer->AddCC($cc); 145 } 146 if(!empty($bccs)) { 147 foreach($bccs as $bcc) $mailer->AddBCC($bcc); 148 } 149 } 150 $status = $mailer->Send(true); 151 if(!$status) { 152 $status = $mailer->getError(); 153 } else { 154 $mailString=$mailer->getMailString(); 155 $mailBoxModel = MailManager_Mailbox_Model::activeInstance(); 156 $folderName = $mailBoxModel->folder(); 157 if(!empty($folderName) && !empty($mailString)) { 158 $connector = MailManager_Connector_Connector::connectorWithModel($mailBoxModel, ''); 159 imap_append($connector->mBox, $connector->mBoxUrl.$folderName, $mailString, "\\Seen"); 160 } 161 } 162 } 163 return $status; 164 } 165 166 /** 167 * Returns the From Email address that will be used for the sent mails 168 * @return <String> - from email address 169 */ 170 function getFromEmailAddress() { 171 $db = PearDatabase::getInstance(); 172 $currentUserModel = Users_Record_Model::getCurrentUserModel(); 173 174 $fromEmail = false; 175 $result = $db->pquery('SELECT from_email_field FROM vtiger_systems WHERE server_type=?', array('email')); 176 if ($db->num_rows($result)) { 177 $fromEmail = decode_html($db->query_result($result, 0, 'from_email_field')); 178 } 179 if (empty($fromEmail)) $fromEmail = $currentUserModel->get('email1'); 180 return $fromEmail; 181 } 182 183 /** 184 * Function returns the attachment details for a email 185 * @return <Array> List of attachments 186 */ 187 function getAttachmentDetails() { 188 $db = PearDatabase::getInstance(); 189 190 $attachmentRes = $db->pquery("SELECT * FROM vtiger_attachments 191 INNER JOIN vtiger_seattachmentsrel ON vtiger_attachments.attachmentsid = vtiger_seattachmentsrel.attachmentsid 192 WHERE vtiger_seattachmentsrel.crmid = ?", array($this->getId())); 193 $numOfRows = $db->num_rows($attachmentRes); 194 $attachmentsList = array(); 195 if($numOfRows) { 196 for($i=0; $i<$numOfRows; $i++) { 197 $attachmentsList[$i]['fileid'] = $db->query_result($attachmentRes, $i, 'attachmentsid'); 198 $attachmentsList[$i]['attachment'] = decode_html($db->query_result($attachmentRes, $i, 'name')); 199 $path = $db->query_result($attachmentRes, $i, 'path'); 200 $attachmentsList[$i]['path'] = $path; 201 $attachmentsList[$i]['size'] = filesize($path.$attachmentsList[$i]['fileid'].'_'.$attachmentsList[$i]['attachment']); 202 $attachmentsList[$i]['type'] = $db->query_result($attachmentRes, $i, 'type'); 203 } 204 } 205 206 $documentsList = $this->getRelatedDocuments(); 207 208 //Attachments are getting duplicated when forwarding a mail in Mail Manager. 209 if($documentsList) { 210 foreach ($documentsList as $document) { 211 $flag = false; 212 foreach ($attachmentsList as $attachment) { 213 if($attachment['fileid'] == $document['fileid']) { 214 $flag = true; 215 break; 216 } 217 } 218 if(!$flag) $attachmentsList[] = $document; 219 } 220 } 221 222 return $attachmentsList; 223 } 224 225 /** 226 * Function returns the document details for a email 227 * @return <Array> List of Documents 228 */ 229 public function getRelatedDocuments() { 230 $db = PearDatabase::getInstance(); 231 232 $documentRes = $db->pquery("SELECT * FROM vtiger_senotesrel 233 INNER JOIN vtiger_crmentity ON vtiger_senotesrel.notesid = vtiger_crmentity.crmid AND vtiger_senotesrel.crmid = ? 234 INNER JOIN vtiger_notes ON vtiger_notes.notesid = vtiger_senotesrel.notesid 235 INNER JOIN vtiger_seattachmentsrel ON vtiger_seattachmentsrel.crmid = vtiger_notes.notesid 236 INNER JOIN vtiger_attachments ON vtiger_attachments.attachmentsid = vtiger_seattachmentsrel.attachmentsid 237 WHERE vtiger_crmentity.deleted = 0", array($this->getId())); 238 $numOfRows = $db->num_rows($documentRes); 239 240 if($numOfRows) { 241 for($i=0; $i<$numOfRows; $i++) { 242 $documentsList[$i]['name'] = $db->query_result($documentRes, $i, 'filename'); 243 $filesize = $db->query_result($documentRes, $i, 'filesize'); 244 $documentsList[$i]['size'] = $this->getFormattedFileSize($filesize); 245 $documentsList[$i]['docid'] = $db->query_result($documentRes, $i, 'notesid'); 246 $documentsList[$i]['path'] = $db->query_result($documentRes, $i, 'path'); 247 $documentsList[$i]['fileid'] = $db->query_result($documentRes, $i, 'attachmentsid'); 248 $documentsList[$i]['attachment'] = $db->query_result($documentRes, $i, 'name'); 249 $documentsList[$i]['type'] = $db->query_result($documentRes, $i, 'type'); 250 } 251 } 252 return $documentsList; 253 } 254 255 /** 256 * Function to get File size 257 * @param <Integer> $filesize 258 * @return <String> filesize 259 */ 260 public function getFormattedFileSize($filesize) { 261 if($filesize < 1024) { 262 $filesize = sprintf("%0.2f",round($filesize, 2)).'B'; 263 } else if($filesize > 1024 && $filesize < 1048576) { 264 $filesize = sprintf("%0.2f",round($filesize/1024, 2)).'KB'; 265 } else if($filesize > 1048576) { 266 $filesize = sprintf("%0.2f",round($filesize/(1024*1024), 2)).'MB'; 267 } 268 return $filesize; 269 } 270 271 /** 272 * Function to save details of document and email 273 */ 274 public function saveDocumentDetails() { 275 $db = PearDatabase::getInstance(); 276 $record = $this->getId(); 277 278 $documentIds = array_unique($this->get('documentids')); 279 280 $count = count($documentIds); 281 for ($i=0; $i<$count; $i++) { 282 $db->pquery("INSERT INTO vtiger_senotesrel(crmid, notesid) VALUES(?, ?)", array($record, $documentIds[$i])); 283 } 284 } 285 286 /** 287 * Function which will remove all the exising document links with email 288 * @param <Array> $idList - array of ids 289 */ 290 public function deleteDocumentLink($idList = array()){ 291 $db = PearDatabase::getInstance(); 292 $query = 'DELETE FROM vtiger_senotesrel where crmid=?'; 293 $params = array($this->getId()); 294 if(count($idList) > 0) { 295 $query .= 'AND notesid IN ('.generateQuestionMarks($idList).')'; 296 $params = array_merge($params,$idList); 297 } 298 $db->pquery($query,$params); 299 } 300 301 /** 302 * Function which will delete the existing attachments for the emails 303 * @param <Array> $emailAttachmentDetails - array of value which will be having fileid key as attachement id which need to be deleted 304 */ 305 public function deleteAttachment($emailAttachmentDetails = array()) { 306 $db = PearDatabase::getInstance(); 307 308 if(count($emailAttachmentDetails) <= 0) { 309 return; 310 } 311 $attachmentIdList = array(); 312 foreach($emailAttachmentDetails as $index => $attachInfo){ 313 $attachmentIdList[] = $attachInfo['fileid']; 314 } 315 316 $db->pquery('UPDATE vtiger_crmentity SET deleted=0 WHERE crmid IN('.generateQuestionMarks($attachmentIdList).')',$attachmentIdList); 317 $db->pquery('DELETE FROM vtiger_attachments WHERE attachmentsid IN('.generateQuestionMarks($attachmentIdList).')',$attachmentIdList); 318 $db->pquery('DELETE FROM vtiger_seattachmentsrel WHERE crmid=? and attachmentsid IN('.generateQuestionMarks($attachmentIdList).')', 319 array_merge(array($this->getId()),$attachmentIdList)); 320 321 } 322 323 /** 324 * Function to check the total size of files is morethan max upload size or not 325 * @param <Array> $documentIds 326 * @return <Boolean> true/false 327 */ 328 public function checkUploadSize($documentIds = false) { 329 $totalFileSize = 0; 330 if (!empty ($_FILES)) { 331 foreach ($_FILES as $fileDetails) { 332 $totalFileSize = $totalFileSize + (int) $fileDetails['size']; 333 } 334 } 335 if (!empty ($documentIds)) { 336 $count = count($documentIds); 337 for ($i=0; $i<$count; $i++) { 338 $documentRecordModel = Vtiger_Record_Model::getInstanceById($documentIds[$i], 'Documents'); 339 $totalFileSize = $totalFileSize + (int) $documentRecordModel->get('filesize'); 340 } 341 } 342 343 if ($totalFileSize > vglobal('upload_maxsize')) { 344 return false; 345 } 346 return true; 347 } 348 349 /** 350 * Function to get Track image details 351 * @param <Integer> $crmId 352 * @param <boolean> $emailTrack true/false 353 * @return <String> 354 */ 355 public function getTrackImageDetails($crmId, $emailTrack = true) { 356 $siteURL = vglobal('site_URL'); 357 $applicationKey = vglobal('application_unique_key'); 358 $emailId = $this->getId(); 359 360 $trackURL = "$siteURL/modules/Emails/actions/TrackAccess.php?record=$emailId&parentId=$crmId&applicationKey=$applicationKey"; 361 $imageDetails = "<img src='$trackURL' alt='' width='1' height='1'>"; 362 return $imageDetails; 363 } 364 365 366 /** 367 * Function check email track enabled or not 368 * @return <boolean> true/false 369 */ 370 public function isEmailTrackEnabled() { 371 //In future this track will be coming from client side/User preferences 372 return true; 373 } 374 375 /** 376 * Function to update Email track details 377 * @param <String> $parentId 378 */ 379 public function updateTrackDetails($parentId) { 380 $db = PearDatabase::getInstance(); 381 $recordId = $this->getId(); 382 383 $db->pquery("INSERT INTO vtiger_email_access(crmid, mailid, accessdate, accesstime) VALUES(?, ?, ?, ?)", array($parentId, $recordId, date('Y-m-d'), date('Y-m-d H:i:s'))); 384 385 $result = $db->pquery("SELECT 1 FROM vtiger_email_track WHERE crmid = ? AND mailid = ?", array($parentId, $recordId)); 386 if ($db->num_rows($result)>0) { 387 $db->pquery("UPDATE vtiger_email_track SET access_count = access_count+1 WHERE crmid = ? AND mailid = ?", array($parentId, $recordId)); 388 } else { 389 $db->pquery("INSERT INTO vtiger_email_track(crmid, mailid, access_count) values(?, ?, ?)", array($parentId, $recordId, 1)); 390 } 391 } 392 393 /** 394 * Function to set Access count value by default as 0 395 */ 396 public function setAccessCountValue() { 397 $record = $this->getId(); 398 $moduleName = $this->getModuleName(); 399 400 $focus = new $moduleName(); 401 $focus->setEmailAccessCountValue($record); 402 } 403 404 /** 405 * Function to get Access count value 406 * @param <String> $parentId 407 * @return <String> 408 */ 409 public function getAccessCountValue($parentId) { 410 $db = PearDatabase::getInstance(); 411 412 $result = $db->pquery("SELECT access_count FROM vtiger_email_track WHERE crmid = ? AND mailid = ?", array($parentId, $this->getId())); 413 return $db->query_result($result, 0, 'access_count'); 414 } 415 416 /** 417 * Function checks if the mail is sent or not 418 * @return <Boolean> 419 */ 420 public function isSentMail(){ 421 if(!array_key_exists('email_flag', $this->getData())){ 422 $db = PearDatabase::getInstance(); 423 $query = 'SELECT email_flag FROM vtiger_emaildetails WHERE emailid=?'; 424 $result = $db->pquery($query,array($this->getId())); 425 if($db->num_rows($result)>0) { 426 $this->set('email_flag',$db->query_result($result,0,'email_flag')); 427 } else { 428 //If not row exits then make it as false 429 return false; 430 } 431 } 432 if($this->get('email_flag') == "SENT"){ 433 return true; 434 } 435 return false; 436 } 437 438 //Opensource fix for data updation for mail attached from mailmanager 439 public function isFromMailManager(){ 440 if(!array_key_exists('email_flag', $this->getData())){ 441 $db = PearDatabase::getInstance(); 442 $query = 'SELECT email_flag FROM vtiger_emaildetails WHERE emailid=?'; 443 $result = $db->pquery($query,array($this->getId())); 444 if($db->num_rows($result)>0) { 445 $this->set('email_flag',$db->query_result($result,0,'email_flag')); 446 } else { 447 //If not row exits then make it as false 448 return false; 449 } 450 } 451 if($this->get('email_flag') == "MailManager"){ 452 return true; 453 } 454 return false; 455 } 456 function getEntityType($id) { 457 $db = PearDatabase::getInstance(); 458 $moduleModel = $this->getModule(); 459 $emailRelatedModules = $moduleModel->getEmailRelatedModules(); 460 $relatedModule = ''; 461 if (!empty($id)) { 462 $sql = "SELECT setype FROM vtiger_crmentity WHERE crmid=?"; 463 $result = $db->pquery($sql, array($id)); 464 $relatedModule = $db->query_result($result, 0, "setype"); 465 466 if(!in_array($relatedModule, $emailRelatedModules)){ 467 $sql = 'SELECT id FROM vtiger_users WHERE id=?'; 468 $result = $db->pquery($sql, array($id)); 469 if($db->num_rows($result) > 0){ 470 $relatedModule = 'Users'; 471 } 472 } 473 } 474 return $relatedModule; 475 } 476 }
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 |