Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-04-11-02-53-53
[civicrm-core.git] / CRM / Activity / BAO / Query.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
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 */
36 class 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) {
156 $grouping = $testCondition = NULL;
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 }
162 if ($query->_params[$id][0] == 'activity_test') {
163 $testCondition = $id;
164 continue;
165 }
166 $grouping = $query->_params[$id][3];
167 self::whereClauseSingle($query->_params[$id], $query);
168 }
169 }
170 // Only add test condition if other fields are selected
171 if ($grouping !== NULL && $testCondition &&
172 // we dont want to include all tests for sql OR CRM-7827
173 $query->getOperator() != 'OR'
174 ) {
175 self::whereClauseSingle($query->_params[$testCondition], $query);
176 }
177 }
178
179 /**
180 * where clause for a single field
181 *
182 * @return void
183 * @access public
184 */
185 static function whereClauseSingle(&$values, &$query) {
186 list($name, $op, $value, $grouping, $wildcard) = $values;
187
188 $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
189 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
190 if ($query->_mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
191 $query->_skipDeleteClause = TRUE;
192 }
193
194 switch ($name) {
195 case 'activity_type_id':
196 $types = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
197
198 //get the component activity types.
199 $compActTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE, TRUE);
200
201 $clause = array();
202 if (is_array($value)) {
203 foreach ($value as $id => $dontCare) {
204 if (array_key_exists($id, $types) && $dontCare) {
205 $clause[] = "'" . CRM_Utils_Type::escape($types[$id], 'String') . "'";
206 if (array_key_exists($id, $compActTypes)) {
207 CRM_Contact_BAO_Query::$_considerCompActivities = TRUE;
208 }
209 }
210 }
211 $activityTypes = implode(',', array_keys($value));
212 }
213 else {
214 $clause[] = "'" . CRM_Utils_Type::escape($value, 'String') . "'";
215 $activityTypes = $value;
216 if (array_key_exists($value, $compActTypes)) {
217 CRM_Contact_BAO_Query::$_considerCompActivities = TRUE;
218 }
219 }
220 $query->_where[$grouping][] = ' civicrm_activity.activity_type_id IN (' . $activityTypes . ')';
221 $query->_qill[$grouping][] = ts('Activity Type') . ' ' . implode(' ' . ts('or') . ' ', $clause);
222 break;
223
224 case 'activity_survey_id':
225 if (!$value) {
226 break;
227 }
228 $value = CRM_Utils_Type::escape($value, 'Integer');
229 $query->_where[$grouping][] = " source_record_id = $value";
230 $query->_qill[$grouping][] = ts('Survey') . ' - ' . CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $value, 'title');
231 break;
232
233 case 'activity_engagement_level':
234 if (!$value) {
235 break;
236 }
237
238 $value = CRM_Utils_Type::escape($value, 'Integer');
239 $query->_where[$grouping][] = " engagement_level = $value";
240 $query->_qill[$grouping][] = ts('Engagement Index') . ' - ' . CRM_Core_OptionGroup::getLabel('engagement_index', $value);
241 break;
242
243 case 'activity_role':
244 CRM_Contact_BAO_Query::$_activityRole = $values[2];
245
246 if ($values[2]) {
247 $query->_tables['civicrm_activity_contact'] = $query->_whereTables['civicrm_activity_contact'] = 1;
248 if ($values[2] == 1) {
249 $query->_where[$grouping][] = " civicrm_activity_contact.record_type = 'Source'";
250 $query->_qill[$grouping][] = ts('Activity created by');
251 }
252 else if ($values[2] == 2) {
253 $query->_where[$grouping][] = " civicrm_activity_contact.record_type = 'Assigneen'";
254 $query->_qill[$grouping][] = ts('Activity assigned to');
255 }
256 else if ($values[2] == 3) {
257 $query->_where[$grouping][] = " civicrm_activity_contact.record_type = 'Target'";
258 $query->_qill[$grouping][] = ts('Activity targeted to');
259 }
260 }
261 break;
262
263 case 'activity_status':
264 $status = CRM_Core_PseudoConstant::activityStatus();
265 $clause = array();
266 if (is_array($value)) {
267 foreach ($value as $k => $v) {
268 if ($k) {
269 $clause[] = "'" . CRM_Utils_Type::escape($status[$k], 'String') . "'";
270 }
271 }
272 }
273 else {
274 $clause[] = "'" . CRM_Utils_Type::escape($value, 'String') . "'";
275 }
276 $query->_where[$grouping][] = ' civicrm_activity.status_id IN (' . implode(',', array_keys($value)) . ')';
277 $query->_qill[$grouping][] = ts('Activity Status') . ' - ' . implode(' ' . ts('or') . ' ', $clause);
278 break;
279
280 case 'activity_subject':
281 $n = trim($value);
282 $value = strtolower(CRM_Core_DAO::escapeString($n));
283 if ($wildcard) {
284 if (strpos($value, '%') !== FALSE) {
285 // only add wild card if not there
286 $value = "'$value'";
287 }
288 else {
289 $value = "'%$value%'";
290 }
291 $op = 'LIKE';
292 }
293 else {
294 $value = "'$value'";
295 }
296 $wc = ($op != 'LIKE') ? "LOWER(civicrm_activity.subject)" : "civicrm_activity.subject";
297 $query->_where[$grouping][] = " $wc $op $value";
298 $query->_qill[$grouping][] = ts('Subject') . " $op - '$n'";
299 break;
300
301 case 'activity_test':
302 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_activity.is_test", $op, $value, "Boolean");
303 if ($value) {
304 $query->_qill[$grouping][] = ts('Activity is a Test');
305 }
306 break;
307
308 case 'activity_date':
309 case 'activity_date_low':
310 case 'activity_date_high':
311 $query->dateQueryBuilder($values,
312 'civicrm_activity', 'activity_date', 'activity_date_time', ts('Activity Date')
313 );
314 break;
315
316 case 'activity_id':
317 if (empty($value)) {
318 break;
319 }
320
321 if (is_array($value)) {
322 foreach ($value as $k => $v) {
323 $value[$k] = CRM_Utils_Type::escape($v, 'Integer');
324 }
325 }
326 else {
327 $value = array(CRM_Utils_Type::escape($value, 'Integer'));
328 }
329 $query->_where[$grouping][] = "civicrm_activity.id IN (" . implode(",", $value) . ")";
330 $query->_qill[$grouping][] = ts('Activity Id(s) %1', array(1 => implode($value)));
331 break;
332
333 case 'activity_taglist':
334 $taglist = $value;
335 $value = array();
336 foreach ($taglist as $val) {
337 if ($val) {
338 $val = explode(',', $val);
339 foreach ($val as $tId) {
340 if (is_numeric($tId)) {
341 $value[$tId] = 1;
342 }
343 }
344 }
345 }
346
347 case 'activity_tags':
348 $value = array_keys($value);
349 $activityTags = CRM_Core_PseudoConstant::tag();
350
351 $names = array();
352 $val = array();
353 if (is_array($value)) {
354 foreach ($value as $k => $v) {
355 $names[] = $activityTags[$v];
356 }
357 }
358 $query->_where[$grouping][] = "civicrm_activity_tag.tag_id IN (" . implode(",", $value) . ")";
359 $query->_qill[$grouping][] = ts('Activity Tag %1', array(1 => $op)) . ' ' . implode(' ' . ts('OR') . ' ', $names);
360 $query->_tables['civicrm_activity_tag'] = $query->_whereTables['civicrm_activity_tag'] = 1;
361 break;
362
363 case 'activity_campaign_id':
364 $campParams = array(
365 'op' => $op,
366 'campaign' => $value,
367 'grouping' => $grouping,
368 'tableName' => 'civicrm_activity',
369 );
370 CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
371 return;
372 }
373 }
374
375 static function from($name, $mode, $side) {
376 $from = NULL;
377 switch ($name) {
378 case 'civicrm_activity':
379 //CRM-7480 we are going to civicrm_activitty table either
380 //from civicrm_activity_target or civicrm_activity_assignment.
381 //as component specific activities does not have entry in
382 //activity target table so lets consider civicrm_activity_assignment.
383 $from .= " INNER JOIN civicrm_activity_contact
384 ON ( civicrm_activity_contact.contact_id = contact_a.id ) ";
385 $from .= " INNER JOIN civicrm_activity
386 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
387 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )";
388
389 $activityRole = CRM_Contact_BAO_Query::$_activityRole;
390 break;
391
392 case 'activity_status':
393 $from .= " $side JOIN civicrm_option_group option_group_activity_status ON (option_group_activity_status.name = 'activity_status')";
394 $from .= " $side JOIN civicrm_option_value activity_status ON (civicrm_activity.status_id = activity_status.value
395 AND option_group_activity_status.id = activity_status.option_group_id ) ";
396 break;
397
398 case 'activity_type':
399 $from .= " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')";
400 $from .= " $side JOIN civicrm_option_value activity_type ON (civicrm_activity.activity_type_id = activity_type.value
401 AND option_group_activity_type.id = activity_type.option_group_id ) ";
402 break;
403
404 case 'civicrm_activity_tag':
405 $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 ) ";
406 break;
407
408 case 'source_contact':
409 $from = " $side JOIN civicrm_contact source_contact ON source_contact.id = civicrm_activity.source_contact_id";
410 break;
411 }
412
413 return $from;
414 }
415
416 /**
417 * getter for the qill object
418 *
419 * @return string
420 * @access public
421 */
422 function qill() {
423 return (isset($this->_qill)) ? $this->_qill : "";
424 }
425
426 /**
427 * add all the elements shared between case activity search and advanaced search
428 *
429 * @access public
430 *
431 * @return void
432 * @static
433 */
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 =&
439 $form->addElement(
440 'checkbox',
441 "activity_type_id[$activityID]",
442 NULL,
443 $activity,
444 array('onClick' => 'showCustomData( this.id );')
445 );
446 }
447
448 CRM_Core_Form_Date::buildDateRange($form, 'activity_date', 1, '_low', '_high', ts('From'), FALSE, FALSE);
449
450 $activityRoles = array(
451 1 => ts('Created by'),
452 2 => ts('Assigned to'),
453 3 => ts('Targeted to')
454 );
455 $form->addRadio('activity_role', NULL, $activityRoles);
456
457 $activityStatus = CRM_Core_PseudoConstant::activityStatus();
458 foreach ($activityStatus as $activityStatusID => $activityStatusName) {
459 $activity_status[] = $form->createElement('checkbox', $activityStatusID, NULL, $activityStatusName);
460 }
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]",
469 NULL, $tagName
470 );
471 }
472 }
473
474 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_activity');
475 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_activity', NULL, TRUE, FALSE, TRUE);
476
477 $surveys = CRM_Campaign_BAO_Survey::getSurveys();
478 if ($surveys) $form->add('select', 'activity_survey_id', ts('Survey'),
479 array(
480 '' => ts('- none -')) + $surveys, FALSE
481 );
482
483 $extends = array('Activity');
484 $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $extends);
485 if ($groupDetails) {
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);
492 }
493 }
494 }
495
496 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'activity_campaign_id');
497
498 //add engagement level CRM-7775
499 $buildEngagementLevel = FALSE;
500 if (CRM_Campaign_BAO_Campaign::isCampaignEnable() &&
501 CRM_Campaign_BAO_Campaign::accessCampaign()
502 ) {
503 $buildEngagementLevel = TRUE;
504 $form->add('select', 'activity_engagement_level',
505 ts('Engagement Index'),
506 array('' => ts('- any -')) + CRM_Campaign_PseudoConstant::engagementLevel()
507 );
508 }
509
510 $form->assign('buildEngagementLevel', $buildEngagementLevel);
511 $form->setDefaults(array('activity_test' => 0));
512 }
513
514 static function addShowHide(&$showHide) {
515 $showHide->addHide('caseActivityForm');
516 $showHide->addShow('caseActivityForm_show');
517 }
518
519 static function defaultReturnProperties($mode, $includeCustomFields = TRUE) {
520 $properties = NULL;
521 if ($mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
522 $properties = array(
523 'activity_id' => 1,
524 'contact_type' => 1,
525 'contact_sub_type' => 1,
526 'sort_name' => 1,
527 'display_name' => 1,
528 'activity_type' => 1,
529 'activity_subject' => 1,
530 'activity_date_time' => 1,
531 'activity_duration' => 1,
532 'activity_location' => 1,
533 'activity_details' => 1,
534 'activity_status' => 1,
535 'source_contact_id' => 1,
536 'source_record_id' => 1,
537 'activity_is_test' => 1,
538 'activity_campaign_id' => 1,
539 'activity_engagement_level' => 1,
540 );
541
542 if ($includeCustomFields) {
543 // also get all the custom activity properties
544 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Activity');
545 if (!empty($fields)) {
546 foreach ($fields as $name => $dontCare) {
547 $properties[$name] = 1;
548 }
549 }
550 }
551 }
552
553 return $properties;
554 }
555 }
556