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