Merge pull request #13232 from JO0st/core-574
[civicrm-core.git] / CRM / Pledge / BAO / PledgeBlock.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Pledge_BAO_PledgeBlock extends CRM_Pledge_DAO_PledgeBlock {
18
19 /**
20 * Class constructor.
21 */
22 public function __construct() {
23 parent::__construct();
24 }
25
26 /**
27 * Retrieve DB object based on input parameters.
28 *
29 * It also stores all the retrieved values in the default array.
30 *
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.
35 *
36 * @return CRM_Pledge_BAO_PledgeBlock
37 */
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);
43 return $pledgeBlock;
44 }
45 return NULL;
46 }
47
48 /**
49 * Takes an associative array and creates a pledgeBlock object.
50 *
51 * @param array $params
52 * (reference ) an assoc array of name/value pairs.
53 *
54 * @return CRM_Pledge_BAO_PledgeBlock
55 */
56 public static function &create(&$params) {
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 /**
73 * Add pledgeBlock.
74 *
75 * @param array $params
76 * Reference array contains the values submitted by the form.
77 *
78 *
79 * @return object
80 */
81 public static function add(&$params) {
82
83 if (!empty($params['id'])) {
84 CRM_Utils_Hook::pre('edit', 'PledgeBlock', $params['id'], $params);
85 }
86 else {
87 CRM_Utils_Hook::pre('create', 'PledgeBlock', NULL, $params);
88 }
89
90 $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
91
92 // fix for pledge_frequency_unit
93 $freqUnits = CRM_Utils_Array::value('pledge_frequency_unit', $params);
94
95 if ($freqUnits && is_array($freqUnits)) {
96 unset($params['pledge_frequency_unit']);
97 $newFreqUnits = array();
98 foreach ($freqUnits as $k => $v) {
99 if ($v) {
100 $newFreqUnits[$k] = $v;
101 }
102 }
103
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;
108 }
109 else {
110 $pledgeBlock->pledge_frequency_unit = '';
111 }
112 }
113
114 $pledgeBlock->copyValues($params);
115 $result = $pledgeBlock->save();
116
117 if (!empty($params['id'])) {
118 CRM_Utils_Hook::post('edit', 'PledgeBlock', $pledgeBlock->id, $pledgeBlock);
119 }
120 else {
121 CRM_Utils_Hook::post('create', 'Pledge', $pledgeBlock->id, $pledgeBlock);
122 }
123
124 return $result;
125 }
126
127 /**
128 * Delete the pledgeBlock.
129 *
130 * @param int $id
131 * PledgeBlock id.
132 *
133 * @return mixed|null
134 */
135 public static function deletePledgeBlock($id) {
136 CRM_Utils_Hook::pre('delete', 'PledgeBlock', $id, CRM_Core_DAO::$_nullArray);
137
138 $transaction = new CRM_Core_Transaction();
139
140 $results = NULL;
141
142 $dao = new CRM_Pledge_DAO_PledgeBlock();
143 $dao->id = $id;
144 $results = $dao->delete();
145
146 $transaction->commit();
147
148 CRM_Utils_Hook::post('delete', 'PledgeBlock', $dao->id, $dao);
149
150 return $results;
151 }
152
153 /**
154 * Return Pledge Block info in Contribution Pages.
155 *
156 * @param int $pageID
157 * Contribution page id.
158 *
159 * @return array
160 */
161 public static function getPledgeBlock($pageID) {
162 $pledgeBlock = array();
163
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);
169 }
170
171 return $pledgeBlock;
172 }
173
174 /**
175 * Build Pledge Block in Contribution Pages.
176 *
177 * @param CRM_Core_Form $form
178 */
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(
185 'status_id',
186 'scheduled_date',
187 'scheduled_amount',
188 'currency',
189 );
190 CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id',
191 $form->_values['pledge_id'], $allPayments, $returnProperties
192 );
193 // get all status
194 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
195
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(
202 'id' => $payID,
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'],
206 '%B %d'
207 ),
208 );
209 }
210 elseif (!$isNextPayment &&
211 $allStatus[$value['status_id']] == 'Pending'
212 ) {
213 // get the next payment.
214 $nextPayment = array(
215 'id' => $payID,
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'],
219 '%B %d'
220 ),
221 );
222 $isNextPayment = TRUE;
223 }
224 }
225
226 // build check box array for payments.
227 $payments = array();
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 => CRM_Utils_Array::value('scheduled_date', $payment),
233 ));
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)));
236 }
237 }
238
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 => CRM_Utils_Array::value('scheduled_date', $nextPayment),
243 ));
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)));
246 }
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."));
250 }
251 else {
252 $form->assign('is_pledge_payment', TRUE);
253 $form->addGroup($payments, 'pledge_amount', ts('Make Pledge Payment(s):'), '<br />');
254 }
255 }
256 else {
257
258 $pledgeBlock = self::getPledgeBlock($form->_id);
259
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'),
264 );
265 $form->addRadio('is_pledge', ts('Pledge Frequency Interval'), $pledgeOptions,
266 NULL, array('<br/>')
267 );
268 $form->addElement('text', 'pledge_installments', ts('Installments'), ['size' => 3, 'aria-label' => ts('Installments')]);
269
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')]);
273 }
274 else {
275 $form->add('hidden', 'pledge_frequency_interval', 1);
276 }
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];
284 }
285 }
286 $form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits, ['aria-label' => ts('Frequency Units')]);
287 // CRM-18854
288 if (!empty($pledgeBlock['is_pledge_start_date_visible'])) {
289 if (!empty($pledgeBlock['pledge_start_date'])) {
290 $defaults = array();
291 $date = (array) json_decode($pledgeBlock['pledge_start_date']);
292 foreach ($date as $field => $value) {
293 switch ($field) {
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);
299 break;
300
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;
306 break;
307
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;
313 break;
314
315 default:
316 break;
317
318 }
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));
327 }
328 }
329 }
330 }
331 }
332 }
333 }
334
335 }