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