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