'doList', 'new' => 'doNew', 'insert' => 'doInsert', 'cancel' => 'doBack' , 'thanks' => 'doThanks' ); /** * @var SiteDoc */ var $Doc; /** * @var App_BattleDonation_Obj */ var $DBObj; var $_cardTypeOptions = array( 'Visa' => 'Visa', 'AmEx' => 'AmEx', 'MC' => 'MC', ); var $_requiredHtml = '*'; function exec($Doc) { $this->Doc = $Doc; $this->DBObj = SiteMap::getObj('BattleDonation/Obj.php'); if (Qs_Request::getRequestValue('action')) { if (CURRENT_PROTOCOL == 'http' && Constant::get('HAS_HTTPS')) { require_once 'class/HTTP.php'; $this->redirect('https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']); } } if (CURRENT_PROTOCOL == 'https') { $this->Doc->enableHTTPS(false); } $this->Doc->setBodyAttrib('class', 'battle'); parent::exec(); } function doList() { $item = array( 'total' => $this->DBObj->getTotalAmount(), 'list' => $this->DBObj->getList(), 'tpl' => BASE_PATH . '/tpl/BattleDonation/list.tpl' ); $this->Doc->addItem($item); $this->Doc->addItemProp('JSs', 'js/jquery.scrollTo.js'); return true; } function doNew() { $form = $this->_getNewForm(); $form->setConstants(array('action' => 'insert')); $defaults = Qs_Request::getGet(); $defaults['billing_state'] = 'MN'; if (null !== ($member = $this->Doc->MemberAuth->getData())) { $defaults['contributor_name'] = $member['first_name'] . ' ' . $member['last_name']; $defaults['billing_name'] = $defaults['contributor_name']; $defaults['contributor_email'] = $member['email']; $defaults['billing_address'] = $member['address']; $defaults['billing_city'] = $member['city']; $defaults['billing_state'] = $member['state']; $defaults['billing_zip'] = $member['zip']; } $form->setDefaults($defaults); $this->renderForm($form); } public static function amountElementMultiSeparatorCallback($html, $name, $inputAttribs, $labelAttribs) { if ('beforeElement' == $name && $inputAttribs['checked'] == 'checked') { $html = str_replace('

', '

', $html); } return $html; } function _bindFormFields(Form $form) { $form->setAttribute('action', REAL_BASE_URL . '/' . CURR_PAGE_FULL); $amount = &$form->addElement( 'multi_checkbox', 'amount', '', $this->DBObj->getAmount4Select(), array( 'onchange' => "$(qs.getParentTag(this, 'P')).toggleClass('checked');", 'onclick' => 'this.blur()' ) ); $amount->setDisabledValues($this->DBObj->getDisabledAmounts()); $amount->setMultiSeparatorsCallback(array($this, 'amountElementMultiSeparatorCallback')); $amount->setMultiSeparators( array( 'beforeElement' => '

', 'afterLabel' => '

' ) ); $form->addElement('header', 'contributor', 'Contributor'); $form->addElement('text', 'contributor_name', 'Contributor Name'); $form->addElement('text', 'contributor_email', 'Contributor Email'); $form->addElement('text', 'billing_name', 'Billing Name'); $form->addElement('text', 'billing_address', 'Billing Address'); $form->addElement('text', 'billing_city', 'City'); $form->addElement( 'select', 'billing_state', 'State', array('' => 'Select One') + (array) $this->DBObj->getStateSelect() ); $form->addElement('text', 'billing_zip', 'Zip Code'); $groupCardType = array(); foreach ($this->_cardTypeOptions as $value => $label) { $groupCardType[] = $form->createElement('radio', 'card_type', '', $label, $value); } $form->addGroup($groupCardType, 'groupCardType', $this->_requiredHtml . 'Card Type', ' ', false); $form->addElement('text', 'card_number', 'Card Number', array('autocomplete' => 'off')); $form->addElement( 'date', 'card_exp_date', $this->_requiredHtml . 'Exp Date (MM/YYYY)', array( 'format' => 'MY', 'addEmptyOption' => true, 'minYear' => date('Y'), 'maxYear' => date('Y') + 10 ) ); $form->addElement('text', 'card_code', '3-Digit Sec Code', array('autocomplete' => 'off')); $form->addRuleRequired( array('contributor_name', 'contributor_email', 'billing_name', 'billing_address', 'billing_city', 'billing_state', 'billing_zip'), 'server' ); $form->addRuleEx(array('contributor_email'), ' is in wrong format', 'email', 'server'); $form->addGroupRule( 'groupCardType', array( 'card_type' => array( array('Card Type is required', 'required') ) ) ); $form->addRuleRequired(array('card_number', 'card_code'), 'server'); $form->addFormRule(array($this, 'validateForm')); return $form; } function validateForm($data) { $errors = array(); if (empty($data['card_exp_date']['M']) || empty($data['card_exp_date']['Y'])) { $errors['card_exp_date'] = 'Exp Date is required'; } if (empty($data['amount'])) { $errors['amount'] = 'Please check donation amount'; } else if (true !== ($amounts = $this->DBObj->isUniqueAmount($data['amount']))) { $parts = array(); foreach ($amounts as $amount) { $parts[] = '$' . number_format($amount, 0, '.', ','); } $errors['amount'] = Qs_Array::implodeLast(', ', ' and ', $parts) . ' ' . Qs_Translate::getPlural('donation', count($parts)) . ' ' . Qs_Translate::getPlural(array('is', 'are'), count($parts)) . ' ' . 'already paid by other person. Select another amount.'; } return (empty($errors)) ? true : $errors; } function renderForm(Form $form) { $form->initElements(); $form->setAttribute('onsubmit', 'return false'); $this->Doc->addItemProp('JSs', 'js/app/battle-donation.js'); $this->Doc->addItemProp('JSs', 'js/jquery.scrollTo.js'); $this->Doc->addItemProp('JSs', 'js/lib/form.js'); $options = array( 'errorDisplayMethod' => 'HTML', 'beforeSubmitCallback' => 'showProcessingBox', 'onCompleteCallback' => 'hideProcessingBox', 'onSuccessCallback' => array('App_BattleDonation_Form.onSuccess' => array($form->getAttribute('id'))) ); $this->Doc->addItemProp( 'initFuncsEx', array( 'name' => 'App_BattleDonation_Form.init', 'params' => array($form->getAttribute('id')) ) ); $this->Doc->addItemProp( 'initFuncsEx', array( 'name' => 'Qs_Form.init', 'params' => array( $form->getAttribute('id'), $options ) ) ); $form->tpl = SiteMap::getPath('BattleDonation/tpl/new.tpl'); $form->return_form_arr = true; $item = $form->exec(false); $item['list'] = $this->DBObj->getList(); if ($this->DBObj->getMaxAmount() > ($max = $this->_getMaxValue($form->exportValue('amount')))) { $this->Doc->addInitFunction("$('#scrollable-check').scrollTo", array('#amount-' . $max, array('offset' => -10))); } $this->Doc->addInitFunction("$.scrollTo", array('#content_wrap')); $this->Doc->addItem($item); } function _getMaxValue($amount = array()) { if (empty($amount)) { $max = $this->DBObj->getMaxAmount(); } else if (count($amount) == 1) { $max = reset($amount); } else { $max = call_user_func_array('max', $amount); } return $max; } function _getFirstName($name) { $parts = explode(' ', $name); return array_shift($parts); } function _getLastName($name) { $parts = explode(' ', $name); array_shift($parts); return implode(' ', $parts); } function getAuthorizeObj($type = 'AIM') { require_once('class/Authorize.php'); return Frwd_Authorize::factory( $type, array( 'login' => AUTHORIZENET_API_LOGIN_ID, 'transactionKey' => AUTHORIZENET_TRANSACTION_KEY, 'testRequest' => (boolean)(AUTHORIZENET_TEST_REQUEST === 'TRUE') ) ); } function doInsertAjax() { $form = $this->_getNewForm(); $result = array(); if ($form->validate()) { $data = $form->exportValues(); $paymentResult = $this->getAuthorizeObj()->captureCreditCard(array( 'x_card_num' => $data['card_number'], 'x_card_code' => $data['card_code'], 'x_exp_date' => $data['card_exp_date']['M'] . $data['card_exp_date']['Y'], 'x_amount' => array_sum($data['amount']), 'x_first_name' => $this->_getFirstName($data['billing_name']), 'x_last_name' => $this->_getLastName($data['billing_name']), 'x_address' => $data['billing_address'], 'x_city' => $data['billing_city'], 'x_state' => $data['billing_state'], 'x_zip' => $data['billing_zip'], 'x_country' => 'USA', 'x_email' => $data['contributor_email'], )); if ($paymentResult[0] == 1) { $result['isValid'] = true; $query = array('action' => 'thanks'); if ($this->DBObj->getMaxAmount() > ($max = $this->_getMaxValue($data['amount']))) { $query['amount'] = $max; } $result['url'] = $this->url($query); $data['transaction_id'] = $paymentResult[6]; $data['card_number'] = str_repeat('*', strlen($data['card_number']) - 4) . substr($data['card_number'], -4); $this->DBObj->insert($data); $this->DBObj->initFromDB(); $this->_sendMail2Admin($this->DBObj->getData()); $this->_sendMail2Contributor($this->DBObj->getData()); } else { $result['isValid'] = false; $result['errors'] = array(); $result['errors']['payment'] = $paymentResult[3]; } } else { $result['isValid'] = false; $result['errors'] = $form->_errors; // move payment element to first position if (isset($result['errors']['payment'])) { $errors = $result['errors']; $result['errors'] = array('payment' => $errors['payment']); unset($errors['payment']); $result['errors'] += (array) $errors; } } $this->displayJSON($result); } function doThanks() { $this->Doc->addItem(array('tpl' => 'BattleDonation/thanks.tpl')); if (null !== ($amount = Qs_Request::getGetValue('amount'))) { $this->Doc->addInitFunction("$('#scrollable-container').scrollTo", array('#amount-' . $amount)); } $this->Doc->addInitFunction("$.scrollTo", array('#content_wrap')); $this->doList(); } protected function _sendMail2Admin(array $data) { require_once('app/Settings/Settings.php'); $this->Doc->assign('item', $data); Constant::set('DEBUG', false); $options = array( 'subject' => 'New donation has been received', 'from' => Settings::get('admin_email_from'), 'to' => Settings::getAdminEmails('battle_donation_emails'), 'html' => $this->Doc->fetch(SiteMap::getPath('BattleDonation/tpl/email2admin.tpl')) ); $this->_sendMail($options); } protected function _sendMail2Contributor(array $data) { require_once('app/Settings/Settings.php'); $this->Doc->assign('item', $data); Constant::set('DEBUG', false); $options = array( 'subject' => 'Thank you for your contribution to Camp Heartland!', 'from' => Settings::get('admin_email_from'), 'to' => $data['contributor_email'], 'html' => $this->Doc->fetch(SiteMap::getPath('BattleDonation/tpl/email2contributor.tpl')) ); $this->_sendMail($options); } }