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