| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
| 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 |
| 31 | * @copyright CiviCRM LLC (c) 2004-2020 |
| 32 | */ |
| 33 | class 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 | /** |
| 41 | * Set variables up before form is built. |
| 42 | */ |
| 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(); |
| 47 | } |
| 48 | } |
| 49 | else { |
| 50 | $this->redirectToListing(); |
| 51 | } |
| 52 | |
| 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 | /** |
| 75 | * Set default values for the form. |
| 76 | */ |
| 77 | public function setDefaultValues() { |
| 78 | $defaults = []; |
| 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 | /** |
| 88 | * Build the form object for the approval/rejection mailing. |
| 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 | |
| 96 | $mailApprovalStatus = CRM_Core_PseudoConstant::get('CRM_Mailing_BAO_Mailing', 'approval_status_id'); |
| 97 | |
| 98 | // eliminate the none option |
| 99 | $noneOptionID = CRM_Core_PseudoConstant::getKey('CRM_Mailing_BAO_Mailing', 'approval_status_id', 'None'); |
| 100 | if ($noneOptionID) { |
| 101 | unset($mailApprovalStatus[$noneOptionID]); |
| 102 | } |
| 103 | |
| 104 | $this->addRadio('approval_status_id', ts('Approval Status'), $mailApprovalStatus, [], NULL, TRUE); |
| 105 | |
| 106 | $buttons = [ |
| 107 | [ |
| 108 | 'type' => 'next', |
| 109 | 'name' => ts('Save'), |
| 110 | 'spacing' => ' ', |
| 111 | 'isDefault' => TRUE, |
| 112 | ], |
| 113 | [ |
| 114 | 'type' => 'cancel', |
| 115 | 'name' => ts('Cancel'), |
| 116 | ], |
| 117 | ]; |
| 118 | |
| 119 | $this->addButtons($buttons); |
| 120 | |
| 121 | // add the preview elements |
| 122 | $preview = []; |
| 123 | |
| 124 | $preview['subject'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing', |
| 125 | $this->_mailingID, |
| 126 | 'subject' |
| 127 | ); |
| 128 | |
| 129 | $mailingKey = $this->_mailingID; |
| 130 | if ($hash = CRM_Mailing_BAO_Mailing::getMailingHash($mailingKey)) { |
| 131 | $mailingKey = $hash; |
| 132 | } |
| 133 | |
| 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); |
| 137 | |
| 138 | $this->assign_by_ref('preview', $preview); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Process the posted form values. Approve /reject a mailing. |
| 143 | */ |
| 144 | public function postProcess() { |
| 145 | // get the submitted form values. |
| 146 | $params = $this->controller->exportValues($this->_name); |
| 147 | |
| 148 | $ids = []; |
| 149 | if (isset($this->_mailingID)) { |
| 150 | $ids['mailing_id'] = $this->_mailingID; |
| 151 | } |
| 152 | else { |
| 153 | $ids['mailing_id'] = $this->get('mailing_id'); |
| 154 | } |
| 155 | |
| 156 | if (!$ids['mailing_id']) { |
| 157 | CRM_Core_Error::statusBounce(ts('No mailing id has been able to be determined')); |
| 158 | } |
| 159 | |
| 160 | $params['approver_id'] = $this->_contactID; |
| 161 | $params['approval_date'] = date('YmdHis'); |
| 162 | |
| 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 |
| 167 | ) { |
| 168 | $params['scheduled_id'] = 'null'; |
| 169 | $params['scheduled_date'] = 'null'; |
| 170 | |
| 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); |
| 176 | } |
| 177 | } |
| 178 | else { |
| 179 | $mailing = new CRM_Mailing_BAO_Mailing(); |
| 180 | $mailing->id = $ids['mailing_id']; |
| 181 | $mailing->find(TRUE); |
| 182 | |
| 183 | $params['scheduled_date'] = CRM_Utils_Date::processDate($mailing->scheduled_date); |
| 184 | } |
| 185 | |
| 186 | CRM_Mailing_BAO_Mailing::create($params, $ids); |
| 187 | |
| 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'; |
| 194 | } |
| 195 | elseif ($this->_action == CRM_Core_Action::PROFILE) { |
| 196 | $fragment = 'search/builder'; |
| 197 | } |
| 198 | elseif ($this->_action == CRM_Core_Action::ADVANCED) { |
| 199 | $fragment = 'search/advanced'; |
| 200 | } |
| 201 | else { |
| 202 | $fragment = 'search/custom'; |
| 203 | } |
| 204 | $context = $this->get('context'); |
| 205 | if (!CRM_Contact_Form_Search::isSearchContext($context)) { |
| 206 | $context = 'search'; |
| 207 | } |
| 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"; |
| 212 | } |
| 213 | |
| 214 | $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams); |
| 215 | return $this->controller->setDestination($url); |
| 216 | } |
| 217 | |
| 218 | $session = CRM_Core_Session::singleton(); |
| 219 | $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled', |
| 220 | 'reset=1&scheduled=true' |
| 221 | )); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Display Name of the form. |
| 226 | * |
| 227 | * |
| 228 | * @return string |
| 229 | */ |
| 230 | public function getTitle() { |
| 231 | return ts('Approve/Reject Mailing'); |
| 232 | } |
| 233 | |
| 234 | } |