daa784f8bbee1fabbec2d104652fc6b65ffb960c
[civicrm-core.git] / CRM / Contact / Form / Search / Custom / ActivitySearch.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 and the CiviCRM Licensing Exception. |
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 and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35 class CRM_Contact_Form_Search_Custom_ActivitySearch implements CRM_Contact_Form_Search_Interface {
36
37 protected $_formValues;
38
39 /**
40 * @param $formValues
41 */
42 /**
43 * @param $formValues
44 */
45 public function __construct(&$formValues) {
46 $this->_formValues = $formValues;
47
48 /**
49 * Define the columns for search result rows
50 */
51 $this->_columns = array(
52 ts('Name') => 'sort_name',
53 ts('Status') => 'activity_status',
54 ts('Activity Type') => 'activity_type',
55 ts('Activity Subject') => 'activity_subject',
56 ts('Scheduled By') => 'source_contact',
57 ts('Scheduled Date') => 'activity_date',
58 ts(' ') => 'activity_id',
59 ts(' ') => 'activity_type_id',
60 ts(' ') => 'case_id',
61 ts('Location') => 'location',
62 ts('Duration') => 'duration',
63 ts('Details') => 'details',
64 ts('Assignee') => 'assignee',
65 );
66
67 $this->_groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
68 'activity_status',
69 'id',
70 'name'
71 );
72
73 //Add custom fields to columns array for inclusion in export
74 $groupTree = &CRM_Core_BAO_CustomGroup::getTree('Activity', $form, NULL,
75 NULL, '', NULL
76 );
77
78 //use simplified formatted groupTree
79 $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, $form);
80
81 //cycle through custom fields and assign to _columns array
82 foreach ($groupTree as $key) {
83 foreach ($key['fields'] as $field) {
84 $fieldlabel = $key['title'] . ": " . $field['label'];
85 $this->_columns[$fieldlabel] = $field['column_name'];
86 }
87 }
88 //end custom fields
89 }
90
91 /**
92 * @param CRM_Core_Form $form
93 */
94 public function buildForm(&$form) {
95
96 /**
97 * You can define a custom title for the search form
98 */
99 $this->setTitle('Find Latest Activities');
100
101 /**
102 * Define the search form fields here
103 */
104 // Allow user to choose which type of contact to limit search on
105 $form->add('select', 'contact_type', ts('Find...'), CRM_Core_SelectValues::contactType());
106
107 // Text box for Activity Subject
108 $form->add('text',
109 'activity_subject',
110 ts('Activity Subject')
111 );
112
113 // Select box for Activity Type
114 $activityType = array('' => ' - select activity - ') + CRM_Core_PseudoConstant::activityType();
115
116 $form->add('select', 'activity_type_id', ts('Activity Type'),
117 $activityType,
118 FALSE
119 );
120
121 // textbox for Activity Status
122 $activityStatus = array('' => ' - select status - ') + CRM_Core_PseudoConstant::activityStatus();
123
124 $form->add('select', 'activity_status_id', ts('Activity Status'),
125 $activityStatus,
126 FALSE
127 );
128
129 // Activity Date range
130 $form->addDate('start_date', ts('Activity Date From'), FALSE, array('formatType' => 'custom'));
131 $form->addDate('end_date', ts('...through'), FALSE, array('formatType' => 'custom'));
132
133 // Contact Name field
134 $form->add('text', 'sort_name', ts('Contact Name'));
135
136 /**
137 * If you are using the sample template, this array tells the template fields to render
138 * for the search form.
139 */
140 $form->assign('elements', array(
141 'contact_type',
142 'activity_subject',
143 'activity_type_id',
144 'activity_status_id',
145 'start_date',
146 'end_date',
147 'sort_name',
148 ));
149 }
150
151 /**
152 * Define the smarty template used to layout the search form and results listings.
153 */
154 public function templateFile() {
155 return 'CRM/Contact/Form/Search/Custom/ActivitySearch.tpl';
156 }
157
158 /**
159 * Construct the search query.
160 */
161 public function all(
162 $offset = 0, $rowcount = 0, $sort = NULL,
163 $includeContactIDs = FALSE, $justIDs = FALSE
164 ) {
165
166 // SELECT clause must include contact_id as an alias for civicrm_contact.id
167 if ($justIDs) {
168 $select = 'contact_a.id as contact_id';
169 }
170 else {
171 $select = '
172 contact_a.id as contact_id,
173 contact_a.sort_name as sort_name,
174 contact_a.contact_type as contact_type,
175 activity.id as activity_id,
176 activity.activity_type_id as activity_type_id,
177 contact_b.sort_name as source_contact,
178 ov1.label as activity_type,
179 activity.subject as activity_subject,
180 activity.activity_date_time as activity_date,
181 ov2.label as activity_status,
182 cca.case_id as case_id,
183 activity.location as location,
184 activity.duration as duration,
185 activity.details as details,
186 assignment.activity_id as assignment_activity,
187 contact_c.display_name as assignee
188 ';
189 }
190
191 $from = $this->from();
192
193 $where = $this->where($includeContactIDs);
194
195 if (!empty($where)) {
196 $where = "WHERE $where";
197 }
198
199 // add custom group fields to SELECT and FROM clause
200 $groupTree = CRM_Core_BAO_CustomGroup::getTree('Activity', $form, NULL, NULL, '', NULL);
201
202 foreach ($groupTree as $key) {
203 if (!empty($key['extends']) && $key['extends'] == 'Activity') {
204 $select .= ", " . $key['table_name'] . ".*";
205 $from .= " LEFT JOIN " . $key['table_name'] . " ON " . $key['table_name'] . ".entity_id = activity.id";
206 }
207 }
208 // end custom groups add
209
210 $sql = " SELECT $select FROM $from $where ";
211
212 //no need to add order when only contact Ids.
213 if (!$justIDs) {
214 // Define ORDER BY for query in $sort, with default value
215 if (!empty($sort)) {
216 if (is_string($sort)) {
217 $sort = CRM_Utils_Type::escape($sort, 'String');
218 $sql .= " ORDER BY $sort ";
219 }
220 else {
221 $sql .= ' ORDER BY ' . trim($sort->orderBy());
222 }
223 }
224 else {
225 $sql .= 'ORDER BY contact_a.sort_name, activity.activity_date_time DESC, activity.activity_type_id, activity.status_id, activity.subject';
226 }
227 }
228 else {
229 //CRM-14107, since there could be multiple activities against same contact,
230 //we need to provide GROUP BY on contact id to prevent duplicacy on prev/next entries
231 $sql .= 'GROUP BY contact_a.id
232 ORDER BY contact_a.sort_name';
233 }
234
235 if ($rowcount > 0 && $offset >= 0) {
236 $offset = CRM_Utils_Type::escape($offset, 'Int');
237 $rowcount = CRM_Utils_Type::escape($rowcount, 'Int');
238 $sql .= " LIMIT $offset, $rowcount ";
239 }
240 return $sql;
241 }
242
243 /**
244 * Alters the date display in the Activity Date Column. We do this after we already have
245 * the result so that sorting on the date column stays pertinent to the numeric date value
246 * @param $row
247 */
248 public function alterRow(&$row) {
249 $row['activity_date'] = CRM_Utils_Date::customFormat($row['activity_date'], '%B %E%f, %Y %l:%M %P');
250 }
251
252 /**
253 * Regular JOIN statements here to limit results to contacts who have activities.
254 * @return string
255 */
256 public function from() {
257 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
258 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
259 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
260 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
261
262 return "
263 civicrm_activity activity
264 LEFT JOIN civicrm_activity_contact target
265 ON activity.id = target.activity_id AND target.record_type_id = {$targetID}
266 JOIN civicrm_contact contact_a
267 ON contact_a.id = target.contact_id
268 JOIN civicrm_option_value ov1
269 ON activity.activity_type_id = ov1.value AND ov1.option_group_id = 2
270 JOIN civicrm_option_value ov2
271 ON activity.status_id = ov2.value AND ov2.option_group_id = {$this->_groupId}
272 LEFT JOIN civicrm_activity_contact sourceContact
273 ON activity.id = sourceContact.activity_id AND sourceContact.record_type_id = {$sourceID}
274 JOIN civicrm_contact contact_b
275 ON sourceContact.contact_id = contact_b.id
276 LEFT JOIN civicrm_case_activity cca
277 ON activity.id = cca.activity_id
278 LEFT JOIN civicrm_activity_contact assignment
279 ON activity.id = assignment.activity_id AND assignment.record_type_id = {$assigneeID}
280 LEFT JOIN civicrm_contact contact_c
281 ON assignment.contact_id = contact_c.id ";
282 }
283
284 /**
285 * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values.
286 *
287 * @param bool $includeContactIDs
288 *
289 * @return string
290 */
291 public function where($includeContactIDs = FALSE) {
292 $clauses = array();
293
294 // add contact name search; search on primary name, source contact, assignee
295 $contactname = $this->_formValues['sort_name'];
296 if (!empty($contactname)) {
297 $dao = new CRM_Core_DAO();
298 $contactname = $dao->escape($contactname);
299 $clauses[] = "(contact_a.sort_name LIKE '%{$contactname}%' OR
300 contact_b.sort_name LIKE '%{$contactname}%' OR
301 contact_c.display_name LIKE '%{$contactname}%')";
302 }
303
304 $subject = $this->_formValues['activity_subject'];
305
306 if (!empty($this->_formValues['contact_type'])) {
307 $clauses[] = "contact_a.contact_type LIKE '%{$this->_formValues['contact_type']}%'";
308 }
309
310 if (!empty($subject)) {
311 $dao = new CRM_Core_DAO();
312 $subject = $dao->escape($subject);
313 $clauses[] = "activity.subject LIKE '%{$subject}%'";
314 }
315
316 if (!empty($this->_formValues['activity_status_id'])) {
317 $clauses[] = "activity.status_id = {$this->_formValues['activity_status_id']}";
318 }
319
320 if (!empty($this->_formValues['activity_type_id'])) {
321 $clauses[] = "activity.activity_type_id = {$this->_formValues['activity_type_id']}";
322 }
323
324 $startDate = $this->_formValues['start_date'];
325 if (!empty($startDate)) {
326 $startDate .= '00:00:00';
327 $startDateFormatted = CRM_Utils_Date::processDate($startDate);
328 if ($startDateFormatted) {
329 $clauses[] = "activity.activity_date_time >= $startDateFormatted";
330 }
331 }
332
333 $endDate = $this->_formValues['end_date'];
334 if (!empty($endDate)) {
335 $endDate .= '23:59:59';
336 $endDateFormatted = CRM_Utils_Date::processDate($endDate);
337 if ($endDateFormatted) {
338 $clauses[] = "activity.activity_date_time <= $endDateFormatted";
339 }
340 }
341
342 if ($includeContactIDs) {
343 $contactIDs = array();
344 foreach ($this->_formValues as $id => $value) {
345 if ($value &&
346 substr($id, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX
347 ) {
348 $contactIDs[] = substr($id, CRM_Core_Form::CB_PREFIX_LEN);
349 }
350 }
351
352 if (!empty($contactIDs)) {
353 $contactIDs = implode(', ', $contactIDs);
354 $clauses[] = "contact_a.id IN ( $contactIDs )";
355 }
356 }
357
358 return implode(' AND ', $clauses);
359 }
360
361 /*
362 * Functions below generally don't need to be modified
363 */
364
365 /**
366 * @inheritDoc
367 */
368 public function count() {
369 $sql = $this->all();
370
371 $dao = CRM_Core_DAO::executeQuery($sql,
372 CRM_Core_DAO::$_nullArray
373 );
374 return $dao->N;
375 }
376
377 /**
378 * @param int $offset
379 * @param int $rowcount
380 * @param null $sort
381 *
382 * @return string
383 */
384 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL) {
385 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
386 }
387
388 /**
389 * @return array
390 */
391 public function &columns() {
392 return $this->_columns;
393 }
394
395 /**
396 * @param $title
397 */
398 public function setTitle($title) {
399 if ($title) {
400 CRM_Utils_System::setTitle($title);
401 }
402 else {
403 CRM_Utils_System::setTitle(ts('Search'));
404 }
405 }
406
407 /**
408 * @return null
409 */
410 public function summary() {
411 return NULL;
412 }
413
414 }