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