CRM-12675 - Hide Contribution activities from users without Access CiviContribute...
[civicrm-core.git] / CRM / Activity / Selector / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 |
c73475ea 12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
6a488035
TO
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 |
c73475ea
WA
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
6a488035
TO
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class is used to retrieve and display a range of
38 * contacts that match the given criteria (specifically for
39 * results of advanced search options.
40 *
41 */
42class CRM_Activity_Selector_Search extends CRM_Core_Selector_Base implements CRM_Core_Selector_API {
43
44 /**
45 * This defines two actions- View and Edit.
46 *
47 * @var array
6a488035
TO
48 */
49 static $_links = NULL;
50
51 /**
100fef9d 52 * We use desc to remind us what that column is, name is used in the tpl
6a488035
TO
53 *
54 * @var array
6a488035
TO
55 */
56 static $_columnHeaders;
57
58 /**
59 * Properties of contact we're interested in displaying
60 * @var array
6a488035
TO
61 */
62
63 static $_properties = array(
64 'contact_id',
65 'contact_type',
66 'contact_sub_type',
67 'sort_name',
68 'display_name',
69 'activity_id',
70 'activity_date_time',
71 'activity_status_id',
72 'activity_status',
73 'activity_subject',
6a488035 74 'source_record_id',
6a488035
TO
75 'activity_type_id',
76 'activity_type',
77 'activity_is_test',
78 'activity_campaign_id',
79 'activity_engagement_level',
80 );
81
82 /**
fe482240 83 * Are we restricting ourselves to a single contact.
6a488035 84 *
6a488035
TO
85 * @var boolean
86 */
87 protected $_single = FALSE;
88
89 /**
fe482240 90 * Are we restricting ourselves to a single contact.
6a488035 91 *
6a488035
TO
92 * @var boolean
93 */
94 protected $_limit = NULL;
95
96 /**
fe482240 97 * What context are we being invoked from.
6a488035 98 *
6a488035
TO
99 * @var string
100 */
101 protected $_context = NULL;
102
103 /**
fe482240 104 * What component context are we being invoked from.
6a488035 105 *
6a488035
TO
106 * @var string
107 */
108 protected $_compContext = NULL;
109
110 /**
fe482240 111 * QueryParams is the array returned by exportValues called on.
6a488035
TO
112 * the HTML_QuickForm_Controller for that page.
113 *
114 * @var array
6a488035
TO
115 */
116 public $_queryParams;
117
118 /**
fe482240 119 * Represent the type of selector.
6a488035
TO
120 *
121 * @var int
6a488035
TO
122 */
123 protected $_action;
124
125 /**
fe482240 126 * The additional clause that we restrict the search with.
6a488035
TO
127 *
128 * @var string
129 */
130 protected $_activityClause = NULL;
131
132 /**
fe482240 133 * The query object.
6a488035
TO
134 *
135 * @var string
136 */
137 protected $_query;
138
139 /**
fe482240 140 * Class constructor.
6a488035 141 *
041ab3d1
TO
142 * @param array $queryParams
143 * Array of parameters for query.
dd244018 144 * @param \const|int $action - action of search basic or advanced.
041ab3d1
TO
145 * @param string $activityClause
146 * If the caller wants to further restrict the search (used in activities).
147 * @param bool $single
148 * Are we dealing only with one contact?.
149 * @param int $limit
150 * How many activities do we want returned.
6a488035 151 *
dd244018
EM
152 * @param string $context
153 * @param null $compContext
154 *
155 * @return \CRM_Activity_Selector_Search
6a488035 156 */
2da40d21 157 public function __construct(
9d5494f7 158 &$queryParams,
32864ccf 159 $action = CRM_Core_Action::NONE,
6a488035 160 $activityClause = NULL,
32864ccf
TO
161 $single = FALSE,
162 $limit = NULL,
163 $context = 'search',
164 $compContext = NULL
6a488035
TO
165 ) {
166 // submitted form values
167 $this->_queryParams = &$queryParams;
168
353ffa53
TO
169 $this->_single = $single;
170 $this->_limit = $limit;
171 $this->_context = $context;
6a488035
TO
172 $this->_compContext = $compContext;
173
174 $this->_activityClause = $activityClause;
175
87767abb 176 // CRM-12675
177 if (! CRM_Core_Permission::check('access CiviContribute')) {
178 $componentRestriction = ' (activity_type.component_id IS NULL OR activity_type.component_id <> 2) ';
179 if (empty($this->_activityClause)) {
180 $this->_activityClause = $componentRestriction;
181 }
182 else {
183 $this->_activityClause .= ' AND ' . $componentRestriction;
184 }
185 }
186
6a488035
TO
187 // type of selector
188 $this->_action = $action;
189 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
190 CRM_Activity_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_ACTIVITY,
191 FALSE
192 ),
193 NULL, FALSE, FALSE,
194 CRM_Contact_BAO_Query::MODE_ACTIVITY
195 );
196 $this->_query->_distinctComponentClause = '( civicrm_activity.id )';
197 $this->_query->_groupByComponentClause = " GROUP BY civicrm_activity.id ";
6a488035 198 }
6a488035
TO
199
200 /**
100fef9d 201 * Getter for array of the parameters required for creating pager.
6a488035 202 *
da6b46f4 203 * @param $action
c490a46a 204 * @param array $params
6a488035 205 */
00be9182 206 public function getPagerParams($action, &$params) {
6a488035
TO
207 $params['status'] = ts('Activities %%StatusMessage%%');
208 $params['csvString'] = NULL;
209 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
210 $params['buttonTop'] = 'PagerTopButton';
211 $params['buttonBottom'] = 'PagerBottomButton';
212 }
6a488035
TO
213
214 /**
215 * Returns total number of rows for the query.
216 *
2e2605fe 217 * @param string $action
6a488035 218 *
a6c01b45
CW
219 * @return int
220 * Total number of rows
6a488035 221 */
00be9182 222 public function getTotalCount($action) {
6a488035
TO
223 return $this->_query->searchQuery(0, 0, NULL,
224 TRUE, FALSE,
225 FALSE, FALSE,
226 FALSE,
227 $this->_activityClause
228 );
229 }
230
231 /**
2e2605fe 232 * Returns all the rows in the given offset and rowCount.
6a488035 233 *
3f8d2862 234 * @param string $action
041ab3d1
TO
235 * The action being performed.
236 * @param int $offset
237 * The row number to start from.
238 * @param int $rowCount
239 * The number of rows to return.
240 * @param string $sort
241 * The sql string that describes the sort order.
3f8d2862 242 * @param string $output
041ab3d1 243 * What should the result set include (web/email/csv).
6a488035 244 *
a6c01b45
CW
245 * @return array
246 * rows in the given offset and rowCount
6a488035 247 */
00be9182 248 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
f04255e4
DL
249 $result = $this->_query->searchQuery(
250 $offset, $rowCount, $sort,
6a488035
TO
251 FALSE, FALSE,
252 FALSE, FALSE,
253 FALSE,
254 $this->_activityClause
255 );
353ffa53
TO
256 $rows = array();
257 $mailingIDs = CRM_Mailing_BAO_Mailing::mailingACLIDs();
6a488035
TO
258 $accessCiviMail = CRM_Core_Permission::check('access CiviMail');
259
260 //get all campaigns.
261 $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
262
263 $engagementLevels = CRM_Campaign_PseudoConstant::engagementLevel();
e7e657f0 264 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 265 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
266 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
267 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
0a7543cd 268 //get all activity types
dd244018 269 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name', TRUE);
f813f78e 270
6a488035
TO
271 while ($result->fetch()) {
272 $row = array();
273
274 // ignore rows where we dont have an activity id
275 if (empty($result->activity_id)) {
276 continue;
277 }
278
279 // the columns we are interested in
280 foreach (self::$_properties as $property) {
281 if (isset($result->$property)) {
282 $row[$property] = $result->$property;
283 }
284 }
285
286 $contactId = CRM_Utils_Array::value('contact_id', $row);
287 if (!$contactId) {
288 $contactId = CRM_Utils_Array::value('source_contact_id', $row);
289 }
290
353ffa53 291 $row['target_contact_name'] = CRM_Activity_BAO_ActivityContact::getNames($row['activity_id'], $targetID);
a24b3694 292 $row['assignee_contact_name'] = CRM_Activity_BAO_ActivityContact::getNames($row['activity_id'], $assigneeID);
293 list($row['source_contact_name'], $row['source_contact_id']) = CRM_Activity_BAO_ActivityContact::getNames($row['activity_id'], $sourceID, TRUE);
f04255e4
DL
294 $row['source_contact_name'] = implode(',', array_values($row['source_contact_name']));
295 $row['source_contact_id'] = implode(',', $row['source_contact_id']);
6a488035 296
6a488035
TO
297 if ($this->_context == 'search') {
298 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->activity_id;
299 }
32864ccf 300 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
6a488035
TO
301 );
302 $accessMailingReport = FALSE;
353ffa53 303 $activityTypeId = $row['activity_type_id'];
6a488035
TO
304 if ($row['activity_is_test']) {
305 $row['activity_type'] = $row['activity_type'] . " (test)";
306 }
0a7543cd 307 $bulkActivityTypeID = CRM_Utils_Array::key('Bulk Email', $activityTypes);
6a488035
TO
308 $row['mailingId'] = '';
309 if (
310 $accessCiviMail &&
311 ($mailingIDs === TRUE || in_array($result->source_record_id, $mailingIDs)) &&
312 ($bulkActivityTypeID == $activityTypeId)
313 ) {
314 $row['mailingId'] = CRM_Utils_System::url('civicrm/mailing/report',
c79f662a 315 "mid={$result->source_record_id}&reset=1&cid={$contactId}&context=activitySelector"
6a488035
TO
316 );
317 $row['recipients'] = ts('(recipients)');
318 $row['target_contact_name'] = '';
319 $row['assignee_contact_name'] = '';
320 $accessMailingReport = TRUE;
321 }
322 $activityActions = new CRM_Activity_Selector_Activity($result->contact_id, NULL);
323 $actionLinks = $activityActions->actionLinks($activityTypeId,
324 CRM_Utils_Array::value('source_record_id', $row),
325 $accessMailingReport,
326 CRM_Utils_Array::value('activity_id', $row),
327 $this->_key,
328 $this->_compContext
329 );
330 $row['action'] = CRM_Core_Action::formLink($actionLinks, NULL,
331 array(
332 'id' => $result->activity_id,
333 'cid' => $contactId,
334 'cxt' => $this->_context,
87dab4a4
AH
335 ),
336 ts('more'),
337 FALSE,
338 'activity.selector.row',
339 'Activity',
340 $result->activity_id
6a488035
TO
341 );
342
343 //carry campaign to selector.
344 $row['campaign'] = CRM_Utils_Array::value($result->activity_campaign_id, $allCampaigns);
345 $row['campaign_id'] = $result->activity_campaign_id;
346
347 if ($engagementLevel = CRM_Utils_Array::value('activity_engagement_level', $row)) {
348 $row['activity_engagement_level'] = CRM_Utils_Array::value($engagementLevel,
349 $engagementLevels, $engagementLevel
350 );
351 }
352
d8786c71 353 //Check if recurring activity
04374d9d 354 $repeat = CRM_Core_BAO_RecurringEntity::getPositionAndCount($row['activity_id'], 'civicrm_activity');
d8786c71 355 $row['repeat'] = '';
04374d9d
CW
356 if ($repeat) {
357 $row['repeat'] = ts('Repeating (%1 of %2)', array(1 => $repeat[0], 2 => $repeat[1]));
d8786c71 358 }
6a488035
TO
359 $rows[] = $row;
360 }
361
362 return $rows;
363 }
364
365 /**
a6c01b45
CW
366 * @return array
367 * which contains an array of strings
6a488035
TO
368 */
369 public function getQILL() {
370 return $this->_query->qill();
371 }
372
373 /**
100fef9d 374 * Returns the column headers as an array of tuples:
6a488035
TO
375 * (name, sortName (key to the sort array))
376 *
041ab3d1
TO
377 * @param string $action
378 * The action being performed.
3f8d2862 379 * @param string $output
041ab3d1 380 * What should the result set include (web/email/csv).
6a488035 381 *
a6c01b45
CW
382 * @return array
383 * the column headers that need to be displayed
6a488035
TO
384 */
385 public function &getColumnHeaders($action = NULL, $output = NULL) {
386 if (!isset(self::$_columnHeaders)) {
387 self::$_columnHeaders = array(
388 array(
389 'name' => ts('Type'),
390 'sort' => 'activity_type_id',
391 'direction' => CRM_Utils_Sort::DONTCARE,
392 ),
393 array(
394 'name' => ts('Subject'),
5fa296f7 395 'sort' => 'activity_subject',
6a488035
TO
396 'direction' => CRM_Utils_Sort::DONTCARE,
397 ),
398 array(
399 'name' => ts('Added By'),
37eb6ff9 400 'sort' => 'source_contact',
6a488035
TO
401 'direction' => CRM_Utils_Sort::DONTCARE,
402 ),
403 array('name' => ts('With')),
404 array('name' => ts('Assigned')),
405 array(
406 'name' => ts('Date'),
407 'sort' => 'activity_date_time',
408 'direction' => CRM_Utils_Sort::DESCENDING,
409 ),
410 array(
411 'name' => ts('Status'),
33a5a53d 412 'sort' => 'activity_status',
6a488035
TO
413 'direction' => CRM_Utils_Sort::DONTCARE,
414 ),
415 array(
416 'desc' => ts('Actions'),
417 ),
418 );
419 }
420 return self::$_columnHeaders;
421 }
422
ffd93213
EM
423 /**
424 * @return mixed
425 */
00be9182 426 public function alphabetQuery() {
6a488035
TO
427 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
428 }
429
ffd93213
EM
430 /**
431 * @return string
432 */
00be9182 433 public function &getQuery() {
6a488035
TO
434 return $this->_query;
435 }
436
437 /**
100fef9d 438 * Name of export file.
6a488035 439 *
041ab3d1
TO
440 * @param string $output
441 * Type of output.
6a488035 442 *
a6c01b45
CW
443 * @return string
444 * name of the file
6a488035 445 */
00be9182 446 public function getExportFileName($output = 'csv') {
6a488035
TO
447 return ts('CiviCRM Activity Search');
448 }
96025800 449
6a488035 450}