3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 public 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
52 * (reference ) an assoc array of name/value pairs.
53 * @param array $defaults
54 * (reference ) an assoc array to hold the flattened values.
56 * @return CRM_Pledge_BAO_PledgeBlock
58 public 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
72 * (reference ) an assoc array of name/value pairs.
74 * @return CRM_Pledge_BAO_PledgeBlock
76 public static function &create(&$params) {
77 $transaction = new CRM_Core_Transaction();
78 $pledgeBlock = self
::add($params);
80 if (is_a($pledgeBlock, 'CRM_Core_Error')) {
81 $pledgeBlock->rollback();
85 $params['id'] = $pledgeBlock->id
;
87 $transaction->commit();
95 * @param array $params
96 * Reference array contains the values submitted by the form.
101 public static function add(&$params) {
103 if (!empty($params['id'])) {
104 CRM_Utils_Hook
::pre('edit', 'PledgeBlock', $params['id'], $params);
107 CRM_Utils_Hook
::pre('create', 'PledgeBlock', NULL, $params);
110 $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
112 //fix for pledge_frequency_unit
113 $freqUnits = CRM_Utils_Array
::value('pledge_frequency_unit', $params);
115 if ($freqUnits && is_array($freqUnits)) {
116 unset($params['pledge_frequency_unit']);
117 $newFreqUnits = array();
118 foreach ($freqUnits as $k => $v) {
120 $newFreqUnits[$k] = $v;
124 $freqUnits = $newFreqUnits;
125 if (is_array($freqUnits) && !empty($freqUnits)) {
126 $freqUnits = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, array_keys($freqUnits));
127 $pledgeBlock->pledge_frequency_unit
= $freqUnits;
130 $pledgeBlock->pledge_frequency_unit
= '';
134 $pledgeBlock->copyValues($params);
135 $result = $pledgeBlock->save();
137 if (!empty($params['id'])) {
138 CRM_Utils_Hook
::post('edit', 'PledgeBlock', $pledgeBlock->id
, $pledgeBlock);
141 CRM_Utils_Hook
::post('create', 'Pledge', $pledgeBlock->id
, $pledgeBlock);
148 * Delete the pledgeBlock
155 public static function deletePledgeBlock($id) {
156 CRM_Utils_Hook
::pre('delete', 'PledgeBlock', $id, CRM_Core_DAO
::$_nullArray);
158 $transaction = new CRM_Core_Transaction();
162 $dao = new CRM_Pledge_DAO_PledgeBlock();
164 $results = $dao->delete();
166 $transaction->commit();
168 CRM_Utils_Hook
::post('delete', 'PledgeBlock', $dao->id
, $dao);
174 * Return Pledge Block info in Contribution Pages
177 * Contribution page id.
181 public static function getPledgeBlock($pageID) {
182 $pledgeBlock = array();
184 $dao = new CRM_Pledge_DAO_PledgeBlock();
185 $dao->entity_table
= 'civicrm_contribution_page';
186 $dao->entity_id
= $pageID;
187 if ($dao->find(TRUE)) {
188 CRM_Core_DAO
::storeValues($dao, $pledgeBlock);
195 * Build Pledge Block in Contribution Pages
197 * @param CRM_Core_Form $form
199 public static function buildPledgeBlock($form) {
200 //build pledge payment fields.
201 if (!empty($form->_values
['pledge_id'])) {
202 //get all payments required details.
203 $allPayments = array();
204 $returnProperties = array(
210 CRM_Core_DAO
::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id',
211 $form->_values
['pledge_id'], $allPayments, $returnProperties
214 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
216 $nextPayment = array();
217 $isNextPayment = FALSE;
218 $overduePayments = array();
220 foreach ($allPayments as $payID => $value) {
221 if ($allStatus[$value['status_id']] == 'Overdue') {
222 $overduePayments[$payID] = array(
224 'scheduled_amount' => CRM_Utils_Rule
::cleanMoney($value['scheduled_amount']),
225 'scheduled_amount_currency' => $value['currency'],
226 'scheduled_date' => CRM_Utils_Date
::customFormat($value['scheduled_date'],
231 elseif (!$isNextPayment &&
232 $allStatus[$value['status_id']] == 'Pending'
234 //get the next payment.
235 $nextPayment = array(
237 'scheduled_amount' => CRM_Utils_Rule
::cleanMoney($value['scheduled_amount']),
238 'scheduled_amount_currency' => $value['currency'],
239 'scheduled_date' => CRM_Utils_Date
::customFormat($value['scheduled_date'],
243 $isNextPayment = TRUE;
247 //build check box array for payments.
249 if (!empty($overduePayments)) {
250 foreach ($overduePayments as $id => $payment) {
251 $key = ts("%1 - due on %2 (overdue)", array(
252 1 => CRM_Utils_Money
::format(CRM_Utils_Array
::value('scheduled_amount', $payment), CRM_Utils_Array
::value('scheduled_amount_currency', $payment)),
253 2 => CRM_Utils_Array
::value('scheduled_date', $payment),
255 $payments[$key] = CRM_Utils_Array
::value('id', $payment);
259 if (!empty($nextPayment)) {
260 $key = ts("%1 - due on %2", array(
261 1 => CRM_Utils_Money
::format(CRM_Utils_Array
::value('scheduled_amount', $nextPayment), CRM_Utils_Array
::value('scheduled_amount_currency', $nextPayment)),
262 2 => CRM_Utils_Array
::value('scheduled_date', $nextPayment),
264 $payments[$key] = CRM_Utils_Array
::value('id', $nextPayment);
266 //give error if empty or build form for payment.
267 if (empty($payments)) {
268 CRM_Core_Error
::fatal(ts("Oops. It looks like there is no valid payment status for online payment."));
271 $form->assign('is_pledge_payment', TRUE);
272 $form->addCheckBox('pledge_amount', ts('Make Pledge Payment(s):'), $payments);
277 $pledgeBlock = self
::getPledgeBlock($form->_id
);
279 //build form for pledge creation.
280 $pledgeOptions = array(
281 '0' => ts('I want to make a one-time contribution'),
282 '1' => ts('I pledge to contribute this amount every'),
284 $form->addRadio('is_pledge', ts('Pledge Frequency Interval'), $pledgeOptions,
287 $form->addElement('text', 'pledge_installments', ts('Installments'), array('size' => 3));
289 if (!empty($pledgeBlock['is_pledge_interval'])) {
290 $form->assign('is_pledge_interval', CRM_Utils_Array
::value('is_pledge_interval', $pledgeBlock));
291 $form->addElement('text', 'pledge_frequency_interval', NULL, array('size' => 3));
294 $form->add('hidden', 'pledge_frequency_interval', 1);
296 //Frequency unit drop-down label suffixes switch from *ly to *(s)
297 $freqUnitVals = explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $pledgeBlock['pledge_frequency_unit']);
298 $freqUnits = array();
299 $frequencyUnits = CRM_Core_OptionGroup
::values('recur_frequency_units');
300 foreach ($freqUnitVals as $key => $val) {
301 if (array_key_exists($val, $frequencyUnits)) {
302 $freqUnits[$val] = !empty($pledgeBlock['is_pledge_interval']) ?
"{$frequencyUnits[$val]}(s)" : $frequencyUnits[$val];
305 $form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits);