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