Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 | 27 | |
6a488035 | 28 | /** |
244bbdd8 | 29 | * This api exposes CiviCRM LocBlock records. |
6a488035 TO |
30 | * |
31 | * @package CiviCRM_APIv3 | |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
244bbdd8 | 35 | * Create or update a LocBlock. |
6a488035 | 36 | * |
cf470720 | 37 | * @param array $params |
a1638b92 | 38 | * Name/value pairs to insert in new 'LocBlock'. |
6a488035 | 39 | * |
a6c01b45 | 40 | * @return array |
1747ab99 | 41 | * API result array. |
df197a56 | 42 | * |
43 | * @throws \API_Exception | |
9a350644 | 44 | * @throws \CiviCRM_API3_Exception |
6a488035 TO |
45 | */ |
46 | function civicrm_api3_loc_block_create($params) { | |
cf8f0fff CW |
47 | $entities = []; |
48 | $any_mandatory = [ | |
210737b6 CB |
49 | 'address', |
50 | 'address_id', | |
51 | 'phone', | |
52 | 'phone_id', | |
53 | 'im', | |
54 | 'im_id', | |
55 | 'email', | |
a1638b92 | 56 | 'email_id', |
cf8f0fff | 57 | ]; |
210737b6 CB |
58 | civicrm_api3_verify_one_mandatory($params, NULL, $any_mandatory); |
59 | // Call the appropriate api to create entities if any are passed in the params. | |
6a488035 | 60 | // This is basically chaining but in reverse - we create the sub-entities first |
210737b6 | 61 | // because chaining does not work in reverse, or with keys like 'email_2'. |
cf8f0fff | 62 | $items = ['address', 'email', 'phone', 'im']; |
6a488035 | 63 | foreach ($items as $item) { |
cf8f0fff | 64 | foreach (['', '_2'] as $suf) { |
6a488035 TO |
65 | $key = $item . $suf; |
66 | if (!empty($params[$key]) && is_array($params[$key])) { | |
67 | $info = $params[$key]; | |
a1638b92 | 68 | // If all we get is an id don't bother calling the api. |
6a488035 TO |
69 | if (count($info) == 1 && !empty($info['id'])) { |
70 | $params[$key . '_id'] = $info['id']; | |
71 | } | |
a1638b92 | 72 | // Bother calling the api. |
6a488035 | 73 | else { |
6a488035 | 74 | $info['contact_id'] = CRM_Utils_Array::value('contact_id', $info, 'null'); |
df197a56 | 75 | $result = civicrm_api3($item, 'create', $info); |
6a488035 TO |
76 | $entities[$key] = $result['values'][$result['id']]; |
77 | $params[$key . '_id'] = $result['id']; | |
78 | } | |
79 | } | |
80 | } | |
81 | } | |
82 | $dao = new CRM_Core_DAO_LocBlock(); | |
83 | $dao->copyValues($params); | |
84 | $dao->save(); | |
85 | if (!empty($dao->id)) { | |
cf8f0fff | 86 | $values = [$dao->id => $entities]; |
6a488035 | 87 | _civicrm_api3_object_to_array($dao, $values[$dao->id]); |
244bbdd8 | 88 | return civicrm_api3_create_success($values, $params, 'LocBlock', 'create', $dao); |
6a488035 | 89 | } |
7c31ae57 | 90 | throw new API_Exception('Unable to create LocBlock. Please check your params.'); |
6a488035 TO |
91 | } |
92 | ||
93 | /** | |
1747ab99 | 94 | * Returns array of loc_blocks matching a set of one or more properties. |
6a488035 | 95 | * |
cf470720 TO |
96 | * @param array $params |
97 | * Array of one or more valid property_name=>value pairs. If $params is set. | |
a1638b92 | 98 | * as null, all loc_blocks will be returned (default limit is 25). |
6a488035 | 99 | * |
a6c01b45 | 100 | * @return array |
1747ab99 | 101 | * API result array. |
6a488035 TO |
102 | */ |
103 | function civicrm_api3_loc_block_get($params) { | |
104 | $options = _civicrm_api3_get_options_from_params($params); | |
105 | // If a return param has been set then fetch the appropriate fk objects | |
a1638b92 | 106 | // This is a helper because api chaining does not work with a key like 'email_2'. |
6a488035 | 107 | if (!empty($options['return'])) { |
82adafa2 | 108 | unset($params['return']); |
cf8f0fff CW |
109 | $values = []; |
110 | $items = ['address', 'email', 'phone', 'im']; | |
6a488035 TO |
111 | $returnAll = !empty($options['return']['all']); |
112 | foreach (_civicrm_api3_basic_get('CRM_Core_DAO_LocBlock', $params, FALSE) as $val) { | |
113 | foreach ($items as $item) { | |
cf8f0fff | 114 | foreach (['', '_2'] as $suf) { |
6a488035 TO |
115 | $key = $item . $suf; |
116 | if (!empty($val[$key . '_id']) && ($returnAll || !empty($options['return'][$key]))) { | |
cf8f0fff | 117 | $val[$key] = civicrm_api($item, 'getsingle', ['version' => 3, 'id' => $val[$key . '_id']]); |
6a488035 TO |
118 | } |
119 | } | |
120 | } | |
121 | $values[$val['id']] = $val; | |
122 | } | |
244bbdd8 | 123 | return civicrm_api3_create_success($values, $params, 'LocBlock', 'get'); |
6a488035 TO |
124 | } |
125 | return _civicrm_api3_basic_get('CRM_Core_DAO_LocBlock', $params); | |
126 | } | |
127 | ||
128 | /** | |
244bbdd8 | 129 | * Delete an existing LocBlock. |
6a488035 | 130 | * |
cf470720 TO |
131 | * @param array $params |
132 | * Array containing id of the record to be deleted. | |
6a488035 | 133 | * |
a6c01b45 | 134 | * @return array |
1747ab99 | 135 | * API result array. |
6a488035 TO |
136 | */ |
137 | function civicrm_api3_loc_block_delete($params) { | |
138 | return _civicrm_api3_basic_delete('CRM_Core_DAO_LocBlock', $params); | |
139 | } |