3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Contact_Form_Search_Custom_ActivitySearch
implements CRM_Contact_Form_Search_Interface
{
37 protected $_formValues;
45 public function __construct(&$formValues) {
46 $this->_formValues
= $formValues;
49 * Define the columns for search result rows
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',
61 ts('Location') => 'location',
62 ts('Duration') => 'duration',
63 ts('Details') => 'details',
64 ts('Assignee') => 'assignee',
67 $this->_groupId
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
73 //Add custom fields to columns array for inclusion in export
74 $groupTree = &CRM_Core_BAO_CustomGroup
::getTree('Activity', $form, NULL,
78 //use simplified formatted groupTree
79 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, 1, $form);
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'];
92 * @param CRM_Core_Form $form
94 public function buildForm(&$form) {
97 * You can define a custom title for the search form
99 $this->setTitle('Find Latest Activities');
102 * Define the search form fields here
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());
107 // Text box for Activity Subject
110 ts('Activity Subject')
113 // Select box for Activity Type
114 $activityType = array('' => ' - select activity - ') + CRM_Core_PseudoConstant
::activityType();
116 $form->add('select', 'activity_type_id', ts('Activity Type'),
121 // textbox for Activity Status
122 $activityStatus = array('' => ' - select status - ') + CRM_Core_PseudoConstant
::activityStatus();
124 $form->add('select', 'activity_status_id', ts('Activity Status'),
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'));
133 // Contact Name field
134 $form->add('text', 'sort_name', ts('Contact Name'));
137 * If you are using the sample template, this array tells the template fields to render
138 * for the search form.
140 $form->assign('elements', array(
141 'contact_type', 'activity_subject', 'activity_type_id',
142 'activity_status_id', 'start_date', 'end_date', 'sort_name',
147 * Define the smarty template used to layout the search form and results listings.
149 public function templateFile() {
150 return 'CRM/Contact/Form/Search/Custom/ActivitySearch.tpl';
154 * Construct the search query
157 $offset = 0, $rowcount = 0, $sort = NULL,
158 $includeContactIDs = FALSE, $justIDs = FALSE
161 // SELECT clause must include contact_id as an alias for civicrm_contact.id
163 $select = 'contact_a.id as contact_id';
167 contact_a.id as contact_id,
168 contact_a.sort_name as sort_name,
169 contact_a.contact_type as contact_type,
170 activity.id as activity_id,
171 activity.activity_type_id as activity_type_id,
172 contact_b.sort_name as source_contact,
173 ov1.label as activity_type,
174 activity.subject as activity_subject,
175 activity.activity_date_time as activity_date,
176 ov2.label as activity_status,
177 cca.case_id as case_id,
178 activity.location as location,
179 activity.duration as duration,
180 activity.details as details,
181 assignment.activity_id as assignment_activity,
182 contact_c.display_name as assignee
186 $from = $this->from();
188 $where = $this->where($includeContactIDs);
190 if (!empty($where)) {
191 $where = "WHERE $where";
194 // add custom group fields to SELECT and FROM clause
195 $groupTree = CRM_Core_BAO_CustomGroup
::getTree('Activity', $form, NULL, NULL, '', NULL);
197 foreach ($groupTree as $key) {
198 if (!empty($key['extends']) && $key['extends'] == 'Activity') {
199 $select .= ", " . $key['table_name'] . ".*";
200 $from .= " LEFT JOIN " . $key['table_name'] . " ON " . $key['table_name'] . ".entity_id = activity.id";
203 // end custom groups add
205 $sql = " SELECT $select FROM $from $where ";
207 //no need to add order when only contact Ids.
209 // Define ORDER BY for query in $sort, with default value
211 if (is_string($sort)) {
212 $sort = CRM_Utils_Type
::escape($sort, 'String');
213 $sql .= " ORDER BY $sort ";
216 $sql .= ' ORDER BY ' . trim($sort->orderBy());
220 $sql .= 'ORDER BY contact_a.sort_name, activity.activity_date_time DESC, activity.activity_type_id, activity.status_id, activity.subject';
224 //CRM-14107, since there could be multiple activities against same contact,
225 //we need to provide GROUP BY on contact id to prevent duplicacy on prev/next entries
226 $sql .= 'GROUP BY contact_a.id
227 ORDER BY contact_a.sort_name';
230 if ($rowcount > 0 && $offset >= 0) {
231 $offset = CRM_Utils_Type
::escape($offset, 'Int');
232 $rowcount = CRM_Utils_Type
::escape($rowcount, 'Int');
233 $sql .= " LIMIT $offset, $rowcount ";
238 // Alters the date display in the Activity Date Column. We do this after we already have
239 // the result so that sorting on the date column stays pertinent to the numeric date value
243 public function alterRow(&$row) {
244 $row['activity_date'] = CRM_Utils_Date
::customFormat($row['activity_date'], '%B %E%f, %Y %l:%M %P');
247 // Regular JOIN statements here to limit results to contacts who have activities.
251 public function from() {
252 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
253 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
254 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
255 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
258 civicrm_activity activity
259 LEFT JOIN civicrm_activity_contact target
260 ON activity.id = target.activity_id AND target.record_type_id = {$targetID}
261 JOIN civicrm_contact contact_a
262 ON contact_a.id = target.contact_id
263 JOIN civicrm_option_value ov1
264 ON activity.activity_type_id = ov1.value AND ov1.option_group_id = 2
265 JOIN civicrm_option_value ov2
266 ON activity.status_id = ov2.value AND ov2.option_group_id = {$this->_groupId}
267 LEFT JOIN civicrm_activity_contact sourceContact
268 ON activity.id = sourceContact.activity_id AND sourceContact.record_type_id = {$sourceID}
269 JOIN civicrm_contact contact_b
270 ON sourceContact.contact_id = contact_b.id
271 LEFT JOIN civicrm_case_activity cca
272 ON activity.id = cca.activity_id
273 LEFT JOIN civicrm_activity_contact assignment
274 ON activity.id = assignment.activity_id AND assignment.record_type_id = {$assigneeID}
275 LEFT JOIN civicrm_contact contact_c
276 ON assignment.contact_id = contact_c.id ";
280 * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values
282 * @param bool $includeContactIDs
286 public function where($includeContactIDs = FALSE) {
289 // add contact name search; search on primary name, source contact, assignee
290 $contactname = $this->_formValues
['sort_name'];
291 if (!empty($contactname)) {
292 $dao = new CRM_Core_DAO();
293 $contactname = $dao->escape($contactname);
294 $clauses[] = "(contact_a.sort_name LIKE '%{$contactname}%' OR
295 contact_b.sort_name LIKE '%{$contactname}%' OR
296 contact_c.display_name LIKE '%{$contactname}%')";
299 $subject = $this->_formValues
['activity_subject'];
301 if (!empty($this->_formValues
['contact_type'])) {
302 $clauses[] = "contact_a.contact_type LIKE '%{$this->_formValues['contact_type']}%'";
305 if (!empty($subject)) {
306 $dao = new CRM_Core_DAO();
307 $subject = $dao->escape($subject);
308 $clauses[] = "activity.subject LIKE '%{$subject}%'";
311 if (!empty($this->_formValues
['activity_status_id'])) {
312 $clauses[] = "activity.status_id = {$this->_formValues['activity_status_id']}";
315 if (!empty($this->_formValues
['activity_type_id'])) {
316 $clauses[] = "activity.activity_type_id = {$this->_formValues['activity_type_id']}";
319 $startDate = $this->_formValues
['start_date'];
320 if (!empty($startDate)) {
321 $startDate .= '00:00:00';
322 $startDateFormatted = CRM_Utils_Date
::processDate($startDate);
323 if ($startDateFormatted) {
324 $clauses[] = "activity.activity_date_time >= $startDateFormatted";
328 $endDate = $this->_formValues
['end_date'];
329 if (!empty($endDate)) {
330 $endDate .= '23:59:59';
331 $endDateFormatted = CRM_Utils_Date
::processDate($endDate);
332 if ($endDateFormatted) {
333 $clauses[] = "activity.activity_date_time <= $endDateFormatted";
337 if ($includeContactIDs) {
338 $contactIDs = array();
339 foreach ($this->_formValues
as $id => $value) {
341 substr($id, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
343 $contactIDs[] = substr($id, CRM_Core_Form
::CB_PREFIX_LEN
);
347 if (!empty($contactIDs)) {
348 $contactIDs = implode(', ', $contactIDs);
349 $clauses[] = "contact_a.id IN ( $contactIDs )";
353 return implode(' AND ', $clauses);
357 * Functions below generally don't need to be modified
360 public function count() {
363 $dao = CRM_Core_DAO
::executeQuery($sql,
364 CRM_Core_DAO
::$_nullArray
371 * @param int $rowcount
376 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL) {
377 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
383 public function &columns() {
384 return $this->_columns
;
390 public function setTitle($title) {
392 CRM_Utils_System
::setTitle($title);
395 CRM_Utils_System
::setTitle(ts('Search'));
402 public function summary() {