Merge pull request #11680 from jitendrapurohit/setting
[civicrm-core.git] / CRM / Activity / BAO / Query.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33class CRM_Activity_BAO_Query {
34
35 /**
2e2605fe 36 * Build select for Case.
6a488035 37 *
b6c94f42 38 * @param CRM_Contact_BAO_Query $query
6a488035 39 */
00be9182 40 public static function select(&$query) {
a7488080 41 if (!empty($query->_returnProperties['activity_id'])) {
da236f9a 42 $query->_select['activity_id'] = 'civicrm_activity.id as activity_id';
6a488035
TO
43 $query->_element['activity_id'] = 1;
44 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
45 }
46
a7488080 47 if (!empty($query->_returnProperties['activity_type_id'])) {
da236f9a 48 $query->_select['activity_type_id'] = 'activity_type.value as activity_type_id';
6a488035
TO
49 $query->_element['activity_type_id'] = 1;
50 $query->_tables['civicrm_activity'] = 1;
51 $query->_tables['activity_type'] = 1;
52 $query->_whereTables['civicrm_activity'] = 1;
53 $query->_whereTables['activity_type'] = 1;
54 }
55
a7488080 56 if (!empty($query->_returnProperties['activity_type'])) {
da236f9a 57 $query->_select['activity_type'] = 'activity_type.label as activity_type';
6a488035
TO
58 $query->_element['activity_type'] = 1;
59 $query->_tables['civicrm_activity'] = 1;
60 $query->_tables['activity_type'] = 1;
61 $query->_whereTables['civicrm_activity'] = 1;
62 $query->_whereTables['activity_type'] = 1;
63 }
64
a7488080 65 if (!empty($query->_returnProperties['activity_subject'])) {
da236f9a 66 $query->_select['activity_subject'] = 'civicrm_activity.subject as activity_subject';
6a488035
TO
67 $query->_element['activity_subject'] = 1;
68 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
69 }
70
a7488080 71 if (!empty($query->_returnProperties['activity_date_time'])) {
da236f9a 72 $query->_select['activity_date_time'] = 'civicrm_activity.activity_date_time as activity_date_time';
6a488035
TO
73 $query->_element['activity_date_time'] = 1;
74 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
75 }
76
a7488080 77 if (!empty($query->_returnProperties['activity_status_id'])) {
7bd16b05 78 $query->_select['activity_status_id'] = 'civicrm_activity.status_id as activity_status_id';
6a488035
TO
79 $query->_element['activity_status_id'] = 1;
80 $query->_tables['civicrm_activity'] = 1;
6a488035 81 $query->_whereTables['civicrm_activity'] = 1;
6a488035
TO
82 }
83
a7488080 84 if (!empty($query->_returnProperties['activity_status'])) {
da236f9a 85 $query->_select['activity_status'] = 'activity_status.label as activity_status,
86 civicrm_activity.status_id as status_id';
6a488035
TO
87 $query->_element['activity_status'] = 1;
88 $query->_tables['civicrm_activity'] = 1;
89 $query->_tables['activity_status'] = 1;
90 $query->_whereTables['civicrm_activity'] = 1;
91 $query->_whereTables['activity_status'] = 1;
92 }
93
a7488080 94 if (!empty($query->_returnProperties['activity_duration'])) {
da236f9a 95 $query->_select['activity_duration'] = 'civicrm_activity.duration as activity_duration';
6a488035
TO
96 $query->_element['activity_duration'] = 1;
97 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
98 }
99
a7488080 100 if (!empty($query->_returnProperties['activity_location'])) {
da236f9a 101 $query->_select['activity_location'] = 'civicrm_activity.location as activity_location';
6a488035
TO
102 $query->_element['activity_location'] = 1;
103 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
104 }
105
a7488080 106 if (!empty($query->_returnProperties['activity_details'])) {
da236f9a 107 $query->_select['activity_details'] = 'civicrm_activity.details as activity_details';
6a488035
TO
108 $query->_element['activity_details'] = 1;
109 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
110 }
111
a7488080 112 if (!empty($query->_returnProperties['source_record_id'])) {
da236f9a 113 $query->_select['source_record_id'] = 'civicrm_activity.source_record_id as source_record_id';
6a488035
TO
114 $query->_element['source_record_id'] = 1;
115 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
116 }
117
a7488080 118 if (!empty($query->_returnProperties['activity_is_test'])) {
da236f9a 119 $query->_select['activity_is_test'] = 'civicrm_activity.is_test as activity_is_test';
6a488035
TO
120 $query->_element['activity_is_test'] = 1;
121 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
122 }
123
a7488080 124 if (!empty($query->_returnProperties['activity_campaign_id'])) {
6a488035
TO
125 $query->_select['activity_campaign_id'] = 'civicrm_activity.campaign_id as activity_campaign_id';
126 $query->_element['activity_campaign_id'] = 1;
127 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
128 }
129
a7488080 130 if (!empty($query->_returnProperties['activity_engagement_level'])) {
6a488035
TO
131 $query->_select['activity_engagement_level'] = 'civicrm_activity.engagement_level as activity_engagement_level';
132 $query->_element['activity_engagement_level'] = 1;
133 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
134 }
135
a7488080 136 if (!empty($query->_returnProperties['source_contact'])) {
37eb6ff9 137 $query->_select['source_contact'] = 'source_contact.sort_name as source_contact';
6a488035
TO
138 $query->_element['source_contact'] = 1;
139 $query->_tables['source_contact'] = $query->_whereTables['source_contact'] = 1;
140 }
4041c9b6 141
a7488080 142 if (!empty($query->_returnProperties['activity_result'])) {
4041c9b6
DG
143 $query->_select['activity_result'] = 'civicrm_activity.result as activity_result';
144 $query->_element['result'] = 1;
145 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
146 }
ffd93213 147
4c24c842
PN
148 if (CRM_Utils_Array::value('parent_id', $query->_returnProperties)) {
149 $query->_tables['parent_id'] = 1;
150 $query->_whereTables['parent_id'] = 1;
151 $query->_element['parent_id'] = 1;
152 }
da236f9a 153
154 if (!empty($query->_returnProperties['activity_priority'])) {
155 $query->_select['activity_priority'] = 'activity_priority.label as activity_priority,
156 civicrm_activity.priority_id as priority_id';
157 $query->_element['activity_priority'] = 1;
158 $query->_tables['activity_priority'] = 1;
159 $query->_whereTables['activity_priority'] = 1;
160 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
161 }
6a488035
TO
162 }
163
164 /**
b6c94f42 165 * Given a list of conditions in query generate the required where clause.
6a488035 166 *
da6b46f4 167 * @param $query
6a488035 168 */
00be9182 169 public static function where(&$query) {
6a488035
TO
170 foreach (array_keys($query->_params) as $id) {
171 if (substr($query->_params[$id][0], 0, 9) == 'activity_') {
172 if ($query->_mode == CRM_Contact_BAO_QUERY::MODE_CONTACTS) {
173 $query->_useDistinct = TRUE;
174 }
56bf8668 175 $query->_params[$id][3];
6a488035
TO
176 self::whereClauseSingle($query->_params[$id], $query);
177 }
178 }
6a488035
TO
179 }
180
181 /**
fe482240 182 * Where clause for a single field.
6a488035 183 *
b6c94f42 184 * @param array $values
185 * @param CRM_Contact_BAO_Query $query
6a488035 186 */
00be9182 187 public static function whereClauseSingle(&$values, &$query) {
56bf8668 188 list($name, $op, $value, $grouping) = $values;
6a488035 189
9ab34172 190 $fields = CRM_Activity_BAO_Activity::exportableFields();
6a488035
TO
191 $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
192 if ($query->_mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
193 $query->_skipDeleteClause = TRUE;
194 }
195
196 switch ($name) {
197 case 'activity_type_id':
9ab34172 198 case 'activity_status_id':
9ab34172 199 case 'activity_engagement_level':
9ab34172 200 case 'activity_id':
6f56dc34 201 case 'activity_campaign_id':
da236f9a 202 case 'activity_priority_id':
288674ef 203 // We no longer expect "subject" as a specific criteria (as of CRM-19447),
1cd7479e 204 // but we still use activity_subject in Activity.Get API
288674ef 205 case 'activity_subject':
6f56dc34 206
9ab34172 207 $qillName = $name;
9ab34172 208 if (in_array($name, array('activity_engagement_level', 'activity_id'))) {
209 $name = $qillName = str_replace('activity_', '', $name);
6a488035 210 }
da236f9a 211 if (in_array($name, array('activity_status_id', 'activity_subject', 'activity_priority_id'))) {
9ab34172 212 $name = str_replace('activity_', '', $name);
213 $qillName = str_replace('_id', '', $qillName);
214 }
6f56dc34 215 if ($name == 'activity_campaign_id') {
216 $name = 'campaign_id';
217 }
218
9ab34172 219 $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String';
220
afa0b07c 221 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_activity.$name", $op, $value, $dataType);
9ab34172 222 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Activity_DAO_Activity', $name, $value, $op);
223 $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value));
6a488035
TO
224 break;
225
92e9c781
AS
226 case 'activity_text':
227 self::whereClauseSingleActivityText($values, $query);
c9c5c58d
AS
228 break;
229
afa0b07c 230 case 'activity_type':
231 case 'activity_status':
da236f9a 232 case 'activity_priority':
afa0b07c 233 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$name.label", $op, $value, 'String');
234 list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Activity_DAO_Activity', $name, $value, $op);
235 $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$name]['title'], 2 => $op, 3 => $value));
236 $query->_tables[$name] = $query->_whereTables[$name] = 1;
237 break;
238
6a488035
TO
239 case 'activity_survey_id':
240 if (!$value) {
241 break;
242 }
243 $value = CRM_Utils_Type::escape($value, 'Integer');
56a5616a 244 $query->_where[$grouping][] = " civicrm_activity.source_record_id = $value";
6a488035
TO
245 $query->_qill[$grouping][] = ts('Survey') . ' - ' . CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $value, 'title');
246 break;
247
6a488035
TO
248 case 'activity_role':
249 CRM_Contact_BAO_Query::$_activityRole = $values[2];
44f817d4 250 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
a24b3694 251 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
252 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
253 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
f813f78e 254
91da6cd5 255 if ($values[2]) {
6a488035 256 $query->_tables['civicrm_activity_contact'] = $query->_whereTables['civicrm_activity_contact'] = 1;
91da6cd5 257 if ($values[2] == 1) {
a24b3694 258 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $sourceID";
91da6cd5
DL
259 $query->_qill[$grouping][] = ts('Activity created by');
260 }
4c9b6178 261 elseif ($values[2] == 2) {
a24b3694 262 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $assigneeID";
91da6cd5
DL
263 $query->_qill[$grouping][] = ts('Activity assigned to');
264 }
4c9b6178 265 elseif ($values[2] == 3) {
a24b3694 266 $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $targetID";
91da6cd5
DL
267 $query->_qill[$grouping][] = ts('Activity targeted to');
268 }
6a488035
TO
269 }
270 break;
271
6a488035 272 case 'activity_test':
56bf8668 273 // We don't want to include all tests for sql OR CRM-7827
28c666be 274 if (!$value || $query->getOperator() != 'OR') {
353ffa53 275 $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_activity.is_test", $op, $value, "Boolean");
28c666be
CW
276 if ($value) {
277 $query->_qill[$grouping][] = ts('Activity is a Test');
278 }
6a488035
TO
279 }
280 break;
281
282 case 'activity_date':
283 case 'activity_date_low':
284 case 'activity_date_high':
285 $query->dateQueryBuilder($values,
286 'civicrm_activity', 'activity_date', 'activity_date_time', ts('Activity Date')
287 );
288 break;
289
6a488035
TO
290 case 'activity_taglist':
291 $taglist = $value;
292 $value = array();
293 foreach ($taglist as $val) {
294 if ($val) {
295 $val = explode(',', $val);
296 foreach ($val as $tId) {
297 if (is_numeric($tId)) {
298 $value[$tId] = 1;
299 }
300 }
301 }
302 }
91da6cd5 303
6a488035
TO
304 case 'activity_tags':
305 $value = array_keys($value);
cd43c5e3 306 $activityTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
6a488035
TO
307
308 $names = array();
6a488035
TO
309 if (is_array($value)) {
310 foreach ($value as $k => $v) {
311 $names[] = $activityTags[$v];
312 }
313 }
314 $query->_where[$grouping][] = "civicrm_activity_tag.tag_id IN (" . implode(",", $value) . ")";
315 $query->_qill[$grouping][] = ts('Activity Tag %1', array(1 => $op)) . ' ' . implode(' ' . ts('OR') . ' ', $names);
316 $query->_tables['civicrm_activity_tag'] = $query->_whereTables['civicrm_activity_tag'] = 1;
317 break;
318
4041c9b6 319 case 'activity_result':
22e263ad 320 if (is_array($value)) {
4041c9b6 321 $safe = NULL;
353ffa53 322 while (list(, $k) = each($value)) {
4041c9b6
DG
323 $safe[] = "'" . CRM_Utils_Type::escape($k, 'String') . "'";
324 }
325 $query->_where[$grouping][] = "civicrm_activity.result IN (" . implode(',', $safe) . ")";
326 $query->_qill[$grouping][] = ts("Activity Result - %1", array(1 => implode(' or ', $safe)));
327 }
328 break;
4c24c842
PN
329
330 case 'parent_id':
331 if ($value == 1) {
332 $query->_where[$grouping][] = "parent_id.parent_id IS NOT NULL";
333 $query->_qill[$grouping][] = ts('Activities which have Followup Activities');
334 }
335 elseif ($value == 2) {
336 $query->_where[$grouping][] = "parent_id.parent_id IS NULL";
337 $query->_qill[$grouping][] = ts('Activities without Followup Activities');
338 }
339 break;
ffd93213 340
4c24c842
PN
341 case 'followup_parent_id':
342 if ($value == 1) {
343 $query->_where[$grouping][] = "civicrm_activity.parent_id IS NOT NULL";
344 $query->_qill[$grouping][] = ts('Activities which are Followup Activities');
345 }
346 elseif ($value == 2) {
347 $query->_where[$grouping][] = "civicrm_activity.parent_id IS NULL";
353ffa53 348 $query->_qill[$grouping][] = ts('Activities which are not Followup Activities');
4c24c842
PN
349 }
350 break;
6a488035
TO
351 }
352 }
353
ffd93213 354 /**
100fef9d 355 * @param string $name
ffd93213
EM
356 * @param $mode
357 * @param $side
358 *
359 * @return null|string
360 */
00be9182 361 public static function from($name, $mode, $side) {
6a488035
TO
362 $from = NULL;
363 switch ($name) {
364 case 'civicrm_activity':
9393897f 365 //CRM-7480 we are going to civicrm_activity table either
6a488035
TO
366 //from civicrm_activity_target or civicrm_activity_assignment.
367 //as component specific activities does not have entry in
368 //activity target table so lets consider civicrm_activity_assignment.
8a06db38 369 $from .= " $side JOIN civicrm_activity_contact
91da6cd5 370 ON ( civicrm_activity_contact.contact_id = contact_a.id ) ";
8a06db38 371 $from .= " $side JOIN civicrm_activity
91da6cd5
DL
372 ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
373 AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )";
db54182d 374 // Do not show deleted contact's activity
7c9cdbb7
DG
375 $from .= " INNER JOIN civicrm_contact
376 ON ( civicrm_activity_contact.contact_id = civicrm_contact.id and civicrm_contact.is_deleted != 1 )";
6a488035
TO
377 break;
378
379 case 'activity_status':
1d85d241 380 $from .= " $side JOIN civicrm_option_group option_group_activity_status ON (option_group_activity_status.name = 'activity_status')";
6a488035
TO
381 $from .= " $side JOIN civicrm_option_value activity_status ON (civicrm_activity.status_id = activity_status.value
382 AND option_group_activity_status.id = activity_status.option_group_id ) ";
383 break;
384
385 case 'activity_type':
1d85d241 386 $from .= " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')";
6a488035
TO
387 $from .= " $side JOIN civicrm_option_value activity_type ON (civicrm_activity.activity_type_id = activity_type.value
388 AND option_group_activity_type.id = activity_type.option_group_id ) ";
389 break;
390
da236f9a 391 case 'activity_priority':
c6ac46ad 392 $from .= " $side JOIN civicrm_option_group option_group_activity_priority ON (option_group_activity_priority.name = 'priority')";
393 $from .= " $side JOIN civicrm_option_value activity_priority ON (civicrm_activity.priority_id = activity_priority.value
394 AND option_group_activity_priority.id = activity_priority.option_group_id ) ";
395 break;
da236f9a 396
6a488035
TO
397 case 'civicrm_activity_tag':
398 $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 ) ";
399 break;
400
401 case 'source_contact':
44f817d4 402 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
37eb6ff9 403 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
404 $from = "
405 LEFT JOIN civicrm_activity_contact ac
406 ON ( ac.activity_id = civicrm_activity_contact.activity_id AND ac.record_type_id = {$sourceID})
407 INNER JOIN civicrm_contact source_contact ON (ac.contact_id = source_contact.id)";
6a488035 408 break;
ffd93213 409
4c24c842
PN
410 case 'parent_id':
411 $from = "$side JOIN civicrm_activity AS parent_id ON civicrm_activity.id = parent_id.parent_id";
412 break;
6a488035
TO
413 }
414
415 return $from;
416 }
417
6a488035 418 /**
fe482240 419 * Add all the elements shared between case activity search and advanced search.
6a488035 420 *
100fef9d 421 * @param CRM_Core_Form $form
6a488035 422 */
00be9182 423 public static function buildSearchForm(&$form) {
9ab34172 424 $form->addSelect('activity_type_id',
9c95efe4 425 array('entity' => 'activity', 'label' => ts('Activity Type(s)'), 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
9ab34172 426 );
6a488035
TO
427
428 CRM_Core_Form_Date::buildDateRange($form, 'activity_date', 1, '_low', '_high', ts('From'), FALSE, FALSE);
c55d8350
SB
429 $form->addElement('hidden', 'activity_date_range_error');
430 $form->addFormRule(array('CRM_Activity_BAO_Query', 'formRule'), $form);
431
4c24c842
PN
432 $followUpActivity = array(
433 1 => ts('Yes'),
434 2 => ts('No'),
435 );
436 $form->addRadio('parent_id', NULL, $followUpActivity, array('allowClear' => TRUE));
437 $form->addRadio('followup_parent_id', NULL, $followUpActivity, array('allowClear' => TRUE));
91da6cd5 438 $activityRoles = array(
18d2d5e2 439 3 => ts('With'),
91da6cd5 440 2 => ts('Assigned to'),
18d2d5e2 441 1 => ts('Added by'),
91da6cd5 442 );
b847e6e7 443 $form->addRadio('activity_role', NULL, $activityRoles, array('allowClear' => TRUE));
18d2d5e2 444 $form->setDefaults(array('activity_role' => 3));
9ab34172 445 $activityStatus = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'status_id', array('flip' => 1, 'labelColumn' => 'name'));
446 $form->addSelect('status_id',
447 array('entity' => 'activity', 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
448 );
d57052f7 449 $ssID = $form->get('ssID');
450 $status = array($activityStatus['Completed'], $activityStatus['Scheduled']);
451 //If status is saved in smart group.
452 if (!empty($ssID) && !empty($form->_formValues['activity_status_id'])) {
453 $status = $form->_formValues['activity_status_id'];
454 }
455 $form->setDefaults(array('status_id' => $status));
456
92e9c781 457 $form->addElement('text', 'activity_text', ts('Activity Text'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
1cd7479e 458
459 $form->addRadio('activity_option', '', CRM_Core_SelectValues::activityTextOptions());
460 $form->setDefaults(array('activity_option' => 'both'));
c9c5c58d 461
da236f9a 462 $priority = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id');
463 $form->addSelect('priority_id',
464 array('entity' => 'activity', 'label' => ts('Priority'), 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
465 );
466
6a488035
TO
467 $form->addYesNo('activity_test', ts('Activity is a Test?'));
468 $activity_tags = CRM_Core_BAO_Tag::getTags('civicrm_activity');
469 if ($activity_tags) {
470 foreach ($activity_tags as $tagID => $tagName) {
471 $form->_tagElement = &$form->addElement('checkbox', "activity_tags[$tagID]",
472 NULL, $tagName
473 );
474 }
475 }
476
477 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_activity');
95ef220a 478 CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_activity', NULL, TRUE, TRUE);
6a488035 479
9231464f 480 $surveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, FALSE, FALSE, TRUE);
28a04ea9 481 if ($surveys) {
482 $form->add('select', 'activity_survey_id', ts('Survey / Petition'),
483 array('' => ts('- none -')) + $surveys, FALSE,
484 array('class' => 'crm-select2')
485 );
486 }
86a0d21e
CW
487
488 CRM_Core_BAO_Query::addCustomFormFields($form, array('Activity'));
6a488035
TO
489
490 CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'activity_campaign_id');
491
7808aae6 492 // Add engagement level CRM-7775.
6a488035 493 $buildEngagementLevel = FALSE;
4041c9b6 494 $buildSurveyResult = FALSE;
6a488035
TO
495 if (CRM_Campaign_BAO_Campaign::isCampaignEnable() &&
496 CRM_Campaign_BAO_Campaign::accessCampaign()
497 ) {
498 $buildEngagementLevel = TRUE;
76773c5a 499 $form->addSelect('activity_engagement_level', array('entity' => 'activity', 'context' => 'search'));
6a488035 500
4041c9b6 501 // Add survey result field.
353ffa53 502 $optionGroups = CRM_Campaign_BAO_Survey::getResultSets('name');
4041c9b6 503 $resultOptions = array();
481a74f4
TO
504 foreach ($optionGroups as $gid => $name) {
505 if ($name) {
4041c9b6
DG
506 $value = CRM_Core_OptionGroup::values($name);
507 if (!empty($value)) {
353ffa53 508 while (list($k, $v) = each($value)) {
4041c9b6
DG
509 $resultOptions[$v] = $v;
510 }
511 }
512 }
513 }
514 // If no survey result options have been created, don't build
515 // the field to avoid clutter.
22e263ad 516 if (count($resultOptions) > 0) {
4041c9b6
DG
517 $buildSurveyResult = TRUE;
518 asort($resultOptions);
519 $form->add('select', 'activity_result', ts("Survey Result"),
520 $resultOptions, FALSE,
02ddf039 521 array('id' => 'activity_result', 'multiple' => 'multiple', 'class' => 'crm-select2')
4041c9b6
DG
522 );
523 }
524 }
77b97be7 525
6a488035 526 $form->assign('buildEngagementLevel', $buildEngagementLevel);
4041c9b6 527 $form->assign('buildSurveyResult', $buildSurveyResult);
6a488035
TO
528 $form->setDefaults(array('activity_test' => 0));
529 }
530
ffd93213
EM
531 /**
532 * @param $mode
533 * @param bool $includeCustomFields
534 *
535 * @return array|null
536 */
00be9182 537 public static function defaultReturnProperties($mode, $includeCustomFields = TRUE) {
6a488035
TO
538 $properties = NULL;
539 if ($mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
540 $properties = array(
541 'activity_id' => 1,
542 'contact_type' => 1,
543 'contact_sub_type' => 1,
544 'sort_name' => 1,
545 'display_name' => 1,
546 'activity_type' => 1,
5511233c 547 'activity_type_id' => 1,
6a488035
TO
548 'activity_subject' => 1,
549 'activity_date_time' => 1,
550 'activity_duration' => 1,
551 'activity_location' => 1,
552 'activity_details' => 1,
553 'activity_status' => 1,
da236f9a 554 'activity_priority' => 1,
37eb6ff9 555 'source_contact' => 1,
6a488035
TO
556 'source_record_id' => 1,
557 'activity_is_test' => 1,
558 'activity_campaign_id' => 1,
4041c9b6 559 'result' => 1,
6a488035 560 'activity_engagement_level' => 1,
4c24c842 561 'parent_id' => 1,
6a488035
TO
562 );
563
564 if ($includeCustomFields) {
565 // also get all the custom activity properties
566 $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Activity');
567 if (!empty($fields)) {
568 foreach ($fields as $name => $dontCare) {
569 $properties[$name] = 1;
570 }
571 }
572 }
573 }
574
575 return $properties;
576 }
96025800 577
7bd16b05 578 /**
579 * Get the list of fields required to populate the selector.
580 *
581 * The default return properties array returns far too many fields for 'everyday use. Every field you add to this array
582 * kills a small kitten so add carefully.
583 */
584 public static function selectorReturnProperties() {
585 $properties = array(
586 'activity_id' => 1,
587 'contact_type' => 1,
588 'contact_sub_type' => 1,
589 'sort_name' => 1,
590 'display_name' => 1,
591 'activity_type_id' => 1,
592 'activity_subject' => 1,
593 'activity_date_time' => 1,
594 'activity_status_id' => 1,
595 'source_contact' => 1,
596 'source_record_id' => 1,
597 'activity_is_test' => 1,
598 'activity_campaign_id' => 1,
599 'activity_engagement_level' => 1,
600 );
601
602 return $properties;
603 }
604
c55d8350 605 /**
79326ee2 606 * Custom form rules.
c55d8350
SB
607 *
608 * @param array $fields
609 * @param array $files
610 * @param CRM_Core_Form $form
611 *
612 * @return bool|array
613 */
614 public static function formRule($fields, $files, $form) {
615 $errors = array();
616
617 if (empty($fields['activity_date_low']) || empty($fields['activity_date_high'])) {
618 return TRUE;
619 }
c55d8350 620
79326ee2
SB
621 CRM_Utils_Rule::validDateRange($fields, 'activity_date', $errors, ts('Activity Date'));
622
c55d8350
SB
623 return empty($errors) ? TRUE : $errors;
624 }
948c107f 625
c9c5c58d
AS
626 /**
627 * Where/qill clause for notes
628 *
629 * @param array $values
630 */
92e9c781 631 public static function whereClauseSingleActivityText(&$values, &$query) {
c9c5c58d 632 list($name, $op, $value, $grouping, $wildcard) = $values;
c9c5c58d 633 $activityOptionValues = $query->getWhereValues('activity_option', $grouping);
1cd7479e 634 $activityOption = CRM_Utils_Array::value(2, $activityOptionValues, 6);
c9c5c58d
AS
635
636 $query->_useDistinct = TRUE;
637
1cd7479e 638 $label = ts('Activity Text (%1)', array(1 => CRM_Utils_Array::value($activityOption, CRM_Core_SelectValues::activityTextOptions())));
c9c5c58d
AS
639 $clauses = array();
640 if ($activityOption % 2 == 0) {
641 $clauses[] = $query->buildClause('civicrm_activity.details', $op, $value, 'String');
c9c5c58d
AS
642 }
643 if ($activityOption % 3 == 0) {
644 $clauses[] = $query->buildClause('civicrm_activity.subject', $op, $value, 'String');
c9c5c58d 645 }
1cd7479e 646
c9c5c58d 647 $query->_where[$grouping][] = "( " . implode(' OR ', $clauses) . " )";
1cd7479e 648 list($qillOp, $qillVal) = $query->buildQillForFieldValue(NULL, $name, $value, $op);
649 $query->_qill[$grouping][] = ts("%1 %2 '%3'", array(1 => $label, 2 => $qillOp, 3 => $qillVal));
c9c5c58d
AS
650 }
651
6a488035 652}