Merge pull request #14018 from seamuslee001/financial_extension_export_cxn_dashlet
[civicrm-core.git] / CRM / Mailing / Form / ForwardMailing.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33class CRM_Mailing_Form_ForwardMailing extends CRM_Core_Form {
00be9182 34 public function preProcess() {
6a488035
TO
35 $job_id = CRM_Utils_Request::retrieve('jid', 'Positive',
36 $this, NULL
37 );
38 $queue_id = CRM_Utils_Request::retrieve('qid', 'Positive',
39 $this, NULL
40 );
41 $hash = CRM_Utils_Request::retrieve('h', 'String',
42 $this, NULL
43 );
44
45 $q = CRM_Mailing_Event_BAO_Queue::verify($job_id, $queue_id, $hash);
46
47 if ($q == NULL) {
48
25606795 49 // ERROR.
6a488035
TO
50 CRM_Core_Error::fatal(ts('Invalid form parameters.'));
51 CRM_Core_Error::statusBounce(ts('Invalid form parameters.'));
52 }
53 $mailing = &$q->getMailing();
54
55 if ($hash) {
56 $emailId = CRM_Core_DAO::getfieldValue('CRM_Mailing_Event_DAO_Queue', $hash, 'email_id', 'hash');
57 $this->_fromEmail = $fromEmail = CRM_Core_DAO::getfieldValue('CRM_Core_DAO_Email', $emailId, 'email');
58 $this->assign('fromEmail', $fromEmail);
59 }
60
25606795
SB
61 // Show the subject instead of the name here, since it's being
62 // displayed to external contacts/users.
6a488035 63
be2fb01f 64 CRM_Utils_System::setTitle(ts('Forward Mailing: %1', [1 => $mailing->subject]));
6a488035
TO
65
66 $this->set('queue_id', $queue_id);
67 $this->set('job_id', $job_id);
68 $this->set('hash', $hash);
69 }
70
71 /**
fe482240 72 * Build the form object.
6a488035
TO
73 */
74 public function buildQuickForm() {
75 for ($i = 0; $i < 5; $i++) {
be2fb01f 76 $this->add('text', "email_$i", ts('Email %1', [1 => $i + 1]));
6a488035
TO
77 $this->addRule("email_$i", ts('Email is not valid.'), 'email');
78 }
79
80 //insert message Text by selecting "Select Template option"
be2fb01f 81 $this->add('textarea', 'forward_comment', ts('Comment'), ['cols' => '80', 'rows' => '8']);
5d51a2f9 82 $this->add('wysiwyg', 'html_comment',
6a488035 83 ts('HTML Message'),
be2fb01f 84 ['cols' => '80', 'rows' => '8']
6a488035
TO
85 );
86
be2fb01f
CW
87 $this->addButtons([
88 [
353ffa53
TO
89 'type' => 'next',
90 'name' => ts('Forward'),
91 'isDefault' => TRUE,
be2fb01f
CW
92 ],
93 [
353ffa53
TO
94 'type' => 'cancel',
95 'name' => ts('Cancel'),
be2fb01f
CW
96 ],
97 ]);
6a488035
TO
98 }
99
100 /**
101 * Form submission of new/edit contact is processed.
6a488035
TO
102 */
103 public function postProcess() {
353ffa53
TO
104 $queue_id = $this->get('queue_id');
105 $job_id = $this->get('job_id');
106 $hash = $this->get('hash');
6a488035
TO
107 $timeStamp = date('YmdHis');
108
353ffa53 109 $formValues = $this->controller->exportValues($this->_name);
be2fb01f 110 $params = [];
6a488035 111 $params['body_text'] = $formValues['forward_comment'];
353ffa53 112 $html_comment = $formValues['html_comment'];
6a488035
TO
113 $params['body_html'] = str_replace('%7B', '{', str_replace('%7D', '}', $html_comment));
114
be2fb01f 115 $emails = [];
6a488035
TO
116 for ($i = 0; $i < 5; $i++) {
117 $email = $this->controller->exportValue($this->_name, "email_$i");
118 if (!empty($email)) {
119 $emails[] = $email;
120 }
121 }
122
123 $forwarded = NULL;
124 foreach ($emails as $email) {
be2fb01f 125 $params = [
6a488035
TO
126 'version' => 3,
127 'job_id' => $job_id,
128 'event_queue_id' => $queue_id,
129 'hash' => $hash,
130 'email' => $email,
131 'time_stamp' => $timeStamp,
132 'fromEmail' => $this->_fromEmail,
133 'params' => $params,
be2fb01f 134 ];
6a488035
TO
135 $result = civicrm_api('Mailing', 'event_forward', $params);
136 if (!civicrm_error($result)) {
137 $forwarded++;
138 }
139 }
140
be2fb01f 141 $status = ts('Mailing is not forwarded to the given email address.', [
353ffa53 142 'count' => count($emails),
3bdca100 143 'plural' => 'Mailing is not forwarded to the given email addresses.',
be2fb01f 144 ]);
6a488035 145 if ($forwarded) {
be2fb01f 146 $status = ts('Mailing is forwarded successfully to %count email address.', [
353ffa53 147 'count' => $forwarded,
3bdca100 148 'plural' => 'Mailing is forwarded successfully to %count email addresses.',
be2fb01f 149 ]);
6a488035
TO
150 }
151
152 CRM_Utils_System::setUFMessage($status);
153
154 // always redirect to front page of url
155 $session = CRM_Core_Session::singleton();
156 $config = CRM_Core_Config::singleton();
157 $session->pushUserContext($config->userFrameworkBaseURL);
158 }
96025800 159
6a488035 160}