3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
33 class CRM_Contact_Form_Search_Custom_ActivitySearch
extends CRM_Contact_Form_Search_Custom_Base
implements CRM_Contact_Form_Search_Interface
{
35 protected $_formValues;
36 protected $_aclFrom = NULL;
37 protected $_aclWhere = NULL;
42 * @param array $formValues
44 public function __construct(&$formValues) {
45 $this->_formValues
= $formValues;
48 * Define the columns for search result rows
50 $this->_columns
= array(
51 ts('Name') => 'sort_name',
52 ts('Status') => 'activity_status',
53 ts('Activity Type') => 'activity_type',
54 ts('Activity Subject') => 'activity_subject',
55 ts('Scheduled By') => 'source_contact',
56 ts('Scheduled Date') => 'activity_date',
58 ' ' => 'activity_type_id',
60 ts('Location') => 'location',
61 ts('Duration') => 'duration',
62 ts('Details') => 'details',
63 ts('Assignee') => 'assignee',
66 $this->_groupId
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup',
72 //Add custom fields to columns array for inclusion in export
73 $groupTree = &CRM_Core_BAO_CustomGroup
::getTree('Activity', $form, NULL,
77 //use simplified formatted groupTree
78 $groupTree = CRM_Core_BAO_CustomGroup
::formatGroupTree($groupTree, 1, $form);
80 //cycle through custom fields and assign to _columns array
81 foreach ($groupTree as $key) {
82 foreach ($key['fields'] as $field) {
83 $fieldlabel = $key['title'] . ": " . $field['label'];
84 $this->_columns
[$fieldlabel] = $field['column_name'];
91 * @param CRM_Core_Form $form
93 public function buildForm(&$form) {
96 * You can define a custom title for the search form
98 $this->setTitle('Find Latest Activities');
101 * Define the search form fields here
103 // Allow user to choose which type of contact to limit search on
104 $form->add('select', 'contact_type', ts('Find...'), CRM_Core_SelectValues
::contactType());
106 // Text box for Activity Subject
109 ts('Activity Subject')
112 // Select box for Activity Type
113 $activityType = array('' => ' - select activity - ') + CRM_Core_PseudoConstant
::activityType();
115 $form->add('select', 'activity_type_id', ts('Activity Type'),
120 // textbox for Activity Status
121 $activityStatus = array('' => ' - select status - ') + CRM_Core_PseudoConstant
::activityStatus();
123 $form->add('select', 'activity_status_id', ts('Activity Status'),
128 // Activity Date range
129 $form->addDate('start_date', ts('Activity Date From'), FALSE, array('formatType' => 'custom'));
130 $form->addDate('end_date', ts('...through'), FALSE, array('formatType' => 'custom'));
132 // Contact Name field
133 $form->add('text', 'sort_name', ts('Contact Name'));
136 * If you are using the sample template, this array tells the template fields to render
137 * for the search form.
139 $form->assign('elements', array(
143 'activity_status_id',
151 * Define the smarty template used to layout the search form and results listings.
153 public function templateFile() {
154 return 'CRM/Contact/Form/Search/Custom/ActivitySearch.tpl';
158 * Construct the search query.
161 $offset = 0, $rowcount = 0, $sort = NULL,
162 $includeContactIDs = FALSE, $justIDs = FALSE
165 // SELECT clause must include contact_id as an alias for civicrm_contact.id
167 $select = 'contact_a.id as contact_id';
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
190 $from = $this->from();
192 $where = $this->where($includeContactIDs);
194 if (!empty($where)) {
195 $where = "WHERE $where";
198 // add custom group fields to SELECT and FROM clause
199 $groupTree = CRM_Core_BAO_CustomGroup
::getTree('Activity', $form, NULL, NULL, '', NULL);
201 foreach ($groupTree as $key) {
202 if (!empty($key['extends']) && $key['extends'] == 'Activity') {
203 $select .= ", " . $key['table_name'] . ".*";
204 $from .= " LEFT JOIN " . $key['table_name'] . " ON " . $key['table_name'] . ".entity_id = activity.id";
207 // end custom groups add
209 $sql = " SELECT $select FROM $from $where ";
211 //no need to add order when only contact Ids.
213 // Define ORDER BY for query in $sort, with default value
215 if (is_string($sort)) {
216 $sort = CRM_Utils_Type
::escape($sort, 'String');
217 $sql .= " ORDER BY $sort ";
220 $sql .= ' ORDER BY ' . trim($sort->orderBy());
224 $sql .= 'ORDER BY contact_a.sort_name, activity.activity_date_time DESC, activity.activity_type_id, activity.status_id, activity.subject';
228 //CRM-14107, since there could be multiple activities against same contact,
229 //we need to provide GROUP BY on contact id to prevent duplicacy on prev/next entries
230 $sql .= 'GROUP BY contact_a.id
231 ORDER BY contact_a.sort_name';
234 if ($rowcount > 0 && $offset >= 0) {
235 $offset = CRM_Utils_Type
::escape($offset, 'Int');
236 $rowcount = CRM_Utils_Type
::escape($rowcount, 'Int');
237 $sql .= " LIMIT $offset, $rowcount ";
243 * Alters the date display in the Activity Date Column. We do this after we already have
244 * the result so that sorting on the date column stays pertinent to the numeric date value
247 public function alterRow(&$row) {
248 $row['activity_date'] = CRM_Utils_Date
::customFormat($row['activity_date'], '%B %E%f, %Y %l:%M %P');
252 * Regular JOIN statements here to limit results to contacts who have activities.
255 public function from() {
256 $this->buildACLClause('contact_a');
257 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
258 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
259 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
260 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
263 civicrm_activity activity
264 LEFT JOIN civicrm_activity_contact target
265 ON activity.id = target.activity_id AND target.record_type_id = {$targetID}
266 JOIN civicrm_contact contact_a
267 ON contact_a.id = target.contact_id
268 JOIN civicrm_option_value ov1
269 ON activity.activity_type_id = ov1.value AND ov1.option_group_id = 2
270 JOIN civicrm_option_value ov2
271 ON activity.status_id = ov2.value AND ov2.option_group_id = {$this->_groupId}
272 LEFT JOIN civicrm_activity_contact sourceContact
273 ON activity.id = sourceContact.activity_id AND sourceContact.record_type_id = {$sourceID}
274 JOIN civicrm_contact contact_b
275 ON sourceContact.contact_id = contact_b.id
276 LEFT JOIN civicrm_case_activity cca
277 ON activity.id = cca.activity_id
278 LEFT JOIN civicrm_activity_contact assignment
279 ON activity.id = assignment.activity_id AND assignment.record_type_id = {$assigneeID}
280 LEFT JOIN civicrm_contact contact_c
281 ON assignment.contact_id = contact_c.id {$this->_aclFrom}";
287 * WHERE clause is an array built from any required JOINS plus conditional filters based on search criteria field values.
289 * @param bool $includeContactIDs
293 public function where($includeContactIDs = FALSE) {
296 // add contact name search; search on primary name, source contact, assignee
297 $contactname = $this->_formValues
['sort_name'];
298 if (!empty($contactname)) {
299 $dao = new CRM_Core_DAO();
300 $contactname = $dao->escape($contactname);
301 $clauses[] = "(contact_a.sort_name LIKE '%{$contactname}%' OR
302 contact_b.sort_name LIKE '%{$contactname}%' OR
303 contact_c.display_name LIKE '%{$contactname}%')";
306 $subject = $this->_formValues
['activity_subject'];
308 if (!empty($this->_formValues
['contact_type'])) {
309 $clauses[] = "contact_a.contact_type LIKE '%{$this->_formValues['contact_type']}%'";
312 if (!empty($subject)) {
313 $dao = new CRM_Core_DAO();
314 $subject = $dao->escape($subject);
315 $clauses[] = "activity.subject LIKE '%{$subject}%'";
318 if (!empty($this->_formValues
['activity_status_id'])) {
319 $clauses[] = "activity.status_id = {$this->_formValues['activity_status_id']}";
322 if (!empty($this->_formValues
['activity_type_id'])) {
323 $clauses[] = "activity.activity_type_id = {$this->_formValues['activity_type_id']}";
326 $startDate = $this->_formValues
['start_date'];
327 if (!empty($startDate)) {
328 $startDate .= '00:00:00';
329 $startDateFormatted = CRM_Utils_Date
::processDate($startDate);
330 if ($startDateFormatted) {
331 $clauses[] = "activity.activity_date_time >= $startDateFormatted";
335 $endDate = $this->_formValues
['end_date'];
336 if (!empty($endDate)) {
337 $endDate .= '23:59:59';
338 $endDateFormatted = CRM_Utils_Date
::processDate($endDate);
339 if ($endDateFormatted) {
340 $clauses[] = "activity.activity_date_time <= $endDateFormatted";
344 if ($includeContactIDs) {
345 $contactIDs = array();
346 foreach ($this->_formValues
as $id => $value) {
348 substr($id, 0, CRM_Core_Form
::CB_PREFIX_LEN
) == CRM_Core_Form
::CB_PREFIX
350 $contactIDs[] = substr($id, CRM_Core_Form
::CB_PREFIX_LEN
);
354 if (!empty($contactIDs)) {
355 $contactIDs = implode(', ', $contactIDs);
356 $clauses[] = "contact_a.id IN ( $contactIDs )";
360 if ($this->_aclWhere
) {
361 $clauses[] = " {$this->_aclWhere} ";
363 return implode(' AND ', $clauses);
367 * Functions below generally don't need to be modified
373 public function count() {
376 $dao = CRM_Core_DAO
::executeQuery($sql,
377 CRM_Core_DAO
::$_nullArray
384 * @param int $rowcount
386 * @param bool $returnSQL Not used; included for consistency with parent; SQL is always returned
390 public function contactIDs($offset = 0, $rowcount = 0, $sort = NULL, $returnSQL = TRUE) {
391 return $this->all($offset, $rowcount, $sort, FALSE, TRUE);
397 public function &columns() {
398 return $this->_columns
;
404 public function setTitle($title) {
406 CRM_Utils_System
::setTitle($title);
409 CRM_Utils_System
::setTitle(ts('Search'));
416 public function summary() {
421 * @param string $tableAlias
423 public function buildACLClause($tableAlias = 'contact') {
424 list($this->_aclFrom
, $this->_aclWhere
) = CRM_Contact_BAO_Contact_Permission
::cacheClause($tableAlias);