3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Pledge_BAO_PledgeBlock
extends CRM_Pledge_DAO_PledgeBlock
{
22 public function __construct() {
23 parent
::__construct();
27 * Retrieve DB object based on input parameters.
29 * It also stores all the retrieved values in the default array.
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.
36 * @return CRM_Pledge_BAO_PledgeBlock
38 public static function retrieve(&$params, &$defaults) {
39 $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
40 $pledgeBlock->copyValues($params);
41 if ($pledgeBlock->find(TRUE)) {
42 CRM_Core_DAO
::storeValues($pledgeBlock, $defaults);
49 * Takes an associative array and creates a pledgeBlock object.
51 * @param array $params
52 * (reference ) an assoc array of name/value pairs.
54 * @return CRM_Pledge_BAO_PledgeBlock
56 public static function &create(&$params) {
57 $transaction = new CRM_Core_Transaction();
58 $pledgeBlock = self
::add($params);
60 if (is_a($pledgeBlock, 'CRM_Core_Error')) {
61 $pledgeBlock->rollback();
65 $params['id'] = $pledgeBlock->id
;
67 $transaction->commit();
75 * @param array $params
76 * Reference array contains the values submitted by the form.
81 public static function add(&$params) {
83 if (!empty($params['id'])) {
84 CRM_Utils_Hook
::pre('edit', 'PledgeBlock', $params['id'], $params);
87 CRM_Utils_Hook
::pre('create', 'PledgeBlock', NULL, $params);
90 $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
92 // fix for pledge_frequency_unit
93 $freqUnits = CRM_Utils_Array
::value('pledge_frequency_unit', $params);
95 if ($freqUnits && is_array($freqUnits)) {
96 unset($params['pledge_frequency_unit']);
97 $newFreqUnits = array();
98 foreach ($freqUnits as $k => $v) {
100 $newFreqUnits[$k] = $v;
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;
110 $pledgeBlock->pledge_frequency_unit
= '';
114 $pledgeBlock->copyValues($params);
115 $result = $pledgeBlock->save();
117 if (!empty($params['id'])) {
118 CRM_Utils_Hook
::post('edit', 'PledgeBlock', $pledgeBlock->id
, $pledgeBlock);
121 CRM_Utils_Hook
::post('create', 'Pledge', $pledgeBlock->id
, $pledgeBlock);
128 * Delete the pledgeBlock.
135 public static function deletePledgeBlock($id) {
136 CRM_Utils_Hook
::pre('delete', 'PledgeBlock', $id, CRM_Core_DAO
::$_nullArray);
138 $transaction = new CRM_Core_Transaction();
142 $dao = new CRM_Pledge_DAO_PledgeBlock();
144 $results = $dao->delete();
146 $transaction->commit();
148 CRM_Utils_Hook
::post('delete', 'PledgeBlock', $dao->id
, $dao);
154 * Return Pledge Block info in Contribution Pages.
157 * Contribution page id.
161 public static function getPledgeBlock($pageID) {
162 $pledgeBlock = array();
164 $dao = new CRM_Pledge_DAO_PledgeBlock();
165 $dao->entity_table
= 'civicrm_contribution_page';
166 $dao->entity_id
= $pageID;
167 if ($dao->find(TRUE)) {
168 CRM_Core_DAO
::storeValues($dao, $pledgeBlock);
175 * Build Pledge Block in Contribution Pages.
177 * @param CRM_Core_Form $form
179 public static function buildPledgeBlock($form) {
180 //build pledge payment fields.
181 if (!empty($form->_values
['pledge_id'])) {
182 //get all payments required details.
183 $allPayments = array();
184 $returnProperties = array(
190 CRM_Core_DAO
::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id',
191 $form->_values
['pledge_id'], $allPayments, $returnProperties
194 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
196 $nextPayment = array();
197 $isNextPayment = FALSE;
198 $overduePayments = array();
199 foreach ($allPayments as $payID => $value) {
200 if ($allStatus[$value['status_id']] == 'Overdue') {
201 $overduePayments[$payID] = array(
203 'scheduled_amount' => CRM_Utils_Rule
::cleanMoney($value['scheduled_amount']),
204 'scheduled_amount_currency' => $value['currency'],
205 'scheduled_date' => CRM_Utils_Date
::customFormat($value['scheduled_date'],
210 elseif (!$isNextPayment &&
211 $allStatus[$value['status_id']] == 'Pending'
213 // get the next payment.
214 $nextPayment = array(
216 'scheduled_amount' => CRM_Utils_Rule
::cleanMoney($value['scheduled_amount']),
217 'scheduled_amount_currency' => $value['currency'],
218 'scheduled_date' => CRM_Utils_Date
::customFormat($value['scheduled_date'],
222 $isNextPayment = TRUE;
226 // build check box array for payments.
228 if (!empty($overduePayments)) {
229 foreach ($overduePayments as $id => $payment) {
230 $label = ts("%1 - due on %2 (overdue)", array(
231 1 => CRM_Utils_Money
::format(CRM_Utils_Array
::value('scheduled_amount', $payment), CRM_Utils_Array
::value('scheduled_amount_currency', $payment)),
232 2 => $payment['scheduled_date'] ??
NULL,
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)));
239 if (!empty($nextPayment)) {
240 $label = ts("%1 - due on %2", array(
241 1 => CRM_Utils_Money
::format(CRM_Utils_Array
::value('scheduled_amount', $nextPayment), CRM_Utils_Array
::value('scheduled_amount_currency', $nextPayment)),
242 2 => $nextPayment['scheduled_date'] ??
NULL,
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)));
247 // give error if empty or build form for payment.
248 if (empty($payments)) {
249 CRM_Core_Error
::fatal(ts("Oops. It looks like there is no valid payment status for online payment."));
252 $form->assign('is_pledge_payment', TRUE);
253 $form->addGroup($payments, 'pledge_amount', ts('Make Pledge Payment(s):'), '<br />');
258 $pledgeBlock = self
::getPledgeBlock($form->_id
);
260 // build form for pledge creation.
261 $pledgeOptions = array(
262 '0' => ts('I want to make a one-time contribution'),
263 '1' => ts('I pledge to contribute this amount every'),
265 $form->addRadio('is_pledge', ts('Pledge Frequency Interval'), $pledgeOptions,
268 $form->addElement('text', 'pledge_installments', ts('Installments'), ['size' => 3, 'aria-label' => ts('Installments')]);
270 if (!empty($pledgeBlock['is_pledge_interval'])) {
271 $form->assign('is_pledge_interval', CRM_Utils_Array
::value('is_pledge_interval', $pledgeBlock));
272 $form->addElement('text', 'pledge_frequency_interval', NULL, ['size' => 3, 'aria-label' => ts('Frequency Intervals')]);
275 $form->add('hidden', 'pledge_frequency_interval', 1);
277 // Frequency unit drop-down label suffixes switch from *ly to *(s)
278 $freqUnitVals = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $pledgeBlock['pledge_frequency_unit']);
279 $freqUnits = array();
280 $frequencyUnits = CRM_Core_OptionGroup
::values('recur_frequency_units');
281 foreach ($freqUnitVals as $key => $val) {
282 if (array_key_exists($val, $frequencyUnits)) {
283 $freqUnits[$val] = !empty($pledgeBlock['is_pledge_interval']) ?
"{$frequencyUnits[$val]}(s)" : $frequencyUnits[$val];
286 $form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits, ['aria-label' => ts('Frequency Units')]);
288 if (!empty($pledgeBlock['is_pledge_start_date_visible'])) {
289 if (!empty($pledgeBlock['pledge_start_date'])) {
291 $date = (array) json_decode($pledgeBlock['pledge_start_date']);
292 foreach ($date as $field => $value) {
294 case 'contribution_date':
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;
298 $form->assign('is_date', TRUE);
301 case 'calendar_date':
302 $form->add('datepicker', 'start_date', ts('First installment payment'), [], FALSE, ['time' => FALSE]);
303 $defaults['start_date'] = $value;
304 $form->assign('is_date', TRUE);
305 $paymentDate = $value;
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);
312 $defaults['start_date'] = $paymentDate;
319 $form->setDefaults($defaults);
320 $form->assign('start_date_display', $paymentDate);
321 $form->assign('start_date_editable', FALSE);
322 if (!empty($pledgeBlock['is_pledge_start_date_editable'])) {
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));