Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-08-31-12-20-39
[civicrm-core.git] / CRM / Pledge / BAO / PledgeBlock.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Pledge_BAO_PledgeBlock extends CRM_Pledge_DAO_PledgeBlock {
36
37 /**
fe482240 38 * Class constructor.
6a488035 39 */
00be9182 40 public function __construct() {
6a488035
TO
41 parent::__construct();
42 }
43
44 /**
fe482240
EM
45 * Retrieve DB object based on input parameters.
46 *
47 * It also stores all the retrieved values in the default array.
6a488035 48 *
3a1617b6
TO
49 * @param array $params
50 * (reference ) an assoc array of name/value pairs.
51 * @param array $defaults
52 * (reference ) an assoc array to hold the flattened values.
6a488035 53 *
16b10e64 54 * @return CRM_Pledge_BAO_PledgeBlock
6a488035 55 */
00be9182 56 public static function retrieve(&$params, &$defaults) {
6a488035
TO
57 $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
58 $pledgeBlock->copyValues($params);
59 if ($pledgeBlock->find(TRUE)) {
60 CRM_Core_DAO::storeValues($pledgeBlock, $defaults);
61 return $pledgeBlock;
62 }
63 return NULL;
64 }
65
66 /**
fe482240 67 * Takes an associative array and creates a pledgeBlock object.
6a488035 68 *
3a1617b6
TO
69 * @param array $params
70 * (reference ) an assoc array of name/value pairs.
6a488035 71 *
16b10e64 72 * @return CRM_Pledge_BAO_PledgeBlock
6a488035 73 */
00be9182 74 public static function &create(&$params) {
6a488035
TO
75 $transaction = new CRM_Core_Transaction();
76 $pledgeBlock = self::add($params);
77
78 if (is_a($pledgeBlock, 'CRM_Core_Error')) {
79 $pledgeBlock->rollback();
80 return $pledgeBlock;
81 }
82
83 $params['id'] = $pledgeBlock->id;
84
85 $transaction->commit();
86
87 return $pledgeBlock;
88 }
89
90 /**
fe482240 91 * Add pledgeBlock.
6a488035 92 *
3a1617b6
TO
93 * @param array $params
94 * Reference array contains the values submitted by the form.
6a488035 95 *
6a488035
TO
96 *
97 * @return object
98 */
00be9182 99 public static function add(&$params) {
6a488035 100
a7488080 101 if (!empty($params['id'])) {
6a488035
TO
102 CRM_Utils_Hook::pre('edit', 'PledgeBlock', $params['id'], $params);
103 }
104 else {
105 CRM_Utils_Hook::pre('create', 'PledgeBlock', NULL, $params);
106 }
107
108 $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
109
110 //fix for pledge_frequency_unit
111 $freqUnits = CRM_Utils_Array::value('pledge_frequency_unit', $params);
112
113 if ($freqUnits && is_array($freqUnits)) {
114 unset($params['pledge_frequency_unit']);
115 $newFreqUnits = array();
116 foreach ($freqUnits as $k => $v) {
117 if ($v) {
118 $newFreqUnits[$k] = $v;
119 }
120 }
121
122 $freqUnits = $newFreqUnits;
123 if (is_array($freqUnits) && !empty($freqUnits)) {
124 $freqUnits = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($freqUnits));
125 $pledgeBlock->pledge_frequency_unit = $freqUnits;
126 }
127 else {
128 $pledgeBlock->pledge_frequency_unit = '';
129 }
130 }
131
132 $pledgeBlock->copyValues($params);
133 $result = $pledgeBlock->save();
134
a7488080 135 if (!empty($params['id'])) {
6a488035
TO
136 CRM_Utils_Hook::post('edit', 'PledgeBlock', $pledgeBlock->id, $pledgeBlock);
137 }
138 else {
139 CRM_Utils_Hook::post('create', 'Pledge', $pledgeBlock->id, $pledgeBlock);
140 }
141
142 return $result;
143 }
144
145 /**
fe482240 146 * Delete the pledgeBlock.
6a488035 147 *
3a1617b6
TO
148 * @param int $id
149 * PledgeBlock id.
6a488035 150 *
77b97be7 151 * @return mixed|null
6a488035 152 */
00be9182 153 public static function deletePledgeBlock($id) {
6a488035
TO
154 CRM_Utils_Hook::pre('delete', 'PledgeBlock', $id, CRM_Core_DAO::$_nullArray);
155
156 $transaction = new CRM_Core_Transaction();
157
158 $results = NULL;
159
353ffa53 160 $dao = new CRM_Pledge_DAO_PledgeBlock();
6a488035
TO
161 $dao->id = $id;
162 $results = $dao->delete();
163
164 $transaction->commit();
165
166 CRM_Utils_Hook::post('delete', 'PledgeBlock', $dao->id, $dao);
167
168 return $results;
169 }
170
171 /**
fe482240 172 * Return Pledge Block info in Contribution Pages.
6a488035 173 *
3a1617b6
TO
174 * @param int $pageID
175 * Contribution page id.
77b97be7
EM
176 *
177 * @return array
6a488035 178 */
00be9182 179 public static function getPledgeBlock($pageID) {
6a488035
TO
180 $pledgeBlock = array();
181
353ffa53 182 $dao = new CRM_Pledge_DAO_PledgeBlock();
6a488035 183 $dao->entity_table = 'civicrm_contribution_page';
353ffa53 184 $dao->entity_id = $pageID;
6a488035
TO
185 if ($dao->find(TRUE)) {
186 CRM_Core_DAO::storeValues($dao, $pledgeBlock);
187 }
188
189 return $pledgeBlock;
190 }
191
192 /**
fe482240 193 * Build Pledge Block in Contribution Pages.
6a488035 194 *
c490a46a 195 * @param CRM_Core_Form $form
6a488035 196 */
00be9182 197 public static function buildPledgeBlock($form) {
6a488035 198 //build pledge payment fields.
a7488080 199 if (!empty($form->_values['pledge_id'])) {
6a488035
TO
200 //get all payments required details.
201 $allPayments = array();
2f6befc8
AH
202 $returnProperties = array(
203 'status_id',
204 'scheduled_date',
205 'scheduled_amount',
206 'currency',
207 );
6a488035
TO
208 CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id',
209 $form->_values['pledge_id'], $allPayments, $returnProperties
210 );
211 //get all status
212 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
213
353ffa53
TO
214 $nextPayment = array();
215 $isNextPayment = FALSE;
6a488035 216 $overduePayments = array();
353ffa53 217 $now = date('Ymd');
6a488035
TO
218 foreach ($allPayments as $payID => $value) {
219 if ($allStatus[$value['status_id']] == 'Overdue') {
220 $overduePayments[$payID] = array(
221 'id' => $payID,
222 'scheduled_amount' => CRM_Utils_Rule::cleanMoney($value['scheduled_amount']),
2f6befc8 223 'scheduled_amount_currency' => $value['currency'],
6a488035
TO
224 'scheduled_date' => CRM_Utils_Date::customFormat($value['scheduled_date'],
225 '%B %d'
226 ),
227 );
228 }
229 elseif (!$isNextPayment &&
230 $allStatus[$value['status_id']] == 'Pending'
231 ) {
232 //get the next payment.
233 $nextPayment = array(
234 'id' => $payID,
235 'scheduled_amount' => CRM_Utils_Rule::cleanMoney($value['scheduled_amount']),
2f6befc8 236 'scheduled_amount_currency' => $value['currency'],
6a488035
TO
237 'scheduled_date' => CRM_Utils_Date::customFormat($value['scheduled_date'],
238 '%B %d'
239 ),
240 );
241 $isNextPayment = TRUE;
242 }
243 }
244
245 //build check box array for payments.
246 $payments = array();
247 if (!empty($overduePayments)) {
248 foreach ($overduePayments as $id => $payment) {
62aaa129 249 $key = ts("%1 - due on %2 (overdue)", array(
353ffa53
TO
250 1 => CRM_Utils_Money::format(CRM_Utils_Array::value('scheduled_amount', $payment), CRM_Utils_Array::value('scheduled_amount_currency', $payment)),
251 2 => CRM_Utils_Array::value('scheduled_date', $payment),
252 ));
6a488035
TO
253 $payments[$key] = CRM_Utils_Array::value('id', $payment);
254 }
255 }
256
257 if (!empty($nextPayment)) {
62aaa129 258 $key = ts("%1 - due on %2", array(
353ffa53
TO
259 1 => CRM_Utils_Money::format(CRM_Utils_Array::value('scheduled_amount', $nextPayment), CRM_Utils_Array::value('scheduled_amount_currency', $nextPayment)),
260 2 => CRM_Utils_Array::value('scheduled_date', $nextPayment),
261 ));
6a488035
TO
262 $payments[$key] = CRM_Utils_Array::value('id', $nextPayment);
263 }
264 //give error if empty or build form for payment.
265 if (empty($payments)) {
266 CRM_Core_Error::fatal(ts("Oops. It looks like there is no valid payment status for online payment."));
267 }
268 else {
269 $form->assign('is_pledge_payment', TRUE);
270 $form->addCheckBox('pledge_amount', ts('Make Pledge Payment(s):'), $payments);
271 }
272 }
273 else {
274
275 $pledgeBlock = self::getPledgeBlock($form->_id);
276
277 //build form for pledge creation.
098201d8 278 $pledgeOptions = array(
353ffa53 279 '0' => ts('I want to make a one-time contribution'),
6a488035
TO
280 '1' => ts('I pledge to contribute this amount every'),
281 );
282 $form->addRadio('is_pledge', ts('Pledge Frequency Interval'), $pledgeOptions,
283 NULL, array('<br/>')
284 );
285 $form->addElement('text', 'pledge_installments', ts('Installments'), array('size' => 3));
286
a7488080 287 if (!empty($pledgeBlock['is_pledge_interval'])) {
6a488035
TO
288 $form->assign('is_pledge_interval', CRM_Utils_Array::value('is_pledge_interval', $pledgeBlock));
289 $form->addElement('text', 'pledge_frequency_interval', NULL, array('size' => 3));
290 }
291 else {
292 $form->add('hidden', 'pledge_frequency_interval', 1);
293 }
294 //Frequency unit drop-down label suffixes switch from *ly to *(s)
353ffa53
TO
295 $freqUnitVals = explode(CRM_Core_DAO::VALUE_SEPARATOR, $pledgeBlock['pledge_frequency_unit']);
296 $freqUnits = array();
6a488035
TO
297 $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units');
298 foreach ($freqUnitVals as $key => $val) {
299 if (array_key_exists($val, $frequencyUnits)) {
0d8afee2 300 $freqUnits[$val] = !empty($pledgeBlock['is_pledge_interval']) ? "{$frequencyUnits[$val]}(s)" : $frequencyUnits[$val];
6a488035
TO
301 }
302 }
303 $form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits);
304 }
305 }
96025800 306
6a488035 307}