Merge pull request #19120 from eileenmcnaughton/fee
[civicrm-core.git] / api / v3 / LocBlock.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM LocBlock records.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * Create or update a LocBlock.
20 *
21 * @param array $params
22 * Name/value pairs to insert in new 'LocBlock'.
23 *
24 * @return array
25 * API result array.
26 *
27 * @throws \API_Exception
28 * @throws \CiviCRM_API3_Exception
29 */
30 function civicrm_api3_loc_block_create($params) {
31 $entities = [];
32 $any_mandatory = [
33 'address',
34 'address_id',
35 'phone',
36 'phone_id',
37 'im',
38 'im_id',
39 'email',
40 'email_id',
41 ];
42 civicrm_api3_verify_one_mandatory($params, NULL, $any_mandatory);
43 // Call the appropriate api to create entities if any are passed in the params.
44 // This is basically chaining but in reverse - we create the sub-entities first
45 // because chaining does not work in reverse, or with keys like 'email_2'.
46 $items = ['address', 'email', 'phone', 'im'];
47 foreach ($items as $item) {
48 foreach (['', '_2'] as $suf) {
49 $key = $item . $suf;
50 if (!empty($params[$key]) && is_array($params[$key])) {
51 $info = $params[$key];
52 // If all we get is an id don't bother calling the api.
53 if (count($info) == 1 && !empty($info['id'])) {
54 $params[$key . '_id'] = $info['id'];
55 }
56 // Bother calling the api.
57 else {
58 $info['contact_id'] = CRM_Utils_Array::value('contact_id', $info, 'null');
59 $result = civicrm_api3($item, 'create', $info);
60 $entities[$key] = $result['values'][$result['id']];
61 $params[$key . '_id'] = $result['id'];
62 }
63 }
64 }
65 }
66 $dao = new CRM_Core_DAO_LocBlock();
67 $dao->copyValues($params);
68 $dao->save();
69 if (!empty($dao->id)) {
70 $values = [$dao->id => $entities];
71 _civicrm_api3_object_to_array($dao, $values[$dao->id]);
72 return civicrm_api3_create_success($values, $params, 'LocBlock', 'create', $dao);
73 }
74 throw new API_Exception('Unable to create LocBlock. Please check your params.');
75 }
76
77 /**
78 * Returns array of loc_blocks matching a set of one or more properties.
79 *
80 * @param array $params
81 * Array of one or more valid property_name=>value pairs. If $params is set.
82 * as null, all loc_blocks will be returned (default limit is 25).
83 *
84 * @return array
85 * API result array.
86 */
87 function civicrm_api3_loc_block_get($params) {
88 $options = _civicrm_api3_get_options_from_params($params);
89 // If a return param has been set then fetch the appropriate fk objects
90 // This is a helper because api chaining does not work with a key like 'email_2'.
91 if (!empty($options['return'])) {
92 unset($params['return']);
93 $values = [];
94 $items = ['address', 'email', 'phone', 'im'];
95 $returnAll = !empty($options['return']['all']);
96 foreach (_civicrm_api3_basic_get('CRM_Core_DAO_LocBlock', $params, FALSE) as $val) {
97 foreach ($items as $item) {
98 foreach (['', '_2'] as $suf) {
99 $key = $item . $suf;
100 if (!empty($val[$key . '_id']) && ($returnAll || !empty($options['return'][$key]))) {
101 $val[$key] = civicrm_api($item, 'getsingle', ['version' => 3, 'id' => $val[$key . '_id']]);
102 }
103 }
104 }
105 $values[$val['id']] = $val;
106 }
107 return civicrm_api3_create_success($values, $params, 'LocBlock', 'get');
108 }
109 return _civicrm_api3_basic_get('CRM_Core_DAO_LocBlock', $params);
110 }
111
112 /**
113 * Delete an existing LocBlock.
114 *
115 * @param array $params
116 * Array containing id of the record to be deleted.
117 *
118 * @return array
119 * API result array.
120 */
121 function civicrm_api3_loc_block_delete($params) {
122 return _civicrm_api3_basic_delete('CRM_Core_DAO_LocBlock', $params);
123 }