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