Merge pull request #4696 from colemanw/CRM-15669
[civicrm-core.git] / CRM / Pledge / BAO / PledgeBlock.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35 class CRM_Pledge_BAO_PledgeBlock extends CRM_Pledge_DAO_PledgeBlock {
36
37 /**
38 * Class constructor
39 */
40 public function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Takes a bunch of params that are needed to match certain criteria and
46 * retrieves the relevant objects. Typically the valid params are only
47 * pledgeBlock id. We'll tweak this function to be more full featured over a period
48 * of time. This is the inverse function of create. It also stores all the retrieved
49 * values in the default array
50 *
51 * @param array $params (reference ) an assoc array of name/value pairs
52 * @param array $defaults (reference ) an assoc array to hold the flattened values
53 *
54 * @return CRM_Pledge_BAO_PledgeBlock object
55 * @static
56 */
57 public static function retrieve(&$params, &$defaults) {
58 $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
59 $pledgeBlock->copyValues($params);
60 if ($pledgeBlock->find(TRUE)) {
61 CRM_Core_DAO::storeValues($pledgeBlock, $defaults);
62 return $pledgeBlock;
63 }
64 return NULL;
65 }
66
67 /**
68 * Takes an associative array and creates a pledgeBlock object
69 *
70 * @param array $params (reference ) an assoc array of name/value pairs
71 *
72 * @return CRM_Pledge_BAO_PledgeBlock object
73 * @static
74 */
75 public static function &create(&$params) {
76 $transaction = new CRM_Core_Transaction();
77 $pledgeBlock = self::add($params);
78
79 if (is_a($pledgeBlock, 'CRM_Core_Error')) {
80 $pledgeBlock->rollback();
81 return $pledgeBlock;
82 }
83
84 $params['id'] = $pledgeBlock->id;
85
86 $transaction->commit();
87
88 return $pledgeBlock;
89 }
90
91 /**
92 * Add pledgeBlock
93 *
94 * @param array $params reference array contains the values submitted by the form
95 *
96 * @static
97 *
98 * @return object
99 */
100 public static function add(&$params) {
101
102 if (!empty($params['id'])) {
103 CRM_Utils_Hook::pre('edit', 'PledgeBlock', $params['id'], $params);
104 }
105 else {
106 CRM_Utils_Hook::pre('create', 'PledgeBlock', NULL, $params);
107 }
108
109 $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
110
111 //fix for pledge_frequency_unit
112 $freqUnits = CRM_Utils_Array::value('pledge_frequency_unit', $params);
113
114 if ($freqUnits && is_array($freqUnits)) {
115 unset($params['pledge_frequency_unit']);
116 $newFreqUnits = array();
117 foreach ($freqUnits as $k => $v) {
118 if ($v) {
119 $newFreqUnits[$k] = $v;
120 }
121 }
122
123 $freqUnits = $newFreqUnits;
124 if (is_array($freqUnits) && !empty($freqUnits)) {
125 $freqUnits = implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($freqUnits));
126 $pledgeBlock->pledge_frequency_unit = $freqUnits;
127 }
128 else {
129 $pledgeBlock->pledge_frequency_unit = '';
130 }
131 }
132
133 $pledgeBlock->copyValues($params);
134 $result = $pledgeBlock->save();
135
136 if (!empty($params['id'])) {
137 CRM_Utils_Hook::post('edit', 'PledgeBlock', $pledgeBlock->id, $pledgeBlock);
138 }
139 else {
140 CRM_Utils_Hook::post('create', 'Pledge', $pledgeBlock->id, $pledgeBlock);
141 }
142
143 return $result;
144 }
145
146 /**
147 * Delete the pledgeBlock
148 *
149 * @param int $id pledgeBlock id
150 *
151 * @return mixed|null
152 * @static
153 */
154 public static function deletePledgeBlock($id) {
155 CRM_Utils_Hook::pre('delete', 'PledgeBlock', $id, CRM_Core_DAO::$_nullArray);
156
157 $transaction = new CRM_Core_Transaction();
158
159 $results = NULL;
160
161 $dao = new CRM_Pledge_DAO_PledgeBlock();
162 $dao->id = $id;
163 $results = $dao->delete();
164
165 $transaction->commit();
166
167 CRM_Utils_Hook::post('delete', 'PledgeBlock', $dao->id, $dao);
168
169 return $results;
170 }
171
172 /**
173 * Return Pledge Block info in Contribution Pages
174 *
175 * @param int $pageID contribution page id
176 *
177 * @return array
178 * @static
179 */
180 public static function getPledgeBlock($pageID) {
181 $pledgeBlock = array();
182
183 $dao = new CRM_Pledge_DAO_PledgeBlock();
184 $dao->entity_table = 'civicrm_contribution_page';
185 $dao->entity_id = $pageID;
186 if ($dao->find(TRUE)) {
187 CRM_Core_DAO::storeValues($dao, $pledgeBlock);
188 }
189
190 return $pledgeBlock;
191 }
192
193 /**
194 * Build Pledge Block in Contribution Pages
195 *
196 * @param CRM_Core_Form $form
197 * @static
198 */
199 public static function buildPledgeBlock($form) {
200 //build pledge payment fields.
201 if (!empty($form->_values['pledge_id'])) {
202 //get all payments required details.
203 $allPayments = array();
204 $returnProperties = array(
205 'status_id',
206 'scheduled_date',
207 'scheduled_amount',
208 'currency',
209 );
210 CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id',
211 $form->_values['pledge_id'], $allPayments, $returnProperties
212 );
213 //get all status
214 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
215
216 $nextPayment = array();
217 $isNextPayment = FALSE;
218 $overduePayments = array();
219 $now = date('Ymd');
220 foreach ($allPayments as $payID => $value) {
221 if ($allStatus[$value['status_id']] == 'Overdue') {
222 $overduePayments[$payID] = array(
223 'id' => $payID,
224 'scheduled_amount' => CRM_Utils_Rule::cleanMoney($value['scheduled_amount']),
225 'scheduled_amount_currency' => $value['currency'],
226 'scheduled_date' => CRM_Utils_Date::customFormat($value['scheduled_date'],
227 '%B %d'
228 ),
229 );
230 }
231 elseif (!$isNextPayment &&
232 $allStatus[$value['status_id']] == 'Pending'
233 ) {
234 //get the next payment.
235 $nextPayment = array(
236 'id' => $payID,
237 'scheduled_amount' => CRM_Utils_Rule::cleanMoney($value['scheduled_amount']),
238 'scheduled_amount_currency' => $value['currency'],
239 'scheduled_date' => CRM_Utils_Date::customFormat($value['scheduled_date'],
240 '%B %d'
241 ),
242 );
243 $isNextPayment = TRUE;
244 }
245 }
246
247 //build check box array for payments.
248 $payments = array();
249 if (!empty($overduePayments)) {
250 foreach ($overduePayments as $id => $payment) {
251 $key = ts("%1 - due on %2 (overdue)", array(
252 1 => CRM_Utils_Money::format(CRM_Utils_Array::value('scheduled_amount', $payment), CRM_Utils_Array::value('scheduled_amount_currency', $payment)),
253 2 => CRM_Utils_Array::value('scheduled_date', $payment),
254 ));
255 $payments[$key] = CRM_Utils_Array::value('id', $payment);
256 }
257 }
258
259 if (!empty($nextPayment)) {
260 $key = ts("%1 - due on %2", array(
261 1 => CRM_Utils_Money::format(CRM_Utils_Array::value('scheduled_amount', $nextPayment), CRM_Utils_Array::value('scheduled_amount_currency', $nextPayment)),
262 2 => CRM_Utils_Array::value('scheduled_date', $nextPayment),
263 ));
264 $payments[$key] = CRM_Utils_Array::value('id', $nextPayment);
265 }
266 //give error if empty or build form for payment.
267 if (empty($payments)) {
268 CRM_Core_Error::fatal(ts("Oops. It looks like there is no valid payment status for online payment."));
269 }
270 else {
271 $form->assign('is_pledge_payment', TRUE);
272 $form->addCheckBox('pledge_amount', ts('Make Pledge Payment(s):'), $payments);
273 }
274 }
275 else {
276
277 $pledgeBlock = self::getPledgeBlock($form->_id);
278
279 //build form for pledge creation.
280 $pledgeOptions = array('0' => ts('I want to make a one-time contribution'),
281 '1' => ts('I pledge to contribute this amount every'),
282 );
283 $form->addRadio('is_pledge', ts('Pledge Frequency Interval'), $pledgeOptions,
284 NULL, array('<br/>')
285 );
286 $form->addElement('text', 'pledge_installments', ts('Installments'), array('size' => 3));
287
288 if (!empty($pledgeBlock['is_pledge_interval'])) {
289 $form->assign('is_pledge_interval', CRM_Utils_Array::value('is_pledge_interval', $pledgeBlock));
290 $form->addElement('text', 'pledge_frequency_interval', NULL, array('size' => 3));
291 }
292 else {
293 $form->add('hidden', 'pledge_frequency_interval', 1);
294 }
295 //Frequency unit drop-down label suffixes switch from *ly to *(s)
296 $freqUnitVals = explode(CRM_Core_DAO::VALUE_SEPARATOR, $pledgeBlock['pledge_frequency_unit']);
297 $freqUnits = array();
298 $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units');
299 foreach ($freqUnitVals as $key => $val) {
300 if (array_key_exists($val, $frequencyUnits)) {
301 $freqUnits[$val] = !empty($pledgeBlock['is_pledge_interval']) ? "{$frequencyUnits[$val]}(s)" : $frequencyUnits[$val];
302 }
303 }
304 $form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits);
305 }
306 }
307 }