Merge pull request #42 from pradpnayak/CRM-11983
[civicrm-core.git] / CRM / Activity / Selector / Search.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 */
41class 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_contact_id',
77 'source_record_id',
78 'source_contact_name',
79 'activity_type_id',
80 'activity_type',
81 'activity_is_test',
82 'activity_campaign_id',
83 'activity_engagement_level',
84 );
85
86 /**
87 * are we restricting ourselves to a single contact
88 *
89 * @access protected
90 * @var boolean
91 */
92 protected $_single = FALSE;
93
94 /**
95 * are we restricting ourselves to a single contact
96 *
97 * @access protected
98 * @var boolean
99 */
100 protected $_limit = NULL;
101
102 /**
103 * what context are we being invoked from
104 *
105 * @access protected
106 * @var string
107 */
108 protected $_context = NULL;
109
110 /**
111 * what component context are we being invoked from
112 *
113 * @access protected
114 * @var string
115 */
116 protected $_compContext = NULL;
117
118 /**
119 * queryParams is the array returned by exportValues called on
120 * the HTML_QuickForm_Controller for that page.
121 *
122 * @var array
123 * @access protected
124 */
125 public $_queryParams;
126
127 /**
128 * represent the type of selector
129 *
130 * @var int
131 * @access protected
132 */
133 protected $_action;
134
135 /**
136 * The additional clause that we restrict the search with
137 *
138 * @var string
139 */
140 protected $_activityClause = NULL;
141
142 /**
143 * The query object
144 *
145 * @var string
146 */
147 protected $_query;
148
149 /**
150 * Class constructor
151 *
152 * @param array $queryParams array of parameters for query
153 * @param int $action - action of search basic or advanced.
154 * @param string $activityClause if the caller wants to further restrict the search (used in activities)
155 * @param boolean $single are we dealing only with one contact?
156 * @param int $limit how many activities do we want returned
157 *
158 * @return CRM_Contact_Selector
159 * @access public
160 */
161 function __construct(&$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 //CRM_Core_Error::debug( $this->_query ); exit();
191 }
192 //end of constructor
193
194 /**
195 * getter for array of the parameters required for creating pager.
196 *
197 * @param
198 * @access public
199 */
200 function getPagerParams($action, &$params) {
201 $params['status'] = ts('Activities %%StatusMessage%%');
202 $params['csvString'] = NULL;
203 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
204 $params['buttonTop'] = 'PagerTopButton';
205 $params['buttonBottom'] = 'PagerBottomButton';
206 }
207 //end of function
208
209 /**
210 * Returns total number of rows for the query.
211 *
212 * @param
213 *
214 * @return int Total number of rows
215 * @access public
216 */
217 function getTotalCount($action) {
218 return $this->_query->searchQuery(0, 0, NULL,
219 TRUE, FALSE,
220 FALSE, FALSE,
221 FALSE,
222 $this->_activityClause
223 );
224 }
225
226 /**
227 * returns all the rows in the given offset and rowCount
228 *
229 * @param enum $action the action being performed
230 * @param int $offset the row number to start from
231 * @param int $rowCount the number of rows to return
232 * @param string $sort the sql string that describes the sort order
233 * @param enum $output what should the result set include (web/email/csv)
234 *
235 * @return array rows in the given offset and rowCount
236 */
237 function &getRows($action, $offset, $rowCount, $sort, $output = NULL) {
238
239 $result = $this->_query->searchQuery($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
254 while ($result->fetch()) {
255 $row = array();
256
257 // ignore rows where we dont have an activity id
258 if (empty($result->activity_id)) {
259 continue;
260 }
261
262 // the columns we are interested in
263 foreach (self::$_properties as $property) {
264 if (isset($result->$property)) {
265 $row[$property] = $result->$property;
266 }
267 }
268
269 $contactId = CRM_Utils_Array::value('contact_id', $row);
270 if (!$contactId) {
271 $contactId = CRM_Utils_Array::value('source_contact_id', $row);
272 }
273
274 $row['target_contact_name'] = CRM_Activity_BAO_ActivityTarget::getTargetNames($row['activity_id']);
275 $row['assignee_contact_name'] = CRM_Activity_BAO_ActivityAssignment::getAssigneeNames($row['activity_id']);
276
277 if (CRM_Utils_Array::value('source_contact_id', $row)) {
278 $row['source_contact_name'] = CRM_Contact_BAO_Contact::displayName($row['source_contact_id']);
279 }
280 if ($this->_context == 'search') {
281 $row['checkbox'] = CRM_Core_Form::CB_PREFIX . $result->activity_id;
282 }
283 $row['contact_type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ?
284 $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
285 );
286 $accessMailingReport = FALSE;
287 $activityType = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'name', TRUE);
288 $activityTypeId = CRM_Utils_Array::key($row['activity_type'], $activityType);
289 if ($row['activity_is_test']) {
290 $row['activity_type'] = $row['activity_type'] . " (test)";
291 }
292 $bulkActivityTypeID = CRM_Utils_Array::key('Bulk Email', $activityType);
293 $row['mailingId'] = '';
294 if (
295 $accessCiviMail &&
296 ($mailingIDs === TRUE || in_array($result->source_record_id, $mailingIDs)) &&
297 ($bulkActivityTypeID == $activityTypeId)
298 ) {
299 $row['mailingId'] = CRM_Utils_System::url('civicrm/mailing/report',
300 "mid={$result->source_record_id}&reset=1&cid={$result->source_contact_id}&context=activitySelector"
301 );
302 $row['recipients'] = ts('(recipients)');
303 $row['target_contact_name'] = '';
304 $row['assignee_contact_name'] = '';
305 $accessMailingReport = TRUE;
306 }
307 $activityActions = new CRM_Activity_Selector_Activity($result->contact_id, NULL);
308 $actionLinks = $activityActions->actionLinks($activityTypeId,
309 CRM_Utils_Array::value('source_record_id', $row),
310 $accessMailingReport,
311 CRM_Utils_Array::value('activity_id', $row),
312 $this->_key,
313 $this->_compContext
314 );
315 $row['action'] = CRM_Core_Action::formLink($actionLinks, NULL,
316 array(
317 'id' => $result->activity_id,
318 'cid' => $contactId,
319 'cxt' => $this->_context,
320 )
321 );
322
323 //carry campaign to selector.
324 $row['campaign'] = CRM_Utils_Array::value($result->activity_campaign_id, $allCampaigns);
325 $row['campaign_id'] = $result->activity_campaign_id;
326
327 if ($engagementLevel = CRM_Utils_Array::value('activity_engagement_level', $row)) {
328 $row['activity_engagement_level'] = CRM_Utils_Array::value($engagementLevel,
329 $engagementLevels, $engagementLevel
330 );
331 }
332
333 $rows[] = $row;
334 }
335
336 return $rows;
337 }
338
339 /**
340 *
341 * @return array $qill which contains an array of strings
342 * @access public
343 */
344 public function getQILL() {
345 return $this->_query->qill();
346 }
347
348 /**
349 * returns the column headers as an array of tuples:
350 * (name, sortName (key to the sort array))
351 *
352 * @param string $action the action being performed
353 * @param enum $output what should the result set include (web/email/csv)
354 *
355 * @return array the column headers that need to be displayed
356 * @access public
357 */
358 public function &getColumnHeaders($action = NULL, $output = NULL) {
359 if (!isset(self::$_columnHeaders)) {
360 self::$_columnHeaders = array(
361 array(
362 'name' => ts('Type'),
363 'sort' => 'activity_type_id',
364 'direction' => CRM_Utils_Sort::DONTCARE,
365 ),
366 array(
367 'name' => ts('Subject'),
368 'sort' => 'subject',
369 'direction' => CRM_Utils_Sort::DONTCARE,
370 ),
371 array(
372 'name' => ts('Added By'),
373 'sort' => 'sort_name',
374 'direction' => CRM_Utils_Sort::DONTCARE,
375 ),
376 array('name' => ts('With')),
377 array('name' => ts('Assigned')),
378 array(
379 'name' => ts('Date'),
380 'sort' => 'activity_date_time',
381 'direction' => CRM_Utils_Sort::DESCENDING,
382 ),
383 array(
384 'name' => ts('Status'),
385 'sort' => 'status_id',
386 'direction' => CRM_Utils_Sort::DONTCARE,
387 ),
388 array(
389 'desc' => ts('Actions'),
390 ),
391 );
392 }
393 return self::$_columnHeaders;
394 }
395
396 function alphabetQuery() {
397 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
398 }
399
400 function &getQuery() {
401 return $this->_query;
402 }
403
404 /**
405 * name of export file.
406 *
407 * @param string $output type of output
408 *
409 * @return string name of the file
410 */
411 function getExportFileName($output = 'csv') {
412 return ts('CiviCRM Activity Search');
413 }
414}
415//end of class
416