Merge pull request #15641 from eileenmcnaughton/cont_clean
[civicrm-core.git] / CRM / Activity / Form / Task.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * Class for activity form task actions.
36 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
37 */
38 class CRM_Activity_Form_Task extends CRM_Core_Form_Task {
39
40 /**
41 * The array that holds all the member ids.
42 *
43 * @var array
44 */
45 public $_activityHolderIds;
46
47 /**
48 * Build all the data structures needed to build the form.
49 */
50 public function preProcess() {
51 self::preProcessCommon($this);
52 }
53
54 /**
55 * Common pre-process function.
56 *
57 * @param CRM_Core_Form $form
58 */
59 public static function preProcessCommon(&$form) {
60 $form->_activityHolderIds = [];
61
62 $values = $form->controller->exportValues($form->get('searchFormName'));
63
64 $form->_task = $values['task'];
65 $activityTasks = CRM_Activity_Task::tasks();
66 $form->assign('taskName', $activityTasks[$form->_task]);
67
68 $ids = [];
69 if ($values['radio_ts'] == 'ts_sel') {
70 foreach ($values as $name => $value) {
71 if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
72 $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
73 }
74 }
75 }
76 else {
77 $queryParams = $form->get('queryParams');
78 $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
79 CRM_Contact_BAO_Query::MODE_ACTIVITY
80 );
81 $query->_distinctComponentClause = '( civicrm_activity.id )';
82 $query->_groupByComponentClause = " GROUP BY civicrm_activity.id ";
83
84 // CRM-12675
85 $activityClause = NULL;
86
87 $components = CRM_Core_Component::getNames();
88 $componentClause = [];
89 foreach ($components as $componentID => $componentName) {
90 if ($componentName != 'CiviCase' && !CRM_Core_Permission::check("access $componentName")) {
91 $componentClause[] = " (activity_type.component_id IS NULL OR activity_type.component_id <> {$componentID}) ";
92 }
93 }
94 if (!empty($componentClause)) {
95 $activityClause = implode(' AND ', $componentClause);
96 }
97 $result = $query->searchQuery(0, 0, NULL, FALSE, FALSE, FALSE, FALSE, FALSE, $activityClause);
98
99 while ($result->fetch()) {
100 if (!empty($result->activity_id)) {
101 $ids[] = $result->activity_id;
102 }
103 }
104 }
105
106 if (!empty($ids)) {
107 $form->_componentClause = ' civicrm_activity.id IN ( ' . implode(',', $ids) . ' ) ';
108 $form->assign('totalSelectedActivities', count($ids));
109 }
110
111 $form->_activityHolderIds = $form->_componentIds = $ids;
112
113 // Set the context for redirection for any task actions.
114 $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
115 $urlParams = 'force=1';
116 if (CRM_Utils_Rule::qfKey($qfKey)) {
117 $urlParams .= "&qfKey=$qfKey";
118 }
119
120 $session = CRM_Core_Session::singleton();
121 $searchFormName = strtolower($form->get('searchFormName'));
122 if ($searchFormName == 'search') {
123 $session->replaceUserContext(CRM_Utils_System::url('civicrm/activity/search', $urlParams));
124 }
125 else {
126 $session->replaceUserContext(CRM_Utils_System::url("civicrm/contact/search/$searchFormName",
127 $urlParams
128 ));
129 }
130 }
131
132 /**
133 * Given the membership id, compute the contact id
134 * since it's used for things like send email.
135 */
136 public function setContactIDs() {
137 $IDs = implode(',', $this->_activityHolderIds);
138
139 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
140 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
141 $query = "
142 SELECT contact_id
143 FROM civicrm_activity_contact
144 WHERE activity_id IN ( $IDs ) AND
145 record_type_id = {$sourceID}";
146
147 $dao = CRM_Core_DAO::executeQuery($query);
148 while ($dao->fetch()) {
149 $contactIDs[] = $dao->contact_id;
150 }
151 $this->_contactIds = $contactIDs;
152 }
153
154 /**
155 * Simple shell that derived classes can call to add buttons to
156 * the form with a customized title for the main Submit
157 *
158 * @param string $title
159 * Title of the main button.
160 * @param string $nextType
161 * Button type for the form after processing.
162 * @param string $backType
163 * @param bool $submitOnce
164 */
165 public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
166 $this->addButtons([
167 [
168 'type' => $nextType,
169 'name' => $title,
170 'isDefault' => TRUE,
171 ],
172 [
173 'type' => $backType,
174 'name' => ts('Cancel'),
175 ],
176 ]);
177 }
178
179 }