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();
73 * Add or update pledgeBlock.
75 * @param array $params
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']));
84 return self
::writeRecord($params);
88 * Delete the pledgeBlock.
95 public static function deletePledgeBlock($id) {
96 CRM_Utils_Hook
::pre('delete', 'PledgeBlock', $id);
98 $transaction = new CRM_Core_Transaction();
102 $dao = new CRM_Pledge_DAO_PledgeBlock();
104 $results = $dao->delete();
106 $transaction->commit();
108 CRM_Utils_Hook
::post('delete', 'PledgeBlock', $dao->id
, $dao);
114 * Return Pledge Block info in Contribution Pages.
117 * Contribution page id.
121 public static function getPledgeBlock($pageID) {
124 $dao = new CRM_Pledge_DAO_PledgeBlock();
125 $dao->entity_table
= 'civicrm_contribution_page';
126 $dao->entity_id
= $pageID;
127 if ($dao->find(TRUE)) {
128 CRM_Core_DAO
::storeValues($dao, $pledgeBlock);
135 * Build Pledge Block in Contribution Pages.
137 * @param CRM_Core_Form $form
139 public static function buildPledgeBlock($form) {
140 //build pledge payment fields.
141 if (!empty($form->_values
['pledge_id'])) {
142 //get all payments required details.
144 $returnProperties = array(
150 CRM_Core_DAO
::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id',
151 $form->_values
['pledge_id'], $allPayments, $returnProperties
154 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
157 $isNextPayment = FALSE;
158 $overduePayments = [];
159 foreach ($allPayments as $payID => $value) {
160 if ($allStatus[$value['status_id']] == 'Overdue') {
161 $overduePayments[$payID] = array(
163 'scheduled_amount' => CRM_Utils_Rule
::cleanMoney($value['scheduled_amount']),
164 'scheduled_amount_currency' => $value['currency'],
165 'scheduled_date' => CRM_Utils_Date
::customFormat($value['scheduled_date'],
170 elseif (!$isNextPayment &&
171 $allStatus[$value['status_id']] == 'Pending'
173 // get the next payment.
174 $nextPayment = array(
176 'scheduled_amount' => CRM_Utils_Rule
::cleanMoney($value['scheduled_amount']),
177 'scheduled_amount_currency' => $value['currency'],
178 'scheduled_date' => CRM_Utils_Date
::customFormat($value['scheduled_date'],
182 $isNextPayment = TRUE;
186 // build check box array for payments.
188 if (!empty($overduePayments)) {
189 foreach ($overduePayments as $id => $payment) {
190 $label = ts("%1 - due on %2 (overdue)", array(
191 1 => CRM_Utils_Money
::format(CRM_Utils_Array
::value('scheduled_amount', $payment), CRM_Utils_Array
::value('scheduled_amount_currency', $payment)),
192 2 => $payment['scheduled_date'] ??
NULL,
194 $paymentID = $payment['id'] ??
NULL;
195 $payments[] = $form->createElement('checkbox', $paymentID, NULL, $label, array('amount' => CRM_Utils_Array
::value('scheduled_amount', $payment)));
199 if (!empty($nextPayment)) {
200 $label = ts("%1 - due on %2", array(
201 1 => CRM_Utils_Money
::format(CRM_Utils_Array
::value('scheduled_amount', $nextPayment), CRM_Utils_Array
::value('scheduled_amount_currency', $nextPayment)),
202 2 => $nextPayment['scheduled_date'] ??
NULL,
204 $paymentID = $nextPayment['id'] ??
NULL;
205 $payments[] = $form->createElement('checkbox', $paymentID, NULL, $label, array('amount' => CRM_Utils_Array
::value('scheduled_amount', $nextPayment)));
207 // give error if empty or build form for payment.
208 if (empty($payments)) {
209 throw new CRM_Core_Exception(ts('Oops. It looks like there is no valid payment status for online payment.'));
212 $form->assign('is_pledge_payment', TRUE);
213 $form->addGroup($payments, 'pledge_amount', ts('Make Pledge Payment(s):'), '<br />');
218 $pledgeBlock = self
::getPledgeBlock($form->_id
);
220 // build form for pledge creation.
221 $pledgeOptions = array(
222 '0' => ts('I want to make a one-time contribution'),
223 '1' => ts('I pledge to contribute this amount every'),
225 $form->addRadio('is_pledge', ts('Pledge Frequency Interval'), $pledgeOptions,
228 $form->addElement('text', 'pledge_installments', ts('Installments'), ['size' => 3, 'aria-label' => ts('Installments')]);
230 if (!empty($pledgeBlock['is_pledge_interval'])) {
231 $form->assign('is_pledge_interval', CRM_Utils_Array
::value('is_pledge_interval', $pledgeBlock));
232 $form->addElement('text', 'pledge_frequency_interval', NULL, ['size' => 3, 'aria-label' => ts('Frequency Intervals')]);
235 $form->add('hidden', 'pledge_frequency_interval', 1);
237 // Frequency unit drop-down label suffixes switch from *ly to *(s)
238 $freqUnitVals = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $pledgeBlock['pledge_frequency_unit']);
240 $frequencyUnits = CRM_Core_OptionGroup
::values('recur_frequency_units');
241 foreach ($freqUnitVals as $key => $val) {
242 if (array_key_exists($val, $frequencyUnits)) {
243 $freqUnits[$val] = !empty($pledgeBlock['is_pledge_interval']) ?
"{$frequencyUnits[$val]}(s)" : $frequencyUnits[$val];
246 $form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits, ['aria-label' => ts('Frequency Units')]);
248 if (!empty($pledgeBlock['is_pledge_start_date_visible'])) {
249 if (!empty($pledgeBlock['pledge_start_date'])) {
251 $date = (array) json_decode($pledgeBlock['pledge_start_date']);
252 foreach ($date as $field => $value) {
254 case 'contribution_date':
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;
258 $form->assign('is_date', TRUE);
261 case 'calendar_date':
262 $form->add('datepicker', 'start_date', ts('First installment payment'), [], FALSE, ['time' => FALSE]);
263 $defaults['start_date'] = $value;
264 $form->assign('is_date', TRUE);
265 $paymentDate = $value;
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);
272 $defaults['start_date'] = $paymentDate;
279 $form->setDefaults($defaults);
280 $form->assign('start_date_display', $paymentDate);
281 $form->assign('start_date_editable', FALSE);
282 if (!empty($pledgeBlock['is_pledge_start_date_editable'])) {
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));