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