| 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 | |
| 18 | /** |
| 19 | * Class for mailing form task actions. |
| 20 | * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions. |
| 21 | */ |
| 22 | class CRM_Mailing_Form_Task extends CRM_Core_Form_Task { |
| 23 | |
| 24 | /** |
| 25 | * Build all the data structures needed to build the form. |
| 26 | */ |
| 27 | public function preProcess() { |
| 28 | self::preProcessCommon($this); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @param CRM_Core_Form $form |
| 33 | */ |
| 34 | public static function preProcessCommon(&$form) { |
| 35 | $values = $form->controller->exportValues($form->get('searchFormName')); |
| 36 | |
| 37 | $form->_task = $values['task'] ?? NULL; |
| 38 | $mailingTasks = CRM_Mailing_Task::tasks(); |
| 39 | $form->assign('taskName', CRM_Utils_Array::value('task', $values)); |
| 40 | |
| 41 | // ids are mailing event queue ids |
| 42 | $ids = []; |
| 43 | if ($values['radio_ts'] == 'ts_sel') { |
| 44 | foreach ($values as $name => $value) { |
| 45 | if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) { |
| 46 | $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | else { |
| 51 | $queryParams = $form->get('queryParams'); |
| 52 | $sortOrder = NULL; |
| 53 | if ($form->get(CRM_Utils_Sort::SORT_ORDER)) { |
| 54 | $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER); |
| 55 | } |
| 56 | |
| 57 | $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE, |
| 58 | CRM_Contact_BAO_Query::MODE_MAILING |
| 59 | ); |
| 60 | |
| 61 | $result = $query->searchQuery(0, 0, $sortOrder); |
| 62 | while ($result->fetch()) { |
| 63 | $ids[] = $result->mailing_recipients_id; |
| 64 | } |
| 65 | $form->assign('totalSelectedMailingRecipients', $form->get('rowCount')); |
| 66 | } |
| 67 | |
| 68 | if (!empty($ids)) { |
| 69 | $form->_componentClause = ' civicrm_mailing_recipients.id IN ( ' . implode(',', $ids) . ' ) '; |
| 70 | } |
| 71 | |
| 72 | // set the context for redirection for any task actions |
| 73 | $session = CRM_Core_Session::singleton(); |
| 74 | |
| 75 | $fragment = 'search'; |
| 76 | if ($form->_action == CRM_Core_Action::ADVANCED) { |
| 77 | $fragment .= '/advanced'; |
| 78 | } |
| 79 | |
| 80 | $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form); |
| 81 | $urlParams = 'force=1'; |
| 82 | if (CRM_Utils_Rule::qfKey($qfKey)) { |
| 83 | $urlParams .= "&qfKey=$qfKey"; |
| 84 | } |
| 85 | |
| 86 | $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams); |
| 87 | $session = CRM_Core_Session::singleton(); |
| 88 | $session->replaceUserContext($url); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Simple shell that derived classes can call to add buttons to |
| 93 | * the form with a customized title for the main Submit |
| 94 | * |
| 95 | * @param string $title |
| 96 | * Title of the main button. |
| 97 | * @param string $nextType |
| 98 | * @param string $backType |
| 99 | * @param bool $submitOnce |
| 100 | */ |
| 101 | public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) { |
| 102 | $this->addButtons([ |
| 103 | [ |
| 104 | 'type' => $nextType, |
| 105 | 'name' => $title, |
| 106 | 'isDefault' => TRUE, |
| 107 | ], |
| 108 | [ |
| 109 | 'type' => $backType, |
| 110 | 'name' => ts('Cancel'), |
| 111 | ], |
| 112 | ]); |
| 113 | } |
| 114 | |
| 115 | } |