Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
c037736a | 35 | * Business object for Saved searches. |
6a488035 TO |
36 | */ |
37 | class CRM_Contact_BAO_SavedSearch extends CRM_Contact_DAO_SavedSearch { | |
38 | ||
39 | /** | |
fe482240 | 40 | * Class constructor. |
6a488035 | 41 | */ |
00be9182 | 42 | public function __construct() { |
6a488035 TO |
43 | parent::__construct(); |
44 | } | |
45 | ||
46 | /** | |
100fef9d | 47 | * Query the db for all saved searches. |
6a488035 | 48 | * |
a6c01b45 CW |
49 | * @return array |
50 | * contains the search name as value and and id as key | |
6a488035 | 51 | */ |
00be9182 | 52 | public function getAll() { |
6a488035 TO |
53 | $savedSearch = new CRM_Contact_DAO_SavedSearch(); |
54 | $savedSearch->selectAdd(); | |
55 | $savedSearch->selectAdd('id, name'); | |
56 | $savedSearch->find(); | |
57 | while ($savedSearch->fetch()) { | |
58 | $aSavedSearch[$savedSearch->id] = $savedSearch->name; | |
59 | } | |
60 | return $aSavedSearch; | |
61 | } | |
62 | ||
63 | /** | |
fe482240 EM |
64 | * Retrieve DB object based on input parameters. |
65 | * | |
66 | * It also stores all the retrieved values in the default array. | |
6a488035 | 67 | * |
77c5b619 TO |
68 | * @param array $params |
69 | * (reference ) an assoc array of name/value pairs. | |
70 | * @param array $defaults | |
71 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 72 | * |
c490a46a | 73 | * @return CRM_Contact_BAO_SavedSearch |
6a488035 | 74 | */ |
00be9182 | 75 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
76 | $savedSearch = new CRM_Contact_DAO_SavedSearch(); |
77 | $savedSearch->copyValues($params); | |
78 | if ($savedSearch->find(TRUE)) { | |
79 | CRM_Core_DAO::storeValues($savedSearch, $defaults); | |
80 | return $savedSearch; | |
81 | } | |
82 | return NULL; | |
83 | } | |
84 | ||
85 | /** | |
54957108 | 86 | * Given an id, extract the formValues of the saved search. |
6a488035 | 87 | * |
77c5b619 TO |
88 | * @param int $id |
89 | * The id of the saved search. | |
6a488035 | 90 | * |
a6c01b45 | 91 | * @return array |
9406ae0b | 92 | * the values of the posted saved search used as default values in various Search Form |
6a488035 | 93 | */ |
e5ad0335 | 94 | public static function getFormValues($id) { |
6a488035 TO |
95 | $fv = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $id, 'form_values'); |
96 | $result = NULL; | |
97 | if ($fv) { | |
98 | // make sure u unserialize - since it's stored in serialized form | |
99 | $result = unserialize($fv); | |
100 | } | |
101 | ||
06d67d53 | 102 | $specialFields = array('contact_type', 'group', 'contact_tags', 'member_membership_type_id', 'member_status_id'); |
103 | foreach ($result as $element => $value) { | |
104 | if (CRM_Contact_BAO_Query::isAlreadyProcessedForQueryFormat($value)) { | |
105 | $id = CRM_Utils_Array::value(0, $value); | |
106 | $value = CRM_Utils_Array::value(2, $value); | |
107 | if (is_array($value) && in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { | |
8e2e771b | 108 | $op = key($value); |
109 | $value = CRM_Utils_Array::value($op, $value); | |
110 | if (in_array($op, array('BETWEEN', '>=', '<='))) { | |
111 | self::decodeRelativeFields($result, $id, $op, $value); | |
112 | unset($result[$element]); | |
113 | continue; | |
114 | } | |
06d67d53 | 115 | } |
709355aa | 116 | if (strpos($id, '_date_low') !== FALSE || strpos($id, '_date_high') !== FALSE) { |
5aa7bc38 | 117 | $entityName = strstr($id, '_date', TRUE); |
9451fffa | 118 | if (!empty($result['relative_dates']) && array_key_exists($entityName, $result['relative_dates'])) { |
8558eb81 | 119 | $result[$id] = NULL; |
9451fffa | 120 | $result["{$entityName}_date_relative"] = $result['relative_dates'][$entityName]; |
121 | } | |
122 | else { | |
aeadc6bf | 123 | $result[$id] = $value; |
9451fffa | 124 | $result["{$entityName}_date_relative"] = 0; |
125 | } | |
709355aa | 126 | } |
127 | else { | |
128 | $result[$id] = $value; | |
129 | } | |
06d67d53 | 130 | unset($result[$element]); |
131 | continue; | |
132 | } | |
133 | if (!empty($value) && is_array($value)) { | |
134 | if (in_array($element, $specialFields)) { | |
e5ad0335 E |
135 | // Remove the element to minimise support for legacy formats. It is stored in $value |
136 | // so will be re-set with the right name. | |
137 | unset($result[$element]); | |
06d67d53 | 138 | $element = str_replace('member_membership_type_id', 'membership_type_id', $element); |
139 | $element = str_replace('member_status_id', 'membership_status_id', $element); | |
140 | CRM_Contact_BAO_Query::legacyConvertFormValues($element, $value); | |
141 | $result[$element] = $value; | |
142 | } | |
143 | // As per the OK (Operator as Key) value format, value array may contain key | |
144 | // as an operator so to ensure the default is always set actual value | |
145 | elseif (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { | |
146 | $result[$element] = CRM_Utils_Array::value(key($value), $value); | |
147 | if (is_string($result[$element])) { | |
148 | $result[$element] = str_replace("%", '', $result[$element]); | |
6a488035 | 149 | } |
6a488035 | 150 | } |
06d67d53 | 151 | } |
152 | if (substr($element, 0, 7) == 'custom_' && | |
153 | (substr($element, -5, 5) == '_from' || substr($element, -3, 3) == '_to') | |
154 | ) { | |
155 | // Ensure the _relative field is set if from or to are set to ensure custom date | |
156 | // fields with 'from' or 'to' values are displayed when the are set in the smart group | |
157 | // being loaded. (CRM-17116) | |
158 | if (!isset($result[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'])) { | |
159 | $result[CRM_Contact_BAO_Query::getCustomFieldName($element) . '_relative'] = 0; | |
160 | } | |
161 | } | |
162 | // check to see if we need to convert the old privacy array | |
163 | // CRM-9180 | |
164 | if (!empty($result['privacy'])) { | |
165 | if (is_array($result['privacy'])) { | |
166 | $result['privacy_operator'] = 'AND'; | |
167 | $result['privacy_toggle'] = 1; | |
168 | if (isset($result['privacy']['do_not_toggle'])) { | |
169 | if ($result['privacy']['do_not_toggle']) { | |
170 | $result['privacy_toggle'] = 2; | |
171 | } | |
172 | unset($result['privacy']['do_not_toggle']); | |
173 | } | |
6a488035 | 174 | |
06d67d53 | 175 | $result['privacy_options'] = array(); |
d0d565e0 | 176 | foreach ($result['privacy'] as $name => $val) { |
177 | if ($val) { | |
06d67d53 | 178 | $result['privacy_options'][] = $name; |
179 | } | |
6a488035 TO |
180 | } |
181 | } | |
06d67d53 | 182 | unset($result['privacy']); |
6a488035 | 183 | } |
6a488035 TO |
184 | } |
185 | ||
186 | return $result; | |
187 | } | |
188 | ||
86538308 | 189 | /** |
54957108 | 190 | * Get search parameters. |
191 | * | |
100fef9d | 192 | * @param int $id |
86538308 EM |
193 | * |
194 | * @return array | |
195 | */ | |
00be9182 | 196 | public static function getSearchParams($id) { |
6a488035 | 197 | $fv = self::getFormValues($id); |
959528d2 | 198 | //check if the saved search has mapping id |
6a488035 TO |
199 | if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $id, 'mapping_id')) { |
200 | return CRM_Core_BAO_Mapping::formattedFields($fv); | |
201 | } | |
a7488080 | 202 | elseif (!empty($fv['customSearchID'])) { |
6a488035 TO |
203 | return $fv; |
204 | } | |
205 | else { | |
206 | return CRM_Contact_BAO_Query::convertFormValues($fv); | |
207 | } | |
208 | } | |
209 | ||
210 | /** | |
fe482240 | 211 | * Get the where clause for a saved search. |
6a488035 | 212 | * |
77c5b619 TO |
213 | * @param int $id |
214 | * Saved search id. | |
215 | * @param array $tables | |
216 | * (reference ) add the tables that are needed for the select clause. | |
217 | * @param array $whereTables | |
218 | * (reference ) add the tables that are needed for the where clause. | |
6a488035 | 219 | * |
a6c01b45 CW |
220 | * @return string |
221 | * the where clause for this saved search | |
6a488035 | 222 | */ |
00be9182 | 223 | public static function whereClause($id, &$tables, &$whereTables) { |
6a488035 TO |
224 | $params = self::getSearchParams($id); |
225 | if ($params) { | |
a7488080 | 226 | if (!empty($params['customSearchID'])) { |
6a488035 | 227 | // this has not yet been implemented |
0db6c3e1 TO |
228 | } |
229 | else { | |
353ffa53 TO |
230 | return CRM_Contact_BAO_Query::getWhereClause($params, NULL, $tables, $whereTables); |
231 | } | |
6a488035 TO |
232 | } |
233 | return NULL; | |
234 | } | |
235 | ||
86538308 | 236 | /** |
54957108 | 237 | * Contact IDS Sql (whatever that means!). |
238 | * | |
100fef9d | 239 | * @param int $id |
86538308 EM |
240 | * |
241 | * @return string | |
242 | */ | |
00be9182 | 243 | public static function contactIDsSQL($id) { |
6a488035 | 244 | $params = self::getSearchParams($id); |
8cc574cf | 245 | if ($params && !empty($params['customSearchID'])) { |
6a488035 TO |
246 | return CRM_Contact_BAO_SearchCustom::contactIDSQL(NULL, $id); |
247 | } | |
248 | else { | |
249 | $tables = $whereTables = array('civicrm_contact' => 1); | |
250 | $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables); | |
251 | if (!$where) { | |
252 | $where = '( 1 )'; | |
253 | } | |
254 | $from = CRM_Contact_BAO_Query::fromClause($whereTables); | |
255 | return " | |
256 | SELECT contact_a.id | |
257 | $from | |
258 | WHERE $where"; | |
259 | } | |
260 | } | |
261 | ||
86538308 | 262 | /** |
54957108 | 263 | * Get from where email (whatever that means!). |
264 | * | |
100fef9d | 265 | * @param int $id |
86538308 EM |
266 | * |
267 | * @return array | |
268 | */ | |
00be9182 | 269 | public static function fromWhereEmail($id) { |
6a488035 TO |
270 | $params = self::getSearchParams($id); |
271 | ||
272 | if ($params) { | |
a7488080 | 273 | if (!empty($params['customSearchID'])) { |
6a488035 TO |
274 | return CRM_Contact_BAO_SearchCustom::fromWhereEmail(NULL, $id); |
275 | } | |
276 | else { | |
277 | $tables = $whereTables = array('civicrm_contact' => 1, 'civicrm_email' => 1); | |
353ffa53 TO |
278 | $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables); |
279 | $from = CRM_Contact_BAO_Query::fromClause($whereTables); | |
6a488035 TO |
280 | return array($from, $where); |
281 | } | |
282 | } | |
283 | else { | |
284 | // fix for CRM-7240 | |
285 | $from = " | |
286 | FROM civicrm_contact contact_a | |
287 | LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1) | |
288 | "; | |
289 | $where = " ( 1 ) "; | |
290 | $tables['civicrm_contact'] = $whereTables['civicrm_contact'] = 1; | |
291 | $tables['civicrm_email'] = $whereTables['civicrm_email'] = 1; | |
292 | return array($from, $where); | |
293 | } | |
294 | } | |
295 | ||
296 | /** | |
54957108 | 297 | * Given a saved search compute the clause and the tables and store it for future use. |
6a488035 | 298 | */ |
00be9182 | 299 | public function buildClause() { |
6a488035 TO |
300 | $fv = unserialize($this->form_values); |
301 | ||
302 | if ($this->mapping_id) { | |
303 | $params = CRM_Core_BAO_Mapping::formattedFields($fv); | |
304 | } | |
305 | else { | |
306 | $params = CRM_Contact_BAO_Query::convertFormValues($fv); | |
307 | } | |
308 | ||
309 | if (!empty($params)) { | |
310 | $tables = $whereTables = array(); | |
311 | $this->where_clause = CRM_Contact_BAO_Query::getWhereClause($params, NULL, $tables, $whereTables); | |
312 | if (!empty($tables)) { | |
313 | $this->select_tables = serialize($tables); | |
314 | } | |
315 | if (!empty($whereTables)) { | |
316 | $this->where_tables = serialize($whereTables); | |
317 | } | |
318 | } | |
6a488035 TO |
319 | } |
320 | ||
54957108 | 321 | /** |
322 | * Save the search. | |
323 | * | |
324 | * @param bool $hook | |
325 | */ | |
8ab09481 | 326 | public function save($hook = TRUE) { |
6a488035 TO |
327 | // first build the computed fields |
328 | $this->buildClause(); | |
329 | ||
8ab09481 | 330 | parent::save($hook); |
6a488035 TO |
331 | } |
332 | ||
333 | /** | |
e8e8f3ad | 334 | * Given an id, get the name of the saved search. |
6a488035 | 335 | * |
77c5b619 TO |
336 | * @param int $id |
337 | * The id of the saved search. | |
6a488035 | 338 | * |
77b97be7 EM |
339 | * @param string $value |
340 | * | |
a6c01b45 CW |
341 | * @return string |
342 | * the name of the saved search | |
6a488035 | 343 | */ |
00be9182 | 344 | public static function getName($id, $value = 'name') { |
6a488035 TO |
345 | $group = new CRM_Contact_DAO_Group(); |
346 | $group->saved_search_id = $id; | |
347 | if ($group->find(TRUE)) { | |
348 | return $group->$value; | |
349 | } | |
350 | return NULL; | |
351 | } | |
352 | ||
353 | /** | |
e8e8f3ad | 354 | * Create a smart group from normalised values. |
54957108 | 355 | * |
356 | * @param array $params | |
357 | * | |
358 | * @return \CRM_Contact_DAO_SavedSearch | |
6a488035 | 359 | */ |
00be9182 | 360 | public static function create(&$params) { |
6a488035 TO |
361 | $savedSearch = new CRM_Contact_DAO_SavedSearch(); |
362 | if (isset($params['formValues']) && | |
363 | !empty($params['formValues']) | |
364 | ) { | |
365 | $savedSearch->form_values = serialize($params['formValues']); | |
366 | } | |
367 | else { | |
1c18b4d5 | 368 | $savedSearch->form_values = NULL; |
6a488035 TO |
369 | } |
370 | ||
371 | $savedSearch->is_active = CRM_Utils_Array::value('is_active', $params, 1); | |
372 | $savedSearch->mapping_id = CRM_Utils_Array::value('mapping_id', $params, 'null'); | |
373 | $savedSearch->custom_search_id = CRM_Utils_Array::value('custom_search_id', $params, 'null'); | |
374 | $savedSearch->id = CRM_Utils_Array::value('id', $params, NULL); | |
375 | ||
376 | $savedSearch->save(); | |
377 | ||
378 | return $savedSearch; | |
379 | } | |
96025800 | 380 | |
54957108 | 381 | /** |
382 | * Assign test value. | |
383 | * | |
384 | * @param string $fieldName | |
385 | * @param array $fieldDef | |
386 | * @param int $counter | |
387 | */ | |
4f0a0e58 JV |
388 | protected function assignTestValue($fieldName, &$fieldDef, $counter) { |
389 | if ($fieldName == 'form_values') { | |
390 | // A dummy value for form_values. | |
391 | $this->{$fieldName} = serialize( | |
392 | array('sort_name' => "SortName{$counter}")); | |
393 | } | |
394 | else { | |
395 | parent::assignTestValues($fieldName, $fieldDef, $counter); | |
396 | } | |
397 | } | |
398 | ||
9451fffa | 399 | /** |
400 | * Store relative dates in separate array format | |
401 | * | |
402 | * @param array $queryParams | |
403 | * @param array $formValues | |
404 | */ | |
405 | public static function saveRelativeDates(&$queryParams, $formValues) { | |
406 | $relativeDates = array('relative_dates' => array()); | |
407 | foreach ($formValues as $id => $value) { | |
408 | if (preg_match('/_date_relative$/', $id) && !empty($value)) { | |
409 | $entityName = strstr($id, '_date', TRUE); | |
410 | $relativeDates['relative_dates'][$entityName] = $value; | |
411 | } | |
412 | } | |
413 | // merge with original queryParams if relative date value(s) found | |
414 | if (count($relativeDates['relative_dates'])) { | |
415 | $queryParams = array_merge($queryParams, $relativeDates); | |
416 | } | |
417 | } | |
418 | ||
3f471f2d | 419 | /** |
420 | * Store search variables in $queryParams which were skipped while processing query params, | |
421 | * precisely at CRM_Contact_BAO_Query::fixWhereValues(...). But these variable are required in | |
422 | * building smart group criteria otherwise it will cause issues like CRM-18585,CRM-19571 | |
423 | * | |
424 | * @param array $queryParams | |
425 | * @param array $formValues | |
426 | */ | |
427 | public static function saveSkippedElement(&$queryParams, $formValues) { | |
428 | // these are elements which are skipped in a smart group criteria | |
429 | $specialElements = array( | |
430 | 'operator', | |
431 | 'component_mode', | |
432 | 'display_relationship_type', | |
433 | ); | |
434 | foreach ($specialElements as $element) { | |
435 | if (!empty($formValues[$element])) { | |
436 | $queryParams[] = array($element, '=', $formValues[$element], 0, 0); | |
437 | } | |
438 | } | |
439 | } | |
440 | ||
8e2e771b | 441 | /** |
442 | * Decode relative custom fields (converted by CRM_Contact_BAO_Query->convertCustomRelativeFields(...)) | |
443 | * into desired formValues | |
444 | * | |
445 | * @param array $formValues | |
446 | * @param string $fieldName | |
447 | * @param string $op | |
448 | * @param array|string|int $value | |
449 | */ | |
450 | public static function decodeRelativeFields(&$formValues, $fieldName, $op, $value) { | |
b2b9d9df | 451 | // check if its a custom date field, if yes then 'searchDate' format the value |
452 | $isCustomDateField = CRM_Contact_BAO_Query::isCustomDateField($fieldName); | |
d8766e36 | 453 | |
b2b9d9df | 454 | // select date range as default |
455 | if ($isCustomDateField) { | |
456 | $formValues[$fieldName . '_relative'] = 0; | |
d8766e36 | 457 | } |
8e2e771b | 458 | switch ($op) { |
459 | case 'BETWEEN': | |
b2b9d9df | 460 | if ($isCustomDateField) { |
461 | list($formValues[$fieldName . '_from'], $formValues[$fieldName . '_from_time']) = CRM_Utils_Date::setDateDefaults($value[0], 'searchDate'); | |
462 | list($formValues[$fieldName . '_to'], $formValues[$fieldName . '_to_time']) = CRM_Utils_Date::setDateDefaults($value[1], 'searchDate'); | |
463 | } | |
464 | else { | |
465 | list($formValues[$fieldName . '_from'], $formValues[$fieldName . '_to']) = $value; | |
466 | } | |
8e2e771b | 467 | break; |
468 | ||
469 | case '>=': | |
b2b9d9df | 470 | if ($isCustomDateField) { |
471 | list($formValues[$fieldName . '_from'], $formValues[$fieldName . '_from_time']) = CRM_Utils_Date::setDateDefaults($value, 'searchDate'); | |
472 | } | |
473 | else { | |
474 | $formValues[$fieldName . '_from'] = $value; | |
475 | } | |
8e2e771b | 476 | break; |
477 | ||
478 | case '<=': | |
b2b9d9df | 479 | if ($isCustomDateField) { |
480 | list($formValues[$fieldName . '_to'], $formValues[$fieldName . '_to_time']) = CRM_Utils_Date::setDateDefaults($value, 'searchDate'); | |
481 | } | |
482 | else { | |
483 | $formValues[$fieldName . '_to'] = $value; | |
484 | } | |
8e2e771b | 485 | break; |
486 | } | |
487 | } | |
488 | ||
6a488035 | 489 | } |