4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * File for the CiviCRM APIv3 Pledge functions
32 * @package CiviCRM_APIv3
33 * @subpackage API_Pledge
35 * @copyright CiviCRM LLC (c) 2004-2014
36 * @version $Id: Pledge.php
41 * Include utility functions
45 * Creates or updates an Activity. See the example for usage
47 * @param array $params
48 * Associative array of property name/value.
49 * pairs for the activity.
50 * {@getfields pledge_create}
53 * Array containing 'is_error' to denote success or failure and details of the created pledge
55 * @example PledgeCreate.php Standard create example
58 function civicrm_api3_pledge_create($params) {
59 _civicrm_api3_pledge_format_params($params, TRUE);
61 //format the custom fields
62 _civicrm_api3_custom_format_params($params, $values, 'Pledge');
63 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $values);
69 * @param array $params
70 * Array included 'pledge_id' of pledge to delete.
73 * true if success, else false
75 * {@getfields pledge_delete}
76 * @example PledgeDelete.php
79 function civicrm_api3_pledge_delete($params) {
80 if (CRM_Pledge_BAO_Pledge
::deletePledge($params['id'])) {
81 return civicrm_api3_create_success(array(
82 'id' => $params['id'],
83 ), $params, 'pledge', 'delete');
86 return civicrm_api3_create_error('Could not delete pledge');
91 * @param array $params
93 function _civicrm_api3_pledge_delete_spec(&$params) {
94 // set as not required as pledge_id also acceptable & no either/or std yet
95 $params['id']['api.aliases'] = array('pledge_id');
99 * return field specification specific to get requests
101 function _civicrm_api3_pledge_get_spec(&$params) {
102 $params['next_pay_date'] = array(
103 'name' => 'next_pay_date',
105 'title' => 'Pledge Made',
109 $params['pledge_is_test']['api.default'] = 0;
110 $params['pledge_financial_type_id']['api.aliases'] = array('contribution_type_id', 'contribution_type');
115 * return field specification specific to get requests
117 function _civicrm_api3_pledge_create_spec(&$params) {
119 $required = array('contact_id', 'amount', 'installments', 'start_date', 'financial_type_id');
120 foreach ($required as $required_field) {
121 $params[$required_field]['api.required'] = 1;
123 // @todo this can come from xml
124 $params['amount']['api.aliases'] = array('pledge_amount');
125 $params['financial_type_id']['api.aliases'] = array('contribution_type_id', 'contribution_type');
129 * Retrieve a set of pledges, given a set of input params
131 * @param array $params
132 * Input parameters. Use interrogate for possible fields.
135 * array of pledges, if error an array with an error id and error message
136 * {@getfields pledge_get}
137 * @example PledgeGet.php
140 function civicrm_api3_pledge_get($params) {
141 $mode = CRM_Contact_BAO_Query
::MODE_PLEDGE
;
144 list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, $entity);
147 while ($dao->fetch()) {
148 $pledge[$dao->pledge_id
] = $query->store($dao);
151 return civicrm_api3_create_success($pledge, $params, 'pledge', 'get', $dao);
155 * Set default to not return test params
157 function _civicrm_api3_pledge_get_defaults() {
158 return array('pledge_test' => 0);
162 * Legacy function - I removed a bunch of stuff no longer required from here but it still needs
164 * take the input parameter list as specified in the data model and
165 * convert it into the same format that we use in QF and BAO object
167 * @param array $values
168 * The reformatted properties that we can use internally.
169 * @param bool $create
171 * @return array|CRM_Error
174 function _civicrm_api3_pledge_format_params(&$values, $create = FALSE) {
176 // probably most of the below can be removed.... just needs a little more review
177 if (array_key_exists('original_installment_amount', $values)) {
178 $values['installment_amount'] = $values['original_installment_amount'];
179 //it seems it will only create correctly with BOTH installment amount AND pledge_installment_amount set
180 //pledge installment amount required for pledge payments
181 $values['pledge_original_installment_amount'] = $values['original_installment_amount'];
184 if (array_key_exists('pledge_original_installment_amount', $values)) {
185 $values['installment_amount'] = $values['pledge_original_installment_amount'];
188 if (empty($values['id'])) {
189 //at this point both should be the same so unset both if not set - passing in empty
190 //value causes crash rather creating new - do it before next section as null values ignored in 'switch'
191 unset($values['id']);
193 //if you have a single installment when creating & you don't set the pledge status (not a required field) then
194 //status id is left null for pledge payments in BAO
195 // so we are hacking in the addition of the pledge_status_id to pending here
196 if (empty($values['status_id']) && $values['installments'] == 1) {
197 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
198 $values['status_id'] = array_search('Pending', $contributionStatus);
201 if (empty($values['scheduled_date']) && array_key_exists('start_date', $values)) {
202 $values['scheduled_date'] = $values['start_date'];