Cleanup phpdoc comments
[civicrm-core.git] / CRM / Activity / Selector / Search.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * @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 \const|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 * @param string $context
157 * @param null $compContext
158 *
159 * @return \CRM_Activity_Selector_Search
160 @access public
161 */
162 function __construct(&$queryParams,
163 $action = CRM_Core_Action::NONE,
164 $activityClause = NULL,
165 $single = FALSE,
166 $limit = NULL,
167 $context = 'search',
168 $compContext = NULL
169 ) {
170 // submitted form values
171 $this->_queryParams = &$queryParams;
172
173 $this->_single = $single;
174 $this->_limit = $limit;
175 $this->_context = $context;
176 $this->_compContext = $compContext;
177
178 $this->_activityClause = $activityClause;
179
180 // type of selector
181 $this->_action = $action;
182 $this->_query = new CRM_Contact_BAO_Query($this->_queryParams,
183 CRM_Activity_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_ACTIVITY,
184 FALSE
185 ),
186 NULL, FALSE, FALSE,
187 CRM_Contact_BAO_Query::MODE_ACTIVITY
188 );
189 $this->_query->_distinctComponentClause = '( civicrm_activity.id )';
190 $this->_query->_groupByComponentClause = " GROUP BY civicrm_activity.id ";
191 }
192
193 /**
194 * Getter for array of the parameters required for creating pager.
195 *
196 * @param $action
197 * @param array $params
198 *
199 * @access public
200 */
201 function getPagerParams($action, &$params) {
202 $params['status'] = ts('Activities %%StatusMessage%%');
203 $params['csvString'] = NULL;
204 $params['rowCount'] = CRM_Utils_Pager::ROWCOUNT;
205 $params['buttonTop'] = 'PagerTopButton';
206 $params['buttonBottom'] = 'PagerBottomButton';
207 }
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 $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 ?
290 $result->contact_sub_type : $result->contact_type, FALSE, $result->contact_id
291 );
292 $accessMailingReport = FALSE;
293 $activityTypeId = $row['activity_type_id'];
294 if ($row['activity_is_test']) {
295 $row['activity_type'] = $row['activity_type'] . " (test)";
296 }
297 $bulkActivityTypeID = CRM_Utils_Array::key('Bulk Email', $activityTypes);
298 $row['mailingId'] = '';
299 if (
300 $accessCiviMail &&
301 ($mailingIDs === TRUE || in_array($result->source_record_id, $mailingIDs)) &&
302 ($bulkActivityTypeID == $activityTypeId)
303 ) {
304 $row['mailingId'] = CRM_Utils_System::url('civicrm/mailing/report',
305 "mid={$result->source_record_id}&reset=1&cid={$contactId}&context=activitySelector"
306 );
307 $row['recipients'] = ts('(recipients)');
308 $row['target_contact_name'] = '';
309 $row['assignee_contact_name'] = '';
310 $accessMailingReport = TRUE;
311 }
312 $activityActions = new CRM_Activity_Selector_Activity($result->contact_id, NULL);
313 $actionLinks = $activityActions->actionLinks($activityTypeId,
314 CRM_Utils_Array::value('source_record_id', $row),
315 $accessMailingReport,
316 CRM_Utils_Array::value('activity_id', $row),
317 $this->_key,
318 $this->_compContext
319 );
320 $row['action'] = CRM_Core_Action::formLink($actionLinks, NULL,
321 array(
322 'id' => $result->activity_id,
323 'cid' => $contactId,
324 'cxt' => $this->_context,
325 ),
326 ts('more'),
327 FALSE,
328 'activity.selector.row',
329 'Activity',
330 $result->activity_id
331 );
332
333 //carry campaign to selector.
334 $row['campaign'] = CRM_Utils_Array::value($result->activity_campaign_id, $allCampaigns);
335 $row['campaign_id'] = $result->activity_campaign_id;
336
337 if ($engagementLevel = CRM_Utils_Array::value('activity_engagement_level', $row)) {
338 $row['activity_engagement_level'] = CRM_Utils_Array::value($engagementLevel,
339 $engagementLevels, $engagementLevel
340 );
341 }
342
343 //Check if recurring activity
344 $isRecurringActivity = CRM_Core_BAO_RecurringEntity::getParentFor($row['activity_id'], 'civicrm_activity');
345 $row['repeat'] = '';
346 if ($isRecurringActivity) {
347 if ($row['activity_id'] == $isRecurringActivity) {
348 $row['repeat'] = 'Recurring Activity - (Parent)';
349 }
350 else {
351 $row['repeat'] = 'Recurring Activity - (Child)';
352 }
353 }
354 $rows[] = $row;
355 }
356
357 return $rows;
358 }
359
360 /**
361 * @return array $qill which contains an array of strings
362 * @access public
363 */
364 public function getQILL() {
365 return $this->_query->qill();
366 }
367
368 /**
369 * Returns the column headers as an array of tuples:
370 * (name, sortName (key to the sort array))
371 *
372 * @param string $action the action being performed
373 * @param enum $output what should the result set include (web/email/csv)
374 *
375 * @return array the column headers that need to be displayed
376 * @access public
377 */
378 public function &getColumnHeaders($action = NULL, $output = NULL) {
379 if (!isset(self::$_columnHeaders)) {
380 self::$_columnHeaders = array(
381 array(
382 'name' => ts('Type'),
383 'sort' => 'activity_type_id',
384 'direction' => CRM_Utils_Sort::DONTCARE,
385 ),
386 array(
387 'name' => ts('Subject'),
388 'sort' => 'activity_subject',
389 'direction' => CRM_Utils_Sort::DONTCARE,
390 ),
391 array(
392 'name' => ts('Added By'),
393 'sort' => 'source_contact',
394 'direction' => CRM_Utils_Sort::DONTCARE,
395 ),
396 array('name' => ts('With')),
397 array('name' => ts('Assigned')),
398 array(
399 'name' => ts('Date'),
400 'sort' => 'activity_date_time',
401 'direction' => CRM_Utils_Sort::DESCENDING,
402 ),
403 array(
404 'name' => ts('Status'),
405 'sort' => 'activity_status',
406 'direction' => CRM_Utils_Sort::DONTCARE,
407 ),
408 array(
409 'desc' => ts('Actions'),
410 ),
411 );
412 }
413 return self::$_columnHeaders;
414 }
415
416 /**
417 * @return mixed
418 */
419 function alphabetQuery() {
420 return $this->_query->searchQuery(NULL, NULL, NULL, FALSE, FALSE, TRUE);
421 }
422
423 /**
424 * @return string
425 */
426 function &getQuery() {
427 return $this->_query;
428 }
429
430 /**
431 * Name of export file.
432 *
433 * @param string $output type of output
434 *
435 * @return string name of the file
436 */
437 function getExportFileName($output = 'csv') {
438 return ts('CiviCRM Activity Search');
439 }
440 }