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