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