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;
41 function __construct(&$formValues) {
42 $this->_formValues
= $formValues;
45 * Define the columns for search result rows
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',
57 ts('Location') => 'location',
58 ts('Duration') => 'duration',
59 ts('Details') => 'details',
60 ts('Assignee') => 'assignee',
63 $this->_groupId
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
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,
76 //use simplified formatted groupTree
77 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, 1, $form);
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'];
89 function buildForm(&$form) {
92 * You can define a custom title for the search form
94 $this->setTitle('Find Latest Activities');
97 * Define the search form fields here
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());
102 // Text box for Activity Subject
105 ts('Activity Subject')
108 // Select box for Activity Type
109 $activityType = array('' => ' - select activity - ') + CRM_Core_PseudoConstant
::activityType();
111 $form->add('select', 'activity_type_id', ts('Activity Type'),
116 // textbox for Activity Status
117 $activityStatus = array('' => ' - select status - ') + CRM_Core_PseudoConstant
::activityStatus();
119 $form->add('select', 'activity_status_id', ts('Activity Status'),
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'));
129 // Contact Name field
130 $form->add('text', 'sort_name', ts('Contact Name'));
133 * If you are using the sample template, this array tells the template fields to render
134 * for the search form.
136 $form->assign('elements', array('contact_type', 'activity_subject', 'activity_type_id',
137 'activity_status_id', 'start_date', 'end_date', 'sort_name',
142 * Define the smarty template used to layout the search form and results listings.
144 function templateFile() {
145 return 'ActivitySearch.tpl';
149 * Construct the search query
151 function all($offset = 0, $rowcount = 0, $sort = NULL,
152 $includeContactIDs = FALSE, $onlyIDs = FALSE
155 // SELECT clause must include contact_id as an alias for civicrm_contact.id
157 $select = 'contact_a.id as contact_id';
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
180 $from = $this->from();
182 $where = $this->where($includeContactIDs);
184 if (!empty($where)) {
185 $where = "WHERE $where";
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);
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";
198 // end custom groups add
200 $sql = " SELECT $select FROM $from $where ";
202 //no need to add order when only contact Ids.
204 // Define ORDER BY for query in $sort, with default value
206 if (is_string($sort)) {
207 $sql .= " ORDER BY $sort ";
210 $sql .= ' ORDER BY ' . trim($sort->orderBy());
214 $sql .= 'ORDER BY contact_a.sort_name, activity.activity_date_time DESC, activity.activity_type_id, activity.status_id, activity.subject';
218 if ($rowcount > 0 && $offset >= 0) {
219 $sql .= " LIMIT $offset, $rowcount ";
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');
230 // Regular JOIN statements here to limit results to contacts who have activities.
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 ";
251 * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values
254 function where($includeContactIDs = FALSE) {
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}%')";
267 $subject = $this->_formValues
['activity_subject'];
269 if (!empty($this->_formValues
['contact_type'])) {
270 $clauses[] = "contact_a.contact_type LIKE '%{$this->_formValues['contact_type']}%'";
273 if (!empty($subject)) {
274 $dao = new CRM_Core_DAO();
275 $subject = $dao->escape($subject);
276 $clauses[] = "activity.subject LIKE '%{$subject}%'";
279 if (!empty($this->_formValues
['activity_status_id'])) {
280 $clauses[] = "activity.status_id = {$this->_formValues['activity_status_id']}";
283 if (!empty($this->_formValues
['activity_type_id'])) {
284 $clauses[] = "activity.activity_type_id = {$this->_formValues['activity_type_id']}";
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";
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";
305 if ($includeContactIDs) {
306 $contactIDs = array();
307 foreach ($this->_formValues
as $id => $value) {
309 substr($id, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
311 $contactIDs[] = substr($id, CRM_Core_Form
::CB_PREFIX_LEN
);
315 if (!empty($contactIDs)) {
316 $contactIDs = implode(', ', $contactIDs);
317 $clauses[] = "contact_a.id IN ( $contactIDs )";
321 return implode(' AND ', $clauses);
325 * Functions below generally don't need to be modified
330 $dao = CRM_Core_DAO
::executeQuery($sql,
331 CRM_Core_DAO
::$_nullArray
336 function contactIDs($offset = 0, $rowcount = 0, $sort = NULL) {
337 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
340 function &columns() {
341 return $this->_columns
;
344 function setTitle($title) {
346 CRM_Utils_System
::setTitle($title);
349 CRM_Utils_System
::setTitle(ts('Search'));