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