Merge pull request #15971 from aydun/report#23
[civicrm-core.git] / CRM / Activity / 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 activity form task actions.
20 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
21 */
22 class CRM_Activity_Form_Task extends CRM_Core_Form_Task {
23
24 /**
25 * The array that holds all the member ids.
26 *
27 * @var array
28 */
29 public $_activityHolderIds;
30
31 /**
32 * Build all the data structures needed to build the form.
33 */
34 public function preProcess() {
35 self::preProcessCommon($this);
36 }
37
38 /**
39 * Common pre-process function.
40 *
41 * @param CRM_Core_Form $form
42 */
43 public static function preProcessCommon(&$form) {
44 $form->_activityHolderIds = [];
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
52 $ids = [];
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 ";
67
68 // CRM-12675
69 $activityClause = NULL;
70
71 $components = CRM_Core_Component::getNames();
72 $componentClause = [];
73 foreach ($components as $componentID => $componentName) {
74 if ($componentName != 'CiviCase' && !CRM_Core_Permission::check("access $componentName")) {
75 $componentClause[] = " (activity_type.component_id IS NULL OR activity_type.component_id <> {$componentID}) ";
76 }
77 }
78 if (!empty($componentClause)) {
79 $activityClause = implode(' AND ', $componentClause);
80 }
81 $result = $query->searchQuery(0, 0, NULL, FALSE, FALSE, FALSE, FALSE, FALSE, $activityClause);
82
83 while ($result->fetch()) {
84 if (!empty($result->activity_id)) {
85 $ids[] = $result->activity_id;
86 }
87 }
88 }
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
97 // Set the context for redirection for any task actions.
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",
111 $urlParams
112 ));
113 }
114 }
115
116 /**
117 * Given the membership id, compute the contact id
118 * since it's used for things like send email.
119 */
120 public function setContactIDs() {
121 $IDs = implode(',', $this->_activityHolderIds);
122
123 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
124 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
125 $query = "
126 SELECT contact_id
127 FROM civicrm_activity_contact
128 WHERE activity_id IN ( $IDs ) AND
129 record_type_id = {$sourceID}";
130
131 $dao = CRM_Core_DAO::executeQuery($query);
132 while ($dao->fetch()) {
133 $contactIDs[] = $dao->contact_id;
134 }
135 $this->_contactIds = $contactIDs;
136 }
137
138 /**
139 * Simple shell that derived classes can call to add buttons to
140 * the form with a customized title for the main Submit
141 *
142 * @param string $title
143 * Title of the main button.
144 * @param string $nextType
145 * Button type for the form after processing.
146 * @param string $backType
147 * @param bool $submitOnce
148 */
149 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
150 $this->addButtons([
151 [
152 'type' => $nextType,
153 'name' => $title,
154 'isDefault' => TRUE,
155 ],
156 [
157 'type' => $backType,
158 'name' => ts('Cancel'),
159 ],
160 ]);
161 }
162
163 }