Merge pull request #15524 from seamuslee001/5_18_3_release_notes
[civicrm-core.git] / api / v3 / LocBlock.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * This api exposes CiviCRM LocBlock records.
30 *
31 * @package CiviCRM_APIv3
32 */
33
34 /**
35 * Create or update a LocBlock.
36 *
37 * @param array $params
38 * Name/value pairs to insert in new 'LocBlock'.
39 *
40 * @return array
41 * API result array.
42 *
43 * @throws \API_Exception
44 */
45 function civicrm_api3_loc_block_create($params) {
46 $entities = [];
47 $any_mandatory = [
48 'address',
49 'address_id',
50 'phone',
51 'phone_id',
52 'im',
53 'im_id',
54 'email',
55 'email_id',
56 ];
57 civicrm_api3_verify_one_mandatory($params, NULL, $any_mandatory);
58 // Call the appropriate api to create entities if any are passed in the params.
59 // This is basically chaining but in reverse - we create the sub-entities first
60 // because chaining does not work in reverse, or with keys like 'email_2'.
61 $items = ['address', 'email', 'phone', 'im'];
62 foreach ($items as $item) {
63 foreach (['', '_2'] as $suf) {
64 $key = $item . $suf;
65 if (!empty($params[$key]) && is_array($params[$key])) {
66 $info = $params[$key];
67 // If all we get is an id don't bother calling the api.
68 if (count($info) == 1 && !empty($info['id'])) {
69 $params[$key . '_id'] = $info['id'];
70 }
71 // Bother calling the api.
72 else {
73 $info['contact_id'] = CRM_Utils_Array::value('contact_id', $info, 'null');
74 $result = civicrm_api3($item, 'create', $info);
75 $entities[$key] = $result['values'][$result['id']];
76 $params[$key . '_id'] = $result['id'];
77 }
78 }
79 }
80 }
81 $dao = new CRM_Core_DAO_LocBlock();
82 $dao->copyValues($params);
83 $dao->save();
84 if (!empty($dao->id)) {
85 $values = [$dao->id => $entities];
86 _civicrm_api3_object_to_array($dao, $values[$dao->id]);
87 return civicrm_api3_create_success($values, $params, 'LocBlock', 'create', $dao);
88 }
89 throw new API_Exception('Unable to create LocBlock. Please check your params.');
90 }
91
92 /**
93 * Returns array of loc_blocks matching a set of one or more properties.
94 *
95 * @param array $params
96 * Array of one or more valid property_name=>value pairs. If $params is set.
97 * as null, all loc_blocks will be returned (default limit is 25).
98 *
99 * @return array
100 * API result array.
101 */
102 function civicrm_api3_loc_block_get($params) {
103 $options = _civicrm_api3_get_options_from_params($params);
104 // If a return param has been set then fetch the appropriate fk objects
105 // This is a helper because api chaining does not work with a key like 'email_2'.
106 if (!empty($options['return'])) {
107 unset($params['return']);
108 $values = [];
109 $items = ['address', 'email', 'phone', 'im'];
110 $returnAll = !empty($options['return']['all']);
111 foreach (_civicrm_api3_basic_get('CRM_Core_DAO_LocBlock', $params, FALSE) as $val) {
112 foreach ($items as $item) {
113 foreach (['', '_2'] as $suf) {
114 $key = $item . $suf;
115 if (!empty($val[$key . '_id']) && ($returnAll || !empty($options['return'][$key]))) {
116 $val[$key] = civicrm_api($item, 'getsingle', ['version' => 3, 'id' => $val[$key . '_id']]);
117 }
118 }
119 }
120 $values[$val['id']] = $val;
121 }
122 return civicrm_api3_create_success($values, $params, 'LocBlock', 'get');
123 }
124 return _civicrm_api3_basic_get('CRM_Core_DAO_LocBlock', $params);
125 }
126
127 /**
128 * Delete an existing LocBlock.
129 *
130 * @param array $params
131 * Array containing id of the record to be deleted.
132 *
133 * @return array
134 * API result array.
135 */
136 function civicrm_api3_loc_block_delete($params) {
137 return _civicrm_api3_basic_delete('CRM_Core_DAO_LocBlock', $params);
138 }