Merge pull request #19375 from civicrm/5.34
[civicrm-core.git] / api / v3 / SavedSearch.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
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 +--------------------------------------------------------------------+
27 */
28
29 /**
30 * This api exposes CiviCRM saved searches.
31 *
32 * @package CiviCRM_APIv3
33 */
34
35 /**
36 * Create or update a saved search.
37 *
38 * @param array $params
39 * Associative array of property name-value pairs to insert in new saved search.
40 *
41 * @return array
42 * api result array
43 * {@getfields saved_search_create}
44 *
45 * @throws \API_Exception
46 *
47 * @example SavedSearch/Create.php Std create example.
48 * @access public
49 */
50 function civicrm_api3_saved_search_create($params) {
51 $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'SavedSearch');
52 _civicrm_api3_saved_search_result_cleanup($result);
53 return $result;
54 }
55
56 /**
57 * @param array $fields
58 */
59 function _civicrm_api3_saved_search_create_spec(&$fields) {
60 $fields['form_values']['api.aliases'][] = 'formValues';
61 $fields['form_values']['api.required'] = TRUE;
62 }
63
64 /**
65 * Delete an existing saved search.
66 *
67 * @param array $params
68 * Associative array of property name-value pairs. $params['id'] should be
69 * the ID of the saved search to be deleted.
70 *
71 * @return array
72 * api result array
73 * {@getfields saved_search_delete}
74 *
75 * @throws \API_Exception
76 * @throws \CiviCRM_API3_Exception
77 *
78 * @example SavedSearch/Delete.php Std delete example.
79 * @access public
80 */
81 function 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
89 * An associative array of name-value pairs.
90 * @example SavedSearch/Get.php Std get example.
91 * @return array
92 * api result array
93 * {@getfields saved_search_get}
94 * @access public
95 */
96 function civicrm_api3_saved_search_get($params) {
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 * Unserialize the form_values field in SavedSearch API results.
104 *
105 * Note: APIv4 handles serialization automatically based on metadata.
106 *
107 * @param array $result API result to be cleaned up.
108 */
109 function _civicrm_api3_saved_search_result_cleanup(&$result) {
110 if (isset($result['values']) && is_array($result['values'])) {
111 // Only run if there are values (getCount for example does not return values).
112 foreach ($result['values'] as $key => $value) {
113 if (isset($value['form_values'])) {
114 $result['values'][$key]['form_values'] = CRM_Utils_String::unserialize($value['form_values']);
115 }
116 }
117 }
118 }