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