Skip to content

Commit

Permalink
Add separate AJAX function for editing the dates of a single instance
Browse files Browse the repository at this point in the history
Fixes #631
  • Loading branch information
joedolson committed Jan 14, 2025
1 parent 2e2609b commit 1b9ffe4
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 46 deletions.
35 changes: 13 additions & 22 deletions src/css/mc-styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -271,32 +271,20 @@ li.datetime-template:nth-of-type(even) {

.recurrences .columns.instance-list,
#e_schedule .columns.instance-list {
grid-template-columns: repeat( 3, 1fr )
}

@media (min-width: 1200px) {
.recurrences .columns.instance-list {
grid-template-columns: repeat( 4, 1fr )
}
}

@media (min-width: 1400px) {
.recurrences .columns.instance-list {
grid-template-columns: repeat( 5, 1fr )
}
}

@media (min-width: 1600px) {
.recurrences .columns.instance-list {
grid-template-columns: repeat( 6, 1fr )
}
display: flex;
flex-wrap: wrap;
}

.recurrences .columns.instance-list li,
#e_schedule .columns.instance-list li {
min-height: 80px;
background: rgba( 0,0,0,.05 );
padding: 0 10px;
padding: 0 8px;
flex-grow: 1;
}

.instance-list button[aria-pressed="true"] {
outline: 2px solid;
outline-offset: 2px;
}

.recurrences input[type=time],
Expand Down Expand Up @@ -1894,7 +1882,10 @@ tr.problem .error {
}

.recurrences .columns p.instance-buttons {
display: block;
display: flex;
flex-wrap: wrap;
gap: 6px;
align-items: center;
}

