4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Activity_BAO_Query
{
39 * build select for Case
46 static function select(&$query) {
47 if (!empty($query->_returnProperties
['activity_id'])) {
48 $query->_select
['activity_id'] = "civicrm_activity.id as activity_id";
49 $query->_element
['activity_id'] = 1;
50 $query->_tables
['civicrm_activity'] = $query->_whereTables
['civicrm_activity'] = 1;
53 if (!empty($query->_returnProperties
['activity_type_id'])) {
54 $query->_select
['activity_type_id'] = "activity_type.id as activity_type_id";
55 $query->_element
['activity_type_id'] = 1;
56 $query->_tables
['civicrm_activity'] = 1;
57 $query->_tables
['activity_type'] = 1;
58 $query->_whereTables
['civicrm_activity'] = 1;
59 $query->_whereTables
['activity_type'] = 1;
62 if (!empty($query->_returnProperties
['activity_type'])) {
63 $query->_select
['activity_type'] = "activity_type.label as activity_type,
64 civicrm_activity.activity_type_id as activity_type_id";
65 $query->_element
['activity_type'] = 1;
66 $query->_tables
['civicrm_activity'] = 1;
67 $query->_tables
['activity_type'] = 1;
68 $query->_whereTables
['civicrm_activity'] = 1;
69 $query->_whereTables
['activity_type'] = 1;
72 if (!empty($query->_returnProperties
['activity_subject'])) {
73 $query->_select
['activity_subject'] = "civicrm_activity.subject as activity_subject";
74 $query->_element
['activity_subject'] = 1;
75 $query->_tables
['civicrm_activity'] = $query->_whereTables
['civicrm_activity'] = 1;
78 if (!empty($query->_returnProperties
['activity_date_time'])) {
79 $query->_select
['activity_date_time'] = "civicrm_activity.activity_date_time as activity_date_time";
80 $query->_element
['activity_date_time'] = 1;
81 $query->_tables
['civicrm_activity'] = $query->_whereTables
['civicrm_activity'] = 1;
84 if (!empty($query->_returnProperties
['activity_status_id'])) {
85 $query->_select
['activity_status_id'] = "activity_status.value as activity_status_id";
86 $query->_element
['activity_status_id'] = 1;
87 $query->_tables
['civicrm_activity'] = 1;
88 $query->_tables
['activity_status'] = 1;
89 $query->_whereTables
['civicrm_activity'] = 1;
90 $query->_whereTables
['activity_status'] = 1;
93 if (!empty($query->_returnProperties
['activity_status'])) {
94 $query->_select
['activity_status'] = "activity_status.label as activity_status";
95 $query->_element
['activity_status'] = 1;
96 $query->_tables
['civicrm_activity'] = 1;
97 $query->_tables
['activity_status'] = 1;
98 $query->_whereTables
['civicrm_activity'] = 1;
99 $query->_whereTables
['activity_status'] = 1;
102 if (!empty($query->_returnProperties
['activity_duration'])) {
103 $query->_select
['activity_duration'] = "civicrm_activity.duration as activity_duration";
104 $query->_element
['activity_duration'] = 1;
105 $query->_tables
['civicrm_activity'] = $query->_whereTables
['civicrm_activity'] = 1;
108 if (!empty($query->_returnProperties
['activity_location'])) {
109 $query->_select
['activity_location'] = "civicrm_activity.location as activity_location";
110 $query->_element
['activity_location'] = 1;
111 $query->_tables
['civicrm_activity'] = $query->_whereTables
['civicrm_activity'] = 1;
114 if (!empty($query->_returnProperties
['activity_details'])) {
115 $query->_select
['activity_details'] = "civicrm_activity.details as activity_details";
116 $query->_element
['activity_details'] = 1;
117 $query->_tables
['civicrm_activity'] = $query->_whereTables
['civicrm_activity'] = 1;
120 if (!empty($query->_returnProperties
['source_record_id'])) {
121 $query->_select
['source_record_id'] = "civicrm_activity.source_record_id as source_record_id";
122 $query->_element
['source_record_id'] = 1;
123 $query->_tables
['civicrm_activity'] = $query->_whereTables
['civicrm_activity'] = 1;
126 if (!empty($query->_returnProperties
['activity_is_test'])) {
127 $query->_select
['activity_is_test'] = "civicrm_activity.is_test as activity_is_test";
128 $query->_element
['activity_is_test'] = 1;
129 $query->_tables
['civicrm_activity'] = $query->_whereTables
['civicrm_activity'] = 1;
132 if (!empty($query->_returnProperties
['activity_campaign_id'])) {
133 $query->_select
['activity_campaign_id'] = 'civicrm_activity.campaign_id as activity_campaign_id';
134 $query->_element
['activity_campaign_id'] = 1;
135 $query->_tables
['civicrm_activity'] = $query->_whereTables
['civicrm_activity'] = 1;
138 if (!empty($query->_returnProperties
['activity_engagement_level'])) {
139 $query->_select
['activity_engagement_level'] = 'civicrm_activity.engagement_level as activity_engagement_level';
140 $query->_element
['activity_engagement_level'] = 1;
141 $query->_tables
['civicrm_activity'] = $query->_whereTables
['civicrm_activity'] = 1;
144 if (!empty($query->_returnProperties
['source_contact'])) {
145 $query->_select
['source_contact'] = 'source_contact.sort_name as source_contact';
146 $query->_element
['source_contact'] = 1;
147 $query->_tables
['source_contact'] = $query->_whereTables
['source_contact'] = 1;
150 if (!empty($query->_returnProperties
['activity_result'])) {
151 $query->_select
['activity_result'] = 'civicrm_activity.result as activity_result';
152 $query->_element
['result'] = 1;
153 $query->_tables
['civicrm_activity'] = $query->_whereTables
['civicrm_activity'] = 1;
158 * Given a list of conditions in query generate the required
166 static function where(&$query) {
168 foreach (array_keys($query->_params
) as $id) {
169 if (substr($query->_params
[$id][0], 0, 9) == 'activity_') {
170 if ($query->_mode
== CRM_Contact_BAO_QUERY
::MODE_CONTACTS
) {
171 $query->_useDistinct
= TRUE;
173 $grouping = $query->_params
[$id][3];
174 self
::whereClauseSingle($query->_params
[$id], $query);
180 * where clause for a single field
187 static function whereClauseSingle(&$values, &$query) {
188 list($name, $op, $value, $grouping, $wildcard) = $values;
190 $strtolower = function_exists('mb_strtolower') ?
'mb_strtolower' : 'strtolower';
191 $query->_tables
['civicrm_activity'] = $query->_whereTables
['civicrm_activity'] = 1;
192 if ($query->_mode
& CRM_Contact_BAO_Query
::MODE_ACTIVITY
) {
193 $query->_skipDeleteClause
= TRUE;
197 case 'activity_type_id':
198 case 'activity_type':
199 $types = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
200 //get the component activity types.
201 $compActTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE, TRUE);
202 $activityTypeIds = self
::buildWhereAndQill($query, $value, $types, $op, $grouping, array('Activity Type', 'civicrm_activity.activity_type_id'));
204 foreach ($activityTypeIds as $activityTypeId) {
205 if (array_key_exists($activityTypeId, $compActTypes)) {
206 CRM_Contact_BAO_Query
::$_considerCompActivities = TRUE;
212 case 'activity_survey_id':
216 $value = CRM_Utils_Type
::escape($value, 'Integer');
217 $query->_where
[$grouping][] = " source_record_id = $value";
218 $query->_qill
[$grouping][] = ts('Survey') . ' - ' . CRM_Core_DAO
::getFieldValue('CRM_Campaign_DAO_Survey', $value, 'title');
221 case 'activity_engagement_level':
226 $value = CRM_Utils_Type
::escape($value, 'Integer');
227 $query->_where
[$grouping][] = " engagement_level = $value";
228 $query->_qill
[$grouping][] = ts('Engagement Index') . ' - ' . CRM_Core_OptionGroup
::getLabel('engagement_index', $value);
231 case 'activity_role':
232 CRM_Contact_BAO_Query
::$_activityRole = $values[2];
233 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
234 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
235 $assigneeID = CRM_Utils_Array
::key('Activity Assignees', $activityContacts);
236 $targetID = CRM_Utils_Array
::key('Activity Targets', $activityContacts);
239 $query->_tables
['civicrm_activity_contact'] = $query->_whereTables
['civicrm_activity_contact'] = 1;
240 if ($values[2] == 1) {
241 $query->_where
[$grouping][] = " civicrm_activity_contact.record_type_id = $sourceID";
242 $query->_qill
[$grouping][] = ts('Activity created by');
244 else if ($values[2] == 2) {
245 $query->_where
[$grouping][] = " civicrm_activity_contact.record_type_id = $assigneeID";
246 $query->_qill
[$grouping][] = ts('Activity assigned to');
248 else if ($values[2] == 3) {
249 $query->_where
[$grouping][] = " civicrm_activity_contact.record_type_id = $targetID";
250 $query->_qill
[$grouping][] = ts('Activity targeted to');
255 case 'activity_status':
256 $status = CRM_Core_PseudoConstant
::activityStatus();
258 $activityTypeIds = self
::buildWhereAndQill($query, $value, $status, $op, $grouping, array('Activity Status', 'civicrm_activity.status_id'));
261 case 'activity_subject':
263 $value = strtolower(CRM_Core_DAO
::escapeString($n));
265 if (strpos($value, '%') !== FALSE) {
266 // only add wild card if not there
270 $value = "'%$value%'";
277 $wc = ($op != 'LIKE') ?
"LOWER(civicrm_activity.subject)" : "civicrm_activity.subject";
278 $query->_where
[$grouping][] = " $wc $op $value";
279 $query->_qill
[$grouping][] = ts('Subject') . " $op - '$n'";
282 case 'activity_test':
283 // We dont want to include all tests for sql OR CRM-7827
284 if (!$value ||
$query->getOperator() != 'OR') {
285 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause("civicrm_activity.is_test", $op, $value, "Boolean");
287 $query->_qill
[$grouping][] = ts('Activity is a Test');
292 case 'activity_date':
293 case 'activity_date_low':
294 case 'activity_date_high':
295 $query->dateQueryBuilder($values,
296 'civicrm_activity', 'activity_date', 'activity_date_time', ts('Activity Date')
305 if (is_array($value)) {
306 foreach ($value as $k => $v) {
307 $value[$k] = CRM_Utils_Type
::escape($v, 'Integer');
311 $value = array(CRM_Utils_Type
::escape($value, 'Integer'));
313 $query->_where
[$grouping][] = "civicrm_activity.id IN (" . implode(",", $value) . ")";
314 $query->_qill
[$grouping][] = ts('Activity Id(s) %1', array(1 => implode($value)));
317 case 'activity_taglist':
320 foreach ($taglist as $val) {
322 $val = explode(',', $val);
323 foreach ($val as $tId) {
324 if (is_numeric($tId)) {
331 case 'activity_tags':
332 $value = array_keys($value);
333 $activityTags = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
337 if (is_array($value)) {
338 foreach ($value as $k => $v) {
339 $names[] = $activityTags[$v];
342 $query->_where
[$grouping][] = "civicrm_activity_tag.tag_id IN (" . implode(",", $value) . ")";
343 $query->_qill
[$grouping][] = ts('Activity Tag %1', array(1 => $op)) . ' ' . implode(' ' . ts('OR') . ' ', $names);
344 $query->_tables
['civicrm_activity_tag'] = $query->_whereTables
['civicrm_activity_tag'] = 1;
347 case 'activity_campaign_id':
350 'campaign' => $value,
351 'grouping' => $grouping,
352 'tableName' => 'civicrm_activity',
354 CRM_Campaign_BAO_Query
::componentSearchClause($campParams, $query);
356 case 'activity_result':
357 if(is_array($value)) {
359 while(list(,$k) = each($value)) {
360 $safe[] = "'" . CRM_Utils_Type
::escape($k, 'String') . "'";
362 $query->_where
[$grouping][] = "civicrm_activity.result IN (" . implode(',', $safe) . ")";
363 $query->_qill
[$grouping][] = ts("Activity Result - %1", array(1 => implode(' or ', $safe)));
369 static function from($name, $mode, $side) {
372 case 'civicrm_activity':
373 //CRM-7480 we are going to civicrm_activity table either
374 //from civicrm_activity_target or civicrm_activity_assignment.
375 //as component specific activities does not have entry in
376 //activity target table so lets consider civicrm_activity_assignment.
377 $from .= " INNER JOIN civicrm_activity_contact
378 ON ( civicrm_activity_contact.contact_id = contact_a.id ) ";
379 $from .= " INNER JOIN civicrm_activity
380 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
381 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )";
383 $activityRole = CRM_Contact_BAO_Query
::$_activityRole;
386 case 'activity_status':
387 $from .= " $side JOIN civicrm_option_group option_group_activity_status ON (option_group_activity_status.name = 'activity_status')";
388 $from .= " $side JOIN civicrm_option_value activity_status ON (civicrm_activity.status_id = activity_status.value
389 AND option_group_activity_status.id = activity_status.option_group_id ) ";
392 case 'activity_type':
393 $from .= " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')";
394 $from .= " $side JOIN civicrm_option_value activity_type ON (civicrm_activity.activity_type_id = activity_type.value
395 AND option_group_activity_type.id = activity_type.option_group_id ) ";
398 case 'civicrm_activity_tag':
399 $from .= " $side JOIN civicrm_entity_tag as civicrm_activity_tag ON ( civicrm_activity_tag.entity_table = 'civicrm_activity' AND civicrm_activity_tag.entity_id = civicrm_activity.id ) ";
402 case 'source_contact':
403 $activityContacts = CRM_Core_OptionGroup
::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
404 $sourceID = CRM_Utils_Array
::key('Activity Source', $activityContacts);
406 LEFT JOIN civicrm_activity_contact ac
407 ON ( ac.activity_id = civicrm_activity_contact.activity_id AND ac.record_type_id = {$sourceID})
408 INNER JOIN civicrm_contact source_contact ON (ac.contact_id = source_contact.id)";
416 * getter for the qill object
422 return (isset($this->_qill
)) ?
$this->_qill
: "";
426 * add all the elements shared between case activity search and advanaced search
430 * @param $form CRM_Core_Form
434 static function buildSearchForm(&$form) {
435 $activityOptions = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
436 asort($activityOptions);
437 foreach ($activityOptions as $activityID => $activity) {
438 $form->_activityElement
=&
441 "activity_type_id[$activityID]",
444 array('onClick' => 'showCustomData( this.id );')
448 CRM_Core_Form_Date
::buildDateRange($form, 'activity_date', 1, '_low', '_high', ts('From'), FALSE, FALSE);
450 $activityRoles = array(
452 2 => ts('Assigned to'),
455 $form->addRadio('activity_role', NULL, $activityRoles, array('allowClear' => TRUE));
456 $form->setDefaults(array('activity_role' => 3));
457 $activityStatus = CRM_Core_PseudoConstant
::activityStatus();
458 foreach ($activityStatus as $activityStatusID => $activityStatusName) {
459 $activity_status[] = $form->createElement('checkbox', $activityStatusID, NULL, $activityStatusName);
461 $form->addGroup($activity_status, 'activity_status', ts('Activity Status'));
462 $form->setDefaults(array('activity_status[1]' => 1, 'activity_status[2]' => 1));
463 $form->addElement('text', 'activity_subject', ts('Subject'), CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
464 $form->addYesNo('activity_test', ts('Activity is a Test?'));
465 $activity_tags = CRM_Core_BAO_Tag
::getTags('civicrm_activity');
466 if ($activity_tags) {
467 foreach ($activity_tags as $tagID => $tagName) {
468 $form->_tagElement
= &$form->addElement('checkbox', "activity_tags[$tagID]",
474 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_activity');
475 CRM_Core_Form_Tag
::buildQuickForm($form, $parentNames, 'civicrm_activity', NULL, TRUE, TRUE);
477 $surveys = CRM_Campaign_BAO_Survey
::getSurveys(TRUE, FALSE, FALSE, TRUE);
478 if ($surveys) $form->add('select', 'activity_survey_id', ts('Survey / Petition'),
479 array('' => ts('- none -')) +
$surveys, FALSE,
480 array('class' => 'crm-select2')
483 $extends = array('Activity');
484 $groupDetails = CRM_Core_BAO_CustomGroup
::getGroupDetail(NULL, TRUE, $extends);
486 $form->assign('activityGroupTree', $groupDetails);
487 foreach ($groupDetails as $group) {
488 foreach ($group['fields'] as $field) {
489 $fieldId = $field['id'];
490 $elementName = 'custom_' . $fieldId;
491 CRM_Core_BAO_CustomField
::addQuickFormElement($form, $elementName, $fieldId, FALSE, FALSE, TRUE);
496 CRM_Campaign_BAO_Campaign
::addCampaignInComponentSearch($form, 'activity_campaign_id');
498 //add engagement level CRM-7775
499 $buildEngagementLevel = FALSE;
500 $buildSurveyResult = FALSE;
501 if (CRM_Campaign_BAO_Campaign
::isCampaignEnable() &&
502 CRM_Campaign_BAO_Campaign
::accessCampaign()
504 $buildEngagementLevel = TRUE;
505 $form->addSelect('activity_engagement_level', array('entity' => 'activity', 'option_url' => NULL));
507 // Add survey result field.
508 $optionGroups = CRM_Campaign_BAO_Survey
::getResultSets( 'name' );
509 $resultOptions = array();
510 foreach ( $optionGroups as $gid => $name ) {
513 $value = CRM_Core_OptionGroup
::values($name);
514 if (!empty($value)) {
515 while(list($k,$v) = each($value)) {
516 $resultOptions[$v] = $v;
521 // If no survey result options have been created, don't build
522 // the field to avoid clutter.
523 if(count($resultOptions) > 0) {
524 $buildSurveyResult = TRUE;
525 asort($resultOptions);
526 $form->add('select', 'activity_result', ts("Survey Result"),
527 $resultOptions, FALSE,
528 array('id' => 'activity_result', 'multiple' => 'multiple', 'class' => 'crm-select2')
533 $form->assign('buildEngagementLevel', $buildEngagementLevel);
534 $form->assign('buildSurveyResult', $buildSurveyResult);
535 $form->setDefaults(array('activity_test' => 0));
538 static function defaultReturnProperties($mode, $includeCustomFields = TRUE) {
540 if ($mode & CRM_Contact_BAO_Query
::MODE_ACTIVITY
) {
544 'contact_sub_type' => 1,
547 'activity_type' => 1,
548 'activity_type_id' => 1,
549 'activity_subject' => 1,
550 'activity_date_time' => 1,
551 'activity_duration' => 1,
552 'activity_location' => 1,
553 'activity_details' => 1,
554 'activity_status' => 1,
555 'source_contact' => 1,
556 'source_record_id' => 1,
557 'activity_is_test' => 1,
558 'activity_campaign_id' => 1,
560 'activity_engagement_level' => 1,
563 if ($includeCustomFields) {
564 // also get all the custom activity properties
565 $fields = CRM_Core_BAO_CustomField
::getFieldsForImport('Activity');
566 if (!empty($fields)) {
567 foreach ($fields as $name => $dontCare) {
568 $properties[$name] = 1;
577 static function buildWhereAndQill(&$query, $value, $pseudoconstantType, $op, $grouping, $params) {
578 $matches = $val = $clause = array();
580 if (is_array($value)) {
581 foreach ($value as $k => $v) {
587 if (count($val) > 0) {
588 // Overwrite $op so it works with an IN where statement.
592 // If we somehow have an empty array, just return
595 $value = $matches[0] = $val;
598 preg_match_all('/\d+/', $value, $matches);
600 foreach ($matches[0] as $qill) {
601 $clause[] = CRM_Utils_Array
::value($qill, $pseudoconstantType);
604 $query->_qill
[$grouping][] = ts($params[0] . ' %1 ', array(1 => $op)) . implode(' ' . ts('or') . ' ', $clause);
605 $query->_where
[$grouping][] = CRM_Contact_BAO_Query
::buildClause($params[1],
606 $op, $value, "Integer"