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