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