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