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