[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/com_vtiger_workflow/tasks/ -> VTEmailTask.inc (source)

   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  require_once ('modules/com_vtiger_workflow/VTEntityCache.inc');
  11  require_once ('modules/com_vtiger_workflow/VTWorkflowUtils.php');
  12  require_once ('modules/com_vtiger_workflow/VTEmailRecipientsTemplate.inc');
  13  require_once ('modules/Emails/mail.php');
  14  require_once ('include/simplehtmldom/simple_html_dom.php');
  15  
  16  class VTEmailTask extends VTTask{
  17      // Sending email takes more time, this should be handled via queue all the time.
  18      public $executeImmediately = false;
  19  
  20  	public function getFieldNames(){
  21          return array("subject", "content", "recepient", 'emailcc', 'emailbcc', 'fromEmail');
  22      }
  23  	public function doTask($entity){
  24          global $adb, $current_user;
  25          $util = new VTWorkflowUtils();
  26          $admin = $util->adminUser();
  27          $module = $entity->getModuleName();
  28  
  29          $taskContents = Zend_Json::decode($this->getContents($entity));
  30          $from_email    = $taskContents['fromEmail'];
  31          $from_name    = $taskContents['fromName'];
  32          $to_email    = $taskContents['toEmail'];
  33          $cc            = $taskContents['ccEmail'];
  34          $bcc        = $taskContents['bccEmail'];
  35          $subject    = $taskContents['subject'];
  36          $content    = $taskContents['content'];
  37  
  38          if(!empty($to_email)) {
  39              //Storing the details of emails
  40              $entityIdDetails = vtws_getIdComponents($entity->getId());
  41              $entityId = $entityIdDetails[1];
  42              $moduleName = 'Emails';
  43              $userId = $current_user->id;
  44              $emailFocus = CRMEntity::getInstance($moduleName);
  45              $emailFieldValues = array(
  46                      'assigned_user_id' => $userId,
  47                      'subject' => $subject,
  48                      'description' => $content,
  49                      'from_email' => $from_email,
  50                      'saved_toid' => $to_email,
  51                      'ccmail' => $cc,
  52                      'bccmail' => $bcc,
  53                      'parent_id' => $entityId."@$userId|",
  54                      'email_flag' => 'SENT',
  55                      'activitytype' => $moduleName,
  56                      'date_start' => date('Y-m-d'),
  57                      'time_start' => date('H:i:s'),
  58                      'mode' => '',
  59                      'id' => ''
  60              );
  61              $emailFocus->column_fields = $emailFieldValues;
  62              $emailFocus->save($moduleName);
  63  
  64  
  65              //Including email tracking details
  66              global $site_URL, $application_unique_key;
  67              $emailId = $emailFocus->id;
  68              $trackURL = "$site_URL/modules/Emails/TrackAccess.php?record=$entityId&mailid=$emailId&app_key=$application_unique_key";
  69              $content = "<img src='$trackURL' alt='' width='1' height='1'>$content";
  70  
  71              if (stripos($content, '<img src="cid:logo" />')) {
  72                  $logo = 1;
  73              }
  74  
  75              $status = send_mail($module, $to_email, $from_name, $from_email, $subject, $content, $cc, $bcc, '', '', $logo);
  76  
  77              if(!empty($emailId)) {
  78                  $emailFocus->setEmailAccessCountValue($emailId);
  79              }
  80              if(!$status) {
  81                  //If mail is not sent then removing the details about email
  82                  $emailFocus->trash($moduleName, $emailId);
  83              }
  84          }
  85          $util->revertUser();
  86  
  87      }
  88  
  89      /**
  90       * Function to get contents of this task
  91       * @param <Object> $entity
  92       * @return <Array> contents
  93       */
  94  	public function getContents($entity, $entityCache=false) {
  95          if (!$this->contents) {
  96              global $adb, $current_user;
  97              $taskContents = array();
  98              $entityId = $entity->getId();
  99  
 100              $utils = new VTWorkflowUtils();
 101              $adminUser = $utils->adminUser();
 102              if (!$entityCache) {
 103                  $entityCache = new VTEntityCache($adminUser);
 104              }
 105  
 106              $fromUserId = Users::getActiveAdminId();
 107              $entityOwnerId = $entity->get('assigned_user_id');
 108              if ($entityOwnerId) {
 109                  list ($moduleId, $fromUserId) = explode('x', $entityOwnerId);
 110              }
 111  
 112              $ownerEntity = $entityCache->forId($entityOwnerId);
 113              if($ownerEntity->getModuleName() === 'Groups') {
 114                  list($moduleId, $recordId) = vtws_getIdComponents($entityId);
 115                  $fromUserId = Vtiger_Util_Helper::getCreator($recordId);
 116              }
 117  
 118              if ($this->fromEmail && !($ownerEntity->getModuleName() === 'Groups' && strpos($this->fromEmail, 'assigned_user_id : (Users) ') !== false)) {
 119                  $et = new VTEmailRecipientsTemplate($this->fromEmail);
 120                  $fromEmailDetails = $et->render($entityCache, $entityId);
 121  
 122                  if(strpos($this->fromEmail, '&lt;') && strpos($this->fromEmail, '&gt;')) {
 123                      list($fromName, $fromEmail) = explode('&lt;', $fromEmailDetails);
 124                      list($fromEmail, $rest) = explode('&gt;', $fromEmail);
 125                  } else {
 126                      $fromEmail = $fromEmailDetails;
 127                  }
 128              } else {
 129                  $userObj = CRMEntity::getInstance('Users');
 130                  $userObj->retrieveCurrentUserInfoFromFile($fromUserId);
 131                  if ($userObj) {
 132                      $fromEmail = $userObj->email1;
 133                      $fromName =    $userObj->user_name;
 134                  } else {
 135                      $result = $adb->pquery('SELECT user_name, email1 FROM vtiger_users WHERE id = ?', array($fromUserId));
 136                      $fromEmail = $adb->query_result($result, 0, 'email1');
 137                      $fromName =    $adb->query_result($result, 0, 'user_name');
 138                  }
 139              }
 140  
 141              if (!$fromEmail) {
 142                  $utils->revertUser();
 143                  return false;
 144              }
 145  
 146              $taskContents['fromEmail'] = $fromEmail;
 147              $taskContents['fromName'] =    $fromName;
 148  
 149              if ($entity->getModuleName() === 'Events') {
 150                  $contactId = $entity->get('contact_id');
 151                  if ($contactId) {
 152                      $contactIds = '';
 153                      list($wsId, $recordId) = explode('x', $entityId);
 154                      $webserviceObject = VtigerWebserviceObject::fromName($adb, 'Contacts');
 155  
 156                      $result = $adb->pquery('SELECT contactid FROM vtiger_cntactivityrel WHERE activityid = ?', array($recordId));
 157                      $numOfRows = $adb->num_rows($result);
 158                      for($i=0; $i<$numOfRows; $i++) {
 159                          $contactIds .= vtws_getId($webserviceObject->getEntityId(), $adb->query_result($result, $i, 'contactid')).',';
 160                      }
 161                  }
 162                  $entity->set('contact_id', trim($contactIds, ','));
 163                  $entityCache->cache[$entityId] = $entity;
 164              }
 165  
 166              $et = new VTEmailRecipientsTemplate($this->recepient);
 167              $toEmail = $et->render($entityCache, $entityId);
 168  
 169              $ecct = new VTEmailRecipientsTemplate($this->emailcc);
 170              $ccEmail = $ecct->render($entityCache, $entityId);
 171  
 172              $ebcct = new VTEmailRecipientsTemplate($this->emailbcc);
 173              $bccEmail = $ebcct->render($entityCache, $entityId);
 174  
 175              if(strlen(trim($toEmail, " \t\n,")) == 0 && strlen(trim($ccEmail, " \t\n,")) == 0 && strlen(trim($bccEmail, " \t\n,")) == 0) {
 176                  $utils->revertUser();
 177                  return false;
 178              }
 179  
 180              $taskContents['toEmail'] = $toEmail;
 181              $taskContents['ccEmail'] = $ccEmail;
 182              $taskContents['bccEmail'] = $bccEmail;
 183  
 184              $st = new VTSimpleTemplate($this->subject);
 185              $taskContents['subject'] = $st->render($entityCache, $entityId);
 186  
 187              $ct = new VTSimpleTemplate($this->content);
 188              $taskContents['content'] = $ct->render($entityCache, $entityId);
 189              $this->contents = $taskContents;
 190              $utils->revertUser();
 191          }
 192          if(is_array($this->contents)) {
 193              $this->contents = Zend_Json::encode($this->contents);
 194          }
 195          return $this->contents;
 196      }
 197  }
 198  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1