Merge pull request #1683 from pradpnayak/CRM-12970
[civicrm-core.git] / CRM / Activity / BAO / Query.php
CommitLineData
6a488035
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
232624b1 5 | CiviCRM version 4.4 |
6a488035
TO
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2013
33 * $Id$
34 *
35 */
36class CRM_Activity_BAO_Query {
37
38 /**
39 * build select for Case
40 *
41 * @return void
42 * @access public
43 */
44 static function select(&$query) {
45 if (CRM_Utils_Array::value('activity_id', $query->_returnProperties)) {
46 $query->_select['activity_id'] = "civicrm_activity.id as activity_id";
47 $query->_element['activity_id'] = 1;
48 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
49 }
50
51 if (CRM_Utils_Array::value('activity_type_id', $query->_returnProperties)) {
52 $query->_select['activity_type_id'] = "activity_type.id as activity_type_id";
53 $query->_element['activity_type_id'] = 1;
54 $query->_tables['civicrm_activity'] = 1;
55 $query->_tables['activity_type'] = 1;
56 $query->_whereTables['civicrm_activity'] = 1;
57 $query->_whereTables['activity_type'] = 1;
58 }
59
60 if (CRM_Utils_Array::value('activity_type', $query->_returnProperties)) {
61 $query->_select['activity_type'] = "activity_type.label as activity_type";
62 $query->_element['activity_type'] = 1;
63 $query->_tables['civicrm_activity'] = 1;
64 $query->_tables['activity_type'] = 1;
65 $query->_whereTables['civicrm_activity'] = 1;
66 $query->_whereTables['activity_type'] = 1;
67 }
68
69 if (CRM_Utils_Array::value('activity_subject', $query->_returnProperties)) {
70 $query->_select['activity_subject'] = "civicrm_activity.subject as activity_subject";
71 $query->_element['activity_subject'] = 1;
72 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
73 }
74
75 if (CRM_Utils_Array::value('activity_date_time', $query->_returnProperties)) {
76 $query->_select['activity_date_time'] = "civicrm_activity.activity_date_time as activity_date_time";
77 $query->_element['activity_date_time'] = 1;
78 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
79 }
80
81 if (CRM_Utils_Array::value('activity_status_id', $query->_returnProperties)) {
82 $query->_select['activity_status_id'] = "activity_status.value as activity_status_id";
83 $query->_element['activity_status_id'] = 1;
84 $query->_tables['civicrm_activity'] = 1;
85 $query->_tables['activity_status'] = 1;
86 $query->_whereTables['civicrm_activity'] = 1;
87 $query->_whereTables['activity_status'] = 1;
88 }
89
90 if (CRM_Utils_Array::value('activity_status', $query->_returnProperties)) {
91 $query->_select['activity_status'] = "activity_status.label as activity_status";
92 $query->_element['activity_status'] = 1;
93 $query->_tables['civicrm_activity'] = 1;
94 $query->_tables['activity_status'] = 1;
95 $query->_whereTables['civicrm_activity'] = 1;
96 $query->_whereTables['activity_status'] = 1;
97 }
98
99 if (CRM_Utils_Array::value('activity_duration', $query->_returnProperties)) {
100 $query->_select['activity_duration'] = "civicrm_activity.duration as activity_duration";
101 $query->_element['activity_duration'] = 1;
102 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
103 }
104
105 if (CRM_Utils_Array::value('activity_location', $query->_returnProperties)) {
106 $query->_select['activity_location'] = "civicrm_activity.location as activity_location";
107 $query->_element['activity_location'] = 1;
108 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
109 }
110
111 if (CRM_Utils_Array::value('activity_details', $query->_returnProperties)) {
112 $query->_select['activity_details'] = "civicrm_activity.details as activity_details";
113 $query->_element['activity_details'] = 1;
114 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
115 }
116
117 if (CRM_Utils_Array::value('source_record_id', $query->_returnProperties)) {
118 $query->_select['source_record_id'] = "civicrm_activity.source_record_id as source_record_id";
119 $query->_element['source_record_id'] = 1;
120 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
121 }
122
123 if (CRM_Utils_Array::value('activity_is_test', $query->_returnProperties)) {
124 $query->_select['activity_is_test'] = "civicrm_activity.is_test as activity_is_test";
125 $query->_element['activity_is_test'] = 1;
126 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
127 }
128
129 if (CRM_Utils_Array::value('activity_campaign_id', $query->_returnProperties)) {
130 $query->_select['activity_campaign_id'] = 'civicrm_activity.campaign_id as activity_campaign_id';
131 $query->_element['activity_campaign_id'] = 1;
132 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
133 }
134
135 if (CRM_Utils_Array::value('activity_engagement_level', $query->_returnProperties)) {
136 $query->_select['activity_engagement_level'] = 'civicrm_activity.engagement_level as activity_engagement_level';
137 $query->_element['activity_engagement_level'] = 1;
138 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
139 }
140
141 if (CRM_Utils_Array::value('source_contact', $query->_returnProperties)) {
142 $query->_select['source_contact'] = 'source_contact.display_name as source_contact';
143 $query->_element['source_contact'] = 1;
144 $query->_tables['source_contact'] = $query->_whereTables['source_contact'] = 1;
145 }
146 }
147
148 /**
149 * Given a list of conditions in query generate the required
150 * where clause
151 *
152 * @return void
153 * @access public
154 */
155 static function where(&$query) {
28c666be 156 $grouping = NULL;
6a488035
TO
157 foreach (array_keys($query->_params) as $id) {
158 if (substr($query->_params[$id][0], 0, 9) == 'activity_') {
159 if ($query->_mode == CRM_Contact_BAO_QUERY::MODE_CONTACTS) {
160 $query->_useDistinct = TRUE;
161 }
6a488035
TO
162 $grouping = $query->_params[$id][3];
163 self::whereClauseSingle($query->_params[$id], $query);
164 }
165 }
6a488035
TO
166 }
167
168 /**
169 * where clause for a single field
170 *
171 * @return void
172 * @access public
173 */
174 static function whereClauseSingle(&$values, &$query) {
175 list($name, $op, $value, $grouping, $wildcard) = $values;
176
177 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
178 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
179 if ($query->_mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
180 $query->_skipDeleteClause = TRUE;
181 }
182
183 switch ($name) {
184 case 'activity_type_id':
185 $types = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
186
187 //get the component activity types.
188 $compActTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE, TRUE);
189
190 $clause = array();
191 if (is_array($value)) {
192 foreach ($value as $id => $dontCare) {
193 if (array_key_exists($id, $types) && $dontCare) {
194 $clause[] = "'" . CRM_Utils_Type::escape($types[$id], 'String') . "'";
195 if (array_key_exists($id, $compActTypes)) {
196 CRM_Contact_BAO_Query::$_considerCompActivities = TRUE;
197 }
198 }
199 }
200 $activityTypes = implode(',', array_keys($value));
201 }
202 else {
203 $clause[] = "'" . CRM_Utils_Type::escape($value, 'String') . "'";
204 $activityTypes = $value;
205 if (array_key_exists($value, $compActTypes)) {
206 CRM_Contact_BAO_Query::$_considerCompActivities = TRUE;
207 }
208 }
209 $query->_where[$grouping][] = ' civicrm_activity.activity_type_id IN (' . $activityTypes . ')';
210 $query->_qill[$grouping][] = ts('Activity Type') . ' ' . implode(' ' . ts('or') . ' ', $clause);
211 break;
212
213 case 'activity_survey_id':
214 if (!$value) {
215 break;
216 }
217 $value = CRM_Utils_Type::escape($value, 'Integer');
218 $query->_where[$grouping][] = " source_record_id = $value";
219 $query->_qill[$grouping][] = ts('Survey') . ' - ' . CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $value, 'title');
220 break;
221
222 case 'activity_engagement_level':
223 if (!$value) {
224 break;
225 }
226
227 $value = CRM_Utils_Type::escape($value, 'Integer');
228 $query->_where[$grouping][] = " engagement_level = $value";
229 $query->_qill[$grouping][] = ts('Engagement Index') . ' - ' . CRM_Core_OptionGroup::getLabel('engagement_index', $value);
230 break;
231
232 case 'activity_role':
233 CRM_Contact_BAO_Query::$_activityRole = $values[2];
e7e657f0 234 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 235 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
236 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
237 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
f813f78e 238
91da6cd5 239 if ($values[2]) {
6a488035 240 $query->_tables['civicrm_activity_contact'] = $query->_whereTables['civicrm_activity_contact'] = 1;
91da6cd5 241 if ($values[2] == 1) {
a24b3694 242 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $sourceID";
91da6cd5
DL
243 $query->_qill[$grouping][] = ts('Activity created by');
244 }
245 else if ($values[2] == 2) {
a24b3694 246 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $assigneeID";
91da6cd5
DL
247 $query->_qill[$grouping][] = ts('Activity assigned to');
248 }
249 else if ($values[2] == 3) {
a24b3694 250 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $targetID";
91da6cd5
DL
251 $query->_qill[$grouping][] = ts('Activity targeted to');
252 }
6a488035
TO
253 }
254 break;
255
256 case 'activity_status':
257 $status = CRM_Core_PseudoConstant::activityStatus();
258 $clause = array();
259 if (is_array($value)) {
260 foreach ($value as $k => $v) {
261 if ($k) {
262 $clause[] = "'" . CRM_Utils_Type::escape($status[$k], 'String') . "'";
263 }
264 }
265 }
266 else {
267 $clause[] = "'" . CRM_Utils_Type::escape($value, 'String') . "'";
268 }
269 $query->_where[$grouping][] = ' civicrm_activity.status_id IN (' . implode(',', array_keys($value)) . ')';
270 $query->_qill[$grouping][] = ts('Activity Status') . ' - ' . implode(' ' . ts('or') . ' ', $clause);
271 break;
272
273 case 'activity_subject':
274 $n = trim($value);
275 $value = strtolower(CRM_Core_DAO::escapeString($n));
276 if ($wildcard) {
277 if (strpos($value, '%') !== FALSE) {
278 // only add wild card if not there
279 $value = "'$value'";
280 }
281 else {
282 $value = "'%$value%'";
283 }
284 $op = 'LIKE';
285 }
286 else {
287 $value = "'$value'";
288 }
289 $wc = ($op != 'LIKE') ? "LOWER(civicrm_activity.subject)" : "civicrm_activity.subject";
290 $query->_where[$grouping][] = " $wc $op $value";
291 $query->_qill[$grouping][] = ts('Subject') . " $op - '$n'";
292 break;
293
294 case 'activity_test':
28c666be
CW
295 // We dont want to include all tests for sql OR CRM-7827
296 if (!$value || $query->getOperator() != 'OR') {
297 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_activity.is_test", $op, $value, "Boolean");
298 if ($value) {
299 $query->_qill[$grouping][] = ts('Activity is a Test');
300 }
6a488035
TO
301 }
302 break;
303
304 case 'activity_date':
305 case 'activity_date_low':
306 case 'activity_date_high':
307 $query->dateQueryBuilder($values,
308 'civicrm_activity', 'activity_date', 'activity_date_time', ts('Activity Date')
309 );
310 break;
311
312 case 'activity_id':
313 if (empty($value)) {
314 break;
315 }
316
317 if (is_array($value)) {
318 foreach ($value as $k => $v) {
319 $value[$k] = CRM_Utils_Type::escape($v, 'Integer');
320 }
321 }
322 else {
323 $value = array(CRM_Utils_Type::escape($value, 'Integer'));
324 }
325 $query->_where[$grouping][] = "civicrm_activity.id IN (" . implode(",", $value) . ")";
326 $query->_qill[$grouping][] = ts('Activity Id(s) %1', array(1 => implode($value)));
327 break;
328
329 case 'activity_taglist':
330 $taglist = $value;
331 $value = array();
332 foreach ($taglist as $val) {
333 if ($val) {
334 $val = explode(',', $val);
335 foreach ($val as $tId) {
336 if (is_numeric($tId)) {
337 $value[$tId] = 1;
338 }
339 }
340 }
341 }
91da6cd5 342
6a488035
TO
343 case 'activity_tags':
344 $value = array_keys($value);
cd43c5e3 345 $activityTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
6a488035
TO
346
347 $names = array();
348 $val = array();
349 if (is_array($value)) {
350 foreach ($value as $k => $v) {
351 $names[] = $activityTags[$v];
352 }
353 }
354 $query->_where[$grouping][] = "civicrm_activity_tag.tag_id IN (" . implode(",", $value) . ")";
355 $query->_qill[$grouping][] = ts('Activity Tag %1', array(1 => $op)) . ' ' . implode(' ' . ts('OR') . ' ', $names);
356 $query->_tables['civicrm_activity_tag'] = $query->_whereTables['civicrm_activity_tag'] = 1;
357 break;
358
359 case 'activity_campaign_id':
360 $campParams = array(
361 'op' => $op,
362 'campaign' => $value,
363 'grouping' => $grouping,
364 'tableName' => 'civicrm_activity',
365 );
366 CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
367 return;
368 }
369 }
370
371 static function from($name, $mode, $side) {
372 $from = NULL;
373 switch ($name) {
374 case 'civicrm_activity':
9393897f 375 //CRM-7480 we are going to civicrm_activity table either
6a488035
TO
376 //from civicrm_activity_target or civicrm_activity_assignment.
377 //as component specific activities does not have entry in
378 //activity target table so lets consider civicrm_activity_assignment.
91da6cd5
DL
379 $from .= " INNER JOIN civicrm_activity_contact
380 ON ( civicrm_activity_contact.contact_id = contact_a.id ) ";
381 $from .= " INNER JOIN civicrm_activity
382 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
383 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )";
6a488035 384
6a488035 385 $activityRole = CRM_Contact_BAO_Query::$_activityRole;
6a488035
TO
386 break;
387
388 case 'activity_status':
1d85d241 389 $from .= " $side JOIN civicrm_option_group option_group_activity_status ON (option_group_activity_status.name = 'activity_status')";
6a488035
TO
390 $from .= " $side JOIN civicrm_option_value activity_status ON (civicrm_activity.status_id = activity_status.value
391 AND option_group_activity_status.id = activity_status.option_group_id ) ";
392 break;
393
394 case 'activity_type':
1d85d241 395 $from .= " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')";
6a488035
TO
396 $from .= " $side JOIN civicrm_option_value activity_type ON (civicrm_activity.activity_type_id = activity_type.value
397 AND option_group_activity_type.id = activity_type.option_group_id ) ";
398 break;
399
400 case 'civicrm_activity_tag':
401 $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 break;
403
404 case 'source_contact':
9393897f 405 $from = " $side JOIN civicrm_contact source_contact ON source_contact.id = civicrm_activity_contact.contact_id";
6a488035
TO
406 break;
407 }
408
409 return $from;
410 }
411
412 /**
413 * getter for the qill object
414 *
415 * @return string
416 * @access public
417 */
418 function qill() {
419 return (isset($this->_qill)) ? $this->_qill : "";
420 }
421
422 /**
423 * add all the elements shared between case activity search and advanaced search
424 *
425 * @access public
426 *
427 * @return void
428 * @static
429 */
430 static function buildSearchForm(&$form) {
431 $activityOptions = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
432 asort($activityOptions);
433 foreach ($activityOptions as $activityID => $activity) {
434 $form->_activityElement =&
435 $form->addElement(
436 'checkbox',
437 "activity_type_id[$activityID]",
438 NULL,
439 $activity,
440 array('onClick' => 'showCustomData( this.id );')
441 );
442 }
443
444 CRM_Core_Form_Date::buildDateRange($form, 'activity_date', 1, '_low', '_high', ts('From'), FALSE, FALSE);
445
91da6cd5
DL
446 $activityRoles = array(
447 1 => ts('Created by'),
448 2 => ts('Assigned to'),
449 3 => ts('Targeted to')
450 );
6a488035
TO
451 $form->addRadio('activity_role', NULL, $activityRoles);
452
6a488035
TO
453 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
454 foreach ($activityStatus as $activityStatusID => $activityStatusName) {
455 $activity_status[] = $form->createElement('checkbox', $activityStatusID, NULL, $activityStatusName);
456 }
457 $form->addGroup($activity_status, 'activity_status', ts('Activity Status'));
458 $form->setDefaults(array('activity_status[1]' => 1, 'activity_status[2]' => 1));
459 $form->addElement('text', 'activity_subject', ts('Subject'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
460 $form->addYesNo('activity_test', ts('Activity is a Test?'));
461 $activity_tags = CRM_Core_BAO_Tag::getTags('civicrm_activity');
462 if ($activity_tags) {
463 foreach ($activity_tags as $tagID => $tagName) {
464 $form->_tagElement = &$form->addElement('checkbox', "activity_tags[$tagID]",
465 NULL, $tagName
466 );
467 }
468 }
469
470 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_activity');
471 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_activity', NULL, TRUE, FALSE, TRUE);
472
9231464f
KJ
473 $surveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, FALSE, FALSE, TRUE);
474 if ($surveys) $form->add('select', 'activity_survey_id', ts('Survey / Petition'),
6a488035
TO
475 array(
476 '' => ts('- none -')) + $surveys, FALSE
477 );
478
479 $extends = array('Activity');
480 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $extends);
481 if ($groupDetails) {
482 $form->assign('activityGroupTree', $groupDetails);
483 foreach ($groupDetails as $group) {
484 foreach ($group['fields'] as $field) {
485 $fieldId = $field['id'];
486 $elementName = 'custom_' . $fieldId;
487 CRM_Core_BAO_CustomField::addQuickFormElement($form, $elementName, $fieldId, FALSE, FALSE, TRUE);
488 }
489 }
490 }
491
492 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'activity_campaign_id');
493
494 //add engagement level CRM-7775
495 $buildEngagementLevel = FALSE;
496 if (CRM_Campaign_BAO_Campaign::isCampaignEnable() &&
497 CRM_Campaign_BAO_Campaign::accessCampaign()
498 ) {
499 $buildEngagementLevel = TRUE;
500 $form->add('select', 'activity_engagement_level',
501 ts('Engagement Index'),
502 array('' => ts('- any -')) + CRM_Campaign_PseudoConstant::engagementLevel()
503 );
504 }
505
506 $form->assign('buildEngagementLevel', $buildEngagementLevel);
507 $form->setDefaults(array('activity_test' => 0));
508 }
509
510 static function addShowHide(&$showHide) {
511 $showHide->addHide('caseActivityForm');
512 $showHide->addShow('caseActivityForm_show');
513 }
514
515 static function defaultReturnProperties($mode, $includeCustomFields = TRUE) {
516 $properties = NULL;
517 if ($mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
518 $properties = array(
519 'activity_id' => 1,
520 'contact_type' => 1,
521 'contact_sub_type' => 1,
522 'sort_name' => 1,
523 'display_name' => 1,
524 'activity_type' => 1,
525 'activity_subject' => 1,
526 'activity_date_time' => 1,
527 'activity_duration' => 1,
528 'activity_location' => 1,
529 'activity_details' => 1,
530 'activity_status' => 1,
531 'source_contact_id' => 1,
532 'source_record_id' => 1,
533 'activity_is_test' => 1,
534 'activity_campaign_id' => 1,
535 'activity_engagement_level' => 1,
536 );
537
538 if ($includeCustomFields) {
539 // also get all the custom activity properties
540 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Activity');
541 if (!empty($fields)) {
542 foreach ($fields as $name => $dontCare) {
543 $properties[$name] = 1;
544 }
545 }
546 }
547 }
548
549 return $properties;
550 }
551}
552