Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
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 | /** | |
29 | * | |
30 | * @package CRM | |
f299f7db | 31 | * @copyright CiviCRM LLC (c) 2004-2020 |
6a488035 TO |
32 | */ |
33 | class CRM_Pledge_BAO_PledgeBlock extends CRM_Pledge_DAO_PledgeBlock { | |
34 | ||
35 | /** | |
fe482240 | 36 | * Class constructor. |
6a488035 | 37 | */ |
00be9182 | 38 | public function __construct() { |
6a488035 TO |
39 | parent::__construct(); |
40 | } | |
41 | ||
42 | /** | |
fe482240 EM |
43 | * Retrieve DB object based on input parameters. |
44 | * | |
45 | * It also stores all the retrieved values in the default array. | |
6a488035 | 46 | * |
3a1617b6 TO |
47 | * @param array $params |
48 | * (reference ) an assoc array of name/value pairs. | |
49 | * @param array $defaults | |
50 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 51 | * |
16b10e64 | 52 | * @return CRM_Pledge_BAO_PledgeBlock |
6a488035 | 53 | */ |
00be9182 | 54 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
55 | $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock(); |
56 | $pledgeBlock->copyValues($params); | |
57 | if ($pledgeBlock->find(TRUE)) { | |
58 | CRM_Core_DAO::storeValues($pledgeBlock, $defaults); | |
59 | return $pledgeBlock; | |
60 | } | |
61 | return NULL; | |
62 | } | |
63 | ||
64 | /** | |
fe482240 | 65 | * Takes an associative array and creates a pledgeBlock object. |
6a488035 | 66 | * |
3a1617b6 TO |
67 | * @param array $params |
68 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 69 | * |
16b10e64 | 70 | * @return CRM_Pledge_BAO_PledgeBlock |
6a488035 | 71 | */ |
00be9182 | 72 | public static function &create(&$params) { |
6a488035 TO |
73 | $transaction = new CRM_Core_Transaction(); |
74 | $pledgeBlock = self::add($params); | |
75 | ||
76 | if (is_a($pledgeBlock, 'CRM_Core_Error')) { | |
77 | $pledgeBlock->rollback(); | |
78 | return $pledgeBlock; | |
79 | } | |
80 | ||
81 | $params['id'] = $pledgeBlock->id; | |
82 | ||
83 | $transaction->commit(); | |
84 | ||
85 | return $pledgeBlock; | |
86 | } | |
87 | ||
88 | /** | |
fe482240 | 89 | * Add pledgeBlock. |
6a488035 | 90 | * |
3a1617b6 TO |
91 | * @param array $params |
92 | * Reference array contains the values submitted by the form. | |
6a488035 | 93 | * |
6a488035 TO |
94 | * |
95 | * @return object | |
96 | */ | |
00be9182 | 97 | public static function add(&$params) { |
6a488035 | 98 | |
a7488080 | 99 | if (!empty($params['id'])) { |
6a488035 TO |
100 | CRM_Utils_Hook::pre('edit', 'PledgeBlock', $params['id'], $params); |
101 | } | |
102 | else { | |
103 | CRM_Utils_Hook::pre('create', 'PledgeBlock', NULL, $params); | |
104 | } | |
105 | ||
106 | $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock(); | |
107 | ||
cc28438b | 108 | // fix for pledge_frequency_unit |
6a488035 TO |
109 | $freqUnits = CRM_Utils_Array::value('pledge_frequency_unit', $params); |
110 | ||
111 | if ($freqUnits && is_array($freqUnits)) { | |
112 | unset($params['pledge_frequency_unit']); | |
113 | $newFreqUnits = array(); | |
114 | foreach ($freqUnits as $k => $v) { | |
115 | if ($v) { | |
116 | $newFreqUnits[$k] = $v; | |
117 | } | |
118 | } | |
119 | ||
120 | $freqUnits = $newFreqUnits; | |
121 | if (is_array($freqUnits) && !empty($freqUnits)) { | |
122 | $freqUnits = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($freqUnits)); | |
123 | $pledgeBlock->pledge_frequency_unit = $freqUnits; | |
124 | } | |
125 | else { | |
126 | $pledgeBlock->pledge_frequency_unit = ''; | |
127 | } | |
128 | } | |
129 | ||
130 | $pledgeBlock->copyValues($params); | |
131 | $result = $pledgeBlock->save(); | |
132 | ||
a7488080 | 133 | if (!empty($params['id'])) { |
6a488035 TO |
134 | CRM_Utils_Hook::post('edit', 'PledgeBlock', $pledgeBlock->id, $pledgeBlock); |
135 | } | |
136 | else { | |
137 | CRM_Utils_Hook::post('create', 'Pledge', $pledgeBlock->id, $pledgeBlock); | |
138 | } | |
139 | ||
140 | return $result; | |
141 | } | |
142 | ||
143 | /** | |
fe482240 | 144 | * Delete the pledgeBlock. |
6a488035 | 145 | * |
3a1617b6 TO |
146 | * @param int $id |
147 | * PledgeBlock id. | |
6a488035 | 148 | * |
77b97be7 | 149 | * @return mixed|null |
6a488035 | 150 | */ |
00be9182 | 151 | public static function deletePledgeBlock($id) { |
6a488035 TO |
152 | CRM_Utils_Hook::pre('delete', 'PledgeBlock', $id, CRM_Core_DAO::$_nullArray); |
153 | ||
154 | $transaction = new CRM_Core_Transaction(); | |
155 | ||
156 | $results = NULL; | |
157 | ||
353ffa53 | 158 | $dao = new CRM_Pledge_DAO_PledgeBlock(); |
6a488035 TO |
159 | $dao->id = $id; |
160 | $results = $dao->delete(); | |
161 | ||
162 | $transaction->commit(); | |
163 | ||
164 | CRM_Utils_Hook::post('delete', 'PledgeBlock', $dao->id, $dao); | |
165 | ||
166 | return $results; | |
167 | } | |
168 | ||
169 | /** | |
fe482240 | 170 | * Return Pledge Block info in Contribution Pages. |
6a488035 | 171 | * |
3a1617b6 TO |
172 | * @param int $pageID |
173 | * Contribution page id. | |
77b97be7 EM |
174 | * |
175 | * @return array | |
6a488035 | 176 | */ |
00be9182 | 177 | public static function getPledgeBlock($pageID) { |
6a488035 TO |
178 | $pledgeBlock = array(); |
179 | ||
353ffa53 | 180 | $dao = new CRM_Pledge_DAO_PledgeBlock(); |
6a488035 | 181 | $dao->entity_table = 'civicrm_contribution_page'; |
353ffa53 | 182 | $dao->entity_id = $pageID; |
6a488035 TO |
183 | if ($dao->find(TRUE)) { |
184 | CRM_Core_DAO::storeValues($dao, $pledgeBlock); | |
185 | } | |
186 | ||
187 | return $pledgeBlock; | |
188 | } | |
189 | ||
190 | /** | |
fe482240 | 191 | * Build Pledge Block in Contribution Pages. |
6a488035 | 192 | * |
c490a46a | 193 | * @param CRM_Core_Form $form |
6a488035 | 194 | */ |
00be9182 | 195 | public static function buildPledgeBlock($form) { |
6a488035 | 196 | //build pledge payment fields. |
a7488080 | 197 | if (!empty($form->_values['pledge_id'])) { |
6a488035 TO |
198 | //get all payments required details. |
199 | $allPayments = array(); | |
2f6befc8 AH |
200 | $returnProperties = array( |
201 | 'status_id', | |
202 | 'scheduled_date', | |
203 | 'scheduled_amount', | |
204 | 'currency', | |
205 | ); | |
6a488035 TO |
206 | CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id', |
207 | $form->_values['pledge_id'], $allPayments, $returnProperties | |
208 | ); | |
cc28438b | 209 | // get all status |
6a488035 TO |
210 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
211 | ||
353ffa53 TO |
212 | $nextPayment = array(); |
213 | $isNextPayment = FALSE; | |
6a488035 | 214 | $overduePayments = array(); |
6a488035 TO |
215 | foreach ($allPayments as $payID => $value) { |
216 | if ($allStatus[$value['status_id']] == 'Overdue') { | |
217 | $overduePayments[$payID] = array( | |
218 | 'id' => $payID, | |
219 | 'scheduled_amount' => CRM_Utils_Rule::cleanMoney($value['scheduled_amount']), | |
2f6befc8 | 220 | 'scheduled_amount_currency' => $value['currency'], |
6a488035 TO |
221 | 'scheduled_date' => CRM_Utils_Date::customFormat($value['scheduled_date'], |
222 | '%B %d' | |
223 | ), | |
224 | ); | |
225 | } | |
226 | elseif (!$isNextPayment && | |
227 | $allStatus[$value['status_id']] == 'Pending' | |
228 | ) { | |
cc28438b | 229 | // get the next payment. |
6a488035 TO |
230 | $nextPayment = array( |
231 | 'id' => $payID, | |
232 | 'scheduled_amount' => CRM_Utils_Rule::cleanMoney($value['scheduled_amount']), | |
2f6befc8 | 233 | 'scheduled_amount_currency' => $value['currency'], |
6a488035 TO |
234 | 'scheduled_date' => CRM_Utils_Date::customFormat($value['scheduled_date'], |
235 | '%B %d' | |
236 | ), | |
237 | ); | |
238 | $isNextPayment = TRUE; | |
239 | } | |
240 | } | |
241 | ||
cc28438b | 242 | // build check box array for payments. |
6a488035 TO |
243 | $payments = array(); |
244 | if (!empty($overduePayments)) { | |
245 | foreach ($overduePayments as $id => $payment) { | |
8c52df67 | 246 | $label = ts("%1 - due on %2 (overdue)", array( |
353ffa53 TO |
247 | 1 => CRM_Utils_Money::format(CRM_Utils_Array::value('scheduled_amount', $payment), CRM_Utils_Array::value('scheduled_amount_currency', $payment)), |
248 | 2 => CRM_Utils_Array::value('scheduled_date', $payment), | |
249 | )); | |
8c52df67 | 250 | $paymentID = CRM_Utils_Array::value('id', $payment); |
251 | $payments[] = $form->createElement('checkbox', $paymentID, NULL, $label, array('amount' => CRM_Utils_Array::value('scheduled_amount', $payment))); | |
6a488035 TO |
252 | } |
253 | } | |
254 | ||
255 | if (!empty($nextPayment)) { | |
8c52df67 | 256 | $label = ts("%1 - due on %2", array( |
353ffa53 TO |
257 | 1 => CRM_Utils_Money::format(CRM_Utils_Array::value('scheduled_amount', $nextPayment), CRM_Utils_Array::value('scheduled_amount_currency', $nextPayment)), |
258 | 2 => CRM_Utils_Array::value('scheduled_date', $nextPayment), | |
259 | )); | |
8c52df67 | 260 | $paymentID = CRM_Utils_Array::value('id', $nextPayment); |
261 | $payments[] = $form->createElement('checkbox', $paymentID, NULL, $label, array('amount' => CRM_Utils_Array::value('scheduled_amount', $nextPayment))); | |
6a488035 | 262 | } |
cc28438b | 263 | // give error if empty or build form for payment. |
6a488035 TO |
264 | if (empty($payments)) { |
265 | CRM_Core_Error::fatal(ts("Oops. It looks like there is no valid payment status for online payment.")); | |
266 | } | |
267 | else { | |
268 | $form->assign('is_pledge_payment', TRUE); | |
8c52df67 | 269 | $form->addGroup($payments, 'pledge_amount', ts('Make Pledge Payment(s):'), '<br />'); |
6a488035 TO |
270 | } |
271 | } | |
272 | else { | |
273 | ||
274 | $pledgeBlock = self::getPledgeBlock($form->_id); | |
275 | ||
cc28438b | 276 | // build form for pledge creation. |
098201d8 | 277 | $pledgeOptions = array( |
353ffa53 | 278 | '0' => ts('I want to make a one-time contribution'), |
6a488035 TO |
279 | '1' => ts('I pledge to contribute this amount every'), |
280 | ); | |
281 | $form->addRadio('is_pledge', ts('Pledge Frequency Interval'), $pledgeOptions, | |
282 | NULL, array('<br/>') | |
283 | ); | |
8cdd6191 | 284 | $form->addElement('text', 'pledge_installments', ts('Installments'), ['size' => 3, 'aria-label' => ts('Installments')]); |
6a488035 | 285 | |
a7488080 | 286 | if (!empty($pledgeBlock['is_pledge_interval'])) { |
6a488035 | 287 | $form->assign('is_pledge_interval', CRM_Utils_Array::value('is_pledge_interval', $pledgeBlock)); |
8cdd6191 | 288 | $form->addElement('text', 'pledge_frequency_interval', NULL, ['size' => 3, 'aria-label' => ts('Frequency Intervals')]); |
6a488035 TO |
289 | } |
290 | else { | |
291 | $form->add('hidden', 'pledge_frequency_interval', 1); | |
292 | } | |
cc28438b | 293 | // Frequency unit drop-down label suffixes switch from *ly to *(s) |
353ffa53 TO |
294 | $freqUnitVals = explode(CRM_Core_DAO::VALUE_SEPARATOR, $pledgeBlock['pledge_frequency_unit']); |
295 | $freqUnits = array(); | |
6a488035 TO |
296 | $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units'); |
297 | foreach ($freqUnitVals as $key => $val) { | |
298 | if (array_key_exists($val, $frequencyUnits)) { | |
0d8afee2 | 299 | $freqUnits[$val] = !empty($pledgeBlock['is_pledge_interval']) ? "{$frequencyUnits[$val]}(s)" : $frequencyUnits[$val]; |
6a488035 TO |
300 | } |
301 | } | |
8cdd6191 | 302 | $form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits, ['aria-label' => ts('Frequency Units')]); |
dccd9f4f | 303 | // CRM-18854 |
de6c59ca CW |
304 | if (!empty($pledgeBlock['is_pledge_start_date_visible'])) { |
305 | if (!empty($pledgeBlock['pledge_start_date'])) { | |
dccd9f4f ERL |
306 | $defaults = array(); |
307 | $date = (array) json_decode($pledgeBlock['pledge_start_date']); | |
9de112cd SL |
308 | foreach ($date as $field => $value) { |
309 | switch ($field) { | |
310 | case 'contribution_date': | |
b59a905f SL |
311 | $form->add('datepicker', 'start_date', ts('First installment payment'), [], FALSE, ['time' => FALSE]); |
312 | $paymentDate = $value = date('Y-m-d'); | |
313 | $defaults['start_date'] = $value; | |
9de112cd SL |
314 | $form->assign('is_date', TRUE); |
315 | break; | |
316 | ||
317 | case 'calendar_date': | |
b59a905f SL |
318 | $form->add('datepicker', 'start_date', ts('First installment payment'), [], FALSE, ['time' => FALSE]); |
319 | $defaults['start_date'] = $value; | |
9de112cd SL |
320 | $form->assign('is_date', TRUE); |
321 | $paymentDate = $value; | |
322 | break; | |
323 | ||
324 | case 'calendar_month': | |
325 | $month = CRM_Utils_Date::getCalendarDayOfMonth(); | |
326 | $form->add('select', 'start_date', ts('Day of month installments paid'), $month); | |
327 | $paymentDate = CRM_Pledge_BAO_Pledge::getPaymentDate($value); | |
b59a905f | 328 | $defaults['start_date'] = $paymentDate; |
9de112cd SL |
329 | break; |
330 | ||
331 | default: | |
332 | break; | |
dccd9f4f | 333 | |
9de112cd SL |
334 | } |
335 | $form->setDefaults($defaults); | |
336 | $form->assign('start_date_display', $paymentDate); | |
337 | $form->assign('start_date_editable', FALSE); | |
de6c59ca | 338 | if (!empty($pledgeBlock['is_pledge_start_date_editable'])) { |
9de112cd SL |
339 | $form->assign('start_date_editable', TRUE); |
340 | if ($field == 'calendar_month') { | |
341 | $form->assign('is_date', FALSE); | |
342 | $form->setDefaults(array('start_date' => $value)); | |
343 | } | |
dccd9f4f ERL |
344 | } |
345 | } | |
346 | } | |
347 | } | |
6a488035 TO |
348 | } |
349 | } | |
96025800 | 350 | |
6a488035 | 351 | } |