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