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