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 and copy to defaults array.
22 * @param array $params
23 * Array of criteria values.
24 * @param array $defaults
25 * Array to be populated with found values.
28 * The DAO object, if found.
32 public static function retrieve($params, &$defaults) {
33 return self
::commonRetrieve(self
::class, $params, $defaults);
37 * Takes an associative array and creates a pledgeBlock object.
39 * @param array $params
40 * (reference ) an assoc array of name/value pairs.
42 * @return CRM_Pledge_BAO_PledgeBlock
44 public static function &create(&$params) {
45 $transaction = new CRM_Core_Transaction();
46 $pledgeBlock = self
::add($params);
48 if (is_a($pledgeBlock, 'CRM_Core_Error')) {
49 $pledgeBlock->rollback();
53 $params['id'] = $pledgeBlock->id
;
55 $transaction->commit();
61 * Add or update pledgeBlock.
63 * @param array $params
67 public static function add($params) {
68 // FIXME: This is assuming checkbox input like ['foo' => 1, 'bar' => 0, 'baz' => 1]. Not API friendly.
69 if (!empty($params['pledge_frequency_unit']) && is_array($params['pledge_frequency_unit'])) {
70 $params['pledge_frequency_unit'] = array_keys(array_filter($params['pledge_frequency_unit']));
72 return self
::writeRecord($params);
76 * Delete the pledgeBlock.
83 public static function deletePledgeBlock($id) {
84 CRM_Utils_Hook
::pre('delete', 'PledgeBlock', $id);
86 $transaction = new CRM_Core_Transaction();
90 $dao = new CRM_Pledge_DAO_PledgeBlock();
92 $results = $dao->delete();
94 $transaction->commit();
96 CRM_Utils_Hook
::post('delete', 'PledgeBlock', $dao->id
, $dao);
102 * Return Pledge Block info in Contribution Pages.
105 * Contribution page id.
109 public static function getPledgeBlock($pageID) {
112 $dao = new CRM_Pledge_DAO_PledgeBlock();
113 $dao->entity_table
= 'civicrm_contribution_page';
114 $dao->entity_id
= $pageID;
115 if ($dao->find(TRUE)) {
116 CRM_Core_DAO
::storeValues($dao, $pledgeBlock);
123 * Build Pledge Block in Contribution Pages.
125 * @param CRM_Core_Form $form
127 public static function buildPledgeBlock($form) {
128 //build pledge payment fields.
129 if (!empty($form->_values
['pledge_id'])) {
130 //get all payments required details.
132 $returnProperties = array(
138 CRM_Core_DAO
::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id',
139 $form->_values
['pledge_id'], $allPayments, $returnProperties
142 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
145 $isNextPayment = FALSE;
146 $overduePayments = [];
147 foreach ($allPayments as $payID => $value) {
148 if ($allStatus[$value['status_id']] == 'Overdue') {
149 $overduePayments[$payID] = array(
151 'scheduled_amount' => CRM_Utils_Rule
::cleanMoney($value['scheduled_amount']),
152 'scheduled_amount_currency' => $value['currency'],
153 'scheduled_date' => CRM_Utils_Date
::customFormat($value['scheduled_date'],
158 elseif (!$isNextPayment &&
159 $allStatus[$value['status_id']] == 'Pending'
161 // get the next payment.
162 $nextPayment = array(
164 'scheduled_amount' => CRM_Utils_Rule
::cleanMoney($value['scheduled_amount']),
165 'scheduled_amount_currency' => $value['currency'],
166 'scheduled_date' => CRM_Utils_Date
::customFormat($value['scheduled_date'],
170 $isNextPayment = TRUE;
174 // build check box array for payments.
176 if (!empty($overduePayments)) {
177 foreach ($overduePayments as $id => $payment) {
178 $label = ts("%1 - due on %2 (overdue)", array(
179 1 => CRM_Utils_Money
::format(CRM_Utils_Array
::value('scheduled_amount', $payment), CRM_Utils_Array
::value('scheduled_amount_currency', $payment)),
180 2 => $payment['scheduled_date'] ??
NULL,
182 $paymentID = $payment['id'] ??
NULL;
183 $payments[] = $form->createElement('checkbox', $paymentID, NULL, $label, array('amount' => CRM_Utils_Array
::value('scheduled_amount', $payment)));
187 if (!empty($nextPayment)) {
188 $label = ts("%1 - due on %2", array(
189 1 => CRM_Utils_Money
::format(CRM_Utils_Array
::value('scheduled_amount', $nextPayment), CRM_Utils_Array
::value('scheduled_amount_currency', $nextPayment)),
190 2 => $nextPayment['scheduled_date'] ??
NULL,
192 $paymentID = $nextPayment['id'] ??
NULL;
193 $payments[] = $form->createElement('checkbox', $paymentID, NULL, $label, array('amount' => CRM_Utils_Array
::value('scheduled_amount', $nextPayment)));
195 // give error if empty or build form for payment.
196 if (empty($payments)) {
197 throw new CRM_Core_Exception(ts('Oops. It looks like there is no valid payment status for online payment.'));
200 $form->assign('is_pledge_payment', TRUE);
201 $form->addGroup($payments, 'pledge_amount', ts('Make Pledge Payment(s):'), '<br />');
206 $pledgeBlock = self
::getPledgeBlock($form->_id
);
208 // build form for pledge creation.
209 $pledgeOptions = array(
210 '0' => ts('I want to make a one-time contribution'),
211 '1' => ts('I pledge to contribute this amount every'),
213 $form->addRadio('is_pledge', ts('Pledge Frequency Interval'), $pledgeOptions,
216 $form->addElement('text', 'pledge_installments', ts('Installments'), ['size' => 3, 'aria-label' => ts('Installments')]);
218 if (!empty($pledgeBlock['is_pledge_interval'])) {
219 $form->assign('is_pledge_interval', CRM_Utils_Array
::value('is_pledge_interval', $pledgeBlock));
220 $form->addElement('text', 'pledge_frequency_interval', NULL, ['size' => 3, 'aria-label' => ts('Frequency Intervals')]);
223 $form->add('hidden', 'pledge_frequency_interval', 1);
225 // Frequency unit drop-down label suffixes switch from *ly to *(s)
226 $freqUnitVals = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $pledgeBlock['pledge_frequency_unit']);
228 $frequencyUnits = CRM_Core_OptionGroup
::values('recur_frequency_units');
229 foreach ($freqUnitVals as $key => $val) {
230 if (array_key_exists($val, $frequencyUnits)) {
231 $freqUnits[$val] = !empty($pledgeBlock['is_pledge_interval']) ?
"{$frequencyUnits[$val]}(s)" : $frequencyUnits[$val];
234 $form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits, ['aria-label' => ts('Frequency Units')]);
236 if (!empty($pledgeBlock['is_pledge_start_date_visible'])) {
237 if (!empty($pledgeBlock['pledge_start_date'])) {
239 $date = (array) json_decode($pledgeBlock['pledge_start_date']);
240 foreach ($date as $field => $value) {
242 case 'contribution_date':
243 $form->add('datepicker', 'start_date', ts('First installment payment'), [], FALSE, ['time' => FALSE]);
244 $paymentDate = $value = date('Y-m-d');
245 $defaults['start_date'] = $value;
246 $form->assign('is_date', TRUE);
249 case 'calendar_date':
250 $form->add('datepicker', 'start_date', ts('First installment payment'), [], FALSE, ['time' => FALSE]);
251 $defaults['start_date'] = $value;
252 $form->assign('is_date', TRUE);
253 $paymentDate = $value;
256 case 'calendar_month':
257 $month = CRM_Utils_Date
::getCalendarDayOfMonth();
258 $form->add('select', 'start_date', ts('Day of month installments paid'), $month);
259 $paymentDate = CRM_Pledge_BAO_Pledge
::getPaymentDate($value);
260 $defaults['start_date'] = $paymentDate;
267 $form->setDefaults($defaults);
268 $form->assign('start_date_display', $paymentDate);
269 $form->assign('start_date_editable', FALSE);
270 if (!empty($pledgeBlock['is_pledge_start_date_editable'])) {
271 $form->assign('start_date_editable', TRUE);
272 if ($field == 'calendar_month') {
273 $form->assign('is_date', FALSE);
274 $form->setDefaults(array('start_date' => $value));