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