Merge pull request #18591 from eileenmcnaughton/search
[civicrm-core.git] / CRM / Mailing / Form / Task.php
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_Task $form
33 *
34 * @throws \CRM_Core_Exception
35 */
36 public static function preProcessCommon(&$form) {
37 $values = $form->getSearchFormValues();
38
39 $form->_task = $values['task'] ?? NULL;
40 $mailingTasks = CRM_Mailing_Task::tasks();
41 $form->assign('taskName', CRM_Utils_Array::value('task', $values));
42
43 // ids are mailing event queue ids
44 $ids = [];
45 if ($values['radio_ts'] == 'ts_sel') {
46 foreach ($values as $name => $value) {
47 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
48 $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
49 }
50 }
51 }
52 else {
53 $queryParams = $form->get('queryParams');
54 $sortOrder = NULL;
55 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
56 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
57 }
58
59 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
60 CRM_Contact_BAO_Query::MODE_MAILING
61 );
62
63 $result = $query->searchQuery(0, 0, $sortOrder);
64 while ($result->fetch()) {
65 $ids[] = $result->mailing_recipients_id;
66 }
67 $form->assign('totalSelectedMailingRecipients', $form->get('rowCount'));
68 }
69
70 if (!empty($ids)) {
71 $form->_componentClause = ' civicrm_mailing_recipients.id IN ( ' . implode(',', $ids) . ' ) ';
72 }
73
74 // set the context for redirection for any task actions
75 $session = CRM_Core_Session::singleton();
76
77 $fragment = 'search';
78 if ($form->_action == CRM_Core_Action::ADVANCED) {
79 $fragment .= '/advanced';
80 }
81
82 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
83 $urlParams = 'force=1';
84 if (CRM_Utils_Rule::qfKey($qfKey)) {
85 $urlParams .= "&qfKey=$qfKey";
86 }
87
88 $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
89 $session = CRM_Core_Session::singleton();
90 $session->replaceUserContext($url);
91 }
92
93 /**
94 * Simple shell that derived classes can call to add buttons to
95 * the form with a customized title for the main Submit
96 *
97 * @param string $title
98 * Title of the main button.
99 * @param string $nextType
100 * @param string $backType
101 * @param bool $submitOnce
102 */
103 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
104 $this->addButtons([
105 [
106 'type' => $nextType,
107 'name' => $title,
108 'isDefault' => TRUE,
109 ],
110 [
111 'type' => $backType,
112 'name' => ts('Cancel'),
113 ],
114 ]);
115 }
116
117 }