Merge pull request #18567 from eileenmcnaughton/apiv3activity
[civicrm-core.git] / CRM / Event / 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 event form task actions.
20 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
21 */
22 class CRM_Event_Form_Task extends CRM_Core_Form_Task {
23
24 /**
25 * The array that holds all the participant ids.
26 *
27 * @var array
28 */
29 protected $_participantIds;
30
31 /**
32 * Build all the data structures needed to build the form.
33 *
34 * @param
35 *
36 * @return void
37 */
38 public function preProcess() {
39 self::preProcessCommon($this);
40 }
41
42 /**
43 * @param CRM_Core_Form_Task $form
44 */
45 public static function preProcessCommon(&$form) {
46 $form->_participantIds = [];
47
48 $values = $form->getSearchFormValues();
49
50 $form->_task = $values['task'];
51 $tasks = CRM_Event_Task::permissionedTaskTitles(CRM_Core_Permission::getPermission());
52 if (!array_key_exists($form->_task, $tasks)) {
53 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
54 }
55 $form->assign('taskName', $tasks[$form->_task]);
56
57 $ids = [];
58 if ($values['radio_ts'] == 'ts_sel') {
59 foreach ($values as $name => $value) {
60 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
61 $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
62 }
63 }
64 }
65 else {
66 $queryParams = $form->get('queryParams');
67 $sortOrder = NULL;
68 if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
69 $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
70 }
71
72 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
73 CRM_Contact_BAO_Query::MODE_EVENT
74 );
75 $query->_distinctComponentClause = "civicrm_participant.id";
76 $query->_groupByComponentClause = " GROUP BY civicrm_participant.id ";
77 $result = $query->searchQuery(0, 0, $sortOrder);
78 while ($result->fetch()) {
79 $ids[] = $result->participant_id;
80 }
81 }
82
83 if (!empty($ids)) {
84 $form->_componentClause = ' civicrm_participant.id IN ( ' . implode(',', $ids) . ' ) ';
85 $form->assign('totalSelectedParticipants', count($ids));
86 }
87
88 $form->_participantIds = $form->_componentIds = $ids;
89
90 //set the context for redirection for any task actions
91 $session = CRM_Core_Session::singleton();
92
93 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
94 $urlParams = 'force=1';
95 if (CRM_Utils_Rule::qfKey($qfKey)) {
96 $urlParams .= "&qfKey=$qfKey";
97 }
98
99 $searchFormName = strtolower($form->get('searchFormName'));
100 if ($searchFormName == 'search') {
101 $session->replaceUserContext(CRM_Utils_System::url('civicrm/event/search', $urlParams));
102 }
103 else {
104 $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
105 $urlParams
106 ));
107 }
108 }
109
110 /**
111 * Given the participant id, compute the contact id
112 * since its used for things like send email
113 */
114 public function setContactIDs() {
115 $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_participantIds,
116 'civicrm_participant'
117 );
118 }
119
120 /**
121 * Simple shell that derived classes can call to add buttons to.
122 * the form with a customized title for the main Submit
123 *
124 * @param string $title
125 * Title of the main button.
126 * @param string $nextType
127 * @param string $backType
128 * @param bool $submitOnce
129 *
130 * @return void
131 */
132 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
133 $this->addButtons([
134 [
135 'type' => $nextType,
136 'name' => $title,
137 'isDefault' => TRUE,
138 ],
139 [
140 'type' => $backType,
141 'name' => ts('Cancel'),
142 ],
143 ]);
144 }
145
146 }