Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
1f4ea726 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
c28e1768 | 29 | * This api exposes CiviCRM Pledge. |
6a488035 TO |
30 | * |
31 | * @package CiviCRM_APIv3 | |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
22242c87 | 35 | * Create or updates an Pledge. |
6a488035 | 36 | * |
2e2605fe EM |
37 | * @param $params |
38 | * | |
a6c01b45 | 39 | * @return array |
72b3a70c | 40 | * Array containing 'is_error' to denote success or failure and details of the created pledge |
2e2605fe | 41 | * @throws \API_Exception |
6a488035 TO |
42 | */ |
43 | function civicrm_api3_pledge_create($params) { | |
44 | _civicrm_api3_pledge_format_params($params, TRUE); | |
98fd592b | 45 | return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Pledge'); |
6a488035 TO |
46 | } |
47 | ||
48 | /** | |
22242c87 | 49 | * Delete a pledge. |
6a488035 | 50 | * |
cf470720 TO |
51 | * @param array $params |
52 | * Array included 'pledge_id' of pledge to delete. | |
6a488035 | 53 | * |
22242c87 | 54 | * @return array |
6a488035 | 55 | */ |
1c88e578 | 56 | function civicrm_api3_pledge_delete($params) { |
57 | if (CRM_Pledge_BAO_Pledge::deletePledge($params['id'])) { | |
58 | return civicrm_api3_create_success(array( | |
21dfd5f5 | 59 | 'id' => $params['id'], |
244bbdd8 | 60 | ), $params, 'Pledge', 'delete'); |
1c88e578 | 61 | } |
62 | else { | |
63 | return civicrm_api3_create_error('Could not delete pledge'); | |
64 | } | |
6a488035 TO |
65 | } |
66 | ||
aa1b1481 | 67 | /** |
22242c87 EM |
68 | * Adjust metadata for pledge delete action. |
69 | * | |
c490a46a | 70 | * @param array $params |
aa1b1481 | 71 | */ |
6a488035 TO |
72 | function _civicrm_api3_pledge_delete_spec(&$params) { |
73 | // set as not required as pledge_id also acceptable & no either/or std yet | |
74 | $params['id']['api.aliases'] = array('pledge_id'); | |
75 | } | |
11e09c59 TO |
76 | |
77 | /** | |
22242c87 EM |
78 | * Adjust field specification specific to get requests. |
79 | * | |
d0997921 | 80 | * @param array $params |
6a488035 TO |
81 | */ |
82 | function _civicrm_api3_pledge_get_spec(&$params) { | |
83 | $params['next_pay_date'] = array( | |
84 | 'name' => 'next_pay_date', | |
85 | 'type' => 12, | |
86 | 'title' => 'Pledge Made', | |
87 | 'api.filter' => 0, | |
88 | 'api.return' => 1, | |
89 | ); | |
90 | $params['pledge_is_test']['api.default'] = 0; | |
91 | $params['pledge_financial_type_id']['api.aliases'] = array('contribution_type_id', 'contribution_type'); | |
92 | ||
93 | } | |
94 | ||
11e09c59 | 95 | /** |
22242c87 EM |
96 | * Adjust field specification specific to get requests. |
97 | * | |
d0997921 | 98 | * @param array $params |
6a488035 TO |
99 | */ |
100 | function _civicrm_api3_pledge_create_spec(&$params) { | |
101 | ||
102 | $required = array('contact_id', 'amount', 'installments', 'start_date', 'financial_type_id'); | |
103 | foreach ($required as $required_field) { | |
104 | $params[$required_field]['api.required'] = 1; | |
105 | } | |
106 | // @todo this can come from xml | |
107 | $params['amount']['api.aliases'] = array('pledge_amount'); | |
108 | $params['financial_type_id']['api.aliases'] = array('contribution_type_id', 'contribution_type'); | |
109 | } | |
110 | ||
111 | /** | |
22242c87 | 112 | * Retrieve a set of pledges, given a set of input params. |
6a488035 | 113 | * |
cf470720 TO |
114 | * @param array $params |
115 | * Input parameters. Use interrogate for possible fields. | |
6a488035 | 116 | * |
a6c01b45 | 117 | * @return array |
72b3a70c | 118 | * array of pledges, if error an array with an error id and error message |
6a488035 TO |
119 | */ |
120 | function civicrm_api3_pledge_get($params) { | |
82f7d8b2 | 121 | $mode = CRM_Contact_BAO_Query::MODE_PLEDGE; |
82f7d8b2 | 122 | |
244bbdd8 | 123 | list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, 'Pledge'); |
6a488035 | 124 | |
6a488035 TO |
125 | $pledge = array(); |
126 | while ($dao->fetch()) { | |
127 | $pledge[$dao->pledge_id] = $query->store($dao); | |
128 | } | |
129 | ||
244bbdd8 | 130 | return civicrm_api3_create_success($pledge, $params, 'Pledge', 'get', $dao); |
6a488035 TO |
131 | } |
132 | ||
11e09c59 | 133 | /** |
22242c87 | 134 | * Set default to not return test params. |
6a488035 TO |
135 | */ |
136 | function _civicrm_api3_pledge_get_defaults() { | |
137 | return array('pledge_test' => 0); | |
138 | } | |
139 | ||
140 | /** | |
22242c87 EM |
141 | * Legacy function to format pledge parameters. |
142 | * | |
143 | * I removed a bunch of stuff no longer required from here but it still needs | |
6a488035 TO |
144 | * more culling |
145 | * take the input parameter list as specified in the data model and | |
146 | * convert it into the same format that we use in QF and BAO object | |
147 | * | |
cf470720 TO |
148 | * @param array $values |
149 | * The reformatted properties that we can use internally. | |
6a488035 | 150 | */ |
54f1aa2a | 151 | function _civicrm_api3_pledge_format_params(&$values) { |
6a488035 TO |
152 | |
153 | // probably most of the below can be removed.... just needs a little more review | |
154 | if (array_key_exists('original_installment_amount', $values)) { | |
155 | $values['installment_amount'] = $values['original_installment_amount']; | |
156 | //it seems it will only create correctly with BOTH installment amount AND pledge_installment_amount set | |
157 | //pledge installment amount required for pledge payments | |
158 | $values['pledge_original_installment_amount'] = $values['original_installment_amount']; | |
159 | } | |
160 | ||
161 | if (array_key_exists('pledge_original_installment_amount', $values)) { | |
162 | $values['installment_amount'] = $values['pledge_original_installment_amount']; | |
163 | } | |
164 | ||
165 | if (empty($values['id'])) { | |
166 | //at this point both should be the same so unset both if not set - passing in empty | |
167 | //value causes crash rather creating new - do it before next section as null values ignored in 'switch' | |
168 | unset($values['id']); | |
169 | ||
170 | //if you have a single installment when creating & you don't set the pledge status (not a required field) then | |
171 | //status id is left null for pledge payments in BAO | |
172 | // so we are hacking in the addition of the pledge_status_id to pending here | |
173 | if (empty($values['status_id']) && $values['installments'] == 1) { | |
174 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
175 | $values['status_id'] = array_search('Pending', $contributionStatus); | |
176 | } | |
177 | } | |
178 | if (empty($values['scheduled_date']) && array_key_exists('start_date', $values)) { | |
179 | $values['scheduled_date'] = $values['start_date']; | |
180 | } | |
181 | } |