3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2020
33 class CRM_Mailing_Form_Approve
extends CRM_Core_Form
{
35 public function redirectToListing() {
36 $url = CRM_Utils_System
::url('civicrm/mailing/browse/scheduled', 'reset=1&scheduled=true');
37 CRM_Utils_System
::redirect($url);
41 * Set variables up before form is built.
43 public function preProcess() {
44 if (CRM_Mailing_Info
::workflowEnabled()) {
45 if (!CRM_Core_Permission
::check('approve mailings') && !CRM_Core_Permission
::check('access CiviMail')) {
46 $this->redirectToListing();
50 $this->redirectToListing();
53 // when user come from search context.
54 $this->_searchBasedMailing
= CRM_Contact_Form_Search
::isSearchContext($this->get('context'));
56 //retrieve mid from different wizard and url contexts
57 $this->_mailingID
= $this->get('mailing_id');
58 $this->_approveFormOnly
= FALSE;
59 if (!$this->_mailingID
) {
60 $this->_mailingID
= CRM_Utils_Request
::retrieve('mid', 'Integer', $this, TRUE);
61 $this->_approveFormOnly
= TRUE;
64 $session = CRM_Core_Session
::singleton();
65 $this->_contactID
= $session->get('userID');
67 $this->_mailing
= new CRM_Mailing_BAO_Mailing();
68 $this->_mailing
->id
= $this->_mailingID
;
69 if (!$this->_mailing
->find(TRUE)) {
70 $this->redirectToListing();
75 * Set default values for the form.
77 public function setDefaultValues() {
79 if ($this->_mailingID
) {
80 $defaults['approval_status_id'] = $this->_mailing
->approval_status_id
;
81 $defaults['approval_note'] = $this->_mailing
->approval_note
;
88 * Build the form object for the approval/rejection mailing.
90 public function buildQuickform() {
91 $title = ts('Approve/Reject Mailing') . " - {$this->_mailing->name}";
92 CRM_Utils_System
::setTitle($title);
94 $this->addElement('textarea', 'approval_note', ts('Approve/Reject Note'));
96 $mailApprovalStatus = CRM_Core_PseudoConstant
::get('CRM_Mailing_BAO_Mailing', 'approval_status_id');
98 // eliminate the none option
99 $noneOptionID = CRM_Core_PseudoConstant
::getKey('CRM_Mailing_BAO_Mailing', 'approval_status_id', 'None');
101 unset($mailApprovalStatus[$noneOptionID]);
104 $this->addRadio('approval_status_id', ts('Approval Status'), $mailApprovalStatus, [], NULL, TRUE);
109 'name' => ts('Save'),
110 'spacing' => ' ',
115 'name' => ts('Cancel'),
119 $this->addButtons($buttons);
121 // add the preview elements
124 $preview['subject'] = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_Mailing',
129 $mailingKey = $this->_mailingID
;
130 if ($hash = CRM_Mailing_BAO_Mailing
::getMailingHash($mailingKey)) {
134 $preview['viewURL'] = CRM_Utils_System
::url('civicrm/mailing/view', "reset=1&id={$mailingKey}");
135 $preview['type'] = $this->_mailing
->body_html ?
'html' : 'text';
136 $preview['attachment'] = CRM_Core_BAO_File
::attachmentInfo('civicrm_mailing', $this->_mailingID
);
138 $this->assign_by_ref('preview', $preview);
142 * Process the posted form values. Approve /reject a mailing.
144 public function postProcess() {
145 // get the submitted form values.
146 $params = $this->controller
->exportValues($this->_name
);
149 if (isset($this->_mailingID
)) {
150 $ids['mailing_id'] = $this->_mailingID
;
153 $ids['mailing_id'] = $this->get('mailing_id');
156 if (!$ids['mailing_id']) {
157 CRM_Core_Error
::statusBounce(ts('No mailing id has been able to be determined'));
160 $params['approver_id'] = $this->_contactID
;
161 $params['approval_date'] = date('YmdHis');
163 // if rejected, then we need to reset the scheduled date and scheduled id
164 $rejectOptionID = CRM_Core_PseudoConstant
::getKey('CRM_Mailing_BAO_Mailing', 'approval_status_id', 'Rejected');
165 if ($rejectOptionID &&
166 $params['approval_status_id'] == $rejectOptionID
168 $params['scheduled_id'] = 'null';
169 $params['scheduled_date'] = 'null';
171 // also delete any jobs associated with this mailing
172 $job = new CRM_Mailing_BAO_MailingJob();
173 $job->mailing_id
= $ids['mailing_id'];
174 while ($job->fetch()) {
175 CRM_Mailing_BAO_MailingJob
::del($job->id
);
179 $mailing = new CRM_Mailing_BAO_Mailing();
180 $mailing->id
= $ids['mailing_id'];
181 $mailing->find(TRUE);
183 $params['scheduled_date'] = CRM_Utils_Date
::processDate($mailing->scheduled_date
);
186 CRM_Mailing_BAO_Mailing
::create($params, $ids);
188 //when user perform mailing from search context
189 //redirect it to search result CRM-3711
190 $ssID = $this->get('ssID');
191 if ($ssID && $this->_searchBasedMailing
) {
192 if ($this->_action
== CRM_Core_Action
::BASIC
) {
193 $fragment = 'search';
195 elseif ($this->_action
== CRM_Core_Action
::PROFILE
) {
196 $fragment = 'search/builder';
198 elseif ($this->_action
== CRM_Core_Action
::ADVANCED
) {
199 $fragment = 'search/advanced';
202 $fragment = 'search/custom';
204 $context = $this->get('context');
205 if (!CRM_Contact_Form_Search
::isSearchContext($context)) {
208 $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}";
209 $qfKey = CRM_Utils_Request
::retrieve('qfKey', 'String', $this);
210 if (CRM_Utils_Rule
::qfKey($qfKey)) {
211 $urlParams .= "&qfKey=$qfKey";
214 $url = CRM_Utils_System
::url('civicrm/contact/' . $fragment, $urlParams);
215 return $this->controller
->setDestination($url);
218 $session = CRM_Core_Session
::singleton();
219 $session->pushUserContext(CRM_Utils_System
::url('civicrm/mailing/browse/scheduled',
220 'reset=1&scheduled=true'
225 * Display Name of the form.
230 public function getTitle() {
231 return ts('Approve/Reject Mailing');