Commit | Line | Data |
---|---|---|
2a55af8e EM |
1 | <?php |
2 | ||
3 | /** | |
4 | * Job for SMS deliery functions. | |
5 | */ | |
6 | class CRM_Mailing_BAO_SMSJob extends CRM_Mailing_BAO_MailingJob { | |
7 | ||
8 | /** | |
9 | * This is used by CiviMail but will be made redundant by FlexMailer. | |
10 | * @param array $fields | |
11 | * List of intended recipients. | |
12 | * Each recipient is an array with keys 'hash', 'contact_id', 'email', etc. | |
13 | * @param $mailing | |
14 | * @param $mailer | |
15 | * @param $job_date | |
16 | * @param $attachments | |
17 | * | |
18 | * @return bool|null | |
19 | * @throws Exception | |
20 | */ | |
21 | public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments) { | |
22 | $count = 0; | |
23 | // dev/core#1768 Get the mail sync interval. | |
24 | $mail_sync_interval = Civi::settings()->get('civimail_sync_interval'); | |
25 | $retryGroup = FALSE; | |
26 | ||
27 | foreach ($fields as $field) { | |
28 | $contact = civicrm_api3('Contact', 'getsingle', ['id' => $field['contact_id']]); | |
29 | ||
30 | $preview = civicrm_api3('Mailing', 'preview', [ | |
31 | 'id' => $mailing->id, | |
32 | 'contact_id' => $field['contact_id'], | |
33 | ])['values']; | |
34 | $mailParams = [ | |
35 | 'text' => $preview['body_text'], | |
36 | 'toName' => $contact['display_name'], | |
37 | 'job_id' => $this->id, | |
38 | ]; | |
39 | CRM_Utils_Hook::alterMailParams($mailParams, 'civimail'); | |
40 | $body = $mailParams['text']; | |
41 | $headers = ['To' => $field['phone']]; | |
42 | ||
43 | try { | |
44 | $result = $mailer->send($headers['To'], $headers, $body, $this->id); | |
45 | ||
46 | // Register the delivery event. | |
47 | $deliveredParams[] = $field['id']; | |
48 | $targetParams[] = $field['contact_id']; | |
49 | ||
50 | $count++; | |
51 | // dev/core#1768 Mail sync interval is now configurable. | |
52 | if ($count % $mail_sync_interval == 0) { | |
53 | $this->writeToDB( | |
54 | $deliveredParams, | |
55 | $targetParams, | |
56 | $mailing, | |
57 | $job_date | |
58 | ); | |
59 | $count = 0; | |
60 | ||
61 | // hack to stop mailing job at run time, CRM-4246. | |
62 | // to avoid making too many DB calls for this rare case | |
63 | // lets do it when we snapshot | |
64 | $status = CRM_Core_DAO::getFieldValue( | |
65 | 'CRM_Mailing_DAO_MailingJob', | |
66 | $this->id, | |
67 | 'status', | |
68 | 'id', | |
69 | TRUE | |
70 | ); | |
71 | ||
72 | if ($status !== 'Running') { | |
73 | return FALSE; | |
74 | } | |
75 | } | |
76 | } | |
77 | catch (CRM_Core_Exception $e) { | |
78 | // Handle SMS errors: CRM-15426 | |
79 | $job_id = (int) $this->id; | |
80 | $mailing_id = (int) $mailing->id; | |
81 | CRM_Core_Error::debug_log_message("Failed to send SMS message. Vars: mailing_id: ${mailing_id}, job_id: ${job_id}. Error message follows."); | |
82 | CRM_Core_Error::debug_log_message($e->getMessage()); | |
83 | } | |
84 | ||
85 | unset($result); | |
86 | ||
87 | // If we have enabled the Throttle option, this is the time to enforce it. | |
88 | $mailThrottleTime = Civi::settings()->get('mailThrottleTime'); | |
89 | if (!empty($mailThrottleTime)) { | |
90 | usleep((int ) $mailThrottleTime); | |
91 | } | |
92 | } | |
93 | ||
94 | $result = $this->writeToDB( | |
95 | $deliveredParams, | |
96 | $targetParams, | |
97 | $mailing, | |
98 | $job_date | |
99 | ); | |
100 | ||
101 | if ($retryGroup) { | |
102 | return FALSE; | |
103 | } | |
104 | ||
105 | return $result; | |
106 | } | |
107 | ||
108 | } |