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