3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
35 class CRM_Pledge_BAO_PledgeBlock
extends CRM_Pledge_DAO_PledgeBlock
{
40 function __construct() {
41 parent
::__construct();
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
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
54 * @return object CRM_Pledge_BAO_PledgeBlock object
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);
69 * takes an associative array and creates a pledgeBlock object
71 * @param array $params (reference ) an assoc array of name/value pairs
73 * @return object CRM_Pledge_BAO_PledgeBlock object
77 static function &create(&$params) {
78 $transaction = new CRM_Core_Transaction();
79 $pledgeBlock = self
::add($params);
81 if (is_a($pledgeBlock, 'CRM_Core_Error')) {
82 $pledgeBlock->rollback();
86 $params['id'] = $pledgeBlock->id
;
88 $transaction->commit();
94 * function to add pledgeBlock
96 * @param array $params reference array contains the values submitted by the form
103 static function add(&$params) {
105 if (!empty($params['id'])) {
106 CRM_Utils_Hook
::pre('edit', 'PledgeBlock', $params['id'], $params);
109 CRM_Utils_Hook
::pre('create', 'PledgeBlock', NULL, $params);
112 $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
114 //fix for pledge_frequency_unit
115 $freqUnits = CRM_Utils_Array
::value('pledge_frequency_unit', $params);
117 if ($freqUnits && is_array($freqUnits)) {
118 unset($params['pledge_frequency_unit']);
119 $newFreqUnits = array();
120 foreach ($freqUnits as $k => $v) {
122 $newFreqUnits[$k] = $v;
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;
132 $pledgeBlock->pledge_frequency_unit
= '';
136 $pledgeBlock->copyValues($params);
137 $result = $pledgeBlock->save();
139 if (!empty($params['id'])) {
140 CRM_Utils_Hook
::post('edit', 'PledgeBlock', $pledgeBlock->id
, $pledgeBlock);
143 CRM_Utils_Hook
::post('create', 'Pledge', $pledgeBlock->id
, $pledgeBlock);
150 * Function to delete the pledgeBlock
152 * @param int $id pledgeBlock id
158 static function deletePledgeBlock($id) {
159 CRM_Utils_Hook
::pre('delete', 'PledgeBlock', $id, CRM_Core_DAO
::$_nullArray);
161 $transaction = new CRM_Core_Transaction();
165 $dao = new CRM_Pledge_DAO_PledgeBlock();
167 $results = $dao->delete();
169 $transaction->commit();
171 CRM_Utils_Hook
::post('delete', 'PledgeBlock', $dao->id
, $dao);
177 * Function to return Pledge Block info in Contribution Pages
182 * @internal param int $pageId contribution page id
186 static function getPledgeBlock($pageID) {
187 $pledgeBlock = array();
189 $dao = new CRM_Pledge_DAO_PledgeBlock();
190 $dao->entity_table
= 'civicrm_contribution_page';
191 $dao->entity_id
= $pageID;
192 if ($dao->find(TRUE)) {
193 CRM_Core_DAO
::storeValues($dao, $pledgeBlock);
200 * Function to build Pledge Block in Contribution Pages
205 static function buildPledgeBlock($form) {
206 //build pledge payment fields.
207 if (!empty($form->_values
['pledge_id'])) {
208 //get all payments required details.
209 $allPayments = array();
210 $returnProperties = array(
216 CRM_Core_DAO
::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id',
217 $form->_values
['pledge_id'], $allPayments, $returnProperties
220 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
222 $nextPayment = array();
223 $isNextPayment = FALSE;
224 $overduePayments = array();
226 foreach ($allPayments as $payID => $value) {
227 if ($allStatus[$value['status_id']] == 'Overdue') {
228 $overduePayments[$payID] = array(
230 'scheduled_amount' => CRM_Utils_Rule
::cleanMoney($value['scheduled_amount']),
231 'scheduled_amount_currency' => $value['currency'],
232 'scheduled_date' => CRM_Utils_Date
::customFormat($value['scheduled_date'],
237 elseif (!$isNextPayment &&
238 $allStatus[$value['status_id']] == 'Pending'
240 //get the next payment.
241 $nextPayment = array(
243 'scheduled_amount' => CRM_Utils_Rule
::cleanMoney($value['scheduled_amount']),
244 'scheduled_amount_currency' => $value['currency'],
245 'scheduled_date' => CRM_Utils_Date
::customFormat($value['scheduled_date'],
249 $isNextPayment = TRUE;
253 //build check box array for payments.
255 if (!empty($overduePayments)) {
256 foreach ($overduePayments as $id => $payment) {
257 $key = ts("%1 - due on %2 (overdue)", array(
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),
261 $payments[$key] = CRM_Utils_Array
::value('id', $payment);
265 if (!empty($nextPayment)) {
266 $key = ts("%1 - due on %2", array(
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),
270 $payments[$key] = CRM_Utils_Array
::value('id', $nextPayment);
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."));
277 $form->assign('is_pledge_payment', TRUE);
278 $form->addCheckBox('pledge_amount', ts('Make Pledge Payment(s):'), $payments);
283 $pledgeBlock = self
::getPledgeBlock($form->_id
);
285 //build form for pledge creation.
286 $pledgeOptions = array('0' => ts('I want to make a one-time contribution'),
287 '1' => ts('I pledge to contribute this amount every'),
289 $form->addRadio('is_pledge', ts('Pledge Frequency Interval'), $pledgeOptions,
292 $form->addElement('text', 'pledge_installments', ts('Installments'), array('size' => 3));
294 if (!empty($pledgeBlock['is_pledge_interval'])) {
295 $form->assign('is_pledge_interval', CRM_Utils_Array
::value('is_pledge_interval', $pledgeBlock));
296 $form->addElement('text', 'pledge_frequency_interval', NULL, array('size' => 3));
299 $form->add('hidden', 'pledge_frequency_interval', 1);
301 //Frequency unit drop-down label suffixes switch from *ly to *(s)
302 $freqUnitVals = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $pledgeBlock['pledge_frequency_unit']);
303 $freqUnits = array();
304 $frequencyUnits = CRM_Core_OptionGroup
::values('recur_frequency_units');
305 foreach ($freqUnitVals as $key => $val) {
306 if (array_key_exists($val, $frequencyUnits)) {
307 $freqUnits[$val] = !empty($pledgeBlock['is_pledge_interval']) ?
"{$frequencyUnits[$val]}(s)" : $frequencyUnits[$val];
310 $form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits);