5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
31 * File for the CiviCRM APIv3 Pledge functions
33 * @package CiviCRM_APIv3
34 * @subpackage API_Pledge
36 * @copyright CiviCRM LLC (c) 2004-2013
37 * @version $Id: Pledge.php
42 * Include utility functions
46 * Creates or updates an Activity. See the example for usage
48 * @param array $params Associative array of property name/value
49 * pairs for the activity.
50 * {@getfields pledge_create}
52 * @return array Array containing 'is_error' to denote success or failure and details of the created pledge
54 * @example PledgeCreate.php Standard create example
57 function civicrm_api3_pledge_create($params) {
58 _civicrm_api3_pledge_format_params($params, TRUE);
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);
68 * @param array $params array included 'pledge_id' of pledge to delete
70 * @return boolean true if success, else false
72 * {@getfields pledge_delete}
73 * @example PledgeDelete.php
76 function civicrm_api3_pledge_delete($params) {
77 if (CRM_Pledge_BAO_Pledge
::deletePledge($params['id'])) {
78 return civicrm_api3_create_success(array(
80 ), $params, 'pledge', 'delete');
83 return civicrm_api3_create_error('Could not delete pledge');
87 function _civicrm_api3_pledge_delete_spec(&$params) {
88 // set as not required as pledge_id also acceptable & no either/or std yet
89 $params['id']['api.aliases'] = array('pledge_id');
93 * return field specification specific to get requests
95 function _civicrm_api3_pledge_get_spec(&$params) {
96 $params['next_pay_date'] = array(
97 'name' => 'next_pay_date',
99 'title' => 'Pledge Made',
103 $params['pledge_is_test']['api.default'] = 0;
104 $params['pledge_financial_type_id']['api.aliases'] = array('contribution_type_id', 'contribution_type');
109 * return field specification specific to get requests
111 function _civicrm_api3_pledge_create_spec(&$params) {
113 $required = array('contact_id', 'amount', 'installments', 'start_date', 'financial_type_id');
114 foreach ($required as $required_field) {
115 $params[$required_field]['api.required'] = 1;
117 // @todo this can come from xml
118 $params['amount']['api.aliases'] = array('pledge_amount');
119 $params['financial_type_id']['api.aliases'] = array('contribution_type_id', 'contribution_type');
123 * Retrieve a set of pledges, given a set of input params
125 * @param array $params (reference ) input parameters. Use interogate for possible fields
127 * @return array (reference ) array of pledges, if error an array with an error id and error message
128 * {@getfields pledge_get}
129 * @example PledgeGet.php
132 function civicrm_api3_pledge_get($params) {
134 $options = _civicrm_api3_get_options_from_params($params, TRUE, 'pledge','get');
135 if (empty($options['return'])) {
136 $options['return'] = CRM_Pledge_BAO_Query
::defaultReturnProperties(CRM_Contact_BAO_Query
::MODE_PLEDGE
);
139 $options['return']['pledge_id'] = 1;
141 $newParams = CRM_Contact_BAO_Query
::convertFormValues($options['input_params']);
142 $query = new CRM_Contact_BAO_Query($newParams, $options['return'], NULL,
143 FALSE, FALSE, CRM_Contact_BAO_Query
::MODE_PLEDGE
145 list($select, $from, $where) = $query->query();
146 $sql = "$select $from $where";
147 if (!empty($options['sort'])) {
148 $sql .= " ORDER BY " . $options['sort'];
150 $sql .= " LIMIT " . $options['offset'] . " , " . $options['limit'];
151 $dao = CRM_Core_DAO
::executeQuery($sql);
153 while ($dao->fetch()) {
154 $pledge[$dao->pledge_id
] = $query->store($dao);
157 return civicrm_api3_create_success($pledge, $params, 'pledge', 'get', $dao);
161 * Set default to not return test params
163 function _civicrm_api3_pledge_get_defaults() {
164 return array('pledge_test' => 0);
168 * Legacy function - I removed a bunch of stuff no longer required from here but it still needs
170 * take the input parameter list as specified in the data model and
171 * convert it into the same format that we use in QF and BAO object
173 * @param array $params Associative array of property name/value
174 * pairs to insert in new contact.
175 * @param array $values The reformatted properties that we can use internally
178 * @return array|CRM_Error
181 function _civicrm_api3_pledge_format_params(&$values, $create = FALSE) {
183 // probably most of the below can be removed.... just needs a little more review
184 if (array_key_exists('original_installment_amount', $values)) {
185 $values['installment_amount'] = $values['original_installment_amount'];
186 //it seems it will only create correctly with BOTH installment amount AND pledge_installment_amount set
187 //pledge installment amount required for pledge payments
188 $values['pledge_original_installment_amount'] = $values['original_installment_amount'];
191 if (array_key_exists('pledge_original_installment_amount', $values)) {
192 $values['installment_amount'] = $values['pledge_original_installment_amount'];
195 if (empty($values['id'])) {
196 //at this point both should be the same so unset both if not set - passing in empty
197 //value causes crash rather creating new - do it before next section as null values ignored in 'switch'
198 unset($values['id']);
200 //if you have a single installment when creating & you don't set the pledge status (not a required field) then
201 //status id is left null for pledge payments in BAO
202 // so we are hacking in the addition of the pledge_status_id to pending here
203 if (empty($values['status_id']) && $values['installments'] == 1) {
204 $contributionStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
205 $values['status_id'] = array_search('Pending', $contributionStatus);
208 if (empty($values['scheduled_date']) && array_key_exists('start_date', $values)) {
209 $values['scheduled_date'] = $values['start_date'];