3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 class CRM_Pledge_BAO_PledgeBlock
extends CRM_Pledge_DAO_PledgeBlock
{
40 public function __construct() {
41 parent
::__construct();
45 * Retrieve DB object based on input parameters.
47 * It also stores all the retrieved values in the default array.
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.
54 * @return CRM_Pledge_BAO_PledgeBlock
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);
67 * Takes an associative array and creates a pledgeBlock object.
69 * @param array $params
70 * (reference ) an assoc array of name/value pairs.
72 * @return CRM_Pledge_BAO_PledgeBlock
74 public static function &create(&$params) {
75 $transaction = new CRM_Core_Transaction();
76 $pledgeBlock = self
::add($params);
78 if (is_a($pledgeBlock, 'CRM_Core_Error')) {
79 $pledgeBlock->rollback();
83 $params['id'] = $pledgeBlock->id
;
85 $transaction->commit();
93 * @param array $params
94 * Reference array contains the values submitted by the form.
99 public static function add(&$params) {
101 if (!empty($params['id'])) {
102 CRM_Utils_Hook
::pre('edit', 'PledgeBlock', $params['id'], $params);
105 CRM_Utils_Hook
::pre('create', 'PledgeBlock', NULL, $params);
108 $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
110 //fix for pledge_frequency_unit
111 $freqUnits = CRM_Utils_Array
::value('pledge_frequency_unit', $params);
113 if ($freqUnits && is_array($freqUnits)) {
114 unset($params['pledge_frequency_unit']);
115 $newFreqUnits = array();
116 foreach ($freqUnits as $k => $v) {
118 $newFreqUnits[$k] = $v;
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;
128 $pledgeBlock->pledge_frequency_unit
= '';
132 $pledgeBlock->copyValues($params);
133 $result = $pledgeBlock->save();
135 if (!empty($params['id'])) {
136 CRM_Utils_Hook
::post('edit', 'PledgeBlock', $pledgeBlock->id
, $pledgeBlock);
139 CRM_Utils_Hook
::post('create', 'Pledge', $pledgeBlock->id
, $pledgeBlock);
146 * Delete the pledgeBlock.
153 public static function deletePledgeBlock($id) {
154 CRM_Utils_Hook
::pre('delete', 'PledgeBlock', $id, CRM_Core_DAO
::$_nullArray);
156 $transaction = new CRM_Core_Transaction();
160 $dao = new CRM_Pledge_DAO_PledgeBlock();
162 $results = $dao->delete();
164 $transaction->commit();
166 CRM_Utils_Hook
::post('delete', 'PledgeBlock', $dao->id
, $dao);
172 * Return Pledge Block info in Contribution Pages.
175 * Contribution page id.
179 public static function getPledgeBlock($pageID) {
180 $pledgeBlock = array();
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);
193 * Build Pledge Block in Contribution Pages.
195 * @param CRM_Core_Form $form
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(
208 CRM_Core_DAO
::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id',
209 $form->_values
['pledge_id'], $allPayments, $returnProperties
212 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
214 $nextPayment = array();
215 $isNextPayment = FALSE;
216 $overduePayments = array();
218 foreach ($allPayments as $payID => $value) {
219 if ($allStatus[$value['status_id']] == 'Overdue') {
220 $overduePayments[$payID] = array(
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'],
229 elseif (!$isNextPayment &&
230 $allStatus[$value['status_id']] == 'Pending'
232 //get the next payment.
233 $nextPayment = array(
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'],
241 $isNextPayment = TRUE;
245 //build check box array for payments.
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),
253 $payments[$key] = CRM_Utils_Array
::value('id', $payment);
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),
262 $payments[$key] = CRM_Utils_Array
::value('id', $nextPayment);
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."));
269 $form->assign('is_pledge_payment', TRUE);
270 $form->addCheckBox('pledge_amount', ts('Make Pledge Payment(s):'), $payments);
275 $pledgeBlock = self
::getPledgeBlock($form->_id
);
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'),
282 $form->addRadio('is_pledge', ts('Pledge Frequency Interval'), $pledgeOptions,
285 $form->addElement('text', 'pledge_installments', ts('Installments'), array('size' => 3));
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));
292 $form->add('hidden', 'pledge_frequency_interval', 1);
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];
303 $form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits);