Merge pull request #15841 from mattwire/participant_cleanup_removeparticipantfrominput
[civicrm-core.git] / CRM / Activity / Form / Task.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
31aaf096
MW
19 * Class for activity form task actions.
20 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
6a488035 21 */
31aaf096 22class CRM_Activity_Form_Task extends CRM_Core_Form_Task {
6a488035
TO
23
24 /**
fe482240 25 * The array that holds all the member ids.
6a488035
TO
26 *
27 * @var array
28 */
29 public $_activityHolderIds;
30
31 /**
fe482240 32 * Build all the data structures needed to build the form.
6a488035 33 */
00be9182 34 public function preProcess() {
6a488035
TO
35 self::preProcessCommon($this);
36 }
37
ffd93213 38 /**
b6c94f42 39 * Common pre-process function.
40 *
c490a46a 41 * @param CRM_Core_Form $form
ffd93213 42 */
2b089ce1 43 public static function preProcessCommon(&$form) {
be2fb01f 44 $form->_activityHolderIds = [];
6a488035
TO
45
46 $values = $form->controller->exportValues($form->get('searchFormName'));
47
48 $form->_task = $values['task'];
49 $activityTasks = CRM_Activity_Task::tasks();
50 $form->assign('taskName', $activityTasks[$form->_task]);
51
be2fb01f 52 $ids = [];
6a488035
TO
53 if ($values['radio_ts'] == 'ts_sel') {
54 foreach ($values as $name => $value) {
55 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
56 $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
57 }
58 }
59 }
60 else {
61 $queryParams = $form->get('queryParams');
62 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
63 CRM_Contact_BAO_Query::MODE_ACTIVITY
64 );
65 $query->_distinctComponentClause = '( civicrm_activity.id )';
66 $query->_groupByComponentClause = " GROUP BY civicrm_activity.id ";
87767abb 67
68 // CRM-12675
69 $activityClause = NULL;
9d6dcd43 70
2f9320ea 71 $components = CRM_Core_Component::getNames();
be2fb01f 72 $componentClause = [];
f121d107 73 foreach ($components as $componentID => $componentName) {
9aa863df 74 if ($componentName != 'CiviCase' && !CRM_Core_Permission::check("access $componentName")) {
4f490fbf 75 $componentClause[] = " (activity_type.component_id IS NULL OR activity_type.component_id <> {$componentID}) ";
2f9320ea 76 }
77 }
78 if (!empty($componentClause)) {
79 $activityClause = implode(' AND ', $componentClause);
87767abb 80 }
87767abb 81 $result = $query->searchQuery(0, 0, NULL, FALSE, FALSE, FALSE, FALSE, FALSE, $activityClause);
6a488035
TO
82
83 while ($result->fetch()) {
22e263ad 84 if (!empty($result->activity_id)) {
32864ccf
TO
85 $ids[] = $result->activity_id;
86 }
6a488035
TO
87 }
88 }
6a488035
TO
89
90 if (!empty($ids)) {
91 $form->_componentClause = ' civicrm_activity.id IN ( ' . implode(',', $ids) . ' ) ';
92 $form->assign('totalSelectedActivities', count($ids));
93 }
94
95 $form->_activityHolderIds = $form->_componentIds = $ids;
96
7808aae6 97 // Set the context for redirection for any task actions.
6a488035
TO
98 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
99 $urlParams = 'force=1';
100 if (CRM_Utils_Rule::qfKey($qfKey)) {
101 $urlParams .= "&qfKey=$qfKey";
102 }
103
104 $session = CRM_Core_Session::singleton();
105 $searchFormName = strtolower($form->get('searchFormName'));
106 if ($searchFormName == 'search') {
107 $session->replaceUserContext(CRM_Utils_System::url('civicrm/activity/search', $urlParams));
108 }
109 else {
110 $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
353ffa53
TO
111 $urlParams
112 ));
6a488035
TO
113 }
114 }
115
116 /**
117 * Given the membership id, compute the contact id
7808aae6 118 * since it's used for things like send email.
6a488035
TO
119 */
120 public function setContactIDs() {
121 $IDs = implode(',', $this->_activityHolderIds);
1adb1bc9 122
44f817d4 123 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
1adb1bc9 124 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
6a488035 125 $query = "
1adb1bc9 126SELECT contact_id
127FROM civicrm_activity_contact
128WHERE activity_id IN ( $IDs ) AND
129 record_type_id = {$sourceID}";
130
6a488035
TO
131 $dao = CRM_Core_DAO::executeQuery($query);
132 while ($dao->fetch()) {
1adb1bc9 133 $contactIDs[] = $dao->contact_id;
6a488035
TO
134 }
135 $this->_contactIds = $contactIDs;
136 }
137
138 /**
100fef9d 139 * Simple shell that derived classes can call to add buttons to
6a488035
TO
140 * the form with a customized title for the main Submit
141 *
041ab3d1
TO
142 * @param string $title
143 * Title of the main button.
144 * @param string $nextType
145 * Button type for the form after processing.
fd31fa4c
EM
146 * @param string $backType
147 * @param bool $submitOnce
6a488035 148 */
00be9182 149 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
be2fb01f
CW
150 $this->addButtons([
151 [
c5c263ca
AH
152 'type' => $nextType,
153 'name' => $title,
154 'isDefault' => TRUE,
be2fb01f
CW
155 ],
156 [
c5c263ca
AH
157 'type' => $backType,
158 'name' => ts('Cancel'),
be2fb01f
CW
159 ],
160 ]);
6a488035 161 }
96025800 162
6a488035 163}