More comment fixes
[civicrm-core.git] / api / v3 / Pledge.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 * File for the CiviCRM APIv3 Pledge functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Pledge
33 *
731a0992 34 * @copyright CiviCRM LLC (c) 2004-2014
6a488035 35 * @version $Id: Pledge.php
6a488035
TO
36 */
37
38/**
22242c87 39 * Create or updates an Pledge.
6a488035 40 *
2e2605fe
EM
41 * @param $params
42 *
a6c01b45 43 * @return array
72b3a70c 44 * Array containing 'is_error' to denote success or failure and details of the created pledge
2e2605fe 45 * @throws \API_Exception
6a488035
TO
46 */
47function civicrm_api3_pledge_create($params) {
48 _civicrm_api3_pledge_format_params($params, TRUE);
49 $values = $params;
50 //format the custom fields
51 _civicrm_api3_custom_format_params($params, $values, 'Pledge');
52 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $values);
53}
54
55/**
22242c87 56 * Delete a pledge.
6a488035 57 *
cf470720
TO
58 * @param array $params
59 * Array included 'pledge_id' of pledge to delete.
6a488035 60 *
22242c87 61 * @return array
6a488035 62 */
1c88e578 63function civicrm_api3_pledge_delete($params) {
64 if (CRM_Pledge_BAO_Pledge::deletePledge($params['id'])) {
65 return civicrm_api3_create_success(array(
21dfd5f5 66 'id' => $params['id'],
1c88e578 67 ), $params, 'pledge', 'delete');
68 }
69 else {
70 return civicrm_api3_create_error('Could not delete pledge');
71 }
6a488035
TO
72}
73
aa1b1481 74/**
22242c87
EM
75 * Adjust metadata for pledge delete action.
76 *
c490a46a 77 * @param array $params
aa1b1481 78 */
6a488035
TO
79function _civicrm_api3_pledge_delete_spec(&$params) {
80 // set as not required as pledge_id also acceptable & no either/or std yet
81 $params['id']['api.aliases'] = array('pledge_id');
82}
11e09c59
TO
83
84/**
22242c87
EM
85 * Adjust field specification specific to get requests.
86 *
d0997921 87 * @param array $params
6a488035
TO
88 */
89function _civicrm_api3_pledge_get_spec(&$params) {
90 $params['next_pay_date'] = array(
91 'name' => 'next_pay_date',
92 'type' => 12,
93 'title' => 'Pledge Made',
94 'api.filter' => 0,
95 'api.return' => 1,
96 );
97 $params['pledge_is_test']['api.default'] = 0;
98 $params['pledge_financial_type_id']['api.aliases'] = array('contribution_type_id', 'contribution_type');
99
100}
101
11e09c59 102/**
22242c87
EM
103 * Adjust field specification specific to get requests.
104 *
d0997921 105 * @param array $params
6a488035
TO
106 */
107function _civicrm_api3_pledge_create_spec(&$params) {
108
109 $required = array('contact_id', 'amount', 'installments', 'start_date', 'financial_type_id');
110 foreach ($required as $required_field) {
111 $params[$required_field]['api.required'] = 1;
112 }
113 // @todo this can come from xml
114 $params['amount']['api.aliases'] = array('pledge_amount');
115 $params['financial_type_id']['api.aliases'] = array('contribution_type_id', 'contribution_type');
116}
117
118/**
22242c87 119 * Retrieve a set of pledges, given a set of input params.
6a488035 120 *
cf470720
TO
121 * @param array $params
122 * Input parameters. Use interrogate for possible fields.
6a488035 123 *
a6c01b45 124 * @return array
72b3a70c 125 * array of pledges, if error an array with an error id and error message
acb1052e 126 * {@getfields pledge_get}
6a488035 127 * @example PledgeGet.php
6a488035
TO
128 */
129function civicrm_api3_pledge_get($params) {
82f7d8b2
EM
130 $mode = CRM_Contact_BAO_Query::MODE_PLEDGE;
131 $entity = 'pledge';
132
133 list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, $entity);
6a488035 134
6a488035
TO
135 $pledge = array();
136 while ($dao->fetch()) {
137 $pledge[$dao->pledge_id] = $query->store($dao);
138 }
139
140 return civicrm_api3_create_success($pledge, $params, 'pledge', 'get', $dao);
141}
142
11e09c59 143/**
22242c87 144 * Set default to not return test params.
6a488035
TO
145 */
146function _civicrm_api3_pledge_get_defaults() {
147 return array('pledge_test' => 0);
148}
149
150/**
22242c87
EM
151 * Legacy function to format pledge parameters.
152 *
153 * I removed a bunch of stuff no longer required from here but it still needs
6a488035
TO
154 * more culling
155 * take the input parameter list as specified in the data model and
156 * convert it into the same format that we use in QF and BAO object
157 *
cf470720
TO
158 * @param array $values
159 * The reformatted properties that we can use internally.
6a488035 160 */
54f1aa2a 161function _civicrm_api3_pledge_format_params(&$values) {
6a488035
TO
162
163 // probably most of the below can be removed.... just needs a little more review
164 if (array_key_exists('original_installment_amount', $values)) {
165 $values['installment_amount'] = $values['original_installment_amount'];
166 //it seems it will only create correctly with BOTH installment amount AND pledge_installment_amount set
167 //pledge installment amount required for pledge payments
168 $values['pledge_original_installment_amount'] = $values['original_installment_amount'];
169 }
170
171 if (array_key_exists('pledge_original_installment_amount', $values)) {
172 $values['installment_amount'] = $values['pledge_original_installment_amount'];
173 }
174
175 if (empty($values['id'])) {
176 //at this point both should be the same so unset both if not set - passing in empty
177 //value causes crash rather creating new - do it before next section as null values ignored in 'switch'
178 unset($values['id']);
179
180 //if you have a single installment when creating & you don't set the pledge status (not a required field) then
181 //status id is left null for pledge payments in BAO
182 // so we are hacking in the addition of the pledge_status_id to pending here
183 if (empty($values['status_id']) && $values['installments'] == 1) {
184 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
185 $values['status_id'] = array_search('Pending', $contributionStatus);
186 }
187 }
188 if (empty($values['scheduled_date']) && array_key_exists('start_date', $values)) {
189 $values['scheduled_date'] = $values['start_date'];
190 }
191}