3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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
33 class CRM_Pledge_BAO_PledgeBlock
extends CRM_Pledge_DAO_PledgeBlock
{
38 public function __construct() {
39 parent
::__construct();
43 * Retrieve DB object based on input parameters.
45 * It also stores all the retrieved values in the default array.
47 * @param array $params
48 * (reference ) an assoc array of name/value pairs.
49 * @param array $defaults
50 * (reference ) an assoc array to hold the flattened values.
52 * @return CRM_Pledge_BAO_PledgeBlock
54 public static function retrieve(&$params, &$defaults) {
55 $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
56 $pledgeBlock->copyValues($params);
57 if ($pledgeBlock->find(TRUE)) {
58 CRM_Core_DAO
::storeValues($pledgeBlock, $defaults);
65 * Takes an associative array and creates a pledgeBlock object.
67 * @param array $params
68 * (reference ) an assoc array of name/value pairs.
70 * @return CRM_Pledge_BAO_PledgeBlock
72 public static function &create(&$params) {
73 $transaction = new CRM_Core_Transaction();
74 $pledgeBlock = self
::add($params);
76 if (is_a($pledgeBlock, 'CRM_Core_Error')) {
77 $pledgeBlock->rollback();
81 $params['id'] = $pledgeBlock->id
;
83 $transaction->commit();
91 * @param array $params
92 * Reference array contains the values submitted by the form.
97 public static function add(&$params) {
99 if (!empty($params['id'])) {
100 CRM_Utils_Hook
::pre('edit', 'PledgeBlock', $params['id'], $params);
103 CRM_Utils_Hook
::pre('create', 'PledgeBlock', NULL, $params);
106 $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
108 // fix for pledge_frequency_unit
109 $freqUnits = CRM_Utils_Array
::value('pledge_frequency_unit', $params);
111 if ($freqUnits && is_array($freqUnits)) {
112 unset($params['pledge_frequency_unit']);
113 $newFreqUnits = array();
114 foreach ($freqUnits as $k => $v) {
116 $newFreqUnits[$k] = $v;
120 $freqUnits = $newFreqUnits;
121 if (is_array($freqUnits) && !empty($freqUnits)) {
122 $freqUnits = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, array_keys($freqUnits));
123 $pledgeBlock->pledge_frequency_unit
= $freqUnits;
126 $pledgeBlock->pledge_frequency_unit
= '';
130 $pledgeBlock->copyValues($params);
131 $result = $pledgeBlock->save();
133 if (!empty($params['id'])) {
134 CRM_Utils_Hook
::post('edit', 'PledgeBlock', $pledgeBlock->id
, $pledgeBlock);
137 CRM_Utils_Hook
::post('create', 'Pledge', $pledgeBlock->id
, $pledgeBlock);
144 * Delete the pledgeBlock.
151 public static function deletePledgeBlock($id) {
152 CRM_Utils_Hook
::pre('delete', 'PledgeBlock', $id, CRM_Core_DAO
::$_nullArray);
154 $transaction = new CRM_Core_Transaction();
158 $dao = new CRM_Pledge_DAO_PledgeBlock();
160 $results = $dao->delete();
162 $transaction->commit();
164 CRM_Utils_Hook
::post('delete', 'PledgeBlock', $dao->id
, $dao);
170 * Return Pledge Block info in Contribution Pages.
173 * Contribution page id.
177 public static function getPledgeBlock($pageID) {
178 $pledgeBlock = array();
180 $dao = new CRM_Pledge_DAO_PledgeBlock();
181 $dao->entity_table
= 'civicrm_contribution_page';
182 $dao->entity_id
= $pageID;
183 if ($dao->find(TRUE)) {
184 CRM_Core_DAO
::storeValues($dao, $pledgeBlock);
191 * Build Pledge Block in Contribution Pages.
193 * @param CRM_Core_Form $form
195 public static function buildPledgeBlock($form) {
196 //build pledge payment fields.
197 if (!empty($form->_values
['pledge_id'])) {
198 //get all payments required details.
199 $allPayments = array();
200 $returnProperties = array(
206 CRM_Core_DAO
::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id',
207 $form->_values
['pledge_id'], $allPayments, $returnProperties
210 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
212 $nextPayment = array();
213 $isNextPayment = FALSE;
214 $overduePayments = array();
216 foreach ($allPayments as $payID => $value) {
217 if ($allStatus[$value['status_id']] == 'Overdue') {
218 $overduePayments[$payID] = array(
220 'scheduled_amount' => CRM_Utils_Rule
::cleanMoney($value['scheduled_amount']),
221 'scheduled_amount_currency' => $value['currency'],
222 'scheduled_date' => CRM_Utils_Date
::customFormat($value['scheduled_date'],
227 elseif (!$isNextPayment &&
228 $allStatus[$value['status_id']] == 'Pending'
230 // get the next payment.
231 $nextPayment = array(
233 'scheduled_amount' => CRM_Utils_Rule
::cleanMoney($value['scheduled_amount']),
234 'scheduled_amount_currency' => $value['currency'],
235 'scheduled_date' => CRM_Utils_Date
::customFormat($value['scheduled_date'],
239 $isNextPayment = TRUE;
243 // build check box array for payments.
245 if (!empty($overduePayments)) {
246 foreach ($overduePayments as $id => $payment) {
247 $key = ts("%1 - due on %2 (overdue)", array(
248 1 => CRM_Utils_Money
::format(CRM_Utils_Array
::value('scheduled_amount', $payment), CRM_Utils_Array
::value('scheduled_amount_currency', $payment)),
249 2 => CRM_Utils_Array
::value('scheduled_date', $payment),
251 $payments[$key] = CRM_Utils_Array
::value('id', $payment);
255 if (!empty($nextPayment)) {
256 $key = ts("%1 - due on %2", array(
257 1 => CRM_Utils_Money
::format(CRM_Utils_Array
::value('scheduled_amount', $nextPayment), CRM_Utils_Array
::value('scheduled_amount_currency', $nextPayment)),
258 2 => CRM_Utils_Array
::value('scheduled_date', $nextPayment),
260 $payments[$key] = CRM_Utils_Array
::value('id', $nextPayment);
262 // give error if empty or build form for payment.
263 if (empty($payments)) {
264 CRM_Core_Error
::fatal(ts("Oops. It looks like there is no valid payment status for online payment."));
267 $form->assign('is_pledge_payment', TRUE);
268 $form->addCheckBox('pledge_amount', ts('Make Pledge Payment(s):'), $payments);
273 $pledgeBlock = self
::getPledgeBlock($form->_id
);
275 // build form for pledge creation.
276 $pledgeOptions = array(
277 '0' => ts('I want to make a one-time contribution'),
278 '1' => ts('I pledge to contribute this amount every'),
280 $form->addRadio('is_pledge', ts('Pledge Frequency Interval'), $pledgeOptions,
283 $form->addElement('text', 'pledge_installments', ts('Installments'), array('size' => 3));
285 if (!empty($pledgeBlock['is_pledge_interval'])) {
286 $form->assign('is_pledge_interval', CRM_Utils_Array
::value('is_pledge_interval', $pledgeBlock));
287 $form->addElement('text', 'pledge_frequency_interval', NULL, array('size' => 3));
290 $form->add('hidden', 'pledge_frequency_interval', 1);
292 // Frequency unit drop-down label suffixes switch from *ly to *(s)
293 $freqUnitVals = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $pledgeBlock['pledge_frequency_unit']);
294 $freqUnits = array();
295 $frequencyUnits = CRM_Core_OptionGroup
::values('recur_frequency_units');
296 foreach ($freqUnitVals as $key => $val) {
297 if (array_key_exists($val, $frequencyUnits)) {
298 $freqUnits[$val] = !empty($pledgeBlock['is_pledge_interval']) ?
"{$frequencyUnits[$val]}(s)" : $frequencyUnits[$val];
301 $form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits);