From f76b27feb9704f550f1de7a4d90ff3e66c3b6d7c Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 22 Oct 2014 22:11:27 -0400 Subject: [PATCH] CRM-15474 - Move js-based form alterations to php --- CRM/Core/Form.php | 19 +++++++++++-------- CRM/Event/BAO/Participant.php | 24 ++++++++++++++++++++++++ CRM/Event/Form/Participant.php | 20 ++++++++++---------- templates/CRM/Event/Form/Participant.tpl | 11 ----------- 4 files changed, 45 insertions(+), 29 deletions(-) diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index f328e17fe7..fee751a873 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -1033,12 +1033,14 @@ class CRM_Core_Form extends HTML_QuickForm_Page { if (!isset($props['field'])) { $props['field'] = strrpos($name, '[') ? rtrim(substr($name, 1 + strrpos($name, '[')), ']') : $name; } - $info = civicrm_api3($props['entity'], 'getoptions', array( - 'field' => $props['field'], - 'options' => array('metadata' => array('fields')) - ) - ); - $options = $info['values']; + // Fetch options from the api unless passed explicitly + if (isset($props['options'])) { + $options = $props['options']; + } + else { + $info = civicrm_api3($props['entity'], 'getoptions', array('field' => $props['field'])); + $options = $info['values']; + } if (!array_key_exists('placeholder', $props)) { $props['placeholder'] = $required ? ts('- select -') : ts('- none -'); } @@ -1051,7 +1053,8 @@ class CRM_Core_Form extends HTML_QuickForm_Page { } // Core field else { - foreach($info['metadata']['fields'] as $uniqueName => $fieldSpec) { + $info = civicrm_api3($props['entity'], 'getfields'); + foreach($info['values'] as $uniqueName => $fieldSpec) { if ( $uniqueName === $props['field'] || CRM_Utils_Array::value('name', $fieldSpec) === $props['field'] || @@ -1066,7 +1069,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page { $props['class'] = (isset($props['class']) ? $props['class'] . ' ' : '') . "crm-select2"; $props['data-api-entity'] = $props['entity']; $props['data-api-field'] = $props['field']; - CRM_Utils_Array::remove($props, 'label', 'entity', 'field', 'option_url'); + CRM_Utils_Array::remove($props, 'label', 'entity', 'field', 'option_url', 'options'); return $this->add('select', $name, $label, $options, $required, $props); } diff --git a/CRM/Event/BAO/Participant.php b/CRM/Event/BAO/Participant.php index c94bd1aabc..6ed0976525 100644 --- a/CRM/Event/BAO/Participant.php +++ b/CRM/Event/BAO/Participant.php @@ -2084,4 +2084,28 @@ WHERE (li.entity_table = 'civicrm_participant' AND li.entity_id = {$participantI } CRM_Activity_BAO_Activity::create($activityParams); } + + /** + * Get options for a given field. + * @see CRM_Core_DAO::buildOptions + * + * @param String $fieldName + * @param String $context : @see CRM_Core_DAO::buildOptionsContext + * @param Array $props : whatever is known about this dao object + * + * @return Array|bool + */ + public static function buildOptions($fieldName, $context = NULL, $props = array()) { + $params = array('condition' => array()); + + if ($fieldName == 'status_id' && $context != 'validate') { + // Get rid of cart-related option if disabled + // FIXME: Why does this option even exist if cart is disabled? + if (!CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::EVENT_PREFERENCES_NAME, 'enable_cart')) { + $params['condition'][] = "name <> 'Pending in cart'"; + } + } + + return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context); + } } diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index 69ba3d9655..e89073f182 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -782,17 +782,17 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task { $notificationStatusIds = implode(',', array_keys(array_intersect($participantStatusName, $notificationStatuses))); $this->assign('notificationStatusIds', $notificationStatusIds); - $this->_participantStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'); - $this->addSelect('status_id', $checkCancelledJs + array('option_url' => 'civicrm/admin/participant_status'), TRUE); + $this->_participantStatuses = $statusOptions = CRM_Event_BAO_Participant::buildOptions('status_id', 'create'); - $enableCart = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::EVENT_PREFERENCES_NAME, - 'enable_cart' - ); - $pendingInCartStatusId = array_search('Pending in cart', $participantStatusName); - $this->assign('pendingInCartStatusId', $pendingInCartStatusId); - $this->assign('enableCart', $enableCart); - $pendingRefundStatusId = array_search('Pending refund', $participantStatusName); - $this->assign('pendingRefundStatusId', $pendingRefundStatusId); + // Only show refund status when editing + if ($this->_action & CRM_Core_Action::ADD) { + $pendingRefundStatusId = array_search('Pending refund', $participantStatusName); + if ($pendingRefundStatusId) { + unset($statusOptions[$pendingRefundStatusId]); + } + } + + $this->addSelect('status_id', $checkCancelledJs + array('options' => $statusOptions, 'option_url' => 'civicrm/admin/participant_status'), TRUE); $this->addElement('checkbox', 'is_notify', ts('Send Notification'), NULL); diff --git a/templates/CRM/Event/Form/Participant.tpl b/templates/CRM/Event/Form/Participant.tpl index 109767ebc6..3c5822baf1 100644 --- a/templates/CRM/Event/Form/Participant.tpl +++ b/templates/CRM/Event/Form/Participant.tpl @@ -340,17 +340,6 @@ var $form = $('form.{/literal}{$form.formClass}{literal}'); - // don't show cart related statuses if it's disabled - {/literal}{if !$enableCart}{literal} - var pendingInCartStatusId = {/literal}{$pendingInCartStatusId}{literal}; - $("#status_id option[value='" + pendingInCartStatusId + "']").remove(); - {/literal}{/if}{literal} - - {/literal}{if $action eq 1}{literal} - var pendingRefundStatusId = {/literal}{$pendingRefundStatusId}{literal}; - $("#status_id option[value='" + pendingRefundStatusId + "']").remove(); - {/literal}{/if}{literal} - // Handle event selection $('#event_id', $form).change(function() { var eventId = $(this).val(); -- 2.25.1