Merge pull request #23767 from colemanw/afformRequireAuthx
[civicrm-core.git] / CRM / Mailing / Form / ForwardMailing.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Mailing_Form_ForwardMailing extends CRM_Core_Form {
18
19 public function preProcess() {
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
34 // ERROR.
35 throw new CRM_Core_Exception(ts('Invalid form parameters.'));
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
46 // Show the subject instead of the name here, since it's being
47 // displayed to external contacts/users.
48
49 $this->setTitle(ts('Forward Mailing: %1', [1 => $mailing->subject]));
50
51 $this->set('queue_id', $queue_id);
52 $this->set('job_id', $job_id);
53 $this->set('hash', $hash);
54 }
55
56 /**
57 * Build the form object.
58 */
59 public function buildQuickForm() {
60 for ($i = 0; $i < 5; $i++) {
61 $this->add('text', "email_$i", ts('Email %1', [1 => $i + 1]));
62 $this->addRule("email_$i", ts('Email is not valid.'), 'email');
63 }
64
65 //insert message Text by selecting "Select Template option"
66 $this->add('textarea', 'forward_comment', ts('Comment'), ['cols' => '80', 'rows' => '8']);
67 $this->add('wysiwyg', 'html_comment',
68 ts('HTML Message'),
69 ['cols' => '80', 'rows' => '8']
70 );
71
72 $this->addButtons([
73 [
74 'type' => 'next',
75 'name' => ts('Forward'),
76 'isDefault' => TRUE,
77 ],
78 ]);
79 }
80
81 /**
82 * Form submission of new/edit contact is processed.
83 */
84 public function postProcess() {
85 $queue_id = $this->get('queue_id');
86 $job_id = $this->get('job_id');
87 $hash = $this->get('hash');
88 $timeStamp = date('YmdHis');
89
90 $formValues = $this->controller->exportValues($this->_name);
91 $params = [];
92 $params['body_text'] = $formValues['forward_comment'];
93 $html_comment = $formValues['html_comment'];
94 $params['body_html'] = str_replace('%7B', '{', str_replace('%7D', '}', $html_comment));
95
96 $emails = [];
97 for ($i = 0; $i < 5; $i++) {
98 $email = $this->controller->exportValue($this->_name, "email_$i");
99 if (!empty($email)) {
100 $emails[] = $email;
101 }
102 }
103
104 $forwarded = NULL;
105 foreach ($emails as $email) {
106 $params = [
107 'version' => 3,
108 'job_id' => $job_id,
109 'event_queue_id' => $queue_id,
110 'hash' => $hash,
111 'email' => $email,
112 'time_stamp' => $timeStamp,
113 'fromEmail' => $this->_fromEmail,
114 'params' => $params,
115 ];
116 $result = civicrm_api('Mailing', 'event_forward', $params);
117 if (!civicrm_error($result)) {
118 $forwarded++;
119 }
120 }
121
122 $status = ts('Mailing is not forwarded to the given email address.', [
123 'count' => count($emails),
124 'plural' => 'Mailing is not forwarded to the given email addresses.',
125 ]);
126 if ($forwarded) {
127 $status = ts('Mailing is forwarded successfully to %count email address.', [
128 'count' => $forwarded,
129 'plural' => 'Mailing is forwarded successfully to %count email addresses.',
130 ]);
131 }
132
133 CRM_Utils_System::setUFMessage($status);
134
135 // always redirect to front page of url
136 $session = CRM_Core_Session::singleton();
137 $config = CRM_Core_Config::singleton();
138 $session->pushUserContext($config->userFrameworkBaseURL);
139 }
140
141 }