Merge branch '5.34' of https://github.com/civicrm/civicrm-core into upit
[civicrm-core.git] / api / v3 / LocBlock.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
a30c801b
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035 11
6a488035 12/**
244bbdd8 13 * This api exposes CiviCRM LocBlock records.
6a488035
TO
14 *
15 * @package CiviCRM_APIv3
6a488035
TO
16 */
17
18/**
244bbdd8 19 * Create or update a LocBlock.
6a488035 20 *
cf470720 21 * @param array $params
a1638b92 22 * Name/value pairs to insert in new 'LocBlock'.
6a488035 23 *
a6c01b45 24 * @return array
1747ab99 25 * API result array.
df197a56 26 *
27 * @throws \API_Exception
9a350644 28 * @throws \CiviCRM_API3_Exception
6a488035
TO
29 */
30function civicrm_api3_loc_block_create($params) {
cf8f0fff
CW
31 $entities = [];
32 $any_mandatory = [
210737b6
CB
33 'address',
34 'address_id',
35 'phone',
36 'phone_id',
37 'im',
38 'im_id',
39 'email',
a1638b92 40 'email_id',
cf8f0fff 41 ];
210737b6
CB
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.
6a488035 44 // This is basically chaining but in reverse - we create the sub-entities first
210737b6 45 // because chaining does not work in reverse, or with keys like 'email_2'.
cf8f0fff 46 $items = ['address', 'email', 'phone', 'im'];
6a488035 47 foreach ($items as $item) {
cf8f0fff 48 foreach (['', '_2'] as $suf) {
6a488035
TO
49 $key = $item . $suf;
50 if (!empty($params[$key]) && is_array($params[$key])) {
51 $info = $params[$key];
a1638b92 52 // If all we get is an id don't bother calling the api.
6a488035
TO
53 if (count($info) == 1 && !empty($info['id'])) {
54 $params[$key . '_id'] = $info['id'];
55 }
a1638b92 56 // Bother calling the api.
6a488035 57 else {
6a488035 58 $info['contact_id'] = CRM_Utils_Array::value('contact_id', $info, 'null');
df197a56 59 $result = civicrm_api3($item, 'create', $info);
6a488035
TO
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)) {
cf8f0fff 70 $values = [$dao->id => $entities];
6a488035 71 _civicrm_api3_object_to_array($dao, $values[$dao->id]);
244bbdd8 72 return civicrm_api3_create_success($values, $params, 'LocBlock', 'create', $dao);
6a488035 73 }
7c31ae57 74 throw new API_Exception('Unable to create LocBlock. Please check your params.');
6a488035
TO
75}
76
77/**
1747ab99 78 * Returns array of loc_blocks matching a set of one or more properties.
6a488035 79 *
cf470720
TO
80 * @param array $params
81 * Array of one or more valid property_name=>value pairs. If $params is set.
a1638b92 82 * as null, all loc_blocks will be returned (default limit is 25).
6a488035 83 *
a6c01b45 84 * @return array
1747ab99 85 * API result array.
6a488035
TO
86 */
87function 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
a1638b92 90 // This is a helper because api chaining does not work with a key like 'email_2'.
6a488035 91 if (!empty($options['return'])) {
82adafa2 92 unset($params['return']);
cf8f0fff
CW
93 $values = [];
94 $items = ['address', 'email', 'phone', 'im'];
6a488035
TO
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) {
cf8f0fff 98 foreach (['', '_2'] as $suf) {
6a488035
TO
99 $key = $item . $suf;
100 if (!empty($val[$key . '_id']) && ($returnAll || !empty($options['return'][$key]))) {
cf8f0fff 101 $val[$key] = civicrm_api($item, 'getsingle', ['version' => 3, 'id' => $val[$key . '_id']]);
6a488035
TO
102 }
103 }
104 }
105 $values[$val['id']] = $val;
106 }
244bbdd8 107 return civicrm_api3_create_success($values, $params, 'LocBlock', 'get');
6a488035
TO
108 }
109 return _civicrm_api3_basic_get('CRM_Core_DAO_LocBlock', $params);
110}
111
112/**
244bbdd8 113 * Delete an existing LocBlock.
6a488035 114 *
cf470720
TO
115 * @param array $params
116 * Array containing id of the record to be deleted.
6a488035 117 *
a6c01b45 118 * @return array
1747ab99 119 * API result array.
6a488035
TO
120 */
121function civicrm_api3_loc_block_delete($params) {
122 return _civicrm_api3_basic_delete('CRM_Core_DAO_LocBlock', $params);
123}