include title
[civicrm-core.git] / CRM / Mailing / Form / Approve.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
26 */
27
28/**
29 *
30 * @package CRM
0f03f337 31 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
32 */
33class CRM_Mailing_Form_Approve extends CRM_Core_Form {
34
35 public function redirectToListing() {
36 $url = CRM_Utils_System::url('civicrm/mailing/browse/scheduled', 'reset=1&scheduled=true');
37 CRM_Utils_System::redirect($url);
38 }
39
40 /**
fe482240 41 * Set variables up before form is built.
6a488035
TO
42 */
43 public function preProcess() {
44 if (CRM_Mailing_Info::workflowEnabled()) {
cb7b73f0 45 if (!CRM_Core_Permission::check('approve mailings') && !CRM_Core_Permission::check('access CiviMail')) {
6a488035
TO
46 $this->redirectToListing();
47 }
48 }
49 else {
50 $this->redirectToListing();
51 }
52
6a488035
TO
53 // when user come from search context.
54 $this->_searchBasedMailing = CRM_Contact_Form_Search::isSearchContext($this->get('context'));
55
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;
62 }
63
64 $session = CRM_Core_Session::singleton();
65 $this->_contactID = $session->get('userID');
66
67 $this->_mailing = new CRM_Mailing_BAO_Mailing();
68 $this->_mailing->id = $this->_mailingID;
69 if (!$this->_mailing->find(TRUE)) {
70 $this->redirectToListing();
71 }
72 }
73
74 /**
c490a46a 75 * Set default values for the form.
6a488035 76 */
00be9182 77 public function setDefaultValues() {
6a488035
TO
78 $defaults = array();
79 if ($this->_mailingID) {
80 $defaults['approval_status_id'] = $this->_mailing->approval_status_id;
81 $defaults['approval_note'] = $this->_mailing->approval_note;
82 }
83
84 return $defaults;
85 }
86
87 /**
25606795 88 * Build the form object for the approval/rejection mailing.
6a488035
TO
89 */
90 public function buildQuickform() {
91 $title = ts('Approve/Reject Mailing') . " - {$this->_mailing->name}";
92 CRM_Utils_System::setTitle($title);
93
94 $this->addElement('textarea', 'approval_note', ts('Approve/Reject Note'));
95
6ce01b02 96 $mailApprovalStatus = CRM_Core_OptionGroup::values('mail_approval_status');
6a488035
TO
97
98 // eliminate the none option
99 $noneOptionID = CRM_Core_OptionGroup::getValue('mail_approval_status',
100 'None',
101 'name'
102 );
103 if ($noneOptionID) {
104 unset($mailApprovalStatus[$noneOptionID]);
105 }
106
385f11fd 107 $this->addRadio('approval_status_id', ts('Approval Status'), $mailApprovalStatus, array(), NULL, TRUE);
6a488035
TO
108
109 $buttons = array(
35f7561f 110 array(
353ffa53 111 'type' => 'next',
6a488035
TO
112 'name' => ts('Save'),
113 'spacing' => '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;',
114 'isDefault' => TRUE,
115 ),
116 array(
117 'type' => 'cancel',
118 'name' => ts('Cancel'),
119 ),
120 );
121
122 $this->addButtons($buttons);
123
124 // add the preview elements
125 $preview = array();
126
127 $preview['subject'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing',
128 $this->_mailingID,
129 'subject'
130 );
c57f36a1
PJ
131
132 $mailingKey = $this->_mailingID;
133 if ($hash = CRM_Mailing_BAO_Mailing::getMailingHash($mailingKey)) {
134 $mailingKey = $hash;
135 }
136
137 $preview['viewURL'] = CRM_Utils_System::url('civicrm/mailing/view', "reset=1&id={$mailingKey}");
6a488035
TO
138 $preview['type'] = $this->_mailing->body_html ? 'html' : 'text';
139 $preview['attachment'] = CRM_Core_BAO_File::attachmentInfo('civicrm_mailing', $this->_mailingID);
140
141 $this->assign_by_ref('preview', $preview);
142 }
143
144 /**
145 * Process the posted form values. Approve /reject a mailing.
6a488035
TO
146 */
147 public function postProcess() {
148 // get the submitted form values.
149 $params = $this->controller->exportValues($this->_name);
150
151 $ids = array();
152 if (isset($this->_mailingID)) {
153 $ids['mailing_id'] = $this->_mailingID;
154 }
155 else {
156 $ids['mailing_id'] = $this->get('mailing_id');
157 }
158
159 if (!$ids['mailing_id']) {
160 CRM_Core_Error::fatal();
161 }
162
163 $params['approver_id'] = $this->_contactID;
164 $params['approval_date'] = date('YmdHis');
165
166 // if rejected, then we need to reset the scheduled date and scheduled id
167 $rejectOptionID = CRM_Core_OptionGroup::getValue('mail_approval_status',
168 'Rejected',
169 'name'
170 );
171 if ($rejectOptionID &&
172 $params['approval_status_id'] == $rejectOptionID
173 ) {
174 $params['scheduled_id'] = 'null';
175 $params['scheduled_date'] = 'null';
176
177 // also delete any jobs associated with this mailing
9da8dc8c 178 $job = new CRM_Mailing_BAO_MailingJob();
6a488035
TO
179 $job->mailing_id = $ids['mailing_id'];
180 $job->delete();
181 }
8817ccc5
BS
182 else {
183 $mailing = new CRM_Mailing_BAO_Mailing();
184 $mailing->id = $ids['mailing_id'];
185 $mailing->find(TRUE);
186
187 $params['scheduled_date'] = CRM_Utils_Date::processDate($mailing->scheduled_date);
188 }
6a488035
TO
189
190 CRM_Mailing_BAO_Mailing::create($params, $ids);
191
6a488035
TO
192 //when user perform mailing from search context
193 //redirect it to search result CRM-3711
194 $ssID = $this->get('ssID');
195 if ($ssID && $this->_searchBasedMailing) {
196 if ($this->_action == CRM_Core_Action::BASIC) {
197 $fragment = 'search';
198 }
199 elseif ($this->_action == CRM_Core_Action::PROFILE) {
200 $fragment = 'search/builder';
201 }
202 elseif ($this->_action == CRM_Core_Action::ADVANCED) {
203 $fragment = 'search/advanced';
204 }
205 else {
206 $fragment = 'search/custom';
207 }
208 $context = $this->get('context');
209 if (!CRM_Contact_Form_Search::isSearchContext($context)) {
210 $context = 'search';
211 }
212 $urlParams = "force=1&reset=1&ssID={$ssID}&context={$context}";
213 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
214 if (CRM_Utils_Rule::qfKey($qfKey)) {
215 $urlParams .= "&qfKey=$qfKey";
216 }
217
218 $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
219 return $this->controller->setDestination($url);
220 }
221
222 $session = CRM_Core_Session::singleton();
223 $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled',
353ffa53
TO
224 'reset=1&scheduled=true'
225 ));
6a488035
TO
226 }
227
228 /**
fe482240 229 * Display Name of the form.
6a488035 230 *
6a488035
TO
231 *
232 * @return string
233 */
234 public function getTitle() {
235 return ts('Approve/Reject Mailing');
236 }
96025800 237
6a488035 238}