Commit | Line | Data |
---|---|---|
bdf67e28 SL |
1 | <?php |
2 | namespace Civi\FlexMailer\API; | |
3 | ||
4 | use Civi\FlexMailer\FlexMailer; | |
5 | use Civi\FlexMailer\FlexMailerTask; | |
6 | use Civi\FlexMailer\Listener\Abdicator; | |
7 | ||
8 | class MailingPreview { | |
9 | ||
10 | /** | |
11 | * Generate a preview of how a mailing would look. | |
12 | * | |
13 | * @param array $apiRequest | |
14 | * - entity: string | |
15 | * - action: string | |
16 | * - params: array | |
17 | * - id: int | |
18 | * - contact_id: int | |
19 | * @return array | |
20 | * @throws \CRM_Core_Exception | |
21 | */ | |
22 | public static function preview($apiRequest) { | |
23 | $params = $apiRequest['params']; | |
24 | ||
25 | /** @var \CRM_Mailing_BAO_Mailing $mailing */ | |
26 | $mailing = new \CRM_Mailing_BAO_Mailing(); | |
27 | $mailingID = \CRM_Utils_Array::value('id', $params); | |
28 | if ($mailingID) { | |
29 | $mailing->id = $mailingID; | |
30 | $mailing->find(TRUE); | |
31 | } | |
32 | else { | |
33 | $mailing->copyValues($params); | |
34 | } | |
35 | ||
2a55af8e | 36 | if (!Abdicator::isFlexmailPreferred($mailing) && empty($mailing->sms_provider_id)) { |
bdf67e28 SL |
37 | require_once 'api/v3/Mailing.php'; |
38 | return civicrm_api3_mailing_preview($params); | |
39 | } | |
40 | ||
41 | $contactID = \CRM_Utils_Array::value('contact_id', $params, | |
42 | \CRM_Core_Session::singleton()->get('userID')); | |
43 | ||
f4dc5180 TO |
44 | $job = new class extends \CRM_Mailing_BAO_MailingJob { |
45 | ||
46 | public function insert() { | |
47 | throw new \RuntimeException('MailingJob is just a preview. It cannot be saved.'); | |
48 | } | |
49 | ||
50 | public function update($dataObject = FALSE) { | |
51 | throw new \RuntimeException('MailingJob is just a preview. It cannot be saved.'); | |
52 | } | |
53 | ||
54 | public function save($hook = TRUE) { | |
55 | throw new \RuntimeException('MailingJob is just a preview. It cannot be saved.'); | |
56 | } | |
57 | ||
58 | }; | |
bdf67e28 | 59 | $job->mailing_id = $mailing->id ?: NULL; |
bdf67e28 | 60 | $job->status = 'Complete'; |
bdf67e28 SL |
61 | |
62 | $flexMailer = new FlexMailer(array( | |
63 | 'is_preview' => TRUE, | |
64 | 'mailing' => $mailing, | |
65 | 'job' => $job, | |
66 | 'attachments' => \CRM_Core_BAO_File::getEntityFile('civicrm_mailing', | |
67 | $mailing->id), | |
68 | )); | |
69 | ||
70 | if (count($flexMailer->validate()) > 0) { | |
71 | throw new \CRM_Core_Exception("FlexMailer cannot execute: invalid context"); | |
72 | } | |
73 | ||
74 | $task = new FlexMailerTask($job->id, $contactID, 'fakehash', | |
75 | 'placeholder@example.com'); | |
76 | ||
77 | $flexMailer->fireComposeBatch(array($task)); | |
78 | ||
79 | return civicrm_api3_create_success(array( | |
80 | 'id' => isset($params['id']) ? $params['id'] : NULL, | |
81 | 'contact_id' => $contactID, | |
82 | 'subject' => $task->getMailParam('Subject'), | |
83 | 'body_html' => $task->getMailParam('html'), | |
84 | 'body_text' => $task->getMailParam('text'), | |
85 | // Flag our role in processing this - to support tests. | |
86 | '_rendered_by_' => 'flexmailer', | |
87 | )); | |
88 | } | |
89 | ||
90 | } |