Replace CRM_Utils_Array::value with ?? in variable assignments
[civicrm-core.git] / CRM / Mailing / Form / ForwardMailing.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Mailing_Form_ForwardMailing extends CRM_Core_Form {
7e8c8317 18
00be9182 19 public function preProcess() {
6a488035
TO
20 $job_id = CRM_Utils_Request::retrieve('jid', 'Positive',
21 $this, NULL
22 );
23 $queue_id = CRM_Utils_Request::retrieve('qid', 'Positive',
24 $this, NULL
25 );
26 $hash = CRM_Utils_Request::retrieve('h', 'String',
27 $this, NULL
28 );
29
30 $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash);
31
32 if ($q == NULL) {
33
25606795 34 // ERROR.
2a7b8221 35 throw new CRM_Core_Exception(ts('Invalid form parameters.'));
6a488035
TO
36 CRM_Core_Error::statusBounce(ts('Invalid form parameters.'));
37 }
38 $mailing = &$q->getMailing();
39
40 if ($hash) {
41 $emailId = CRM_Core_DAO::getfieldValue('CRM_Mailing_Event_DAO_Queue', $hash, 'email_id', 'hash');
42 $this->_fromEmail = $fromEmail = CRM_Core_DAO::getfieldValue('CRM_Core_DAO_Email', $emailId, 'email');
43 $this->assign('fromEmail', $fromEmail);
44 }
45
25606795
SB
46 // Show the subject instead of the name here, since it's being
47 // displayed to external contacts/users.
6a488035 48
be2fb01f 49 CRM_Utils_System::setTitle(ts('Forward Mailing: %1', [1 => $mailing->subject]));
6a488035
TO
50
51 $this->set('queue_id', $queue_id);
52 $this->set('job_id', $job_id);
53 $this->set('hash', $hash);
54 }
55
56 /**
fe482240 57 * Build the form object.
6a488035
TO
58 */
59 public function buildQuickForm() {
60 for ($i = 0; $i < 5; $i++) {
be2fb01f 61 $this->add('text', "email_$i", ts('Email %1', [1 => $i + 1]));
6a488035
TO
62 $this->addRule("email_$i", ts('Email is not valid.'), 'email');
63 }
64
65 //insert message Text by selecting "Select Template option"
be2fb01f 66 $this->add('textarea', 'forward_comment', ts('Comment'), ['cols' => '80', 'rows' => '8']);
5d51a2f9 67 $this->add('wysiwyg', 'html_comment',
6a488035 68 ts('HTML Message'),
be2fb01f 69 ['cols' => '80', 'rows' => '8']
6a488035
TO
70 );
71
be2fb01f
CW
72 $this->addButtons([
73 [
353ffa53
TO
74 'type' => 'next',
75 'name' => ts('Forward'),
76 'isDefault' => TRUE,
be2fb01f
CW
77 ],
78 [
353ffa53
TO
79 'type' => 'cancel',
80 'name' => ts('Cancel'),
be2fb01f
CW
81 ],
82 ]);
6a488035
TO
83 }
84
85 /**
86 * Form submission of new/edit contact is processed.
6a488035
TO
87 */
88 public function postProcess() {
353ffa53
TO
89 $queue_id = $this->get('queue_id');
90 $job_id = $this->get('job_id');
91 $hash = $this->get('hash');
6a488035
TO
92 $timeStamp = date('YmdHis');
93
353ffa53 94 $formValues = $this->controller->exportValues($this->_name);
be2fb01f 95 $params = [];
6a488035 96 $params['body_text'] = $formValues['forward_comment'];
353ffa53 97 $html_comment = $formValues['html_comment'];
6a488035
TO
98 $params['body_html'] = str_replace('%7B', '{', str_replace('%7D', '}', $html_comment));
99
be2fb01f 100 $emails = [];
6a488035
TO
101 for ($i = 0; $i < 5; $i++) {
102 $email = $this->controller->exportValue($this->_name, "email_$i");
103 if (!empty($email)) {
104 $emails[] = $email;
105 }
106 }
107
108 $forwarded = NULL;
109 foreach ($emails as $email) {
be2fb01f 110 $params = [
6a488035
TO
111 'version' => 3,
112 'job_id' => $job_id,
113 'event_queue_id' => $queue_id,
114 'hash' => $hash,
115 'email' => $email,
116 'time_stamp' => $timeStamp,
117 'fromEmail' => $this->_fromEmail,
118 'params' => $params,
be2fb01f 119 ];
6a488035
TO
120 $result = civicrm_api('Mailing', 'event_forward', $params);
121 if (!civicrm_error($result)) {
122 $forwarded++;
123 }
124 }
125
be2fb01f 126 $status = ts('Mailing is not forwarded to the given email address.', [
7e8c8317
SL
127 'count' => count($emails),
128 'plural' => 'Mailing is not forwarded to the given email addresses.',
129 ]);
6a488035 130 if ($forwarded) {
be2fb01f 131 $status = ts('Mailing is forwarded successfully to %count email address.', [
7e8c8317
SL
132 'count' => $forwarded,
133 'plural' => 'Mailing is forwarded successfully to %count email addresses.',
134 ]);
6a488035
TO
135 }
136
137 CRM_Utils_System::setUFMessage($status);
138
139 // always redirect to front page of url
140 $session = CRM_Core_Session::singleton();
141 $config = CRM_Core_Config::singleton();
142 $session->pushUserContext($config->userFrameworkBaseURL);
143 }
96025800 144
6a488035 145}