Skip to content

Commit

Permalink
Update add fields to support multiple uses
Browse files Browse the repository at this point in the history
  • Loading branch information
joedolson committed Dec 27, 2023
1 parent a665ab8 commit 2f2e082
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
34 changes: 18 additions & 16 deletions src/js/pricing-table.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
jQuery(document).ready(function ($) {

$('#add_price').on('click', function () {
var num = $('.clonedPrice').length; // how many "duplicatable" input fields we currently have
$('.add-price').on('click', function () {
var context = $( this ).attr( 'data-context' );
var num = $('.clonedPrice.' + context).length; // how many "duplicatable" input fields we currently have
var newNum = new Number(num + 1); // the numeric ID of the new input field being added
// create the new element via clone(), and manipulate it's ID using newNum value
var newElem = $('#price' + num).clone().attr('id', 'price' + newNum);
var newElem = $('#price' + context + num).clone().attr('id', 'price' + context + newNum);
// manipulate the name/id values of the input inside the new element
// insert the new element after the last "duplicatable" input field
$('#price' + num).after(newElem);
$('#price' + context + num).after(newElem);
// enable the "remove" button
$('#del_price').removeAttr('disabled');
$('.del-price.' + context).removeAttr('disabled');
// business rule: you can only add 20 variations
if (newNum == 20)
$('#add_price').attr('disabled', 'disabled');
if (newNum == 20) {
$( this ).attr('disabled', 'disabled');
}
});

$('#del_price').on('click', function () {
var num = $('.clonedPrice').length; // how many "duplicatable" input fields we currently have
$('#price' + num).remove(); // remove the last element
$('.del-price').on('click', function () {
var context = $( this ).attr( 'data-context' );
var num = $('.clonedPrice.' + context).length; // how many "duplicatable" input fields we currently have
$('#price' + context + num).remove(); // remove the last element
// enable the "add" button
$('#add_price').removeAttr('disabled');
$( this ).removeAttr('disabled');
// if only one element remains, disable the "remove" button
if (num - 1 == 1)
$('#del_price').attr('disabled', 'disabled');
$('#event_span').attr('disabled', 'disabled');
if (num - 1 == 1) {
$( this ).attr('disabled', 'disabled');
}
});
$('#del_price').attr('disabled', 'disabled');
$('.del-price').attr('disabled', 'disabled');

$("button.up,button.down").on( 'click', function(e){
e.preventDefault();
Expand Down
28 changes: 14 additions & 14 deletions src/mt-processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,19 +407,19 @@ function mt_prices_table( $registration = array() ) {
$total = '<input type="hidden" name="mt_tickets_total" value="inherit" />';
} else {
$disabled = ( 'general' === $counting ) ? ' disabled="disabled"' : '';
$notice = ( 'general' === $counting ) ? ' <em id="ticket-counting-status">' . __( 'Ticket counting is disabled for general admission events.', 'my-tickets' ) . '</em>' : '';
$notice = ( 'general' === $counting ) ? ' <em id="ticket-counting-status_' . $counting . '">' . __( 'Ticket counting is disabled for general admission events.', 'my-tickets' ) . '</em>' : '';
$value = ( isset( $registration['total'] ) && 'inherit' !== $registration['total'] ) ? $registration['total'] : $tickets;
$available_empty = "<input type='hidden' name='mt_tickets[]' id='mt_tickets' value='inherit' />";
$total = "<p class='mt-available-tickets'><label for='mt_tickets_total'>" . __( 'Total Tickets Available', 'my-tickets' ) . ':</label> <input ' . $disabled . ' type="text" name="mt_tickets_total" id="mt_tickets_total" aria-describedby="ticket-counting-status" value="' . esc_attr( $value ) . '" />' . $notice . '</p>';
$available_empty = "<input type='hidden' name='mt_tickets[]' id='mt_tickets_' . $counting . '' value='inherit' />";
$total = "<p class='mt-available-tickets'><label for='mt_tickets_total_' . $counting . ''>" . __( 'Total Tickets Available', 'my-tickets' ) . ':</label> <input ' . $disabled . ' type="text" name="mt_tickets_total" id="mt_tickets_total_' . $counting . '" aria-describedby="ticket-counting-status" value="' . esc_attr( $value ) . '" />' . $notice . '</p>';
}
$labels_index = array();
$pricing = ( isset( $registration['prices'] ) ) ? $registration['prices'] : $registration['pricing']; // array of prices; label => cost/available/sold.
if ( is_array( $pricing ) ) {
foreach ( $pricing as $label => $options ) {
if ( 'discrete' === $counting || 'event' === $counting ) {
$available = "<input type='text' name='mt_tickets[]' id='mt_tickets_$label' value='" . esc_attr( $options['tickets'] ) . "' size='8' />";
$available = "<input type='text' name='mt_tickets[]' id='mt_tickets_$counting . '_' . $label' value='" . esc_attr( $options['tickets'] ) . "' size='8' />";
} else {
$available = "<input type='hidden' name='mt_tickets[]' id='mt_tickets_$label' value='inherit' />";
$available = "<input type='hidden' name='mt_tickets[]' id='mt_tickets_$counting . '_' . $label' value='inherit' />";
}
if ( $label ) {
$date = ( '' !== $options['label'] ) ? $options['label'] : gmdate( 'Y-m-d' );
Expand All @@ -441,8 +441,8 @@ function mt_prices_table( $registration = array() ) {
<button type='button' class='button up'><span class='dashicons dashicons-arrow-up-alt'></span><span class='screen-reader-text'>" . __( 'Move Up', 'my-tickets' ) . "</span></button>
<button type='button' class='button down'><span class='dashicons dashicons-arrow-down-alt'></span><span class='screen-reader-text'>" . __( 'Move Down', 'my-tickets' ) . "</span></button>
</td>
<td>$label_field<input type='$type' class='$label_class' name='mt_label[]' id='mt_label_$label' value='" . esc_attr( stripslashes( strip_tags( $options['label'] ) ) ) . "' />$comps</td>
<td><input type='number' name='mt_price[]' step='0.01' id='mt_price_$label' value='" . esc_attr( $options['price'] ) . "' size='8' /></td>
<td>$label_field<input type='$type' class='$label_class' name='mt_label[]' id='mt_label_$counting . '_' . $label' value='" . esc_attr( stripslashes( strip_tags( $options['label'] ) ) ) . "' />$comps</td>
<td><input type='number' name='mt_price[]' step='0.01' id='mt_price_$counting . '_' . $label' value='" . esc_attr( $options['price'] ) . "' size='8' /></td>
<td>$available</td>
<td><input type='hidden' name='mt_sold[]' value='" . $sold . "' />" . $sold . '</td>
<td><input type="date" name="mt_close[]" value="' . ( ( $close ) ? gmdate( 'Y-m-d', $close ) : '' ) . '" /></td>
Expand All @@ -465,19 +465,19 @@ function mt_prices_table( $registration = array() ) {
<button type='button' class='button up'><span class='dashicons dashicons-arrow-up-alt'></span><span class='screen-reader-text'>" . __( 'Move Up', 'my-tickets' ) . "</span></button>
<button type='button' class='button down'><span class='dashicons dashicons-arrow-down-alt'></span><span class='screen-reader-text'>" . __( 'Move Down', 'my-tickets' ) . "</span></button>
</td>
<td><input type='text' readonly name='mt_label[]' id='mt_label_complimentary' value='Complimentary' /><br />" . __( 'Note: complimentary tickets can only be added by logged-in administrators.', 'my-tickets' ) . "</td>
<td><input type='text' readonly name='mt_price[]' id='mt_price_complimentary' value='0' size='8' /></td>
<td><input type='text' readonly name='mt_label[]' id='mt_label_$counting . '_' . complimentary' value='Complimentary' /><br />" . __( 'Note: complimentary tickets can only be added by logged-in administrators.', 'my-tickets' ) . "</td>
<td><input type='text' readonly name='mt_price[]' id='mt_price_$counting . '_' . complimentary' value='0' size='8' /></td>
<td>$available</td>
<td></td>
<td></td>
</tr>";
}
}
$return .= "
<tr class='clonedPrice' id='price1'>
<tr class='clonedPrice $counting' id='price" . $counting . "1'>
<td></td>
<td>$label_field<input type='text' class='" . $label_class . "' name='mt_label[]' id='mt_label' /></td>
<td><input type='text' name='mt_price[]' id='mt_price' step='0.01' size='8' /></td>
<td>$label_field<input type='text' class='" . $label_class . "' name='mt_label[]' id='mt_$counting . '_' . label' /></td>
<td><input type='text' name='mt_price[]' id='mt_$counting . '_' . price' step='0.01' size='8' /></td>
<td>$available_empty</td>
<td></td>
<td><input type='date' name='mt_close[]' value='' /></td>
Expand All @@ -487,8 +487,8 @@ function mt_prices_table( $registration = array() ) {
$del_field = __( 'Remove last price group', 'my-tickets' );
$return .= '
<p>
<input type="button" id="add_price" value="' . $add_field . '" class="button" />
<input type="button" id="del_price" value="' . $del_field . '" class="button" />
<input type="button" value="' . $add_field . '" class="add-price button ' . $counting . '" data-context="' . $counting . '" />
<input type="button" value="' . $del_field . '" class="del-price button ' . $counting . '" data-context="' . $counting . '" />
</p>';

return $total . $return;
Expand Down
18 changes: 9 additions & 9 deletions src/mt-ticketing-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,25 +263,25 @@ function mt_ticketing_settings() {
<div class="mt-flex">
<div class="ticket-sale-expiration">
<p>
<label for='reg_expires'><?php _e( 'Stop online sales <em>x</em> hours before event', 'my-tickets' ); ?></label>
<input type='number' name='defaults[reg_expires]' id='reg_expires' value='<?php stripslashes( esc_attr( $displayed['reg_expires'] ) ); ?>'/>
<label for='reg_expires_<?php $model; ?>'><?php _e( 'Stop online sales <em>x</em> hours before event', 'my-tickets' ); ?></label>
<input type='number' name='defaults[<?php $model; ?>][reg_expires]' id='reg_expires_<?php $model; ?>' value='<?php stripslashes( esc_attr( $displayed['reg_expires'] ) ); ?>'/>
</p>
<p>
<label for='multiple'><?php _e( 'Allow multiple tickets/ticket type per purchaser', 'my-tickets' ); ?></label>
<input type='checkbox' name='defaults[multiple]' id='multiple' value='true' <?php echo ( $multiple ) ? ' checked="checked"' : ''; ?> />
<label for='multiple_<?php $model; ?>'><?php _e( 'Allow multiple tickets/ticket type per purchaser', 'my-tickets' ); ?></label>
<input type='checkbox' name='defaults[<?php $model; ?>][multiple]' id='multiple_<?php $model; ?>' value='true' <?php echo ( $multiple ) ? ' checked="checked"' : ''; ?> />
</p>
</div>
<div class="ticket-sale-types">
<fieldset>
<legend><?php _e( 'Type of Sale', 'my-tickets' ); ?></legend>
<p>
<input type='radio' name='defaults[sales_type]' id='mt_sales_type_tickets' value='tickets'<?php checked( $is_tickets, true ); ?> />
<label for='mt_sales_type_tickets'><?php _e( 'Ticket Sales', 'my-tickets' ); ?></label><br/>
<input type='radio' name='defaults[sales_type]' id='mt_sales_type_registration' value='registration'<?php checked( $is_registration, true ); ?> />
<label for='mt_sales_type_registration'><?php _e( 'Event Registration', 'my-tickets' ); ?></label>
<input type='radio' name='defaults[<?php $model; ?>][sales_type]' id='mt_sales_type_tickets_<?php $model; ?>' value='tickets'<?php checked( $is_tickets, true ); ?> />
<label for='mt_sales_type_tickets_<?php $model; ?>'><?php _e( 'Ticket Sales', 'my-tickets' ); ?></label><br/>
<input type='radio' name='defaults[<?php $model; ?>][sales_type]' id='mt_sales_type_registration_<?php $model; ?>' value='registration'<?php checked( $is_registration, true ); ?> />
<label for='mt_sales_type_registration_<?php $model; ?>'><?php _e( 'Event Registration', 'my-tickets' ); ?></label>
</p>
</fieldset>
<input type="hidden" name='defaults[counting_method]' value='<?php echo esc_attr( $method ); ?>' />
<input type="hidden" name='defaults[<?php $model; ?>][counting_method]' value='<?php echo esc_attr( $method ); ?>' />
</div>
</div>
<?php echo mt_prices_table( $displayed ); ?>
Expand Down

0 comments on commit 2f2e082

Please sign in to comment.