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
{
20 * Retrieve DB object based on input parameters.
22 * It also stores all the retrieved values in the default array.
24 * @param array $params
25 * (reference ) an assoc array of name/value pairs.
26 * @param array $defaults
27 * (reference ) an assoc array to hold the flattened values.
29 * @return CRM_Pledge_BAO_PledgeBlock
31 public static function retrieve(&$params, &$defaults) {
32 $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
33 $pledgeBlock->copyValues($params);
34 if ($pledgeBlock->find(TRUE)) {
35 CRM_Core_DAO
::storeValues($pledgeBlock, $defaults);
42 * Takes an associative array and creates a pledgeBlock object.
44 * @param array $params
45 * (reference ) an assoc array of name/value pairs.
47 * @return CRM_Pledge_BAO_PledgeBlock
49 public static function &create(&$params) {
50 $transaction = new CRM_Core_Transaction();
51 $pledgeBlock = self
::add($params);
53 if (is_a($pledgeBlock, 'CRM_Core_Error')) {
54 $pledgeBlock->rollback();
58 $params['id'] = $pledgeBlock->id
;
60 $transaction->commit();
66 * Add or update pledgeBlock.
68 * @param array $params
72 public static function add($params) {
73 // FIXME: This is assuming checkbox input like ['foo' => 1, 'bar' => 0, 'baz' => 1]. Not API friendly.
74 if (!empty($params['pledge_frequency_unit']) && is_array($params['pledge_frequency_unit'])) {
75 $params['pledge_frequency_unit'] = array_keys(array_filter($params['pledge_frequency_unit']));
77 return self
::writeRecord($params);
81 * Delete the pledgeBlock.
88 public static function deletePledgeBlock($id) {
89 CRM_Utils_Hook
::pre('delete', 'PledgeBlock', $id);
91 $transaction = new CRM_Core_Transaction();
95 $dao = new CRM_Pledge_DAO_PledgeBlock();
97 $results = $dao->delete();
99 $transaction->commit();
101 CRM_Utils_Hook
::post('delete', 'PledgeBlock', $dao->id
, $dao);
107 * Return Pledge Block info in Contribution Pages.
110 * Contribution page id.
114 public static function getPledgeBlock($pageID) {
117 $dao = new CRM_Pledge_DAO_PledgeBlock();
118 $dao->entity_table
= 'civicrm_contribution_page';
119 $dao->entity_id
= $pageID;
120 if ($dao->find(TRUE)) {
121 CRM_Core_DAO
::storeValues($dao, $pledgeBlock);
128 * Build Pledge Block in Contribution Pages.
130 * @param CRM_Core_Form $form
132 public static function buildPledgeBlock($form) {
133 //build pledge payment fields.
134 if (!empty($form->_values
['pledge_id'])) {
135 //get all payments required details.
137 $returnProperties = array(
143 CRM_Core_DAO
::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id',
144 $form->_values
['pledge_id'], $allPayments, $returnProperties
147 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
150 $isNextPayment = FALSE;
151 $overduePayments = [];
152 foreach ($allPayments as $payID => $value) {
153 if ($allStatus[$value['status_id']] == 'Overdue') {
154 $overduePayments[$payID] = array(
156 'scheduled_amount' => CRM_Utils_Rule
::cleanMoney($value['scheduled_amount']),
157 'scheduled_amount_currency' => $value['currency'],
158 'scheduled_date' => CRM_Utils_Date
::customFormat($value['scheduled_date'],
163 elseif (!$isNextPayment &&
164 $allStatus[$value['status_id']] == 'Pending'
166 // get the next payment.
167 $nextPayment = array(
169 'scheduled_amount' => CRM_Utils_Rule
::cleanMoney($value['scheduled_amount']),
170 'scheduled_amount_currency' => $value['currency'],
171 'scheduled_date' => CRM_Utils_Date
::customFormat($value['scheduled_date'],
175 $isNextPayment = TRUE;
179 // build check box array for payments.
181 if (!empty($overduePayments)) {
182 foreach ($overduePayments as $id => $payment) {
183 $label = ts("%1 - due on %2 (overdue)", array(
184 1 => CRM_Utils_Money
::format(CRM_Utils_Array
::value('scheduled_amount', $payment), CRM_Utils_Array
::value('scheduled_amount_currency', $payment)),
185 2 => $payment['scheduled_date'] ??
NULL,
187 $paymentID = $payment['id'] ??
NULL;
188 $payments[] = $form->createElement('checkbox', $paymentID, NULL, $label, array('amount' => CRM_Utils_Array
::value('scheduled_amount', $payment)));
192 if (!empty($nextPayment)) {
193 $label = ts("%1 - due on %2", array(
194 1 => CRM_Utils_Money
::format(CRM_Utils_Array
::value('scheduled_amount', $nextPayment), CRM_Utils_Array
::value('scheduled_amount_currency', $nextPayment)),
195 2 => $nextPayment['scheduled_date'] ??
NULL,
197 $paymentID = $nextPayment['id'] ??
NULL;
198 $payments[] = $form->createElement('checkbox', $paymentID, NULL, $label, array('amount' => CRM_Utils_Array
::value('scheduled_amount', $nextPayment)));
200 // give error if empty or build form for payment.
201 if (empty($payments)) {
202 throw new CRM_Core_Exception(ts('Oops. It looks like there is no valid payment status for online payment.'));
205 $form->assign('is_pledge_payment', TRUE);
206 $form->addGroup($payments, 'pledge_amount', ts('Make Pledge Payment(s):'), '<br />');
211 $pledgeBlock = self
::getPledgeBlock($form->_id
);
213 // build form for pledge creation.
214 $pledgeOptions = array(
215 '0' => ts('I want to make a one-time contribution'),
216 '1' => ts('I pledge to contribute this amount every'),
218 $form->addRadio('is_pledge', ts('Pledge Frequency Interval'), $pledgeOptions,
221 $form->addElement('text', 'pledge_installments', ts('Installments'), ['size' => 3, 'aria-label' => ts('Installments')]);
223 if (!empty($pledgeBlock['is_pledge_interval'])) {
224 $form->assign('is_pledge_interval', CRM_Utils_Array
::value('is_pledge_interval', $pledgeBlock));
225 $form->addElement('text', 'pledge_frequency_interval', NULL, ['size' => 3, 'aria-label' => ts('Frequency Intervals')]);
228 $form->add('hidden', 'pledge_frequency_interval', 1);
230 // Frequency unit drop-down label suffixes switch from *ly to *(s)
231 $freqUnitVals = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $pledgeBlock['pledge_frequency_unit']);
233 $frequencyUnits = CRM_Core_OptionGroup
::values('recur_frequency_units');
234 foreach ($freqUnitVals as $key => $val) {
235 if (array_key_exists($val, $frequencyUnits)) {
236 $freqUnits[$val] = !empty($pledgeBlock['is_pledge_interval']) ?
"{$frequencyUnits[$val]}(s)" : $frequencyUnits[$val];
239 $form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits, ['aria-label' => ts('Frequency Units')]);
241 if (!empty($pledgeBlock['is_pledge_start_date_visible'])) {
242 if (!empty($pledgeBlock['pledge_start_date'])) {
244 $date = (array) json_decode($pledgeBlock['pledge_start_date']);
245 foreach ($date as $field => $value) {
247 case 'contribution_date':
248 $form->add('datepicker', 'start_date', ts('First installment payment'), [], FALSE, ['time' => FALSE]);
249 $paymentDate = $value = date('Y-m-d');
250 $defaults['start_date'] = $value;
251 $form->assign('is_date', TRUE);
254 case 'calendar_date':
255 $form->add('datepicker', 'start_date', ts('First installment payment'), [], FALSE, ['time' => FALSE]);
256 $defaults['start_date'] = $value;
257 $form->assign('is_date', TRUE);
258 $paymentDate = $value;
261 case 'calendar_month':
262 $month = CRM_Utils_Date
::getCalendarDayOfMonth();
263 $form->add('select', 'start_date', ts('Day of month installments paid'), $month);
264 $paymentDate = CRM_Pledge_BAO_Pledge
::getPaymentDate($value);
265 $defaults['start_date'] = $paymentDate;
272 $form->setDefaults($defaults);
273 $form->assign('start_date_display', $paymentDate);
274 $form->assign('start_date_editable', FALSE);
275 if (!empty($pledgeBlock['is_pledge_start_date_editable'])) {
276 $form->assign('start_date_editable', TRUE);
277 if ($field == 'calendar_month') {
278 $form->assign('is_date', FALSE);
279 $form->setDefaults(array('start_date' => $value));