input#mc_event_endtime:invalid+span,
Expand Down
3 changes: 3 additions & 0 deletions src/includes/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ function mc_kses_elements() {
'data-modal-prefix-class' => array(),
'data-modal-close-text' => array(),
'data-modal-title' => array(),
'data-begin' => array(),
'data-end' => array(),
'data-value' => array(),
),
'ul' => array(
'class' => array(),
Expand Down
47 changes: 40 additions & 7 deletions src/js/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,38 @@

$( '.mc_add_new' ).hide();

$( 'button.edit_occurrence').on( 'click', function() {
let expanded = $( this ).attr( 'aria-pressed' );
let visible = $( 'button.save-occurrence' ).is( ':visible' );
if ( expanded == 'true' ) {
$( this ).removeAttr( 'aria-pressed' );
} else {
$( this ).attr( 'aria-pressed', 'true' );
$( 'button.edit_occurrence' ).not( this ).removeAttr( 'aria-pressed' );
$( '#r_time' ).trigger( 'focus' );
$( '.save-occurrence' ).text( 'Edit Date' );
let instance_id = $( this ).data( 'value' );
let begin = $( this ).data( 'begin' ).split(' ');
let end = $( this ).data( 'end' ).split(' ');
$( 'input[name="r_instance"]' ).val( instance_id );
$( '#r_time' ).val( begin[1] );
$( '#r_begin' ).val( begin[0] );
$( '#r_endtime' ).val( end[1] );
$( '#r_end' ).val( end[0] );
}
if ( ! visible ) {
$( '.mc_add_new' ).toggle();
}

});

$( 'button.add-occurrence').on( 'click', function() {
let expanded = $( this ).attr( 'aria-expanded' );
$( 'input[name="r_instance"]' ).val( '' );
if ( expanded == 'true' ) {
$( this ).attr( 'aria-expanded', 'false' ).find( '.dashicons' ).addClass( 'dashicons-plus' ).removeClass( 'dashicons-minus' );
$( this ).attr( 'data-action', 'shiftback' );
$( '.save-occurrence' ).text( 'Add Date' );
} else {
$( this ).attr( 'aria-expanded', 'true' ).find( '.dashicons' ).addClass( 'dashicons-minus' ).removeClass( 'dashicons-plus' );
$( this ).attr( 'data-action', '' );
Expand All @@ -41,12 +68,13 @@
* Save additional date.
*/
$( 'button.save-occurrence').on( 'click', function() {
let date = $( '#r_begin' ).val();
let begin = $( '#r_time' ).val();
let end = $( '#r_endtime' ).val();
let enddate = $( '#r_enddate' ).val();
let event_id = $( 'input[name="event_id"]' ).val();
let group_id = $( 'input[name="event_group_id"]' ).val();
let date = $( '#r_begin' ).val();
let begin = $( '#r_time' ).val();
let end = $( '#r_endtime' ).val();
let enddate = $( '#r_enddate' ).val();
let event_id = $( 'input[name="event_id"]' ).val();
let group_id = $( 'input[name="event_group_id"]' ).val();
let instance_id = $( 'input[name="r_instance"]' ).val();

const data = {
'action': mc_data.recur,
Expand All @@ -56,6 +84,7 @@
'event_time' : begin,
'event_endtime' : end,
'event_enddate' : enddate,
'event_instance' : instance_id,
'security': mc_data.security
};
$.post( ajaxurl, data, function (response) {
Expand All @@ -64,7 +93,11 @@
let display = time[0] + ':' + time[1];
let edit_url = mc_data.url + response.id;
let dateEnd = ( typeof( enddate ) === 'undefined' ) ? date : enddate;
$( '.instance-list' ).append( '<li class="new"><p><span id="occur_date_' + response.id + '"><strong>Added:</strong> ' + date + ' @ ' + display + '</span></p><p class="instance-buttons"><button class="button delete_occurrence" type="button" data-event="' + event_id + '" data-begin="' + date + ' ' + begin + '" data-end="' + dateEnd + ' ' + end + '" data-value="' + response.id + '" aria-describedby="occur_date_' + response.id + '">Delete</button> <a href="' + edit_url + '" class="button">Edit</a></p></li>' );
if ( instance_id ) {
$( '.instance-list' ).append( '<li class="new"><p><span id="occur_date_' + response.id + '"><strong>Edited:</strong> ' + date + ' @ ' + display + '</span></p></li>' );
} else {
$( '.instance-list' ).append( '<li class="new"><p><span id="occur_date_' + response.id + '"><strong>Added:</strong> ' + date + ' @ ' + display + '</span></p><p class="instance-buttons"><button class="button delete_occurrence" type="button" data-event="' + event_id + '" data-begin="' + date + ' ' + begin + '" data-end="' + dateEnd + ' ' + end + '" data-value="' + response.id + '" aria-describedby="occur_date_' + response.id + '">Delete</button> <a href="' + edit_url + '" class="button">Edit</a></p></li>' );
}
}
$('.mc_response').text( response.response ).show( 300 );
}, "json" );
Expand Down
41 changes: 30 additions & 11 deletions src/my-calendar-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,30 +304,49 @@ function mc_ajax_add_date() {
$event_time = sanitize_text_field( $_REQUEST['event_time'] );
$event_endtime = isset( $_REQUEST['event_endtime'] ) ? sanitize_text_field( $_REQUEST['event_endtime'] ) : '';
$group_id = (int) $_REQUEST['group_id'];
$update = false;

$args = array(
'id' => $event_id,
'event_date' => $event_date,
'event_end' => $event_end,
'event_time' => $event_time,
'event_endtime' => $event_endtime,
'group' => $group_id,
);
$id = mc_insert_instance( $args );
if ( isset( $_REQUEST['event_instance'] ) && ! empty( $_REQUEST['event_instance'] ) ) {
$instance = absint( $_REQUEST['event_instance'] );
$args = array(
'event_begin' => $event_date,
'event_time' => $event_time,
'event_end' => $event_end,
'event_endtime' => $event_endtime,
'event_group_id' => $group_id,
);
$id = mc_update_instance( $instance, $event_id, $args );
$update = true;
} else {
$args = array(
'id' => $event_id,
'event_date' => $event_date,
'event_end' => $event_end,
'event_time' => $event_time,
'event_endtime' => $event_endtime,
'group' => $group_id,
);
$id = mc_insert_instance( $args );
}

if ( $id ) {
if ( $update ) {
$message = esc_html__( 'The date of your occurrence has been changed.', 'my-calendar' );
} else {
$message = esc_html__( 'A new date has been added to this event.', 'my-calendar' );
}
wp_send_json(
array(
'success' => 1,
'id' => (int) $id,
'response' => esc_html__( 'A new date has been added to this event.', 'my-calendar' ),
'response' => $message,
)
);
} else {
wp_send_json(
array(
'success' => 0,
'response' => esc_html__( 'Sorry! I failed to add that date to your event.', 'my-calendar' ),
'response' => esc_html__( "Sorry! I wasn't able to update your event.", 'my-calendar' ),
)
);
}
Expand Down
1 change: 1 addition & 0 deletions src/my-calendar-event-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2981,6 +2981,7 @@ function mc_recur_datetime_input( $data ) {

$form = '
<div class="columns">
<input type="hidden" name="r_instance" value="" />
<p>
<label for="r_time">' . __( 'Start Time', 'my-calendar' ) . '</label>
<input type="time" id="r_time" name="recur_time[]" size="8" value="' . esc_attr( $starttime ) . '" />
Expand Down
17 changes: 11 additions & 6 deletions src/my-calendar-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -1075,14 +1075,19 @@ function mc_admin_instances( $id, $occur = 0 ) {
$date = "<span id='occur_date_$result->occur_id'><strong>" . $date . '</strong><br />' . $time . '</span>';
$class = ( my_calendar_date_xcomp( mc_date( 'Y-m-d H:i:00', $start ), mc_date( 'Y-m-d H:i:00', time() ) ) ) ? 'past-event' : 'future-event';
if ( (int) $result->occur_id === (int) $occur || 1 === $count ) {
$control = '';
$edit = "<p>$date</p><p><em>" . __( 'Editing Now', 'my-calendar' ) . '</em></p>';
$class = 'current-event';
$control = '';
$control .= "<p>$date</p><p><em>" . __( 'Editing Now', 'my-calendar' ) . '</em></p>';
$class = 'current-event';
} else {
$control = "<p>$date</p><p class='instance-buttons'><button class='button delete_occurrence' type='button' data-event='$result->occur_event_id' data-begin='$result->occur_begin' data-end='$result->occur_end' data-value='$result->occur_id' aria-describedby='occur_date_$result->occur_id' />" . __( 'Delete', 'my-calendar' ) . '</button> ';
$edit = "<a href='" . admin_url( 'admin.php?page=my-calendar' ) . "&amp;mode=edit&amp;event_id=$id&amp;date=$result->occur_id' class='button' aria-describedby='occur_date_$result->occur_id'>" . __( 'Edit', 'my-calendar' ) . '</a></p>';
$control = "<p>$date</p>";
$control .= "<p class='instance-buttons'>";
$control .= "<button class='button delete_occurrence' type='button' data-event='$result->occur_event_id' data-begin='$result->occur_begin' data-end='$result->occur_end' data-value='$result->occur_id' aria-describedby='occur_date_$result->occur_id' />" . __( 'Delete', 'my-calendar' ) . '</button> ';
$control .= "<button class='button edit_occurrence' type='button' data-event='$result->occur_event_id' data-begin='$result->occur_begin' data-end='$result->occur_end' data-value='$result->occur_id' aria-describedby='occur_date_$result->occur_id' />" . __( 'Edit Date', 'my-calendar' ) . '</button>';
$control .= '</p><p>';
$control .= "<a href='" . admin_url( 'admin.php?page=my-calendar' ) . "&amp;mode=edit&amp;event_id=$id&amp;date=$result->occur_id' aria-describedby='occur_date_$result->occur_id'>" . __( 'Edit Details', 'my-calendar' ) . '</a>';
$control .= '</p>';
}
$output .= "<li class='$class'>$control$edit</li>";
$output .= "<li class='$class'>$control</li>";
}
}

Expand Down

0 comments on commit 1b9ffe4

Please sign in to comment.