| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 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 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @package CRM |
| 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 16 | */ |
| 17 | class CRM_Mailing_Form_Approve extends CRM_Core_Form { |
| 18 | |
| 19 | public function redirectToListing() { |
| 20 | $url = CRM_Utils_System::url('civicrm/mailing/browse/scheduled', 'reset=1&scheduled=true'); |
| 21 | CRM_Utils_System::redirect($url); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Set variables up before form is built. |
| 26 | */ |
| 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(); |
| 31 | } |
| 32 | } |
| 33 | else { |
| 34 | $this->redirectToListing(); |
| 35 | } |
| 36 | |
| 37 | // when user come from search context. |
| 38 | $this->_searchBasedMailing = CRM_Contact_Form_Search::isSearchContext($this->get('context')); |
| 39 | |
| 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; |
| 46 | } |
| 47 | |
| 48 | $session = CRM_Core_Session::singleton(); |
| 49 | $this->_contactID = $session->get('userID'); |
| 50 | |
| 51 | $this->_mailing = new CRM_Mailing_BAO_Mailing(); |
| 52 | $this->_mailing->id = $this->_mailingID; |
| 53 | if (!$this->_mailing->find(TRUE)) { |
| 54 | $this->redirectToListing(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set default values for the form. |
| 60 | */ |
| 61 | public function setDefaultValues() { |
| 62 | $defaults = []; |
| 63 | if ($this->_mailingID) { |
| 64 | $defaults['approval_status_id'] = $this->_mailing->approval_status_id; |
| 65 | $defaults['approval_note'] = $this->_mailing->approval_note; |
| 66 | } |
| 67 | |
| 68 | return $defaults; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Build the form object for the approval/rejection mailing. |
| 73 | */ |
| 74 | public function buildQuickform() { |
| 75 | $title = ts('Approve/Reject Mailing') . " - {$this->_mailing->name}"; |
| 76 | $this->setTitle($title); |
| 77 | |
| 78 | $this->addElement('textarea', 'approval_note', ts('Approve/Reject Note')); |
| 79 | |
| 80 | $mailApprovalStatus = CRM_Core_PseudoConstant::get('CRM_Mailing_BAO_Mailing', 'approval_status_id'); |
| 81 | |
| 82 | // eliminate the none option |
| 83 | $noneOptionID = CRM_Core_PseudoConstant::getKey('CRM_Mailing_BAO_Mailing', 'approval_status_id', 'None'); |
| 84 | if ($noneOptionID) { |
| 85 | unset($mailApprovalStatus[$noneOptionID]); |
| 86 | } |
| 87 | |
| 88 | $this->addRadio('approval_status_id', ts('Approval Status'), $mailApprovalStatus, [], NULL, TRUE); |
| 89 | |
| 90 | $buttons = [ |
| 91 | [ |
| 92 | 'type' => 'next', |
| 93 | 'name' => ts('Save'), |
| 94 | 'spacing' => ' ', |
| 95 | 'isDefault' => TRUE, |
| 96 | ], |
| 97 | [ |
| 98 | 'type' => 'cancel', |
| 99 | 'name' => ts('Cancel'), |
| 100 | ], |
| 101 | ]; |
| 102 | |
| 103 | $this->addButtons($buttons); |
| 104 | |
| 105 | // add the preview elements |
| 106 | $preview = []; |
| 107 | |
| 108 | $preview['subject'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing', |
| 109 | $this->_mailingID, |
| 110 | 'subject' |
| 111 | ); |
| 112 | |
| 113 | $mailingKey = $this->_mailingID; |
| 114 | if ($hash = CRM_Mailing_BAO_Mailing::getMailingHash($mailingKey)) { |
| 115 | $mailingKey = $hash; |
| 116 | } |
| 117 | |
| 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); |
| 121 | |
| 122 | $this->assign_by_ref('preview', $preview); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Process the posted form values. Approve /reject a mailing. |
| 127 | */ |
| 128 | public function postProcess() { |
| 129 | // get the submitted form values. |
| 130 | $params = $this->controller->exportValues($this->_name); |
| 131 | |
| 132 | $ids = []; |
| 133 | if (isset($this->_mailingID)) { |
| 134 | $ids['mailing_id'] = $this->_mailingID; |
| 135 | } |
| 136 | else { |
| 137 | $ids['mailing_id'] = $this->get('mailing_id'); |
| 138 | } |
| 139 | |
| 140 | if (!$ids['mailing_id']) { |
| 141 | CRM_Core_Error::statusBounce(ts('No mailing id has been able to be determined')); |
| 142 | } |
| 143 | |
| 144 | $params['approver_id'] = $this->_contactID; |
| 145 | $params['approval_date'] = date('YmdHis'); |
| 146 | |
| 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 |
| 151 | ) { |
| 152 | $params['scheduled_id'] = 'null'; |
| 153 | $params['scheduled_date'] = 'null'; |
| 154 | |
| 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); |
| 160 | } |
| 161 | } |
| 162 | else { |
| 163 | $mailing = new CRM_Mailing_BAO_Mailing(); |
| 164 | $mailing->id = $ids['mailing_id']; |
| 165 | $mailing->find(TRUE); |
| 166 | |
| 167 | $params['scheduled_date'] = CRM_Utils_Date::processDate($mailing->scheduled_date); |
| 168 | } |
| 169 | |
| 170 | CRM_Mailing_BAO_Mailing::create($params); |
| 171 | |
| 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'; |
| 178 | } |
| 179 | elseif ($this->_action == CRM_Core_Action::PROFILE) { |
| 180 | $fragment = 'search/builder'; |
| 181 | } |
| 182 | elseif ($this->_action == CRM_Core_Action::ADVANCED) { |
| 183 | $fragment = 'search/advanced'; |
| 184 | } |
| 185 | else { |
| 186 | $fragment = 'search/custom'; |
| 187 | } |
| 188 | $context = $this->get('context'); |
| 189 | if (!CRM_Contact_Form_Search::isSearchContext($context)) { |
| 190 | $context = 'search'; |
| 191 | } |
| 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"; |
| 196 | } |
| 197 | |
| 198 | $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams); |
| 199 | return $this->controller->setDestination($url); |
| 200 | } |
| 201 | |
| 202 | $session = CRM_Core_Session::singleton(); |
| 203 | $session->pushUserContext(CRM_Utils_System::url('civicrm/mailing/browse/scheduled', |
| 204 | 'reset=1&scheduled=true' |
| 205 | )); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Display Name of the form. |
| 210 | * |
| 211 | * |
| 212 | * @return string |
| 213 | */ |
| 214 | public function getTitle() { |
| 215 | return ts('Approve/Reject Mailing'); |
| 216 | } |
| 217 | |
| 218 | } |