Merge pull request #16529 from mattwire/altermailing_templatetype
[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 * @example SavedSearch/Create.php Std create example.
41 * @return array
42 * api result array
43 * {@getfields saved_search_create}
44 * @access public
45 */
46 function civicrm_api3_saved_search_create($params) {
47 civicrm_api3_verify_one_mandatory($params, NULL, ['form_values', 'where_clause']);
48
49 $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'SavedSearch');
50 _civicrm_api3_saved_search_result_cleanup($result);
51 return $result;
52 }
53
54 /**
55 * @param array $fields
56 */
57 function _civicrm_api3_saved_search_create_spec(&$fields) {
58 $fields['form_values']['api.aliases'][] = 'formValues';
59 }
60
61 /**
62 * Delete an existing saved search.
63 *
64 * @param array $params
65 * Associative array of property name-value pairs. $params['id'] should be
66 * the ID of the saved search to be deleted.
67 * @example SavedSearch/Delete.php Std delete example.
68 * @return array
69 * api result array
70 * {@getfields saved_search_delete}
71 * @access public
72 */
73 function civicrm_api3_saved_search_delete($params) {
74 return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
75 }
76
77 /**
78 * Retrieve one or more saved search(es).
79 *
80 * @param array $params
81 * An associative array of name-value pairs.
82 * @example SavedSearch/Get.php Std get example.
83 * @return array
84 * api result array
85 * {@getfields saved_search_get}
86 * @access public
87 */
88 function civicrm_api3_saved_search_get($params) {
89 $result = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
90 _civicrm_api3_saved_search_result_cleanup($result);
91 return $result;
92 }
93
94 /**
95 * This function unserializes the form_values in an SavedSearch API result.
96 *
97 * @param array $result API result to be cleaned up.
98 */
99 function _civicrm_api3_saved_search_result_cleanup(&$result) {
100 if (isset($result['values']) && is_array($result['values'])) {
101 // Only clean up the values if there are values. (A getCount operation
102 // for example does not return values.)
103 foreach ($result['values'] as $key => $value) {
104 $result['values'][$key]['form_values'] = \CRM_Utils_String::unserialize($value['form_values']);
105 }
106 }
107 }