Merge pull request #16392 from jaapjansma/dev_1522_tests
[civicrm-core.git] / api / v3 / SavedSearch.php
CommitLineData
b4ee829e 1<?php
627adbf9 2
b4ee829e 3/*
627adbf9 4 +--------------------------------------------------------------------+
fee14197 5 | CiviCRM version 5 |
627adbf9
JV
6 +--------------------------------------------------------------------+
7 | Copyright Chirojeugd-Vlaanderen vzw 2015 |
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 +--------------------------------------------------------------------+
b4ee829e
JV
27 */
28
29/**
30 * This api exposes CiviCRM saved searches.
31 *
32 * @package CiviCRM_APIv3
33 */
34
35/**
627adbf9 36 * Create or update a saved search.
b4ee829e
JV
37 *
38 * @param array $params
627adbf9
JV
39 * Associative array of property name-value pairs to insert in new saved search.
40 * @example SavedSearch/Create.php Std create example.
7c31ae57
SL
41 * @return array
42 * api result array
627adbf9
JV
43 * {@getfields saved_search_create}
44 * @access public
b4ee829e
JV
45 */
46function civicrm_api3_saved_search_create($params) {
cf8f0fff 47 civicrm_api3_verify_one_mandatory($params, NULL, ['form_values', 'where_clause']);
bc970fdf
JV
48 // The create function of the dao expects a 'formValues' that is
49 // not serialized. The get function returns form_values, that is
d30f435f
JV
50 // serialized.
51 // So for the create API, I guess it should work for serialized and
52 // unserialized form_values.
bc970fdf 53
8472d3c3
JV
54 if (isset($params["form_values"])) {
55 if (is_array($params["form_values"])) {
56 $params["formValues"] = $params["form_values"];
57 }
58 else {
59 // Assume that form_values is serialized.
b8db7c8c 60 $params["formValues"] = \CRM_Utils_String::unserialize($params["form_values"]);
8472d3c3 61 }
d30f435f
JV
62 }
63
a25b46e9 64 $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'SavedSearch');
d30f435f
JV
65 _civicrm_api3_saved_search_result_cleanup($result);
66 return $result;
b4ee829e
JV
67}
68
69/**
627adbf9 70 * Delete an existing saved search.
b4ee829e
JV
71 *
72 * @param array $params
627adbf9
JV
73 * Associative array of property name-value pairs. $params['id'] should be
74 * the ID of the saved search to be deleted.
75 * @example SavedSearch/Delete.php Std delete example.
7c31ae57
SL
76 * @return array
77 * api result array
627adbf9
JV
78 * {@getfields saved_search_delete}
79 * @access public
b4ee829e
JV
80 */
81function civicrm_api3_saved_search_delete($params) {
82 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
83}
84
85/**
86 * Retrieve one or more saved search(es).
87 *
88 * @param array $params
627adbf9
JV
89 * An associative array of name-value pairs.
90 * @example SavedSearch/Get.php Std get example.
7c31ae57
SL
91 * @return array
92 * api result array
627adbf9
JV
93 * {@getfields saved_search_get}
94 * @access public
b4ee829e
JV
95 */
96function civicrm_api3_saved_search_get($params) {
d30f435f
JV
97 $result = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
98 _civicrm_api3_saved_search_result_cleanup($result);
99 return $result;
100}
101
102/**
103 * This function unserializes the form_values in an SavedSearch API result.
104 *
105 * @param array $result API result to be cleaned up.
106 */
107function _civicrm_api3_saved_search_result_cleanup(&$result) {
859c2bc3 108 if (isset($result['values']) && is_array($result['values'])) {
132a94c1
JV
109 // Only clean up the values if there are values. (A getCount operation
110 // for example does not return values.)
f00c38c8 111 foreach ($result['values'] as $key => $value) {
b8db7c8c 112 $result['values'][$key]['form_values'] = \CRM_Utils_String::unserialize($value['form_values']);
f00c38c8 113 }
d30f435f 114 }
b4ee829e 115}