3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class is used to retrieve and display a range of contacts that match the given criteria.
21 * Specifically for results of advanced search options.
23 class CRM_Activity_Selector_Search
extends CRM_Core_Selector_Base
implements CRM_Core_Selector_API
{
26 * This defines two actions- View and Edit.
30 public static $_links = NULL;
33 * We use desc to remind us what that column is, name is used in the tpl
37 public static $_columnHeaders;
40 * Properties of contact we're interested in displaying
44 public static $_properties = [
59 'activity_campaign_id',
60 'activity_engagement_level',
64 * Are we restricting ourselves to a single contact.
68 protected $_single = FALSE;
71 * Are we restricting ourselves to a single contact.
75 protected $_limit = NULL;
78 * What context are we being invoked from.
82 protected $_context = NULL;
85 * What component context are we being invoked from.
89 protected $_compContext = NULL;
92 * QueryParams is the array returned by exportValues called on.
93 * the HTML_QuickForm_Controller for that page.
100 * Represent the type of selector.
107 * The additional clause that we restrict the search with.
111 protected $_activityClause = NULL;
116 * @var \CRM_Contact_BAO_Query
123 * @param array $queryParams
124 * Array of parameters for query.
125 * @param \const|int $action - action of search basic or advanced.
126 * @param string $activityClause
127 * If the caller wants to further restrict the search (used in activities).
128 * @param bool $single
129 * Are we dealing only with one contact?.
131 * How many activities do we want returned.
133 * @param string $context
134 * @param null $compContext
136 * @return \CRM_Activity_Selector_Search
138 public function __construct(
140 $action = CRM_Core_Action
::NONE
,
141 $activityClause = NULL,
147 // submitted form values
148 $this->_queryParams
= &$queryParams;
150 $this->_single
= $single;
151 $this->_limit
= $limit;
152 $this->_context
= $context;
153 $this->_compContext
= $compContext;
155 $this->_activityClause
= $activityClause;
158 $components = CRM_Core_Component
::getNames();
159 $componentClause = [];
160 foreach ($components as $componentID => $componentName) {
161 // CRM-19201: Add support for searching CiviCampaign and CiviCase
162 // activities. For CiviCase, "access all cases and activities" is
163 // required here rather than "access my cases and activities" to
164 // prevent those with only the later permission from seeing a list
165 // of all cases which might present a privacy issue.
166 // @todo this is the cause of the current devastatingly bad performance on
167 // activity search - it involves a bad join.
168 // The correct fix is to use the permission infrastrucutre - ie. add in the
169 // clause generated by CRM_Activity_BAO_Query::addSelectWhere
170 // but some testing needs to check that before making the change
171 // see https://github.com/civicrm/civicrm-core/blob/be2fb01f90f5f299dd07402a41fed7c7c7567f00/CRM/Utils/SQL.php#L48
172 // for how it's done in the api kernel.
173 if (!CRM_Core_Permission
::access($componentName, TRUE, TRUE)) {
174 $componentClause[] = " (activity_type.component_id IS NULL OR activity_type.component_id <> {$componentID}) ";
178 if (!empty($componentClause)) {
179 $componentRestriction = implode(' AND ', $componentClause);
180 if (empty($this->_activityClause
)) {
181 $this->_activityClause
= $componentRestriction;
184 $this->_activityClause
.= ' AND ' . $componentRestriction;
189 $this->_action
= $action;
190 $this->_query
= new CRM_Contact_BAO_Query($this->_queryParams
,
191 CRM_Activity_BAO_Query
::selectorReturnProperties(),
193 CRM_Contact_BAO_Query
::MODE_ACTIVITY
195 $this->_query
->_distinctComponentClause
= '( civicrm_activity.id )';
196 $this->_query
->_groupByComponentClause
= " GROUP BY civicrm_activity.id ";
200 * Getter for array of the parameters required for creating pager.
203 * @param array $params
205 public function getPagerParams($action, &$params) {
206 $params['status'] = ts('Activities %%StatusMessage%%');
207 $params['csvString'] = NULL;
208 $params['rowCount'] = CRM_Utils_Pager
::ROWCOUNT
;
209 $params['buttonTop'] = 'PagerTopButton';
210 $params['buttonBottom'] = 'PagerBottomButton';
214 * Returns total number of rows for the query.
216 * @param string $action
219 * Total number of rows
221 public function getTotalCount($action) {
222 return $this->_query
->searchQuery(0, 0, NULL,
226 $this->_activityClause
231 * Returns all the rows in the given offset and rowCount.
233 * @param string $action
234 * The action being performed.
236 * The row number to start from.
237 * @param int $rowCount
238 * The number of rows to return.
239 * @param string $sort
240 * The sql string that describes the sort order.
241 * @param string $output
242 * What should the result set include (web/email/csv).
245 * rows in the given offset and rowCount
247 public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
248 $result = $this->_query
->searchQuery(
249 $offset, $rowCount, $sort,
253 $this->_activityClause
256 $mailingIDs = CRM_Mailing_BAO_Mailing
::mailingACLIDs();
257 $accessCiviMail = CRM_Core_Permission
::check('access CiviMail');
259 // Get all campaigns.
260 $allCampaigns = CRM_Campaign_BAO_Campaign
::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE);
262 $engagementLevels = CRM_Campaign_PseudoConstant
::engagementLevel();
263 $activityContacts = CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
264 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
265 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
266 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
267 $bulkActivityTypeID = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Bulk Email');
269 while ($result->fetch()) {
272 // Ignore rows where we dont have an activity id.
273 if (empty($result->activity_id
)) {
276 $this->_query
->convertToPseudoNames($result);
278 // the columns we are interested in
279 foreach (self
::$_properties as $property) {
280 if (isset($result->$property)) {
281 $row[$property] = $result->$property;
285 $contactId = CRM_Utils_Array
::value('contact_id', $row);
287 $contactId = CRM_Utils_Array
::value('source_contact_id', $row);
290 $row['target_contact_name'] = CRM_Activity_BAO_ActivityContact
::getNames($row['activity_id'], $targetID);
291 $row['assignee_contact_name'] = CRM_Activity_BAO_ActivityContact
::getNames($row['activity_id'], $assigneeID);
292 list($row['source_contact_name'], $row['source_contact_id']) = CRM_Activity_BAO_ActivityContact
::getNames($row['activity_id'], $sourceID, TRUE);
293 $row['source_contact_name'] = implode(',', array_values($row['source_contact_name']));
294 $row['source_contact_id'] = implode(',', $row['source_contact_id']);
296 if ($this->_context
== 'search') {
297 $row['checkbox'] = CRM_Core_Form
::CB_PREFIX
. $result->activity_id
;
299 $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 $accessMailingReport = FALSE;
302 $activityTypeId = $row['activity_type_id'];
303 if ($row['activity_is_test']) {
304 $row['activity_type'] = CRM_Core_TestEntity
::appendTestText($row['activity_type']);
306 $row['mailingId'] = '';
309 ($mailingIDs === TRUE ||
in_array($result->source_record_id
, $mailingIDs)) &&
310 ($bulkActivityTypeID == $activityTypeId)
312 $row['mailingId'] = CRM_Utils_System
::url('civicrm/mailing/report',
313 "mid={$result->source_record_id}&reset=1&cid={$contactId}&context=activitySelector"
315 $row['recipients'] = ts('(recipients)');
316 $row['target_contact_name'] = '';
317 $row['assignee_contact_name'] = '';
318 $accessMailingReport = TRUE;
320 $activityActions = new CRM_Activity_Selector_Activity($result->contact_id
, NULL);
321 $actionLinks = $activityActions->actionLinks($activityTypeId,
322 CRM_Utils_Array
::value('source_record_id', $row),
323 $accessMailingReport,
324 CRM_Utils_Array
::value('activity_id', $row),
328 $row['action'] = CRM_Core_Action
::formLink($actionLinks, NULL,
330 'id' => $result->activity_id
,
332 'cxt' => $this->_context
,
336 'activity.selector.row',
341 // Carry campaign to selector.
342 $row['campaign'] = CRM_Utils_Array
::value($result->activity_campaign_id
, $allCampaigns);
343 $row['campaign_id'] = $result->activity_campaign_id
;
345 if ($engagementLevel = CRM_Utils_Array
::value('activity_engagement_level', $row)) {
346 $row['activity_engagement_level'] = CRM_Utils_Array
::value($engagementLevel,
347 $engagementLevels, $engagementLevel
351 // Check if recurring activity.
352 $repeat = CRM_Core_BAO_RecurringEntity
::getPositionAndCount($row['activity_id'], 'civicrm_activity');
355 $row['repeat'] = ts('Repeating (%1 of %2)', [1 => $repeat[0], 2 => $repeat[1]]);
365 * which contains an array of strings
367 public function getQILL() {
368 return $this->_query
->qill();
372 * Returns the column headers as an array of tuples:
373 * (name, sortName (key to the sort array))
375 * @param string $action
376 * The action being performed.
377 * @param string $output
378 * What should the result set include (web/email/csv).
381 * the column headers that need to be displayed
383 public function &getColumnHeaders($action = NULL, $output = NULL) {
384 if (!isset(self
::$_columnHeaders)) {
385 self
::$_columnHeaders = [
387 'name' => ts('Type'),
388 'sort' => 'activity_type_id',
389 'direction' => CRM_Utils_Sort
::DONTCARE
,
392 'name' => ts('Subject'),
393 'sort' => 'activity_subject',
394 'direction' => CRM_Utils_Sort
::DONTCARE
,
397 'name' => ts('Added by'),
398 'sort' => 'source_contact',
399 'direction' => CRM_Utils_Sort
::DONTCARE
,
401 ['name' => ts('With')],
402 ['name' => ts('Assigned')],
404 'name' => ts('Date'),
405 'sort' => 'activity_date_time',
406 'direction' => CRM_Utils_Sort
::DESCENDING
,
409 'name' => ts('Status'),
410 'sort' => 'activity_status',
411 'direction' => CRM_Utils_Sort
::DONTCARE
,
414 'desc' => ts('Actions'),
418 return self
::$_columnHeaders;
424 public function alphabetQuery() {
425 return $this->_query
->alphabetQuery();
429 * @return \CRM_Contact_BAO_Query
431 public function &getQuery() {
432 return $this->_query
;
436 * Name of export file.
438 * @param string $output
444 public function getExportFileName($output = 'csv') {
445 return ts('CiviCRM Activity Search');