/** * Renders a list of timezone and defaults to the server timezone. * * @param string $name * The name of the form element to be used in form processing. * @param string $default */ function wcs3_generate_timezones_select_list($name = '', $default = NULL) { $server_timezone = wcs3_get_system_timezone(); $timezones_list = DateTimeZone::listIdentifiers(); if ($default == NULL) { $default = $server_timezone; } $option_groups = array("Africa", "America", "Antarctica", "Arctic", "Asia", "Atlantic", "Australia", "Europe", "Indian", "Pacific"); $output = "<select id='{$name}' name='{$name}'>"; foreach ($option_groups as $group) { $group_timezones = array(); $output .= "<optgroup label='{$group}'>"; foreach ($timezones_list as $timezone) { if (preg_match("/^{$group}/", $timezone) > 0) { $short_timezone = str_replace($group . '/', '', $timezone); $group_timezones[$timezone] = $short_timezone; } } foreach ($group_timezones as $timezone => $short_timezone) { if ($timezone == $default) { $output .= "<option value='{$timezone}' selected='selected'>{$short_timezone}</option>"; } else { $output .= "<option value='{$timezone}'>{$short_timezone}</option>"; } } $output .= '</optgroup>'; } $output .= '</select>'; return $output; }
/** * Gets the wcs2 schedule data and create new wcs3 entries. * * @param array $data: wcs2 data. */ function wcs3_get_wcs2_schedule_data($data) { global $wpdb; $table = $wpdb->prefix . 'wcs2_schedule'; $wcs3_table = wcs3_get_table_name(); $query = "SELECT class_id, instructor_id, classroom_id, weekday, start_hour,\n end_hour, visibility, notes FROM {$table}"; $r = $wpdb->get_results($query); $timezone = wcs3_get_system_timezone(); foreach ($r as $entry) { $new_class_id = $data['classes'][$entry->class_id]['new_id']; $new_inst_id = $data['instructors'][$entry->instructor_id]['new_id']; $new_loc_id = $data['classrooms'][$entry->classroom_id]['new_id']; $wpdb->insert($wcs3_table, array('class_id' => $new_class_id, 'instructor_id' => $new_inst_id, 'location_id' => $new_loc_id, 'weekday' => $entry->weekday, 'start_hour' => $entry->start_hour, 'end_hour' => $entry->end_hour, 'timezone' => $timezone, 'visible' => $entry->visibility, 'notes' => $entry->notes), array('%d', '%d', '%d', '%d', '%s', '%s', '%s', '%d', '%s')); } }
/** * Sets PHP's global timezone var. */ function wcs3_set_global_timezone() { $timezone = wcs3_get_system_timezone(); date_default_timezone_set($timezone); }
/** * Add or update schedule entry handler. */ function wcs3_add_or_update_schedule_entry_callback() { wcs3_verify_nonce(); global $wpdb; $response = __('Schedule entry added successfully', 'wcs3'); $result = 'updated'; $update_request = FALSE; $row_id = NULL; $days_to_update = array(); $table = wcs3_get_table_name(); $required = array('class_id' => __('Class ID'), 'instructor_id' => __('Instructor ID'), 'location_id' => __('Location ID'), 'weekday' => __('Weekday'), 'start_hour' => __('Start Hour'), 'start_minute' => __('Start Minute'), 'end_hour' => __('End Hour'), 'end_minute' => __('End Minute'), 'visible' => __('Visible')); wcs3_verify_required_fields($required); if (isset($_POST['row_id'])) { // This is an update request and not an insert. $update_request = TRUE; $row_id = sanitize_text_field($_POST['row_id']); } $wcs3_options = wcs3_load_settings(); $class_id = sanitize_text_field($_POST['class_id']); $instructor_id = sanitize_text_field($_POST['instructor_id']); $location_id = sanitize_text_field($_POST['location_id']); $weekday = sanitize_text_field($_POST['weekday']); $start_hour = sanitize_text_field($_POST['start_hour']); $start_minute = sanitize_text_field($_POST['start_minute']); $end_hour = sanitize_text_field($_POST['end_hour']); $end_minute = sanitize_text_field($_POST['end_minute']); $visible = sanitize_text_field($_POST['visible']); $notes = ''; // Check if we need to sanitize the notes or leave as is. if ($_POST['notes'] != NULL) { if ($wcs3_options['allow_html_in_notes'] == 'yes') { $notes = stripslashes_deep($_POST['notes']); $notes = stripslashes_deep($notes); } else { global $wcs3_allowed_html; $notes = wp_kses($_POST['notes'], $wcs3_allowed_html); } } $start = $start_hour . ':' . $start_minute . ':00'; $end = $end_hour . ':' . $end_minute . ':00'; $days_to_update[$weekday] = TRUE; // Validate time logic $timezone = wcs3_get_system_timezone(); $tz = new DateTimeZone($timezone); $start_dt = new DateTime(WCS3_BASE_DATE . ' ' . $start, $tz); $end_dt = new DateTime(WCS3_BASE_DATE . ' ' . $end, $tz); $wcs3_settings = wcs3_load_settings(); echo ''; if ($wcs3_settings['location_collision'] == 'yes') { // Validate location collision (if applicable) $location_collision = $wpdb->get_col($wpdb->prepare("\n \t\tSELECT id FROM {$table}\n \t\tWHERE location_id = %d AND weekday = %d\n \t\tAND %s < end_hour AND %s > start_hour\n \t\tAND id != %d\n \t\t", array($location_id, $weekday, $start, $end, $row_id))); } if ($wcs3_settings['instructor_collision'] == 'yes') { // Validate instructor collision (if applicable) $instructor_collision = $wpdb->get_col($wpdb->prepare("\n \t\tSELECT id FROM {$table}\n \t\tWHERE instructor_id = %d AND weekday = %d\n \t\tAND %s < end_hour AND %s > start_hour\n \t\tAND id != %d\n \t\t", array($instructor_id, $weekday, $start, $end, $row_id))); } // Prepare response if ($wcs3_settings['location_collision'] == 'yes' && !empty($location_collision)) { $response = __('Location is not available at this time', 'wcs3'); $result = 'error'; } else { if ($wcs3_settings['instructor_collision'] == 'yes' && !empty($instructor_collision)) { $response = __('Instructor is not available at this time', 'wcs3'); $result = 'error'; } else { if ($start_dt >= $end_dt) { // Invalid class time $response = __('A class cannot start before it ends', 'wcs3'); $result = 'error'; } else { $data = array('class_id' => $class_id, 'instructor_id' => $instructor_id, 'location_id' => $location_id, 'weekday' => $weekday, 'start_hour' => $start, 'end_hour' => $end, 'timezone' => $timezone, 'visible' => $visible == 'visible' ? 1 : 0, 'notes' => $notes); if ($update_request) { $old_weekday = $wpdb->get_var($wpdb->prepare("\n \t\tSELECT weekday FROM {$table}\n \t\tWHERE id = %d;\n \t\t", array($row_id))); $days_to_update[$old_weekday] = TRUE; $r = $wpdb->update($table, $data, array('id' => $row_id), array('%d', '%d', '%d', '%d', '%s', '%s', '%s', '%d', '%s'), array('%d')); if ($r === FALSE) { $response = __('Failed to update schedule entry', 'wcs3'); $result = 'error'; } else { $response = __('Schedule entry updated successfully'); } } else { $r = $wpdb->insert($table, $data, array('%d', '%d', '%d', '%d', '%s', '%s', '%s', '%d', '%s')); if ($r === FALSE) { $response = __('Failed to add schedule entry', 'wcs3'); $result = 'error'; } } } } } wcs3_json_response(array('response' => $response, 'result' => $result, 'days_to_update' => $days_to_update)); die; }