Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
bc77d7c0 TO |
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 | +--------------------------------------------------------------------+ | |
6a488035 TO |
10 | */ |
11 | ||
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
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 | /** | |
fe482240 | 25 | * Set variables up before form is built. |
6a488035 TO |
26 | */ |
27 | public function preProcess() { | |
28 | if (CRM_Mailing_Info::workflowEnabled()) { | |
cb7b73f0 | 29 | if (!CRM_Core_Permission::check('approve mailings') && !CRM_Core_Permission::check('access CiviMail')) { |
6a488035 TO |
30 | $this->redirectToListing(); |
31 | } | |
32 | } | |
33 | else { | |
34 | $this->redirectToListing(); | |
35 | } | |
36 | ||
6a488035 TO |
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 | /** | |
c490a46a | 59 | * Set default values for the form. |
6a488035 | 60 | */ |
00be9182 | 61 | public function setDefaultValues() { |
be2fb01f | 62 | $defaults = []; |
6a488035 TO |
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 | /** | |
25606795 | 72 | * Build the form object for the approval/rejection mailing. |
6a488035 TO |
73 | */ |
74 | public function buildQuickform() { | |
75 | $title = ts('Approve/Reject Mailing') . " - {$this->_mailing->name}"; | |
94fd9d17 | 76 | $this->setTitle($title); |
6a488035 TO |
77 | |
78 | $this->addElement('textarea', 'approval_note', ts('Approve/Reject Note')); | |
79 | ||
676159c9 | 80 | $mailApprovalStatus = CRM_Core_PseudoConstant::get('CRM_Mailing_BAO_Mailing', 'approval_status_id'); |
6a488035 TO |
81 | |
82 | // eliminate the none option | |
676159c9 | 83 | $noneOptionID = CRM_Core_PseudoConstant::getKey('CRM_Mailing_BAO_Mailing', 'approval_status_id', 'None'); |
6a488035 TO |
84 | if ($noneOptionID) { |
85 | unset($mailApprovalStatus[$noneOptionID]); | |
86 | } | |
87 | ||
be2fb01f | 88 | $this->addRadio('approval_status_id', ts('Approval Status'), $mailApprovalStatus, [], NULL, TRUE); |
6a488035 | 89 | |
be2fb01f CW |
90 | $buttons = [ |
91 | [ | |
353ffa53 | 92 | 'type' => 'next', |
6a488035 TO |
93 | 'name' => ts('Save'), |
94 | 'spacing' => ' ', | |
95 | 'isDefault' => TRUE, | |
be2fb01f CW |
96 | ], |
97 | [ | |
6a488035 TO |
98 | 'type' => 'cancel', |
99 | 'name' => ts('Cancel'), | |
be2fb01f CW |
100 | ], |
101 | ]; | |
6a488035 TO |
102 | |
103 | $this->addButtons($buttons); | |
104 | ||
105 | // add the preview elements | |
be2fb01f | 106 | $preview = []; |
6a488035 TO |
107 | |
108 | $preview['subject'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing', | |
109 | $this->_mailingID, | |
110 | 'subject' | |
111 | ); | |
c57f36a1 PJ |
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}"); | |
6a488035 TO |
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. | |
6a488035 TO |
127 | */ |
128 | public function postProcess() { | |
129 | // get the submitted form values. | |
130 | $params = $this->controller->exportValues($this->_name); | |
131 | ||
be2fb01f | 132 | $ids = []; |
6a488035 TO |
133 | if (isset($this->_mailingID)) { |
134 | $ids['mailing_id'] = $this->_mailingID; | |
135 | } | |
136 | else { | |
137 | $ids['mailing_id'] = $this->get('mailing_id'); | |
138 | } | |
139 | ||
140 | if (!$ids['mailing_id']) { | |
2a7b8221 | 141 | CRM_Core_Error::statusBounce(ts('No mailing id has been able to be determined')); |
6a488035 TO |
142 | } |
143 | ||
144 | $params['approver_id'] = $this->_contactID; | |
145 | $params['approval_date'] = date('YmdHis'); | |
146 | ||
147 | // if rejected, then we need to reset the scheduled date and scheduled id | |
676159c9 | 148 | $rejectOptionID = CRM_Core_PseudoConstant::getKey('CRM_Mailing_BAO_Mailing', 'approval_status_id', 'Rejected'); |
6a488035 TO |
149 | if ($rejectOptionID && |
150 | $params['approval_status_id'] == $rejectOptionID | |
151 | ) { | |
152 | $params['scheduled_id'] = 'null'; | |
153 | $params['scheduled_date'] = 'null'; | |
154 | ||
155 | // also delete any jobs associated with this mailing | |
9da8dc8c | 156 | $job = new CRM_Mailing_BAO_MailingJob(); |
6a488035 | 157 | $job->mailing_id = $ids['mailing_id']; |
6f65a84f | 158 | while ($job->fetch()) { |
159 | CRM_Mailing_BAO_MailingJob::del($job->id); | |
160 | } | |
6a488035 | 161 | } |
8817ccc5 BS |
162 | else { |
163 | $mailing = new CRM_Mailing_BAO_Mailing(); | |
164 | $mailing->id = $ids['mailing_id']; | |
165 | $mailing->find(TRUE); | |
166 | ||
167 | $params['scheduled_date'] = CRM_Utils_Date::processDate($mailing->scheduled_date); | |
168 | } | |
6a488035 TO |
169 | |
170 | CRM_Mailing_BAO_Mailing::create($params, $ids); | |
171 | ||
6a488035 TO |
172 | //when user perform mailing from search context |
173 | //redirect it to search result CRM-3711 | |
174 | $ssID = $this->get('ssID'); | |
175 | if ($ssID && $this->_searchBasedMailing) { | |
176 | if ($this->_action == CRM_Core_Action::BASIC) { | |
177 | $fragment = 'search'; | |
178 | } | |
179 | elseif ($this->_action == CRM_Core_Action::PROFILE) { | |
180 | $fragment = 'search/builder'; | |
181 | } | |
182 | elseif ($this->_action == CRM_Core_Action::ADVANCED) { | |
183 | $fragment = 'search/advanced'; | |
184 | } | |
185 | else { | |
186 | $fragment = 'search/custom'; | |
187 | } | |
188 | $context = $this->get('context'); | |
189 | if (!CRM_Contact_Form_Search::isSearchContext($context)) { | |
190 | $context = 'search'; | |
191 | } | |
192 | $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}"; | |
193 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this); | |
194 | if (CRM_Utils_Rule::qfKey($qfKey)) { | |
195 | $urlParams .= "&qfKey=$qfKey"; | |
196 | } | |
197 | ||
198 | $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams); | |
199 | return $this->controller->setDestination($url); | |
200 | } | |
201 | ||
202 | $session = CRM_Core_Session::singleton(); | |
203 | $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled', | |
353ffa53 TO |
204 | 'reset=1&scheduled=true' |
205 | )); | |
6a488035 TO |
206 | } |
207 | ||
208 | /** | |
fe482240 | 209 | * Display Name of the form. |
6a488035 | 210 | * |
6a488035 TO |
211 | * |
212 | * @return string | |
213 | */ | |
214 | public function getTitle() { | |
215 | return ts('Approve/Reject Mailing'); | |
216 | } | |
96025800 | 217 | |
6a488035 | 218 | } |