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