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