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