| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | CiviCRM version 4.7 | |
| 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 api result array |
| 42 | * {@getfields saved_search_create} |
| 43 | * @access public |
| 44 | */ |
| 45 | function civicrm_api3_saved_search_create($params) { |
| 46 | // The create function of the dao expects a 'formValues' that is |
| 47 | // not serialized. The get function returns form_values, that is |
| 48 | // serialized. |
| 49 | // So for the create API, I guess it should work for serialized and |
| 50 | // unserialized form_values. |
| 51 | |
| 52 | if (isset($params["form_values"])) { |
| 53 | if (is_array($params["form_values"])) { |
| 54 | $params["formValues"] = $params["form_values"]; |
| 55 | } |
| 56 | else { |
| 57 | // Assume that form_values is serialized. |
| 58 | $params["formValues"] = unserialize($params["form_values"]); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 63 | _civicrm_api3_saved_search_result_cleanup($result); |
| 64 | return $result; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Delete an existing saved search. |
| 69 | * |
| 70 | * @param array $params |
| 71 | * Associative array of property name-value pairs. $params['id'] should be |
| 72 | * the ID of the saved search to be deleted. |
| 73 | * @example SavedSearch/Delete.php Std delete example. |
| 74 | * @return array api result array |
| 75 | * {@getfields saved_search_delete} |
| 76 | * @access public |
| 77 | */ |
| 78 | function civicrm_api3_saved_search_delete($params) { |
| 79 | return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Retrieve one or more saved search(es). |
| 84 | * |
| 85 | * @param array $params |
| 86 | * An associative array of name-value pairs. |
| 87 | * @example SavedSearch/Get.php Std get example. |
| 88 | * @return array api result array |
| 89 | * {@getfields saved_search_get} |
| 90 | * @access public |
| 91 | */ |
| 92 | function civicrm_api3_saved_search_get($params) { |
| 93 | $result = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
| 94 | _civicrm_api3_saved_search_result_cleanup($result); |
| 95 | return $result; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * This function unserializes the form_values in an SavedSearch API result. |
| 100 | * |
| 101 | * @param array $result API result to be cleaned up. |
| 102 | */ |
| 103 | function _civicrm_api3_saved_search_result_cleanup(&$result) { |
| 104 | if (isset($result['values']) && is_array($result['values'])) { |
| 105 | // Only clean up the values if there are values. (A getCount operation |
| 106 | // for example does not return values.) |
| 107 | foreach ($result['values'] as $key => $value) { |
| 108 | $result['values'][$key]['form_values'] = unserialize($value['form_values']); |
| 109 | } |
| 110 | } |
| 111 | } |