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