3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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_OptionGroup
::values('mail_approval_status');
98 // eliminate the none option
99 $noneOptionID = CRM_Core_OptionGroup
::getValue('mail_approval_status',
104 unset($mailApprovalStatus[$noneOptionID]);
107 $this->addRadio('approval_status_id', ts('Approval Status'), $mailApprovalStatus, array(), NULL, TRUE);
112 'name' => ts('Save'),
113 'spacing' => ' ',
118 'name' => ts('Cancel'),
122 $this->addButtons($buttons);
124 // add the preview elements
127 $preview['subject'] = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_Mailing',
132 $mailingKey = $this->_mailingID
;
133 if ($hash = CRM_Mailing_BAO_Mailing
::getMailingHash($mailingKey)) {
137 $preview['viewURL'] = CRM_Utils_System
::url('civicrm/mailing/view', "reset=1&id={$mailingKey}");
138 $preview['type'] = $this->_mailing
->body_html ?
'html' : 'text';
139 $preview['attachment'] = CRM_Core_BAO_File
::attachmentInfo('civicrm_mailing', $this->_mailingID
);
141 $this->assign_by_ref('preview', $preview);
145 * Process the posted form values. Approve /reject a mailing.
147 public function postProcess() {
148 // get the submitted form values.
149 $params = $this->controller
->exportValues($this->_name
);
152 if (isset($this->_mailingID
)) {
153 $ids['mailing_id'] = $this->_mailingID
;
156 $ids['mailing_id'] = $this->get('mailing_id');
159 if (!$ids['mailing_id']) {
160 CRM_Core_Error
::fatal();
163 $params['approver_id'] = $this->_contactID
;
164 $params['approval_date'] = date('YmdHis');
166 // if rejected, then we need to reset the scheduled date and scheduled id
167 $rejectOptionID = CRM_Core_OptionGroup
::getValue('mail_approval_status',
171 if ($rejectOptionID &&
172 $params['approval_status_id'] == $rejectOptionID
174 $params['scheduled_id'] = 'null';
175 $params['scheduled_date'] = 'null';
177 // also delete any jobs associated with this mailing
178 $job = new CRM_Mailing_BAO_MailingJob();
179 $job->mailing_id
= $ids['mailing_id'];
183 $mailing = new CRM_Mailing_BAO_Mailing();
184 $mailing->id
= $ids['mailing_id'];
185 $mailing->find(TRUE);
187 $params['scheduled_date'] = CRM_Utils_Date
::processDate($mailing->scheduled_date
);
190 CRM_Mailing_BAO_Mailing
::create($params, $ids);
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';
199 elseif ($this->_action
== CRM_Core_Action
::PROFILE
) {
200 $fragment = 'search/builder';
202 elseif ($this->_action
== CRM_Core_Action
::ADVANCED
) {
203 $fragment = 'search/advanced';
206 $fragment = 'search/custom';
208 $context = $this->get('context');
209 if (!CRM_Contact_Form_Search
::isSearchContext($context)) {
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";
218 $url = CRM_Utils_System
::url('civicrm/contact/' . $fragment, $urlParams);
219 return $this->controller
->setDestination($url);
222 $session = CRM_Core_Session
::singleton();
223 $session->pushUserContext(CRM_Utils_System
::url('civicrm/mailing/browse/scheduled',
224 'reset=1&scheduled=true'
229 * Display Name of the form.
234 public function getTitle() {
235 return ts('Approve/Reject Mailing');