From bec0826ad7afc544b63255a2a8545c12731aba8b Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Thu, 5 Mar 2020 22:11:55 +0000 Subject: [PATCH] Code cleanup in event cart --- CRM/Event/Cart/BAO/MerParticipant.php | 19 +++- CRM/Event/Cart/Form/Cart.php | 16 +-- .../Form/Checkout/ParticipantsAndPrices.php | 4 +- CRM/Event/Cart/Form/Checkout/Payment.php | 104 ++++++------------ CRM/Event/Cart/Form/MerParticipant.php | 20 ++-- CRM/Event/Cart/Page/AddToCart.php | 21 ++-- .../CRM/Event/Cart/Form/Checkout/Payment.tpl | 39 ------- 7 files changed, 81 insertions(+), 142 deletions(-) diff --git a/CRM/Event/Cart/BAO/MerParticipant.php b/CRM/Event/Cart/BAO/MerParticipant.php index 4ae7dc1062..5d307df7be 100644 --- a/CRM/Event/Cart/BAO/MerParticipant.php +++ b/CRM/Event/Cart/BAO/MerParticipant.php @@ -13,8 +13,20 @@ * Class CRM_Event_Cart_BAO_MerParticipant */ class CRM_Event_Cart_BAO_MerParticipant extends CRM_Event_BAO_Participant { + + /** + * @var string + */ public $email = NULL; + + /** + * @var int + */ public $contribution_id = NULL; + + /** + * @var \CRM_Event_Cart_BAO_Cart + */ public $cart = NULL; /** @@ -43,19 +55,14 @@ class CRM_Event_Cart_BAO_MerParticipant extends CRM_Event_BAO_Participant { 'contact_id' => $params['contact_id'], 'event_id' => $params['event_id'], 'cart_id' => $params['cart_id'], - //XXX - //'registered_by_id' => - //'discount_amount' => - //'fee_level' => $params['fee_level'], ]; $participant = CRM_Event_BAO_Participant::create($participantParams); if (is_a($participant, 'CRM_Core_Error')) { - CRM_Core_Error::fatal(ts('There was an error creating a cart participant')); + throw new CRM_Core_Exception(ts('There was an error creating a cart participant')); } $mer_participant = new CRM_Event_Cart_BAO_MerParticipant($participant); - return $mer_participant; } diff --git a/CRM/Event/Cart/Form/Cart.php b/CRM/Event/Cart/Form/Cart.php index 6e16ac7a31..475c88ffbc 100644 --- a/CRM/Event/Cart/Form/Cart.php +++ b/CRM/Event/Cart/Form/Cart.php @@ -4,6 +4,10 @@ * Class CRM_Event_Cart_Form_Cart */ class CRM_Event_Cart_Form_Cart extends CRM_Core_Form { + + /** + * @var \CRM_Event_Cart_BAO_Cart + */ public $cart; public $_action; @@ -13,7 +17,7 @@ class CRM_Event_Cart_Form_Cart extends CRM_Core_Form { public $participants; public function preProcess() { - $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE); + $this->_action = CRM_Utils_Request::retrieveValue('action', 'String'); $this->_mode = 'live'; $this->loadCart(); @@ -50,7 +54,7 @@ class CRM_Event_Cart_Form_Cart extends CRM_Core_Form { $participant_params = [ 'cart_id' => $this->cart->id, 'event_id' => $event_in_cart->event_id, - 'contact_id' => self::find_or_create_contact($this->getContactID()), + 'contact_id' => self::find_or_create_contact(), ]; $participant = CRM_Event_Cart_BAO_MerParticipant::create($participant_params); $participant->save(); @@ -104,11 +108,10 @@ class CRM_Event_Cart_Form_Cart extends CRM_Core_Form { * @return mixed */ public function getContactID() { - //XXX when do we query 'cid' ? - $tempID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); + $tempID = CRM_Utils_Request::retrieveValue('cid', 'Positive'); //check if this is a checksum authentication - $userChecksum = CRM_Utils_Request::retrieve('cs', 'String', $this); + $userChecksum = CRM_Utils_Request::retrieveValue('cs', 'String'); if ($userChecksum) { //check for anonymous user. $validUser = CRM_Contact_BAO_Contact_Utils::validChecksum($tempID, $userChecksum); @@ -132,12 +135,11 @@ class CRM_Event_Cart_Form_Cart extends CRM_Core_Form { } /** - * @param int $registeringContactID * @param array $fields * * @return int|mixed|null */ - public static function find_or_create_contact($registeringContactID = NULL, $fields = []) { + public static function find_or_create_contact($fields = []) { $contact_id = self::find_contact($fields); if ($contact_id) { diff --git a/CRM/Event/Cart/Form/Checkout/ParticipantsAndPrices.php b/CRM/Event/Cart/Form/Checkout/ParticipantsAndPrices.php index 72bcc8f921..408478e436 100644 --- a/CRM/Event/Cart/Form/Checkout/ParticipantsAndPrices.php +++ b/CRM/Event/Cart/Form/Checkout/ParticipantsAndPrices.php @@ -210,7 +210,7 @@ class CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices extends CRM_Event_Cart_ && $participant->contact_id == self::getContactID() ) { $participant->email = NULL; - $participant->contact_id = self::find_or_create_contact($this->getContactID()); + $participant->contact_id = self::find_or_create_contact(); } $defaults += $form->setDefaultValues(); //Set price defaults if any @@ -258,7 +258,7 @@ class CRM_Event_Cart_Form_Checkout_ParticipantsAndPrices extends CRM_Event_Cart_ $contact_id = $email_to_contact_id[$fields['email']]; } else { - $contact_id = self::find_or_create_contact($this->getContactID(), $fields); + $contact_id = self::find_or_create_contact($fields); $email_to_contact_id[$fields['email']] = $contact_id; } diff --git a/CRM/Event/Cart/Form/Checkout/Payment.php b/CRM/Event/Cart/Form/Checkout/Payment.php index cf67ad1be4..15f8499e93 100644 --- a/CRM/Event/Cart/Form/Checkout/Payment.php +++ b/CRM/Event/Cart/Form/Checkout/Payment.php @@ -25,22 +25,16 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { * @param CRM_Event_BAO_Event $event * * @return mixed + * @throws \CiviCRM_API3_Exception */ public function registerParticipant($params, &$participant, $event) { - $transaction = new CRM_Core_Transaction(); - - // handle register date CRM-4320 - $registerDate = date('YmdHis'); $participantParams = [ 'id' => $participant->id, 'event_id' => $event->id, - 'register_date' => $registerDate, + 'register_date' => date('YmdHis'), 'source' => CRM_Utils_Array::value('participant_source', $params, $this->description), - //'fee_level' => $participant->fee_level, 'is_pay_later' => $this->is_pay_later, 'fee_amount' => CRM_Utils_Array::value('amount', $params, 0), - //XXX why is this a ref to participant and not contact?: - //'registered_by_id' => $this->payer_contact_id, 'fee_currency' => CRM_Utils_Array::value('currencyID', $params), ]; @@ -53,30 +47,17 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { else { $participant_status = 'Registered'; } - $participant_statuses = CRM_Event_PseudoConstant::participantStatus(); - $participantParams['status_id'] = array_search($participant_status, $participant_statuses); - $participant_status_label = CRM_Utils_Array::value($participantParams['status_id'], CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label')); - $participantParams['participant_status'] = $participant_status_label; + $participantParams['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Event_BAO_Participant', 'status_id', $participant_status); + $participantParams['participant_status'] = CRM_Core_PseudoConstant::getLabel('CRM_Event_BAO_Participant', 'status_id', $participantParams['status_id']); $this->assign('isOnWaitlist', $participant->must_wait); + $participantParams['is_test'] = 0; if ($this->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') { $participantParams['is_test'] = 1; } - else { - $participantParams['is_test'] = 0; - } - if (self::is_administrator()) { - if (!empty($params['note'])) { - $note_params = [ - 'participant_id' => $participant->id, - 'contact_id' => self::getContactID(), - 'note' => $params['note'], - ]; - CRM_Event_BAO_Participant::update_note($note_params); - } - } + $transaction = new CRM_Core_Transaction(); $participant->copyValues($participantParams); $participant->save(); @@ -138,23 +119,26 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { $pay_later_text = ""; $this->pay_later_receipt = ""; foreach ($this->cart->get_main_events_in_carts() as $event_in_cart) { - if ($payment_processor_id == NULL && $event_in_cart->event->payment_processor != NULL) { + if ($payment_processor_id === NULL && $event_in_cart->event->payment_processor !== NULL) { $payment_processor_id = $event_in_cart->event->payment_processor; $this->financial_type_id = $event_in_cart->event->financial_type_id; } else { - if ($event_in_cart->event->payment_processor != NULL && $event_in_cart->event->payment_processor != $payment_processor_id) { + if ($event_in_cart->event->payment_processor !== NULL && $event_in_cart->event->payment_processor !== $payment_processor_id) { CRM_Core_Error::statusBounce(ts('When registering for multiple events all events must use the same payment processor. ')); } } - if (!$event_in_cart->event->is_pay_later) { + if ($payment_processor_id) { $can_pay_later = FALSE; } - else { + elseif ($event_in_cart->event->is_pay_later) { //XXX $pay_later_text = $event_in_cart->event->pay_later_text; $this->pay_later_receipt = $event_in_cart->event->pay_later_receipt; } + else { + CRM_Core_Error::statusBounce(ts('A payment processor must be selected for this event registration page, or the event must be configured to give users the option to pay later (contact the site administrator for assistance).')); + } } if ($payment_processor_id == NULL) { @@ -185,7 +169,6 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { * Build QuickForm. */ public function buildQuickForm() { - $this->line_items = []; $this->sub_total = 0; $this->_price_values = $this->getValuesForPage('ParticipantsAndPrices'); @@ -207,13 +190,11 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { $buttons = []; $buttons[] = [ 'name' => ts('Go Back'), - 'spacing' => '    ', 'type' => 'back', ]; $buttons[] = [ 'isDefault' => TRUE, 'name' => ts('Complete Transaction'), - 'spacing' => '                  ', 'type' => 'next', ]; @@ -266,9 +247,8 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { $price_set = $price_sets[$price_set_id]; $price_set_amount = []; CRM_Price_BAO_PriceSet::processAmount($price_set['fields'], $event_price_values, $price_set_amount); - $discountCode = $this->_price_values['discountcode']; - if (!empty($discountCode)) { - $ret = $this->apply_discount($discountCode, $price_set_amount, $cost, $event_in_cart->event_id); + if (!empty($this->_price_values['discountcode'])) { + $ret = $this->apply_discount($this->_price_values['discountcode'], $price_set_amount, $cost, $event_in_cart->event_id); if ($ret == FALSE) { $cost = $event_price_values['amount']; } @@ -407,39 +387,23 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { * * @param array $fields * @param array $files - * @param CRM_Core_Form $self + * @param CRM_Core_Form $form * * @return array|bool */ - public static function formRule($fields, $files, $self) { + public static function formRule($fields, $files, $form) { $errors = []; - if ($self->payment_required && empty($self->_submitValues['is_pay_later'])) { - CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors); + if ($form->payment_required && empty($form->_submitValues['is_pay_later'])) { + CRM_Core_Form::validateMandatoryFields($form->_fields, $fields, $errors); // validate payment instrument values (e.g. credit card number) - CRM_Core_Payment_Form::validatePaymentInstrument($self->_paymentProcessor['id'], $fields, $errors, NULL); + CRM_Core_Payment_Form::validatePaymentInstrument($form->_paymentProcessor['id'], $fields, $errors, NULL); } return empty($errors) ? TRUE : $errors; } - /** - * Validate form. - * - * @todo this should surely go! Test & remove. - * @return bool - */ - public function validate() { - if ($this->is_pay_later) { - $this->_fields['credit_card_number']['is_required'] = FALSE; - $this->_fields['cvv2']['is_required'] = FALSE; - $this->_fields['credit_card_exp_date']['is_required'] = FALSE; - $this->_fields['credit_card_type']['is_required'] = FALSE; - } - return parent::validate(); - } - /** * Pre-process form. */ @@ -454,12 +418,13 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { * Post process form. */ public function postProcess() { - - $transaction = new CRM_Core_Transaction(); $trxnDetails = NULL; $params = $this->_submitValues; $main_participants = $this->cart->get_main_event_participants(); + + $transaction = new CRM_Core_Transaction(); + foreach ($main_participants as $participant) { $defaults = []; $ids = ['contact_id' => $participant->contact_id]; @@ -470,7 +435,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { $trxn_prefix = 'VR'; if (array_key_exists('billing_contact_email', $params)) { - $this->payer_contact_id = self::find_or_create_contact($this->getContactID(), [ + $this->payer_contact_id = self::find_or_create_contact([ 'email' => $params['billing_contact_email'], 'first_name' => $params['billing_first_name'], 'last_name' => $params['billing_last_name'], @@ -700,10 +665,8 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { * @return array */ public function setDefaultValues() { - $defaults = parent::setDefaultValues(); - $config = CRM_Core_Config::singleton(); $default_country = new CRM_Core_DAO_Country(); $default_country->iso_code = CRM_Core_BAO_Country::defaultContactCountry(); $default_country->find(TRUE); @@ -755,17 +718,18 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { * @param int $event_id * * @return bool + * @throws \CiviCRM_API3_Exception */ protected function apply_discount($discountCode, &$price_set_amount, &$cost, $event_id) { - //need better way to determine if cividiscount installed - $autoDiscount = []; - $sql = "select is_active from civicrm_extension where name like 'CiviDiscount%'"; - $dao = CRM_Core_DAO::executeQuery($sql, ''); - while ($dao->fetch()) { - if ($dao->is_active != '1') { - return FALSE; - } + $extensions = civicrm_api3('Extension', 'get', [ + 'full_name' => 'org.civicrm.module.cividiscount', + ]); + if (empty($extensions['id']) || ($extensions['values'][$extensions['id']]['status'] !== 'installed')) { + return FALSE; } + + $autoDiscount = []; + $discounted_priceset_ids = _cividiscount_get_discounted_priceset_ids(); $discounts = _cividiscount_get_discounts(); @@ -801,7 +765,7 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { if ($discountValue['is_active'] == TRUE && ($discountValue['count_max'] == 0 || ($discountValue['count_max'] > $discountValue['count_use'])) && $active1 == TRUE && $active2 == TRUE && $event_match == TRUE) { foreach ($price_set_amount as $key => $price) { if (array_search($price['price_field_value_id'], $discounted_priceset_ids) != NULL) { - $discounted = _cividiscount_calc_discount($price['line_total'], $price['label'], $discountValue, $autoDiscount, "USD"); + $discounted = _cividiscount_calc_discount($price['line_total'], $price['label'], $discountValue, $autoDiscount, $this->getCurrency()); $price_set_amount[$key]['line_total'] = $discounted[0]; $cost += $discounted[0]; $price_set_amount[$key]['label'] = $discounted[1]; diff --git a/CRM/Event/Cart/Form/MerParticipant.php b/CRM/Event/Cart/Form/MerParticipant.php index ef45df9b54..6a4d5c07d8 100644 --- a/CRM/Event/Cart/Form/MerParticipant.php +++ b/CRM/Event/Cart/Form/MerParticipant.php @@ -2,8 +2,13 @@ /** * Class CRM_Event_Cart_Form_MerParticipant + * @fixme What is a MerParticipant! */ class CRM_Event_Cart_Form_MerParticipant extends CRM_Core_Form { + + /** + * @var \CRM_Event_BAO_Participant + */ public $participant = NULL; /** @@ -11,21 +16,17 @@ class CRM_Event_Cart_Form_MerParticipant extends CRM_Core_Form { */ public function __construct($participant) { parent::__construct(); - //XXX $this->participant = $participant; } /** - * @param CRM_Core_Form $form + * @param \CRM_Core_Form $form */ public function appendQuickForm(&$form) { $textarea_size = ['size' => 30, 'maxlength' => 60]; $form->add('text', $this->email_field_name(), ts('Email Address'), $textarea_size, TRUE); - list( - $custom_fields_pre, - $custom_fields_post - ) = $this->get_participant_custom_data_fields($this->participant->event_id); + list($custom_fields_pre, $custom_fields_post) = $this->get_participant_custom_data_fields(); foreach ($custom_fields_pre as $key => $field) { CRM_Core_BAO_UFGroup::buildProfile($form, $field, CRM_Profile_Form::MODE_CREATE, $this->participant->id); @@ -58,6 +59,7 @@ class CRM_Event_Cart_Form_MerParticipant extends CRM_Core_Form { /** * @return array + * @throws \CRM_Core_Exception */ public function get_participant_custom_data_fields() { list($custom_pre_id, $custom_post_id) = self::get_profile_groups($this->participant->event_id); @@ -108,8 +110,7 @@ class CRM_Event_Cart_Form_MerParticipant extends CRM_Core_Form { } /** - * XXX poor name. - * @param $participant + * @param \CRM_Event_BAO_Participant $participant * * @return CRM_Event_Cart_Form_MerParticipant */ @@ -119,12 +120,13 @@ class CRM_Event_Cart_Form_MerParticipant extends CRM_Core_Form { /** * @return array + * @throws \CRM_Core_Exception */ public function setDefaultValues() { $defaults = [ $this->html_field_name('email') => $this->participant->email, ]; - list($custom_fields_pre, $custom_fields_post) = $this->get_participant_custom_data_fields($this->participant->event_id); + list($custom_fields_pre, $custom_fields_post) = $this->get_participant_custom_data_fields(); $all_fields = $custom_fields_pre + $custom_fields_post; $flat = []; CRM_Core_BAO_UFGroup::setProfileDefaults($this->participant->contact_id, $all_fields, $flat); diff --git a/CRM/Event/Cart/Page/AddToCart.php b/CRM/Event/Cart/Page/AddToCart.php index e9d9b60c36..6f82018486 100644 --- a/CRM/Event/Cart/Page/AddToCart.php +++ b/CRM/Event/Cart/Page/AddToCart.php @@ -5,31 +5,34 @@ */ class CRM_Event_Cart_Page_AddToCart extends CRM_Core_Page { + use CRM_Core_Page_EntityPageTrait; + /** * This function takes care of all the things common to all pages. * * This typically involves assigning the appropriate smarty variables :) */ public function run() { - $transaction = new CRM_Core_Transaction(); - - $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); + $this->_id = CRM_Utils_Request::retrieveValue('id', 'Positive', NULL); + if (empty($this->_id)) { + CRM_Core_Error::statusBounce(ts('Missing required parameters'), NULL, ts('Add to cart')); + } if (!CRM_Core_Permission::event(CRM_Core_Permission::VIEW, $this->_id, 'register for events')) { CRM_Core_Error::statusBounce(ts('You do not have permission to register for this event')); } + $transaction = new CRM_Core_Transaction(); $cart = CRM_Event_Cart_BAO_Cart::find_or_create_for_current_session(); $event_in_cart = $cart->add_event($this->_id); + $transaction->commit(); - $url = CRM_Utils_System::url('civicrm/event/view_cart'); - CRM_Utils_System::setUFMessage(ts("%1 has been added to your cart. View your cart.", [ + $cartUrl = CRM_Utils_System::url('civicrm/event/view_cart'); + CRM_Utils_System::setUFMessage(ts("%1 has been added to your cart.", [ 1 => $event_in_cart->event->title, - 2 => $url, + 2 => $cartUrl, ])); - $transaction->commit(); - - return CRM_Utils_System::redirect($_SERVER['HTTP_REFERER']); + CRM_Utils_System::redirect($_SERVER['HTTP_REFERER']); } } diff --git a/templates/CRM/Event/Cart/Form/Checkout/Payment.tpl b/templates/CRM/Event/Cart/Form/Checkout/Payment.tpl index 1c9555bd46..0f3168ab85 100644 --- a/templates/CRM/Event/Cart/Form/Checkout/Payment.tpl +++ b/templates/CRM/Event/Cart/Form/Checkout/Payment.tpl @@ -115,45 +115,6 @@ {/if} -{if $administrator} - -{/if} -