Skip to content

Commit

Permalink
entityRef fields for events and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
agh1 committed Aug 7, 2015
1 parent f800a1e commit 77eb558
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 113 deletions.
27 changes: 27 additions & 0 deletions js/pricefieldOthersignup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
cj(function ($) {
function showHide(obj) {
if ($(obj).val() == 'Membership') {
$(obj).siblings('.membershipselect').show();
$(obj).siblings('.eventselect').hide();
}
else if ($(obj).val() == 'Participant') {
$(obj).siblings('.membershipselect').hide();
$(obj).siblings('.eventselect').show();
}
else {
$(obj).siblings('.membershipselect').hide();
$(obj).siblings('.eventselect').hide();
}
}
$('#optionField tr:eq(0) th:nth-last-child(2)').after('<th id="othersignup-header">'+ts('Additional Signup?', {'domain': 'com.aghstrategies.eventmembershipsignup'})+'</th>');
$('#optionField tr:gt(0)').each( function(index) {
$(this).children('td:nth-last-child(2)').after($('#othersignup-group-'+(index+1)));
});
$('.othersignup-group select[name^="othersignup["]').each( function() {
showHide($(this));
$(this).change( function() {
showHide($(this));
});
});
$('.deleteme').remove();
});
22 changes: 22 additions & 0 deletions js/priceoptionOthersignup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cj(function($){
function showHide(obj) {
if ($(obj).val() == 'Membership') {
$('.crm-price-option-membershiptype').show();
$('.crm-price-option-event').hide();
}
else if ($(obj).val() == 'Participant') {
$('.crm-price-option-membershiptype').hide();
$('.crm-price-option-event').show();
}
else {
$('.crm-price-option-membershiptype').hide();
$('.crm-price-option-event').hide();
}
}
$('#other-sign-up>*').insertAfter('.crm-price-option-form-block-is_default');
showHide($('#othersignup'));
$('#othersignup').change(function(){
showHide($('#othersignup'));
});
$('.deleteme').remove();
});
46 changes: 38 additions & 8 deletions otherSignupAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function eventmembershipsignup_entity_options() {
* Handle options created upon field creation.
*/
function eventmembershipsignup_field_admin_form(&$form) {
$select2version = version_compare(CRM_Utils_System::version(), '4.5.0', '>=');
$membershipSelector = array();
$eventSelector = array();
eventmembershipsignup_populate_selects($membershipSelector, $eventSelector);
Expand All @@ -52,7 +53,16 @@ function eventmembershipsignup_field_admin_form(&$form) {
// Add the field element in the form.
$form->add('select', "othersignup[$i]", ts('Additional Signup?', array('domain' => 'com.aghstrategies.eventmembershipsignup')), $entityOptions);
$form->add('select', "membershipselect[$i]", ts('Select Membership Type', array('domain' => 'com.aghstrategies.eventmembershipsignup')), $membershipSelector);
$form->add('select', "eventselect[$i]", ts('Select Event', array('domain' => 'com.aghstrategies.eventmembershipsignup')), $eventSelector);
if ($select2version) {
$form->addEntityRef("eventselect[$i]", ts('Select Event', array('domain' => 'com.aghstrategies.eventmembershipsignup')), array(
'entity' => 'event',
'placeholder' => '- ' . ts('Select Event', array('domain' => 'com.aghstrategies.eventmembershipsignup')) . ' -',
'select' => array('minimumInputLength' => 0),
));
}
else {
$form->add('select', "eventselect[$i]", ts('Select Event', array('domain' => 'com.aghstrategies.eventmembershipsignup')), $eventSelector);
}
$selectors[] = $i;
}
$form->assign('numOptions', $numOptions);
Expand Down Expand Up @@ -116,17 +126,25 @@ function eventmembershipsignup_field_admin_postProcess(&$form) {
* Handle single option form.
*/
function eventmembershipsignup_option_admin_form(&$form) {
$select2version = version_compare(CRM_Utils_System::version(), '4.5.0', '>=');

$id = $form->getVar('_oid');
$form->assign('option_signup_id', 0);
$form->assign('signupselectvalue', 0);
$form->assign('eventmembershipvalue', 0);
$defaults = array();
if (!is_null($id)) {
$sql = "SELECT id, entity_table, entity_ref_id FROM civicrm_option_signup WHERE price_option_id = {$id};";
$dao = CRM_Core_DAO::executeQuery($sql);
if ($dao->fetch()) {
$form->assign('option_signup_id', $dao->id);
$form->assign('signupselectvalue', $dao->entity_table);
$form->assign('eventmembershipvalue', $dao->entity_ref_id);
if ($dao->entity_table == 'Event') {
$defaults['othersignup'] = 'Participant';
$defaults['eventselect'] = $dao->entity_ref_id;
}
elseif ($dao->entity_table == 'MembershipType') {
$defaults['othersignup'] = 'Membership';
$defaults['membershipselect'] = $dao->entity_ref_id;
}
}
}

Expand All @@ -136,9 +154,21 @@ function eventmembershipsignup_option_admin_form(&$form) {
$entityOptions = eventmembershipsignup_entity_options();

// Add the field element in the form.
$form->add('select', 'othersignup', ts('Other Sign Up?'), $entityOptions);
$form->add('select', 'membershipselect', ts('Select Membership Type'), $membershipSelector);
$form->add('select', 'eventselect', ts('Select Event'), $eventSelector);
$form->add('select', 'othersignup', ts('Other Sign Up?', array('domain' => 'com.aghstrategies.eventmembershipsignup')), $entityOptions);
$form->add('select', 'membershipselect', ts('Select Membership Type', array('domain' => 'com.aghstrategies.eventmembershipsignup')), $membershipSelector);
if ($select2version) {
$form->addEntityRef('eventselect', ts('Select Event', array('domain' => 'com.aghstrategies.eventmembershipsignup')), array(
'entity' => 'event',
'placeholder' => '- ' . ts('Select Event', array('domain' => 'com.aghstrategies.eventmembershipsignup')) . ' -',
'select' => array('minimumInputLength' => 0),
));
}
else {
$form->add('select', 'eventselect', ts('Select Event', array('domain' => 'com.aghstrategies.eventmembershipsignup')), $eventSelector);
}

$form->setDefaults($defaults);

// Assumes templates are in a templates folder relative to this file.
$templatePath = realpath(dirname(__FILE__) . "/templates");
// Dynamically insert a template block in the page.
Expand Down Expand Up @@ -196,7 +226,7 @@ function eventmembershipsignup_populate_selects(&$membershipSelector, &$eventSel
'is_active' => 1,
));
foreach ($result['values'] as $event) {
$formattedDate = CRM_Utils_Date::format($event['start_date']);
$formattedDate = CRM_Utils_Date::customFormat($event['start_date']);
$eventSelector[$event['id']] = "{$event['title']} ($formattedDate)";
}
}
63 changes: 8 additions & 55 deletions templates/pricefieldOthersignup.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,14 @@
{* Licensed under the GNU Affero Public License 3.0 (see LICENSE.txt) *}

{crmScope extensionKey='com.aghstrategies.eventmembershipsignup'}
{* template block that contains the new field *}
<table id="other-sign-up">
<tr class="othersignup-field-tr">
<th id="othersignup-header">{ts}Additional Signup?{/ts}</th>

{crmScript ext=com.aghstrategies.eventmembershipsignup file=js/pricefieldOthersignup.js}
<table class="deleteme"><tr>
{foreach from=$selectors item=i}
<td>{$form.othersignup[$i].html}</td>
</tr>
<tr>
<td id="membership_row[{$i}]">{$form.membershipselect[$i].html}</td>
</tr>
<tr>
<td id="event_row[{$i}]">{$form.eventselect[$i].html}</td>
</tr>
{* reposition the above block after #someOtherBlock *}
<script type="text/javascript">
{literal}
cj(function ($) {
{/literal}
var i = {$i};
{literal}
var weightHeaderSelector = $('#optionField').children('tbody').children('tr').filter(':first').children('th').filter(':nth-child(8n)');
$('#othersignup-header').insertAfter(weightHeaderSelector);
$('#eventselect_'+i).insertAfter($('#option_weight_'+i).parent());
$('#membershipselect_'+i).insertAfter($('#option_weight_'+i).parent());
$('#othersignup_'+i).insertAfter($('#option_weight_'+i).parent());
$('#othersignup_'+i).change(function(){
if ($(this).val()=='Membership'){
$('#membershipselect_'+i).show();
$('#eventselect_'+i).hide();
}
else if ($(this).val()=='Participant'){
$('#membershipselect_'+i).hide();
$('#eventselect_'+i).show();
}
else if($(this).val()==0){
$('#membershipselect_'+i).hide();
$('#eventselect_'+i).hide();
}
});
if ($('#othersignup_'+i).val()=='Membership'){
$('#membershipselect_'+i).show();
$('#eventselect_'+i).hide();
}
else if ($('#othersignup_'+i).val()=='Participant'){
$('#membershipselect_'+i).hide();
$('#eventselect_'+i).show();
}
else if($('#othersignup_'+i).val()==0){
$('#membershipselect_'+i).hide();
$('#eventselect_'+i).hide();
}
});
{/literal}
</script>
<td id="othersignup-group-{$i}" class='othersignup-group'>
{$form.othersignup[$i].html}
<span class="membershipselect">{$form.membershipselect[$i].html}</span>
<span class="eventselect">{$form.eventselect[$i].html}</span>
</td>
{/foreach}
</table>
</tr></table>
{/crmScope}
53 changes: 3 additions & 50 deletions templates/priceoptionOthersignup.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
{* Licensed under the GNU Affero Public License 3.0 (see LICENSE.txt) *}

{crmScope extensionKey='com.aghstrategies.eventmembershipsignup'}
<table id="other-sign-up-table">
{crmScript ext=com.aghstrategies.eventmembershipsignup file=js/priceoptionOthersignup.js}

<table class="deleteme">
<tbody id="other-sign-up">
<tr id="other-sign-up-header"><td colspan=2><h3>{ts}Sign Up for Other Membership or Event{/ts}</h3></td></tr>
<tr class="crm-price-option-other-sign-up">
Expand All @@ -19,53 +21,4 @@
</tr>
</tbody>
</table>
<script type="text/javascript">
{literal}
cj(function($){
{/literal}
var optionSignUp = {$option_signup_id};
var signupselectvalue = "{$signupselectvalue}";
var eventmembershipvalue = {$eventmembershipvalue};
{literal}
var signupKeys = {MembershipType:'Membership', Event:'Participant'};
if (optionSignUp){
$('select#othersignup').val(signupKeys[signupselectvalue]);
if (signupKeys[signupselectvalue]=='Membership'){
$('#membershipselect').val(eventmembershipvalue);
}
else if (signupKeys[signupselectvalue]=='Participant'){
$('#eventselect').val(eventmembershipvalue);
}
}
$('#other-sign-up>*').insertAfter('.crm-price-option-form-block-is_default');
$('#othersignup').change(function(){
if ($('#othersignup').val()=='Membership'){
$('.crm-price-option-membershiptype').show();
$('.crm-price-option-event').hide();
}
else if ($('#othersignup').val()=='Participant'){
$('.crm-price-option-membershiptype').hide();
$('.crm-price-option-event').show();
}
else if($('#othersignup').val()==0){
$('.crm-price-option-membershiptype').hide();
$('.crm-price-option-event').hide();
}
});
if ($('#othersignup').val()=='Membership'){
$('.crm-price-option-membershiptype').show();
$('.crm-price-option-event').hide();
}
else if ($('#othersignup').val()=='Participant'){
$('.crm-price-option-membershiptype').hide();
$('.crm-price-option-event').show();
}
else if($('#othersignup').val()==0){
$('.crm-price-option-membershiptype').hide();
$('.crm-price-option-event').hide();
}
});
</script>
{/literal}
{/crmScope}

0 comments on commit 77eb558

Please sign in to comment.