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