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 | /** | |
0419bf7b | 73 | * Add or update pledgeBlock. |
6a488035 | 74 | * |
3a1617b6 | 75 | * @param array $params |
6a488035 TO |
76 | * |
77 | * @return object | |
78 | */ | |
0419bf7b CW |
79 | public static function add($params) { |
80 | // FIXME: This is assuming checkbox input like ['foo' => 1, 'bar' => 0, 'baz' => 1]. Not API friendly. | |
81 | if (!empty($params['pledge_frequency_unit']) && is_array($params['pledge_frequency_unit'])) { | |
82 | $params['pledge_frequency_unit'] = array_keys(array_filter($params['pledge_frequency_unit'])); | |
6a488035 | 83 | } |
0419bf7b | 84 | return self::writeRecord($params); |
6a488035 TO |
85 | } |
86 | ||
87 | /** | |
fe482240 | 88 | * Delete the pledgeBlock. |
6a488035 | 89 | * |
3a1617b6 TO |
90 | * @param int $id |
91 | * PledgeBlock id. | |
6a488035 | 92 | * |
77b97be7 | 93 | * @return mixed|null |
6a488035 | 94 | */ |
00be9182 | 95 | public static function deletePledgeBlock($id) { |
6a488035 TO |
96 | CRM_Utils_Hook::pre('delete', 'PledgeBlock', $id, CRM_Core_DAO::$_nullArray); |
97 | ||
98 | $transaction = new CRM_Core_Transaction(); | |
99 | ||
100 | $results = NULL; | |
101 | ||
353ffa53 | 102 | $dao = new CRM_Pledge_DAO_PledgeBlock(); |
6a488035 TO |
103 | $dao->id = $id; |
104 | $results = $dao->delete(); | |
105 | ||
106 | $transaction->commit(); | |
107 | ||
108 | CRM_Utils_Hook::post('delete', 'PledgeBlock', $dao->id, $dao); | |
109 | ||
110 | return $results; | |
111 | } | |
112 | ||
113 | /** | |
fe482240 | 114 | * Return Pledge Block info in Contribution Pages. |
6a488035 | 115 | * |
3a1617b6 TO |
116 | * @param int $pageID |
117 | * Contribution page id. | |
77b97be7 EM |
118 | * |
119 | * @return array | |
6a488035 | 120 | */ |
00be9182 | 121 | public static function getPledgeBlock($pageID) { |
affcc9d2 | 122 | $pledgeBlock = []; |
6a488035 | 123 | |
353ffa53 | 124 | $dao = new CRM_Pledge_DAO_PledgeBlock(); |
6a488035 | 125 | $dao->entity_table = 'civicrm_contribution_page'; |
353ffa53 | 126 | $dao->entity_id = $pageID; |
6a488035 TO |
127 | if ($dao->find(TRUE)) { |
128 | CRM_Core_DAO::storeValues($dao, $pledgeBlock); | |
129 | } | |
130 | ||
131 | return $pledgeBlock; | |
132 | } | |
133 | ||
134 | /** | |
fe482240 | 135 | * Build Pledge Block in Contribution Pages. |
6a488035 | 136 | * |
c490a46a | 137 | * @param CRM_Core_Form $form |
6a488035 | 138 | */ |
00be9182 | 139 | public static function buildPledgeBlock($form) { |
6a488035 | 140 | //build pledge payment fields. |
a7488080 | 141 | if (!empty($form->_values['pledge_id'])) { |
6a488035 | 142 | //get all payments required details. |
affcc9d2 | 143 | $allPayments = []; |
2f6befc8 AH |
144 | $returnProperties = array( |
145 | 'status_id', | |
146 | 'scheduled_date', | |
147 | 'scheduled_amount', | |
148 | 'currency', | |
149 | ); | |
6a488035 TO |
150 | CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id', |
151 | $form->_values['pledge_id'], $allPayments, $returnProperties | |
152 | ); | |
cc28438b | 153 | // get all status |
6a488035 TO |
154 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
155 | ||
affcc9d2 | 156 | $nextPayment = []; |
353ffa53 | 157 | $isNextPayment = FALSE; |
affcc9d2 | 158 | $overduePayments = []; |
6a488035 TO |
159 | foreach ($allPayments as $payID => $value) { |
160 | if ($allStatus[$value['status_id']] == 'Overdue') { | |
161 | $overduePayments[$payID] = array( | |
162 | 'id' => $payID, | |
163 | 'scheduled_amount' => CRM_Utils_Rule::cleanMoney($value['scheduled_amount']), | |
2f6befc8 | 164 | 'scheduled_amount_currency' => $value['currency'], |
6a488035 TO |
165 | 'scheduled_date' => CRM_Utils_Date::customFormat($value['scheduled_date'], |
166 | '%B %d' | |
167 | ), | |
168 | ); | |
169 | } | |
170 | elseif (!$isNextPayment && | |
171 | $allStatus[$value['status_id']] == 'Pending' | |
172 | ) { | |
cc28438b | 173 | // get the next payment. |
6a488035 TO |
174 | $nextPayment = array( |
175 | 'id' => $payID, | |
176 | 'scheduled_amount' => CRM_Utils_Rule::cleanMoney($value['scheduled_amount']), | |
2f6befc8 | 177 | 'scheduled_amount_currency' => $value['currency'], |
6a488035 TO |
178 | 'scheduled_date' => CRM_Utils_Date::customFormat($value['scheduled_date'], |
179 | '%B %d' | |
180 | ), | |
181 | ); | |
182 | $isNextPayment = TRUE; | |
183 | } | |
184 | } | |
185 | ||
cc28438b | 186 | // build check box array for payments. |
affcc9d2 | 187 | $payments = []; |
6a488035 TO |
188 | if (!empty($overduePayments)) { |
189 | foreach ($overduePayments as $id => $payment) { | |
8c52df67 | 190 | $label = ts("%1 - due on %2 (overdue)", array( |
353ffa53 | 191 | 1 => CRM_Utils_Money::format(CRM_Utils_Array::value('scheduled_amount', $payment), CRM_Utils_Array::value('scheduled_amount_currency', $payment)), |
6b409353 | 192 | 2 => $payment['scheduled_date'] ?? NULL, |
353ffa53 | 193 | )); |
9c1bc317 | 194 | $paymentID = $payment['id'] ?? NULL; |
8c52df67 | 195 | $payments[] = $form->createElement('checkbox', $paymentID, NULL, $label, array('amount' => CRM_Utils_Array::value('scheduled_amount', $payment))); |
6a488035 TO |
196 | } |
197 | } | |
198 | ||
199 | if (!empty($nextPayment)) { | |
8c52df67 | 200 | $label = ts("%1 - due on %2", array( |
353ffa53 | 201 | 1 => CRM_Utils_Money::format(CRM_Utils_Array::value('scheduled_amount', $nextPayment), CRM_Utils_Array::value('scheduled_amount_currency', $nextPayment)), |
6b409353 | 202 | 2 => $nextPayment['scheduled_date'] ?? NULL, |
353ffa53 | 203 | )); |
9c1bc317 | 204 | $paymentID = $nextPayment['id'] ?? NULL; |
8c52df67 | 205 | $payments[] = $form->createElement('checkbox', $paymentID, NULL, $label, array('amount' => CRM_Utils_Array::value('scheduled_amount', $nextPayment))); |
6a488035 | 206 | } |
cc28438b | 207 | // give error if empty or build form for payment. |
6a488035 TO |
208 | if (empty($payments)) { |
209 | CRM_Core_Error::fatal(ts("Oops. It looks like there is no valid payment status for online payment.")); | |
210 | } | |
211 | else { | |
212 | $form->assign('is_pledge_payment', TRUE); | |
8c52df67 | 213 | $form->addGroup($payments, 'pledge_amount', ts('Make Pledge Payment(s):'), '<br />'); |
6a488035 TO |
214 | } |
215 | } | |
216 | else { | |
217 | ||
218 | $pledgeBlock = self::getPledgeBlock($form->_id); | |
219 | ||
cc28438b | 220 | // build form for pledge creation. |
098201d8 | 221 | $pledgeOptions = array( |
353ffa53 | 222 | '0' => ts('I want to make a one-time contribution'), |
6a488035 TO |
223 | '1' => ts('I pledge to contribute this amount every'), |
224 | ); | |
225 | $form->addRadio('is_pledge', ts('Pledge Frequency Interval'), $pledgeOptions, | |
226 | NULL, array('<br/>') | |
227 | ); | |
8cdd6191 | 228 | $form->addElement('text', 'pledge_installments', ts('Installments'), ['size' => 3, 'aria-label' => ts('Installments')]); |
6a488035 | 229 | |
a7488080 | 230 | if (!empty($pledgeBlock['is_pledge_interval'])) { |
6a488035 | 231 | $form->assign('is_pledge_interval', CRM_Utils_Array::value('is_pledge_interval', $pledgeBlock)); |
8cdd6191 | 232 | $form->addElement('text', 'pledge_frequency_interval', NULL, ['size' => 3, 'aria-label' => ts('Frequency Intervals')]); |
6a488035 TO |
233 | } |
234 | else { | |
235 | $form->add('hidden', 'pledge_frequency_interval', 1); | |
236 | } | |
cc28438b | 237 | // Frequency unit drop-down label suffixes switch from *ly to *(s) |
353ffa53 | 238 | $freqUnitVals = explode(CRM_Core_DAO::VALUE_SEPARATOR, $pledgeBlock['pledge_frequency_unit']); |
affcc9d2 | 239 | $freqUnits = []; |
6a488035 TO |
240 | $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units'); |
241 | foreach ($freqUnitVals as $key => $val) { | |
242 | if (array_key_exists($val, $frequencyUnits)) { | |
0d8afee2 | 243 | $freqUnits[$val] = !empty($pledgeBlock['is_pledge_interval']) ? "{$frequencyUnits[$val]}(s)" : $frequencyUnits[$val]; |
6a488035 TO |
244 | } |
245 | } | |
8cdd6191 | 246 | $form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits, ['aria-label' => ts('Frequency Units')]); |
dccd9f4f | 247 | // CRM-18854 |
de6c59ca CW |
248 | if (!empty($pledgeBlock['is_pledge_start_date_visible'])) { |
249 | if (!empty($pledgeBlock['pledge_start_date'])) { | |
affcc9d2 | 250 | $defaults = []; |
dccd9f4f | 251 | $date = (array) json_decode($pledgeBlock['pledge_start_date']); |
9de112cd SL |
252 | foreach ($date as $field => $value) { |
253 | switch ($field) { | |
254 | case 'contribution_date': | |
b59a905f SL |
255 | $form->add('datepicker', 'start_date', ts('First installment payment'), [], FALSE, ['time' => FALSE]); |
256 | $paymentDate = $value = date('Y-m-d'); | |
257 | $defaults['start_date'] = $value; | |
9de112cd SL |
258 | $form->assign('is_date', TRUE); |
259 | break; | |
260 | ||
261 | case 'calendar_date': | |
b59a905f SL |
262 | $form->add('datepicker', 'start_date', ts('First installment payment'), [], FALSE, ['time' => FALSE]); |
263 | $defaults['start_date'] = $value; | |
9de112cd SL |
264 | $form->assign('is_date', TRUE); |
265 | $paymentDate = $value; | |
266 | break; | |
267 | ||
268 | case 'calendar_month': | |
269 | $month = CRM_Utils_Date::getCalendarDayOfMonth(); | |
270 | $form->add('select', 'start_date', ts('Day of month installments paid'), $month); | |
271 | $paymentDate = CRM_Pledge_BAO_Pledge::getPaymentDate($value); | |
b59a905f | 272 | $defaults['start_date'] = $paymentDate; |
9de112cd SL |
273 | break; |
274 | ||
275 | default: | |
276 | break; | |
dccd9f4f | 277 | |
9de112cd SL |
278 | } |
279 | $form->setDefaults($defaults); | |
280 | $form->assign('start_date_display', $paymentDate); | |
281 | $form->assign('start_date_editable', FALSE); | |
de6c59ca | 282 | if (!empty($pledgeBlock['is_pledge_start_date_editable'])) { |
9de112cd SL |
283 | $form->assign('start_date_editable', TRUE); |
284 | if ($field == 'calendar_month') { | |
285 | $form->assign('is_date', FALSE); | |
286 | $form->setDefaults(array('start_date' => $value)); | |
287 | } | |
dccd9f4f ERL |
288 | } |
289 | } | |
290 | } | |
291 | } | |
6a488035 TO |
292 | } |
293 | } | |
96025800 | 294 | |
6a488035 | 295 | } |