Merge pull request #14097 from eileenmcnaughton/mailin_test
[civicrm-core.git] / CRM / Activity / Selector / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
32 */
33
34 /**
35 * This class is used to retrieve and display a range of contacts that match the given criteria.
36 *
37 * Specifically for results of advanced search options.
38 */
39 class 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
45 */
46 public static $_links = NULL;
47
48 /**
49 * We use desc to remind us what that column is, name is used in the tpl
50 *
51 * @var array
52 */
53 public static $_columnHeaders;
54
55 /**
56 * Properties of contact we're interested in displaying
57 * @var array
58 */
59
60 public static $_properties = [
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',
71 'source_record_id',
72 'activity_type_id',
73 'activity_type',
74 'activity_is_test',
75 'activity_campaign_id',
76 'activity_engagement_level',
77 ];
78
79 /**
80 * Are we restricting ourselves to a single contact.
81 *
82 * @var bool
83 */
84 protected $_single = FALSE;
85
86 /**
87 * Are we restricting ourselves to a single contact.
88 *
89 * @var bool
90 */
91 protected $_limit = NULL;
92
93 /**
94 * What context are we being invoked from.
95 *
96 * @var string
97 */
98 protected $_context = NULL;
99
100 /**
101 * What component context are we being invoked from.
102 *
103 * @var string
104 */
105 protected $_compContext = NULL;
106
107 /**
108 * QueryParams is the array returned by exportValues called on.
109 * the HTML_QuickForm_Controller for that page.
110 *
111 * @var array
112 */
113 public $_queryParams;
114
115 /**
116 * Represent the type of selector.
117 *
118 * @var int
119 */
120 protected $_action;
121
122 /**
123 * The additional clause that we restrict the search with.
124 *
125 * @var string
126 */
127 protected $_activityClause = NULL;
128
129 /**
130 * The query object.
131 *
132 * @var string
133 */
134 protected $_query;
135
136 /**
137 * Class constructor.
138 *
139 * @param array $queryParams
140 * Array of parameters for query.
141 * @param \const|int $action - action of search basic or advanced.
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.
148 *
149 * @param string $context
150 * @param null $compContext
151 *
152 * @return \CRM_Activity_Selector_Search
153 */
154 public function __construct(
155 &$queryParams,
156 $action = CRM_Core_Action::NONE,
157 $activityClause = NULL,
158 $single = FALSE,
159 $limit = NULL,
160 $context = 'search',
161 $compContext = NULL
162 ) {
163 // submitted form values
164 $this->_queryParams = &$queryParams;
165
166 $this->_single = $single;
167 $this->_limit = $limit;
168 $this->_context = $context;
169 $this->_compContext = $compContext;
170
171 $this->_activityClause = $activityClause;
172
173 // CRM-12675
174 $components = CRM_Core_Component::getNames();
175 $componentClause = [];
176 foreach ($components as $componentID => $componentName) {
177 // CRM-19201: Add support for searching CiviCampaign and CiviCase
178 // activities. For CiviCase, "access all cases and activities" is
179 // required here rather than "access my cases and activities" to
180 // prevent those with only the later permission from seeing a list
181 // of all cases which might present a privacy issue.
182 if (!CRM_Core_Permission::access($componentName, TRUE, TRUE)) {
183 $componentClause[] = " (activity_type.component_id IS NULL OR activity_type.component_id <> {$componentID}) ";
184 }
185 }
186
187 if (!empty($componentClause)) {
188 $componentRestriction = implode(' AND ', $componentClause);
189 if (empty($this->_activityClause)) {
190 $this->_activityClause = $componentRestriction;
191 }
192 else {
193 $this->_activityClause .= ' AND ' . $componentRestriction;
194 }
195 }
196
197 // type of selector
198 $this->_action = $action;
199 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
200 CRM_Activity_BAO_Query::selectorReturnProperties(),
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 = [];
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_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
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 $bulkActivityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Bulk Email');
277
278 while ($result->fetch()) {
279 $row = [];
280
281 // Ignore rows where we dont have an activity id.
282 if (empty($result->activity_id)) {
283 continue;
284 }
285 $this->_query->convertToPseudoNames($result);
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'] = CRM_Core_TestEntity::appendTestText($row['activity_type']);
314 }
315 $row['mailingId'] = '';
316 if (
317 $accessCiviMail &&
318 ($mailingIDs === TRUE || in_array($result->source_record_id, $mailingIDs)) &&
319 ($bulkActivityTypeID == $activityTypeId)
320 ) {
321 $row['mailingId'] = CRM_Utils_System::url('civicrm/mailing/report',
322 "mid={$result->source_record_id}&reset=1&cid={$contactId}&context=activitySelector"
323 );
324 $row['recipients'] = ts('(recipients)');
325 $row['target_contact_name'] = '';
326 $row['assignee_contact_name'] = '';
327 $accessMailingReport = TRUE;
328 }
329 $activityActions = new CRM_Activity_Selector_Activity($result->contact_id, NULL);
330 $actionLinks = $activityActions->actionLinks($activityTypeId,
331 CRM_Utils_Array::value('source_record_id', $row),
332 $accessMailingReport,
333 CRM_Utils_Array::value('activity_id', $row),
334 $this->_key,
335 $this->_compContext
336 );
337 $row['action'] = CRM_Core_Action::formLink($actionLinks, NULL,
338 [
339 'id' => $result->activity_id,
340 'cid' => $contactId,
341 'cxt' => $this->_context,
342 ],
343 ts('more'),
344 FALSE,
345 'activity.selector.row',
346 'Activity',
347 $result->activity_id
348 );
349
350 // Carry campaign to selector.
351 $row['campaign'] = CRM_Utils_Array::value($result->activity_campaign_id, $allCampaigns);
352 $row['campaign_id'] = $result->activity_campaign_id;
353
354 if ($engagementLevel = CRM_Utils_Array::value('activity_engagement_level', $row)) {
355 $row['activity_engagement_level'] = CRM_Utils_Array::value($engagementLevel,
356 $engagementLevels, $engagementLevel
357 );
358 }
359
360 // Check if recurring activity.
361 $repeat = CRM_Core_BAO_RecurringEntity::getPositionAndCount($row['activity_id'], 'civicrm_activity');
362 $row['repeat'] = '';
363 if ($repeat) {
364 $row['repeat'] = ts('Repeating (%1 of %2)', [1 => $repeat[0], 2 => $repeat[1]]);
365 }
366 $rows[] = $row;
367 }
368
369 return $rows;
370 }
371
372 /**
373 * @return array
374 * which contains an array of strings
375 */
376 public function getQILL() {
377 return $this->_query->qill();
378 }
379
380 /**
381 * Returns the column headers as an array of tuples:
382 * (name, sortName (key to the sort array))
383 *
384 * @param string $action
385 * The action being performed.
386 * @param string $output
387 * What should the result set include (web/email/csv).
388 *
389 * @return array
390 * the column headers that need to be displayed
391 */
392 public function &getColumnHeaders($action = NULL, $output = NULL) {
393 if (!isset(self::$_columnHeaders)) {
394 self::$_columnHeaders = [
395 [
396 'name' => ts('Type'),
397 'sort' => 'activity_type_id',
398 'direction' => CRM_Utils_Sort::DONTCARE,
399 ],
400 [
401 'name' => ts('Subject'),
402 'sort' => 'activity_subject',
403 'direction' => CRM_Utils_Sort::DONTCARE,
404 ],
405 [
406 'name' => ts('Added By'),
407 'sort' => 'source_contact',
408 'direction' => CRM_Utils_Sort::DONTCARE,
409 ],
410 ['name' => ts('With')],
411 ['name' => ts('Assigned')],
412 [
413 'name' => ts('Date'),
414 'sort' => 'activity_date_time',
415 'direction' => CRM_Utils_Sort::DESCENDING,
416 ],
417 [
418 'name' => ts('Status'),
419 'sort' => 'activity_status',
420 'direction' => CRM_Utils_Sort::DONTCARE,
421 ],
422 [
423 'desc' => ts('Actions'),
424 ],
425 ];
426 }
427 return self::$_columnHeaders;
428 }
429
430 /**
431 * @return mixed
432 */
433 public function alphabetQuery() {
434 return $this->_query->alphabetQuery();
435 }
436
437 /**
438 * @return string
439 */
440 public function &getQuery() {
441 return $this->_query;
442 }
443
444 /**
445 * Name of export file.
446 *
447 * @param string $output
448 * Type of output.
449 *
450 * @return string
451 * name of the file
452 */
453 public function getExportFileName($output = 'csv') {
454 return ts('CiviCRM Activity Search');
455 }
456
457 }