CRM-12675 - Hide Contribution activities from users without Access CiviContribute...
[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 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
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 ";
198 }
199
200 /**
201 * Getter for array of the parameters required for creating pager.
202 *
203 * @param $action
204 * @param array $params
205 */
206 public function getPagerParams($action, &$params) {
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 }
213
214 /**
215 * Returns total number of rows for the query.
216 *
217 * @param string $action
218 *
219 * @return int
220 * Total number of rows
221 */
222 public function getTotalCount($action) {
223 return $this->_query->searchQuery(0, 0, NULL,
224 TRUE, FALSE,
225 FALSE, FALSE,
226 FALSE,
227 $this->_activityClause
228 );
229 }
230
231 /**
232 * Returns all the rows in the given offset and rowCount.
233 *
234 * @param string $action
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.
242 * @param string $output
243 * What should the result set include (web/email/csv).
244 *
245 * @return array
246 * rows in the given offset and rowCount
247 */
248 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
249 $result = $this->_query->searchQuery(
250 $offset, $rowCount, $sort,
251 FALSE, FALSE,
252 FALSE, FALSE,
253 FALSE,
254 $this->_activityClause
255 );
256 $rows = array();
257 $mailingIDs = CRM_Mailing_BAO_Mailing::mailingACLIDs();
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();
264 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
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);
268 //get all activity types
269 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name', TRUE);
270
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
291 $row['target_contact_name'] = CRM_Activity_BAO_ActivityContact::getNames($row['activity_id'], $targetID);
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);
294 $row['source_contact_name'] = implode(',', array_values($row['source_contact_name']));
295 $row['source_contact_id'] = implode(',', $row['source_contact_id']);
296
297 if ($this->_context == 'search') {
298 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->activity_id;
299 }
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
301 );
302 $accessMailingReport = FALSE;
303 $activityTypeId = $row['activity_type_id'];
304 if ($row['activity_is_test']) {
305 $row['activity_type'] = $row['activity_type'] . " (test)";
306 }
307 $bulkActivityTypeID = CRM_Utils_Array::key('Bulk Email', $activityTypes);
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',
315 "mid={$result->source_record_id}&reset=1&cid={$contactId}&context=activitySelector"
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,
335 ),
336 ts('more'),
337 FALSE,
338 'activity.selector.row',
339 'Activity',
340 $result->activity_id
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
353 //Check if recurring activity
354 $repeat = CRM_Core_BAO_RecurringEntity::getPositionAndCount($row['activity_id'], 'civicrm_activity');
355 $row['repeat'] = '';
356 if ($repeat) {
357 $row['repeat'] = ts('Repeating (%1 of %2)', array(1 => $repeat[0], 2 => $repeat[1]));
358 }
359 $rows[] = $row;
360 }
361
362 return $rows;
363 }
364
365 /**
366 * @return array
367 * which contains an array of strings
368 */
369 public function getQILL() {
370 return $this->_query->qill();
371 }
372
373 /**
374 * Returns the column headers as an array of tuples:
375 * (name, sortName (key to the sort array))
376 *
377 * @param string $action
378 * The action being performed.
379 * @param string $output
380 * What should the result set include (web/email/csv).
381 *
382 * @return array
383 * the column headers that need to be displayed
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'),
395 'sort' => 'activity_subject',
396 'direction' => CRM_Utils_Sort::DONTCARE,
397 ),
398 array(
399 'name' => ts('Added By'),
400 'sort' => 'source_contact',
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'),
412 'sort' => 'activity_status',
413 'direction' => CRM_Utils_Sort::DONTCARE,
414 ),
415 array(
416 'desc' => ts('Actions'),
417 ),
418 );
419 }
420 return self::$_columnHeaders;
421 }
422
423 /**
424 * @return mixed
425 */
426 public function alphabetQuery() {
427 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
428 }
429
430 /**
431 * @return string
432 */
433 public function &getQuery() {
434 return $this->_query;
435 }
436
437 /**
438 * Name of export file.
439 *
440 * @param string $output
441 * Type of output.
442 *
443 * @return string
444 * name of the file
445 */
446 public function getExportFileName($output = 'csv') {
447 return ts('CiviCRM Activity Search');
448 }
449
450 }