commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Pledge / BAO / PledgeBlock.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 * Retrieve DB object based on input parameters.
46 *
47 * It also stores all the retrieved values in the default array.
48 *
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.
53 *
54 * @return CRM_Pledge_BAO_PledgeBlock
55 */
56 public static function retrieve(&$params, &$defaults) {
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 /**
67 * Takes an associative array and creates a pledgeBlock object.
68 *
69 * @param array $params
70 * (reference ) an assoc array of name/value pairs.
71 *
72 * @return CRM_Pledge_BAO_PledgeBlock
73 */
74 public static function &create(&$params) {
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 /**
91 * Add pledgeBlock.
92 *
93 * @param array $params
94 * Reference array contains the values submitted by the form.
95 *
96 *
97 * @return object
98 */
99 public static function add(&$params) {
100
101 if (!empty($params['id'])) {
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
135 if (!empty($params['id'])) {
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 /**
146 * Delete the pledgeBlock.
147 *
148 * @param int $id
149 * PledgeBlock id.
150 *
151 * @return mixed|null
152 */
153 public static function deletePledgeBlock($id) {
154 CRM_Utils_Hook::pre('delete', 'PledgeBlock', $id, CRM_Core_DAO::$_nullArray);
155
156 $transaction = new CRM_Core_Transaction();
157
158 $results = NULL;
159
160 $dao = new CRM_Pledge_DAO_PledgeBlock();
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 /**
172 * Return Pledge Block info in Contribution Pages.
173 *
174 * @param int $pageID
175 * Contribution page id.
176 *
177 * @return array
178 */
179 public static function getPledgeBlock($pageID) {
180 $pledgeBlock = array();
181
182 $dao = new CRM_Pledge_DAO_PledgeBlock();
183 $dao->entity_table = 'civicrm_contribution_page';
184 $dao->entity_id = $pageID;
185 if ($dao->find(TRUE)) {
186 CRM_Core_DAO::storeValues($dao, $pledgeBlock);
187 }
188
189 return $pledgeBlock;
190 }
191
192 /**
193 * Build Pledge Block in Contribution Pages.
194 *
195 * @param CRM_Core_Form $form
196 */
197 public static function buildPledgeBlock($form) {
198 //build pledge payment fields.
199 if (!empty($form->_values['pledge_id'])) {
200 //get all payments required details.
201 $allPayments = array();
202 $returnProperties = array(
203 'status_id',
204 'scheduled_date',
205 'scheduled_amount',
206 'currency',
207 );
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
214 $nextPayment = array();
215 $isNextPayment = FALSE;
216 $overduePayments = array();
217 $now = date('Ymd');
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']),
223 'scheduled_amount_currency' => $value['currency'],
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']),
236 'scheduled_amount_currency' => $value['currency'],
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) {
249 $key = ts("%1 - due on %2 (overdue)", array(
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 ));
253 $payments[$key] = CRM_Utils_Array::value('id', $payment);
254 }
255 }
256
257 if (!empty($nextPayment)) {
258 $key = ts("%1 - due on %2", array(
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 ));
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.
278 $pledgeOptions = array(
279 '0' => ts('I want to make a one-time contribution'),
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
287 if (!empty($pledgeBlock['is_pledge_interval'])) {
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)
295 $freqUnitVals = explode(CRM_Core_DAO::VALUE_SEPARATOR, $pledgeBlock['pledge_frequency_unit']);
296 $freqUnits = array();
297 $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units');
298 foreach ($freqUnitVals as $key => $val) {
299 if (array_key_exists($val, $frequencyUnits)) {
300 $freqUnits[$val] = !empty($pledgeBlock['is_pledge_interval']) ? "{$frequencyUnits[$val]}(s)" : $frequencyUnits[$val];
301 }
302 }
303 $form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits);
304 }
305 }
306
307 }