From cd120ea0e65be2c9f8116fd5ec67d6ee5c959868 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 26 Apr 2014 22:11:39 -0700 Subject: [PATCH] Change participant role to select2 --- CRM/Event/Form/Participant.php | 89 ++-------------- templates/CRM/Event/Form/Participant.tpl | 128 ++--------------------- xml/schema/Event/Participant.xml | 2 +- 3 files changed, 19 insertions(+), 200 deletions(-) diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index e1709c4b9e..75f6cfaec3 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -192,11 +192,6 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task { */ public $_paymentId = NULL; - /** - * array of participant role custom data - */ - public $_participantRoleIds = array(); - /** * Function to set variables up before form is built * @@ -459,52 +454,6 @@ class CRM_Event_Form_Participant extends CRM_Contact_Form_Task { ); } $this->set('onlinePendingContributionId', $this->_onlinePendingContributionId); - $roleIds = CRM_Event_PseudoConstant::participantRole(); - if (!empty($roleIds)) { - $query = " -SELECT civicrm_custom_group.name as name, - civicrm_custom_group.id as id, - extends_entity_column_value as value - FROM civicrm_custom_group - WHERE ( extends_entity_column_value REGEXP '[[:<:]]" . implode('[[:>:]]|[[:<:]]', array_keys($roleIds)) . "[[:>:]]' - OR extends_entity_column_value IS NULL ) - AND extends_entity_column_id = '{$this->_roleCustomDataTypeID}' - AND extends = 'Participant' - AND is_active = 1"; - - $dao = CRM_Core_DAO::executeQuery($query); - while ($dao->fetch()) { - if ($dao->value) { - $getRole = explode(CRM_Core_DAO::VALUE_SEPARATOR, $dao->value); - foreach ($getRole as $r) { - if (!$r) { - continue; - } - if (isset($this->_participantRoleIds[$r])) { - $this->_participantRoleIds[$r] .= ',' . $dao->name; - } - else { - $this->_participantRoleIds[$r] = $dao->name; - } - } - } - else { - if (isset($this->_participantRoleIds[0])) { - $this->_participantRoleIds[0] .= ',' . $dao->name; - } - else { - $this->_participantRoleIds[0] = $dao->name; - } - } - } - $dao->free(); - } - foreach ($roleIds as $k => $v) { - if (!isset($this->_participantRoleIds[$k])) { - $this->_participantRoleIds[$k] = ''; - } - } - $this->assign('participantRoleIds', $this->_participantRoleIds); } /** @@ -666,11 +615,7 @@ SELECT civicrm_custom_group.name as name, $roleIDs = explode(',', $urlRoleIDS); } if (isset($roleIDs)) { - foreach ($roleIDs as $roleID) { - $defaults[$this->_id]["role_id[{$roleID}]"] = 1; - } - unset($defaults[$this->_id]['role_id']); - $this->assign('roleID', $roleIDs); + $defaults[$this->_id]['role_id'] = implode(',', $roleIDs); } if (isset($eventID)) { @@ -822,14 +767,7 @@ SELECT civicrm_custom_group.name as name, $this->assign('entityID', $this->_id); } - $roleids = CRM_Event_PseudoConstant::participantRole(); - - foreach ($roleids as $rolekey => $rolevalue) { - $roleTypes[] = $this->createElement('checkbox', $rolekey, NULL, $rolevalue); - } - - $this->addGroup($roleTypes, 'role_id', ts('Participant Role')); - $this->addRule('role_id', ts('Role is required'), 'required'); + $this->addSelect('role_id', array('multiple' => TRUE, 'class' => 'huge'), TRUE); // CRM-4395 $checkCancelledJs = array('onchange' => "return sendNotification( );"); @@ -1154,7 +1092,7 @@ SELECT civicrm_custom_group.name as name, //modify params according to parameter used in create //participant method (addParticipant) $this->_params['participant_status_id'] = $params['status_id']; - $this->_params['participant_role_id'] = $params['role_id']; + $this->_params['participant_role_id'] = explode(',', $params['role_id']); $this->_params['participant_register_date'] = $params['register_date']; $eventTitle = @@ -1213,9 +1151,8 @@ SELECT civicrm_custom_group.name as name, $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, $this->_contactId, NULL, NULL, $ctype); } - $roleAllIds = CRM_Utils_Array::value('role_id', $params); - if ($roleAllIds) { - foreach ($roleAllIds as $rkey => $rvalue) { + if ($this->_params['participant_role_id']) { + foreach ($this->_params['participant_role_id'] as $rkey) { $customFieldsRole = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, $rkey, $this->_roleCustomDataTypeID); $customFieldsEvent = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, @@ -1312,7 +1249,7 @@ SELECT civicrm_custom_group.name as name, $participants = array(); if (!empty($this->_params['participant_role_id']) && is_array($this->_params['participant_role_id'])) { $this->_params['participant_role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, - array_keys($this->_params['participant_role_id']) + $this->_params['participant_role_id'] ); } $participants[] = CRM_Event_Form_Registration::addParticipant($this->_params, $contactID); @@ -1338,11 +1275,7 @@ SELECT civicrm_custom_group.name as name, $participants = array(); if ($this->_single) { if ($params['role_id']) { - foreach ($params['role_id'] as $k => $v) { - $rolesIDS[] = $k; - } - $seperator = CRM_Core_DAO::VALUE_SEPARATOR; - $params['role_id'] = implode($seperator, $rolesIDS); + $params['role_id'] = str_replace(',', CRM_Core_DAO::VALUE_SEPARATOR, $params['role_id']); } else { $params['role_id'] = 'NULL'; @@ -1354,13 +1287,7 @@ SELECT civicrm_custom_group.name as name, $commonParams = $params; $commonParams['contact_id'] = $contactID; if ($commonParams['role_id']) { - $rolesIDS = array(); - foreach ($commonParams['role_id'] as $k => $v) { - $rolesIDS[] = $k; - } - $seperator = CRM_Core_DAO::VALUE_SEPARATOR; - $commonParams['role_id'] = implode($seperator, $rolesIDS); - $commonParams['participant_role_id'] = implode($seperator, $rolesIDS); + $commonParams['participant_role_id'] = $commonParams['role_id'] = str_replace(',', CRM_Core_DAO::VALUE_SEPARATOR, $params['role_id']); } else { $commonParams['role_id'] = 'NULL'; diff --git a/templates/CRM/Event/Form/Participant.tpl b/templates/CRM/Event/Form/Participant.tpl index 4b76db50d8..fae24aa2f2 100644 --- a/templates/CRM/Event/Form/Participant.tpl +++ b/templates/CRM/Event/Form/Participant.tpl @@ -365,12 +365,7 @@ var info = $(this).select2('data').extra; // Set role from default - $('[name^=role_id]:checkbox', $form).each(function() { - var check = $(this).is('[name="role_id[' + info.default_role_id + ']"]'); - if (check !== $(this).prop('checked')) { - $(this).prop('checked', check).change(); - } - }); + $('select[name^=role_id]', $form).select2('val', [info.default_role_id], true); // Set campaign default $('#campaign_id', $form).select2('val', info.campaign_id); @@ -383,11 +378,10 @@ }); // Handle participant role selection - $('[name^=role_id]:checkbox', $form).change(function() { - var roleId = $(this).attr('name').replace(/role_id\[|\]/g, ''); - showCustomData('Participant', roleId, {/literal}{$roleCustomDataTypeID}{literal}); - }); - $('[name^=role_id]:checked', $form).change(); + $('select[name^=role_id]', $form).change(buildRoleCustomData); + if ($('select[name^=role_id]', $form).val()) { + buildRoleCustomData(); + } buildFeeBlock(); @@ -396,6 +390,11 @@ buildFeeBlock($('#discount_id', $form).val()); } + function buildRoleCustomData() { + var roleId = $('select[name^=role_id]', $form).select2('val').join(); + CRM.buildCustomData('Participant', roleId, {/literal}{$roleCustomDataTypeID}{literal}); + } + //build fee block function buildFeeBlock(discountId) { var dataUrl = {/literal}"{crmURL p=$urlPath h=0 q="snippet=4&qfKey=$qfKey"}"; @@ -434,8 +433,6 @@ $.ajax({ url: dataUrl, - async: false, - global: false, success: function ( html ) { $("#feeBlock").html( html ).trigger('crmLoad'); } @@ -458,111 +455,6 @@ } } - var roleGroupMapper = {/literal}{$participantRoleIds|@json_encode}{literal}; - function showCustomData( type, subType, subName ) { - var dataUrl = {/literal}"{crmURL p=$urlPath h=0 q='snippet=4&type='}"{literal} + type; - var roleid = "role_id_"+subType; - var loadData = false; - - if ( document.getElementById( roleid ).checked == true ) { - if ( typeof roleGroupMapper !== 'undefined' && roleGroupMapper[subType] ) { - var splitGroup = roleGroupMapper[subType].split(","); - for ( i = 0; i < splitGroup.length; i++ ) { - var roleCustomGroupId = splitGroup[i]; - if ( $( '#'+roleCustomGroupId ).length > 0 ) { - $( '#'+roleCustomGroupId ).remove( ); - } - } - loadData = true; - } - } - else { - var groupUnload = []; - var x = 0; - - if ( roleGroupMapper[0] ) { - var splitGroup = roleGroupMapper[0].split(","); - for ( x = 0; x < splitGroup.length; x++ ) { - groupUnload[x] = splitGroup[x]; - } - } - - for ( var i in roleGroupMapper ) { - if ( ( i > 0 ) && ( document.getElementById( "role_id_"+i ).checked ) ) { - var splitGroup = roleGroupMapper[i].split(","); - for ( j = 0; j < splitGroup.length; j++ ) { - groupUnload[x+j+1] = splitGroup[j]; - } - } - } - - if ( roleGroupMapper[subType] ) { - var splitGroup = roleGroupMapper[subType].split(","); - for ( i = 0; i < splitGroup.length; i++ ) { - var roleCustomGroupId = splitGroup[i]; - if ( $( '#'+roleCustomGroupId ).length > 0 ) { - if ( $.inArray( roleCustomGroupId, groupUnload ) == -1 ) { - $( '#'+roleCustomGroupId ).remove( ); - } - } - } - } - } - - if ( !( loadData ) ) { - return false; - } - - if ( subType ) { - dataUrl += '&subType=' + subType; - } - - if ( subName ) { - dataUrl += '&subName=' + subName; - $( '#customData' + subName ).show( ); - } - else { - $( '#customData' ).show( ); - } - - {/literal} - {if $urlPathVar} - dataUrl += '&{$urlPathVar}'; - {/if} - {if $groupID} - dataUrl += '&groupID=' + '{$groupID}'; - {/if} - {if $qfKey} - dataUrl += '&qfKey=' + '{$qfKey}'; - {/if} - {if $entityID} - dataUrl += '&entityID=' + '{$entityID}'; - {/if} - - {literal} - - if ( subName && subName != 'null' ) { - var fname = '#customData' + subName; - } - else { - var fname = '#customData'; - } - - var response = $.ajax({url: dataUrl, - async: false - }).responseText; - - if ( subType != 'null' ) { - if ( document.getElementById(roleid).checked == true ) { - var response_text = '
'+response+'
'; - $( fname ).append(response_text).trigger('crmLoad'); - } - else { - $('#'+subType+'_chk').remove(); - } - } - } - {/literal} CRM.buildCustomData( '{$customDataType}', 'null', 'null' ); {if $eventID} diff --git a/xml/schema/Event/Participant.xml b/xml/schema/Event/Participant.xml index 6677e5fb58..e76bca9db9 100644 --- a/xml/schema/Event/Participant.xml +++ b/xml/schema/Event/Participant.xml @@ -89,7 +89,7 @@ role_id participant_role_id - Participant Role Id + Participant Role(s) participant_role -- 2.25.1