Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | /* | |
5 | +--------------------------------------------------------------------+ | |
6 | | CiviCRM version 4.3 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
9 | +--------------------------------------------------------------------+ | |
10 | | This file is a part of CiviCRM. | | |
11 | | | | |
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. | | |
15 | | | | |
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. | | |
20 | | | | |
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 | +--------------------------------------------------------------------+ | |
28 | */ | |
29 | ||
30 | /** | |
31 | * File for the CiviCRM APIv3 Pledge functions | |
32 | * | |
33 | * @package CiviCRM_APIv3 | |
34 | * @subpackage API_Pledge | |
35 | * | |
36 | * @copyright CiviCRM LLC (c) 2004-2013 | |
37 | * @version $Id: Pledge.php | |
38 | * | |
39 | */ | |
40 | ||
41 | /** | |
42 | * Include utility functions | |
43 | */ | |
44 | ||
45 | /** | |
46 | * Creates or updates an Activity. See the example for usage | |
47 | * | |
48 | * @param array $params Associative array of property name/value | |
49 | * pairs for the activity. | |
50 | * {@getfields pledge_create} | |
51 | * | |
52 | * @return array Array containing 'is_error' to denote success or failure and details of the created pledge | |
53 | * | |
54 | * @example PledgeCreate.php Standard create example | |
55 | * | |
56 | */ | |
57 | function 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 | * | |
68 | * @param array $params array included 'pledge_id' of pledge to delete | |
69 | * | |
70 | * @return boolean true if success, else false | |
71 | * @static void | |
72 | * {@getfields pledge_delete} | |
73 | * @example PledgeDelete.php | |
74 | * @access public | |
75 | */ | |
76 | function civicrm_api3_pledge_delete($params) {\r | |
77 | if (CRM_Pledge_BAO_Pledge::deletePledge($params['id'])) {\r | |
78 | return civicrm_api3_create_success(array(\r | |
79 | 'id' => $params['id']\r | |
80 | ), $params, 'pledge', 'delete');\r | |
81 | }\r | |
82 | else {\r | |
83 | return civicrm_api3_create_error('Could not delete pledge');\r | |
84 | }\r | |
85 | } | |
86 | ||
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'); | |
90 | } | |
11e09c59 TO |
91 | |
92 | /** | |
6a488035 TO |
93 | * return field specification specific to get requests |
94 | */ | |
95 | function _civicrm_api3_pledge_get_spec(&$params) { | |
96 | $params['next_pay_date'] = array( | |
97 | 'name' => 'next_pay_date', | |
98 | 'type' => 12, | |
99 | 'title' => 'Pledge Made', | |
100 | 'api.filter' => 0, | |
101 | 'api.return' => 1, | |
102 | ); | |
103 | $params['pledge_is_test']['api.default'] = 0; | |
104 | $params['pledge_financial_type_id']['api.aliases'] = array('contribution_type_id', 'contribution_type'); | |
105 | ||
106 | } | |
107 | ||
11e09c59 | 108 | /** |
6a488035 TO |
109 | * return field specification specific to get requests |
110 | */ | |
111 | function _civicrm_api3_pledge_create_spec(&$params) { | |
112 | ||
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; | |
116 | } | |
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'); | |
120 | } | |
121 | ||
122 | /** | |
123 | * Retrieve a set of pledges, given a set of input params | |
124 | * | |
125 | * @param array $params (reference ) input parameters. Use interogate for possible fields | |
126 | * | |
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 | |
130 | * @access public | |
131 | */ | |
132 | function civicrm_api3_pledge_get($params) { | |
133 | ||
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); | |
137 | } | |
138 | else { | |
139 | $options['return']['pledge_id'] = 1; | |
140 | } | |
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 | |
144 | ); | |
145 | list($select, $from, $where) = $query->query(); | |
146 | $sql = "$select $from $where"; | |
147 | if (!empty($options['sort'])) { | |
148 | $sql .= " ORDER BY " . $options['sort']; | |
149 | } | |
150 | $sql .= " LIMIT " . $options['offset'] . " , " . $options['limit']; | |
151 | $dao = CRM_Core_DAO::executeQuery($sql); | |
152 | $pledge = array(); | |
153 | while ($dao->fetch()) { | |
154 | $pledge[$dao->pledge_id] = $query->store($dao); | |
155 | } | |
156 | ||
157 | return civicrm_api3_create_success($pledge, $params, 'pledge', 'get', $dao); | |
158 | } | |
159 | ||
11e09c59 | 160 | /** |
6a488035 TO |
161 | * Set default to not return test params |
162 | */ | |
163 | function _civicrm_api3_pledge_get_defaults() { | |
164 | return array('pledge_test' => 0); | |
165 | } | |
166 | ||
167 | /** | |
168 | * Legacy function - I removed a bunch of stuff no longer required from here but it still needs | |
169 | * more culling | |
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 | |
172 | * | |
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 | |
176 | * ' | |
177 | * | |
178 | * @return array|CRM_Error | |
179 | * @access public | |
180 | */ | |
181 | function _civicrm_api3_pledge_format_params(&$values, $create = FALSE) { | |
182 | ||
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']; | |
189 | } | |
190 | ||
191 | if (array_key_exists('pledge_original_installment_amount', $values)) { | |
192 | $values['installment_amount'] = $values['pledge_original_installment_amount']; | |
193 | } | |
194 | ||
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']); | |
199 | ||
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); | |
206 | } | |
207 | } | |
208 | if (empty($values['scheduled_date']) && array_key_exists('start_date', $values)) { | |
209 | $values['scheduled_date'] = $values['start_date']; | |
210 | } | |
211 | } | |
212 |