3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Mailing_Form_Approve
extends CRM_Core_Form
{
19 public function redirectToListing() {
20 $url = CRM_Utils_System
::url('civicrm/mailing/browse/scheduled', 'reset=1&scheduled=true');
21 CRM_Utils_System
::redirect($url);
25 * Set variables up before form is built.
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();
34 $this->redirectToListing();
37 // when user come from search context.
38 $this->_searchBasedMailing
= CRM_Contact_Form_Search
::isSearchContext($this->get('context'));
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;
48 $session = CRM_Core_Session
::singleton();
49 $this->_contactID
= $session->get('userID');
51 $this->_mailing
= new CRM_Mailing_BAO_Mailing();
52 $this->_mailing
->id
= $this->_mailingID
;
53 if (!$this->_mailing
->find(TRUE)) {
54 $this->redirectToListing();
59 * Set default values for the form.
61 public function setDefaultValues() {
63 if ($this->_mailingID
) {
64 $defaults['approval_status_id'] = $this->_mailing
->approval_status_id
;
65 $defaults['approval_note'] = $this->_mailing
->approval_note
;
72 * Build the form object for the approval/rejection mailing.
74 public function buildQuickform() {
75 $title = ts('Approve/Reject Mailing') . " - {$this->_mailing->name}";
76 $this->setTitle($title);
78 $this->addElement('textarea', 'approval_note', ts('Approve/Reject Note'));
80 $mailApprovalStatus = CRM_Core_PseudoConstant
::get('CRM_Mailing_BAO_Mailing', 'approval_status_id');
82 // eliminate the none option
83 $noneOptionID = CRM_Core_PseudoConstant
::getKey('CRM_Mailing_BAO_Mailing', 'approval_status_id', 'None');
85 unset($mailApprovalStatus[$noneOptionID]);
88 $this->addRadio('approval_status_id', ts('Approval Status'), $mailApprovalStatus, [], NULL, TRUE);
94 'spacing' => ' ',
99 'name' => ts('Cancel'),
103 $this->addButtons($buttons);
105 // add the preview elements
108 $preview['subject'] = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_Mailing',
113 $mailingKey = $this->_mailingID
;
114 if ($hash = CRM_Mailing_BAO_Mailing
::getMailingHash($mailingKey)) {
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
);
122 $this->assign_by_ref('preview', $preview);
126 * Process the posted form values. Approve /reject a mailing.
128 public function postProcess() {
129 // get the submitted form values.
130 $params = $this->controller
->exportValues($this->_name
);
133 if (isset($this->_mailingID
)) {
134 $ids['mailing_id'] = $this->_mailingID
;
137 $ids['mailing_id'] = $this->get('mailing_id');
140 if (!$ids['mailing_id']) {
141 CRM_Core_Error
::statusBounce(ts('No mailing id has been able to be determined'));
144 $params['approver_id'] = $this->_contactID
;
145 $params['approval_date'] = date('YmdHis');
147 // if rejected, then we need to reset the scheduled date and scheduled id
148 $rejectOptionID = CRM_Core_PseudoConstant
::getKey('CRM_Mailing_BAO_Mailing', 'approval_status_id', 'Rejected');
149 if ($rejectOptionID &&
150 $params['approval_status_id'] == $rejectOptionID
152 $params['scheduled_id'] = 'null';
153 $params['scheduled_date'] = 'null';
155 // also delete any jobs associated with this mailing
156 $job = new CRM_Mailing_BAO_MailingJob();
157 $job->mailing_id
= $ids['mailing_id'];
158 while ($job->fetch()) {
159 CRM_Mailing_BAO_MailingJob
::del($job->id
);
163 $mailing = new CRM_Mailing_BAO_Mailing();
164 $mailing->id
= $ids['mailing_id'];
165 $mailing->find(TRUE);
167 $params['scheduled_date'] = CRM_Utils_Date
::processDate($mailing->scheduled_date
);
170 CRM_Mailing_BAO_Mailing
::create($params, $ids);
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';
179 elseif ($this->_action
== CRM_Core_Action
::PROFILE
) {
180 $fragment = 'search/builder';
182 elseif ($this->_action
== CRM_Core_Action
::ADVANCED
) {
183 $fragment = 'search/advanced';
186 $fragment = 'search/custom';
188 $context = $this->get('context');
189 if (!CRM_Contact_Form_Search
::isSearchContext($context)) {
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";
198 $url = CRM_Utils_System
::url('civicrm/contact/' . $fragment, $urlParams);
199 return $this->controller
->setDestination($url);
202 $session = CRM_Core_Session
::singleton();
203 $session->pushUserContext(CRM_Utils_System
::url('civicrm/mailing/browse/scheduled',
204 'reset=1&scheduled=true'
209 * Display Name of the form.
214 public function getTitle() {
215 return ts('Approve/Reject Mailing');