Merge pull request #23283 from eileenmcnaughton/import_saved_map
[civicrm-core.git] / CRM / Mailing / Form / Approve.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_Approve extends CRM_Core_Form {
18
19 public function redirectToListing() {
20 $url = CRM_Utils_System::url('civicrm/mailing/browse/scheduled', 'reset=1&scheduled=true');
21 CRM_Utils_System::redirect($url);
22 }
23
24 /**
25 * Set variables up before form is built.
26 */
27 public function preProcess() {
28 if (CRM_Mailing_Info::workflowEnabled()) {
29 if (!CRM_Core_Permission::check('approve mailings') && !CRM_Core_Permission::check('access CiviMail')) {
30 $this->redirectToListing();
31 }
32 }
33 else {
34 $this->redirectToListing();
35 }
36
37 // when user come from search context.
38 $this->_searchBasedMailing = CRM_Contact_Form_Search::isSearchContext($this->get('context'));
39
40 //retrieve mid from different wizard and url contexts
41 $this->_mailingID = $this->get('mailing_id');
42 $this->_approveFormOnly = FALSE;
43 if (!$this->_mailingID) {
44 $this->_mailingID = CRM_Utils_Request::retrieve('mid', 'Integer', $this, TRUE);
45 $this->_approveFormOnly = TRUE;
46 }
47
48 $session = CRM_Core_Session::singleton();
49 $this->_contactID = $session->get('userID');
50
51 $this->_mailing = new CRM_Mailing_BAO_Mailing();
52 $this->_mailing->id = $this->_mailingID;
53 if (!$this->_mailing->find(TRUE)) {
54 $this->redirectToListing();
55 }
56 }
57
58 /**
59 * Set default values for the form.
60 */
61 public function setDefaultValues() {
62 $defaults = [];
63 if ($this->_mailingID) {
64 $defaults['approval_status_id'] = $this->_mailing->approval_status_id;
65 $defaults['approval_note'] = $this->_mailing->approval_note;
66 }
67
68 return $defaults;
69 }
70
71 /**
72 * Build the form object for the approval/rejection mailing.
73 */
74 public function buildQuickform() {
75 $title = ts('Approve/Reject Mailing') . " - {$this->_mailing->name}";
76 $this->setTitle($title);
77
78 $this->addElement('textarea', 'approval_note', ts('Approve/Reject Note'));
79
80 $mailApprovalStatus = CRM_Core_PseudoConstant::get('CRM_Mailing_BAO_Mailing', 'approval_status_id');
81
82 // eliminate the none option
83 $noneOptionID = CRM_Core_PseudoConstant::getKey('CRM_Mailing_BAO_Mailing', 'approval_status_id', 'None');
84 if ($noneOptionID) {
85 unset($mailApprovalStatus[$noneOptionID]);
86 }
87
88 $this->addRadio('approval_status_id', ts('Approval Status'), $mailApprovalStatus, [], NULL, TRUE);
89
90 $buttons = [
91 [
92 'type' => 'next',
93 'name' => ts('Save'),
94 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
95 'isDefault' => TRUE,
96 ],
97 [
98 'type' => 'cancel',
99 'name' => ts('Cancel'),
100 ],
101 ];
102
103 $this->addButtons($buttons);
104
105 // add the preview elements
106 $preview = [];
107
108 $preview['subject'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing',
109 $this->_mailingID,
110 'subject'
111 );
112
113 $mailingKey = $this->_mailingID;
114 if ($hash = CRM_Mailing_BAO_Mailing::getMailingHash($mailingKey)) {
115 $mailingKey = $hash;
116 }
117
118 $preview['viewURL'] = CRM_Utils_System::url('civicrm/mailing/view', "reset=1&id={$mailingKey}");
119 $preview['type'] = $this->_mailing->body_html ? 'html' : 'text';
120 $preview['attachment'] = CRM_Core_BAO_File::attachmentInfo('civicrm_mailing', $this->_mailingID);
121
122 $this->assign_by_ref('preview', $preview);
123 }
124
125 /**
126 * Process the posted form values. Approve /reject a mailing.
127 */
128 public function postProcess() {
129 // get the submitted form values.
130 $params = $this->controller->exportValues($this->_name);
131
132 if (isset($this->_mailingID)) {
133 $params['id'] = $this->_mailingID;
134 }
135 else {
136 $params['id'] = $this->get('mailing_id');
137 }
138
139 if (!$params['id']) {
140 CRM_Core_Error::statusBounce(ts('No mailing id has been able to be determined'));
141 }
142
143 $params['approver_id'] = $this->_contactID;
144 $params['approval_date'] = date('YmdHis');
145
146 // if rejected, then we need to reset the scheduled date and scheduled id
147 $rejectOptionID = CRM_Core_PseudoConstant::getKey('CRM_Mailing_BAO_Mailing', 'approval_status_id', 'Rejected');
148 if ($rejectOptionID &&
149 $params['approval_status_id'] == $rejectOptionID
150 ) {
151 $params['scheduled_id'] = 'null';
152 $params['scheduled_date'] = 'null';
153
154 // also delete any jobs associated with this mailing
155 $job = new CRM_Mailing_BAO_MailingJob();
156 $job->mailing_id = $params['id'];
157 while ($job->fetch()) {
158 CRM_Mailing_BAO_MailingJob::del($job->id);
159 }
160 }
161 else {
162 $mailing = new CRM_Mailing_BAO_Mailing();
163 $mailing->id = $params['id'];
164 $mailing->find(TRUE);
165
166 $params['scheduled_date'] = CRM_Utils_Date::processDate($mailing->scheduled_date);
167 }
168
169 CRM_Mailing_BAO_Mailing::create($params);
170
171 //when user perform mailing from search context
172 //redirect it to search result CRM-3711
173 $ssID = $this->get('ssID');
174 if ($ssID && $this->_searchBasedMailing) {
175 if ($this->_action == CRM_Core_Action::BASIC) {
176 $fragment = 'search';
177 }
178 elseif ($this->_action == CRM_Core_Action::PROFILE) {
179 $fragment = 'search/builder';
180 }
181 elseif ($this->_action == CRM_Core_Action::ADVANCED) {
182 $fragment = 'search/advanced';
183 }
184 else {
185 $fragment = 'search/custom';
186 }
187 $context = $this->get('context');
188 if (!CRM_Contact_Form_Search::isSearchContext($context)) {
189 $context = 'search';
190 }
191 $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}";
192 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
193 if (CRM_Utils_Rule::qfKey($qfKey)) {
194 $urlParams .= "&qfKey=$qfKey";
195 }
196
197 $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
198 return $this->controller->setDestination($url);
199 }
200
201 $session = CRM_Core_Session::singleton();
202 $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled',
203 'reset=1&scheduled=true'
204 ));
205 }
206
207 /**
208 * Display Name of the form.
209 *
210 *
211 * @return string
212 */
213 public function getTitle() {
214 return ts('Approve/Reject Mailing');
215 }
216
217 }