3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.1 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2011 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2011
36 require_once 'CRM/Contact/Form/Search/Interface.php';
37 class org_civicrm_search_activityimplementsCRM_Contact_Form_Search_Interface
{
39 protected $_formValues; function __construct(&$formValues) {
40 $this->_formValues
= $formValues;
43 * Define the columns for search result rows
45 $this->_columns
= array(
46 ts('Name') => 'sort_name',
47 ts('Status') => 'activity_status',
48 ts('Activity Type') => 'activity_type',
49 ts('Activity Subject') => 'activity_subject',
50 ts('Scheduled By') => 'source_contact',
51 ts('Scheduled Date') => 'activity_date',
52 ts(' ') => 'activity_id',
53 ts(' ') => 'activity_type_id',
55 ts('Location') => 'location',
56 ts('Duration') => 'duration',
57 ts('Details') => 'details',
58 ts('Assignee') => 'assignee',
61 $this->_groupId
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
67 //Add custom fields to columns array for inclusion in export
68 require_once 'CRM/Core/BAO/CustomGroup.php';
69 $groupTree = &CRM_Core_BAO_CustomGroup
::getTree('Activity', $form, NULL,
74 //use simplified formatted groupTree
75 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, 1, $form);
77 //cycle through custom fields and assign to _columns array
78 foreach ($groupTree as $key) {
79 foreach ($key['fields'] as $field) {
80 $fieldlabel = $key['title'] . ": " . $field['label'];
81 $this->_columns
[$fieldlabel] = $field['column_name'];
87 function buildForm(&$form) {
90 * You can define a custom title for the search form
92 $this->setTitle('Find Latest Activities');
95 * Define the search form fields here
97 // Allow user to choose which type of contact to limit search on
98 $form->add('select', 'contact_type', ts('Find...'), CRM_Core_SelectValues
::contactType());
100 // Text box for Activity Subject
103 ts('Activity Subject')
106 // Select box for Activity Type
107 $activityType = array('' => ' - select activity - ') + CRM_Core_PseudoConstant
::activityType();
109 $form->add('select', 'activity_type_id', ts('Activity Type'),
114 // textbox for Activity Status
115 $activityStatus = array('' => ' - select status - ') + CRM_Core_PseudoConstant
::activityStatus();
117 $form->add('select', 'activity_status_id', ts('Activity Status'),
122 // Activity Date range
123 $form->addDate('start_date', ts('Activity Date From'), FALSE, array('formatType' => 'custom'));
124 $form->addDate('end_date', ts('...through'), FALSE, array('formatType' => 'custom'));
127 // Contact Name field
128 $form->add('text', 'sort_name', ts('Contact Name'));
131 * If you are using the sample template, this array tells the template fields to render
132 * for the search form.
134 $form->assign('elements', array('contact_type', 'activity_subject', 'activity_type_id',
135 'activity_status_id', 'start_date', 'end_date', 'sort_name',
140 * Define the smarty template used to layout the search form and results listings.
142 function templateFile() {
143 return 'ActivitySearch.tpl';
147 * Construct the search query
149 function all($offset = 0, $rowcount = 0, $sort = NULL,
150 $includeContactIDs = FALSE, $onlyIDs = FALSE
153 // SELECT clause must include contact_id as an alias for civicrm_contact.id
155 $select = 'contact_a.id as contact_id';
159 contact_a.id as contact_id,
160 contact_a.sort_name as sort_name,
161 contact_a.contact_type as contact_type,
162 activity.id as activity_id,
163 activity.activity_type_id as activity_type_id,
164 contact_b.sort_name as source_contact,
165 ov1.label as activity_type,
166 activity.subject as activity_subject,
167 activity.activity_date_time as activity_date,
168 ov2.label as activity_status,
169 cca.case_id as case_id,
170 activity.location as location,
171 activity.duration as duration,
172 activity.details as details,
173 assignment.activity_id as assignment_activity,
174 contact_c.display_name as assignee
178 $from = $this->from();
180 $where = $this->where($includeContactIDs);
182 if (!empty($where)) {
183 $where = "WHERE $where";
186 // add custom group fields to SELECT and FROM clause
187 require_once 'CRM/Core/BAO/CustomGroup.php';
188 $groupTree = &CRM_Core_BAO_CustomGroup
::getTree('Activity', $form, NULL, NULL, '', NULL);
190 foreach ($groupTree as $key) {
191 if ($key['extends'] == 'Activity') {
192 $select .= ", " . $key['table_name'] . ".*";
193 $from .= " LEFT JOIN " . $key['table_name'] . " ON " . $key['table_name'] . ".entity_id = activity.id";
196 // end custom groups add
198 $sql = " SELECT $select FROM $from $where ";
200 //no need to add order when only contact Ids.
202 // Define ORDER BY for query in $sort, with default value
204 if (is_string($sort)) {
205 $sql .= " ORDER BY $sort ";
208 $sql .= ' ORDER BY ' . trim($sort->orderBy());
212 $sql .= 'ORDER BY contact_a.sort_name, activity.activity_date_time DESC, activity.activity_type_id, activity.status_id, activity.subject';
216 if ($rowcount > 0 && $offset >= 0) {
217 $sql .= " LIMIT $offset, $rowcount ";
222 // Alters the date display in the Activity Date Column. We do this after we already have
223 // the result so that sorting on the date column stays pertinent to the numeric date value
224 function alterRow(&$row) {
225 $row['activity_date'] = CRM_Utils_Date
::customFormat($row['activity_date'], '%B %E%f, %Y %l:%M %P');
228 // Regular JOIN statements here to limit results to contacts who have activities.
231 civicrm_contact contact_a
232 JOIN civicrm_activity activity
233 ON contact_a.id = activity.source_contact_id
234 JOIN civicrm_option_value ov1
235 ON activity.activity_type_id = ov1.value AND ov1.option_group_id = 2
236 JOIN civicrm_option_value ov2
237 ON activity.status_id = ov2.value AND ov2.option_group_id = {$this->_groupId}
238 JOIN civicrm_contact contact_b
239 ON activity.source_contact_id = contact_b.id
240 LEFT JOIN civicrm_case_activity cca
241 ON activity.id = cca.activity_id
242 LEFT JOIN civicrm_activity_assignment assignment
243 ON activity.id = assignment.activity_id
244 LEFT JOIN civicrm_contact contact_c
245 ON assignment.assignee_contact_id = contact_c.id ";
249 * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values
252 function where($includeContactIDs = FALSE) {
255 // add contact name search; search on primary name, source contact, assignee
256 $contactname = $this->_formValues
['sort_name'];
257 if (!empty($contactname)) {
258 $dao = new CRM_Core_DAO();
259 $contactname = $dao->escape($contactname);
260 $clauses[] = "(contact_a.sort_name LIKE '%{$contactname}%' OR
261 contact_b.sort_name LIKE '%{$contactname}%' OR
262 contact_c.display_name LIKE '%{$contactname}%')";
265 $subject = $this->_formValues
['activity_subject'];
267 if (!empty($this->_formValues
['contact_type'])) {
268 $clauses[] = "contact_a.contact_type LIKE '%{$this->_formValues['contact_type']}%'";
271 if (!empty($subject)) {
272 $dao = new CRM_Core_DAO();
273 $subject = $dao->escape($subject);
274 $clauses[] = "activity.subject LIKE '%{$subject}%'";
277 if (!empty($this->_formValues
['activity_status_id'])) {
278 $clauses[] = "activity.status_id = {$this->_formValues['activity_status_id']}";
281 if (!empty($this->_formValues
['activity_type_id'])) {
282 $clauses[] = "activity.activity_type_id = {$this->_formValues['activity_type_id']}";
285 $startDate = $this->_formValues
['start_date'];
286 if (!empty($startDate)) {
287 $startDate .= '00:00:00';
288 $startDateFormatted = CRM_Utils_Date
::processDate($startDate);
289 if ($startDateFormatted) {
290 $clauses[] = "activity.activity_date_time >= $startDateFormatted";
294 $endDate = $this->_formValues
['end_date'];
295 if (!empty($endDate)) {
296 $endDate .= '23:59:59';
297 $endDateFormatted = CRM_Utils_Date
::processDate($endDate);
298 if ($endDateFormatted) {
299 $clauses[] = "activity.activity_date_time <= $endDateFormatted";
303 if ($includeContactIDs) {
304 $contactIDs = array();
305 foreach ($this->_formValues
as $id => $value) {
307 substr($id, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
309 $contactIDs[] = substr($id, CRM_Core_Form
::CB_PREFIX_LEN
);
313 if (!empty($contactIDs)) {
314 $contactIDs = implode(', ', $contactIDs);
315 $clauses[] = "contact_a.id IN ( $contactIDs )";
319 return implode(' AND ', $clauses);
323 * Functions below generally don't need to be modified
328 $dao = CRM_Core_DAO
::executeQuery($sql,
329 CRM_Core_DAO
::$_nullArray
334 function contactIDs($offset = 0, $rowcount = 0, $sort = NULL) {
335 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
338 function &columns() {
339 return $this->_columns
;
342 function setTitle($title) {
344 CRM_Utils_System
::setTitle($title);
347 CRM_Utils_System
::setTitle(ts('Search'));