[FRAMEWORK] Xây dựng page Account Phần 7 (ok)
Like Event, Unlike Event
Previous[FRAMEWORK] Xây dựng page Account Phần 6 (ok)Next[FRAMEWORK] Xây dựng page Account Phần 8 (ok)
Last updated
Was this helpful?
Like Event, Unlike Event
Last updated
Was this helpful?
C:\xampp\htdocs\wordpress8\wp-content\themes\addframwork\framework\ajax\event.php
<?php
add_action('wp_ajax_event_favorite_process', 'event_favorite_process');
function event_favorite_process() {
if (!isset($_POST['token']) or !is_numeric($_POST["id"]) or !wp_verify_nonce($_POST['token'], BJ_NONCE_KEY . 'event' . $_POST["id"])) {
die(json_encode(["error"]));
}
$type = isset($_POST["type"]) && in_array($_POST["type"], ["going", "maybe"]) ? $_POST['type'] : "maybe";
$current_user_id = get_current_user_id();
global $bj_controller;
$model = $bj_controller->Model("event");
$result = $model->event_favorite_process_user($_POST["id"], $current_user_id, $type);
if ($result != false) {
if (isset($_POST['page']) && $_POST['page'] == "single") {
die(json_encode(["success", count($model->get_event_list_user($_POST["id"])), count($model->get_event_list_user($_POST["id"], "maybe"))]));
} else {
die(json_encode(["success"]));
}
}
die(json_encode(["error"]));
}
?>
C:\xampp\htdocs\wordpress8\wp-content\themes\addframwork\framework\models\event.php
<?php
class BJ_event_Model {
private $table_event = 'bj_event';
public function get_list_event_id_saved($user_id) {
global $wpdb;
$arr = [];
$table = $wpdb->prefix . "bj_event_user";
$result = $wpdb->get_results($wpdb->prepare(" SELECT event_id FROM {$table} WHERE user_id = %d ", $user_id), ARRAY_A);
if (!empty($result)) {
foreach ($result as $value) {
$arr[] = $value["event_id"];
}
}
return $arr;
}
public function get_list_event_saved($user_id) {
global $wpdb;
$table = $wpdb->prefix . $this->table_event;
$list_event_id = $this->get_list_event_id_saved($user_id);
if (!empty($list_event_id)) {
$array = implode("','", $list_event_id);
$result = $wpdb->get_results("SELECT * FROM {$table} WHERE id IN ('{$array}')", ARRAY_A);
if (!empty($result)) {
return $result;
}
}
}
public function get_event_user_status($event_id, $user_id) {
global $wpdb;
$table = $wpdb->prefix . "bj_event_user";
$result = $wpdb->get_results($wpdb->prepare(" SELECT type FROM {$table} WHERE event_id = %d AND user_id = %d", $event_id, $user_id), ARRAY_A);
if (!empty($result)) {
return $result[0]["type"];
}
}
public function get_event_list_user($event_id,$type = "going"){
global $wpdb;
$arr = [];
$table = $wpdb->prefix."bj_event_user";
$result = $wpdb->get_results ( $wpdb->prepare(" SELECT user_id FROM {$table} WHERE event_id = %d AND type = %s",$event_id,$type) ,ARRAY_A);
if(!empty($result)){
foreach ($result as $value) {
$arr[] = $value["user_id"];
}
};
return $arr;
}
public function event_favorite_process_user($event_id, $user_id, $type = "maybe") {
global $wpdb;
$table = $wpdb->prefix . "bj_event_user";
$result = $wpdb->get_results($wpdb->prepare(" SELECT id ,type FROM {$table} WHERE event_id = %d AND user_id = %d", $event_id, $user_id), ARRAY_A);
if (empty($result)) {
$wpdb->insert($table, ["event_id" => $event_id, "user_id" => $user_id, "type" => $type], ["%d", "%d", "%s"]);
$favorite_result = $wpdb->insert_id;
} else {
$result = $result[0];
if ($result["type"] == $type) {
$favorite_result = $wpdb->delete($table, ["event_id" => $event_id, "user_id" => $user_id], ["%d", "%d"]);
} else {
$favorite_result = $wpdb->update($table, ["type" => $type], ["event_id" => $event_id, "user_id" => $user_id], ["%s"], ["%d", "%d"]);
}
}
$table = $wpdb->prefix . "bj_event";
$result = $wpdb->update($table, ["going" => count($this->get_event_list_user($event_id))], ["id" => $event_id], ["%d"], ["%d"]);
return $favorite_result;
}
}
C:\xampp\htdocs\wordpress8\wp-content\themes\addframwork\functions.php
<?php
add_action('after_setup_theme', 'blankslate_setup');
function blankslate_setup() {
load_theme_textdomain('blankslate', get_template_directory() . '/languages');
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_theme_support('responsive-embeds');
add_theme_support('automatic-feed-links');
add_theme_support('html5', array('search-form', 'navigation-widgets'));
add_theme_support('woocommerce');
global $content_width;
if (!isset($content_width)) {$content_width = 1920;}
register_nav_menus(array('main-menu' => esc_html__('Main Menu', 'blankslate')));
}
add_filter('document_title_separator', 'blankslate_document_title_separator');
function blankslate_document_title_separator($sep) {
$sep = esc_html('|');
return $sep;
}
add_filter('the_title', 'blankslate_title');
function blankslate_title($title) {
if ($title == '') {
return esc_html('...');
} else {
return wp_kses_post($title);
}
}
function blankslate_schema_type() {
$schema = 'https://schema.org/';
if (is_single()) {
$type = "Article";
} elseif (is_author()) {
$type = 'ProfilePage';
} elseif (is_search()) {
$type = 'SearchResultsPage';
} else {
$type = 'WebPage';
}
echo 'itemscope itemtype="' . esc_url($schema) . esc_attr($type) . '"';
}
add_filter('nav_menu_link_attributes', 'blankslate_schema_url', 10);
function blankslate_schema_url($atts) {
$atts['itemprop'] = 'url';
return $atts;
}
if (!function_exists('blankslate_wp_body_open')) {
function blankslate_wp_body_open() {
do_action('wp_body_open');
}
}
add_action('wp_body_open', 'blankslate_skip_link', 5);
function blankslate_skip_link() {
echo '<a href="#content" class="skip-link screen-reader-text">' . esc_html__('Skip to the content', 'blankslate') . '</a>';
}
add_filter('the_content_more_link', 'blankslate_read_more_link');
function blankslate_read_more_link() {
if (!is_admin()) {
return ' <a href="' . esc_url(get_permalink()) . '" class="more-link">' . sprintf(__('...%s', 'blankslate'), '<span class="screen-reader-text"> ' . esc_html(get_the_title()) . '</span>') . '</a>';
}
}
add_filter('excerpt_more', 'blankslate_excerpt_read_more_link');
function blankslate_excerpt_read_more_link($more) {
if (!is_admin()) {
global $post;
return ' <a href="' . esc_url(get_permalink($post->ID)) . '" class="more-link">' . sprintf(__('...%s', 'blankslate'), '<span class="screen-reader-text"> ' . esc_html(get_the_title()) . '</span>') . '</a>';
}
}
add_filter('big_image_size_threshold', '__return_false');
add_filter('intermediate_image_sizes', 'remove_default_img_sizes', 10, 1);
function remove_default_img_sizes($sizes) {
$targets = ['medium_large', 'large', '1536x1536', '2048x2048', 'woocommerce_thumbnail', 'woocommerce_single', 'woocommerce_gallery_thumbnail', 'shop_catalog', 'shop_single', 'shop_thumbnail'];
foreach ($sizes as $size_index => $size) {
if (in_array($size, $targets)) {
unset($sizes[$size_index]);
}
}
return $sizes;
}
// == Start Script
add_action('wp_enqueue_scripts', 'ecademy_enqueue_style');
function ecademy_enqueue_style() {
wp_enqueue_script('jquery');
wp_enqueue_style("custom_css", get_stylesheet_directory_uri() . "/css/custom.css", array(), '1.1.0', 'all');
$script_data_array = array(
'ajaxurl' => admin_url('admin-ajax.php'),
'security' => wp_create_nonce('file_upload'),
);
wp_enqueue_script('custom_js', get_stylesheet_directory_uri() . '/js/custom.js', array('jquery'), '123', 'all', true);
wp_localize_script('custom_js', 'app', $script_data_array);
}
function wpdocs_selectively_enqueue_admin_script($hook) {
wp_enqueue_script('custom_js', get_stylesheet_directory_uri() . '/js/admin.js', array('jquery'), '123', 'all', true);
wp_enqueue_style("custom_css", get_stylesheet_directory_uri() . "/css/admin.css", array(), '1.1.0', 'all');
}
add_action('admin_enqueue_scripts', 'wpdocs_selectively_enqueue_admin_script');
// == End Script
// Start App
require get_template_directory() . '/inc/define.php';
require get_template_directory() . '/framework/init.php';
require get_template_directory() . '/inc/functions/function-setup.php';
require get_template_directory() . '/inc/functions/function-global.php';
require_once get_template_directory().'/framework/ajax/business.php';
require_once get_template_directory().'/framework/ajax/deal.php';
require_once get_template_directory().'/framework/ajax/event.php';
// End App
C:\xampp\htdocs\wordpress8\wp-content\themes\addframwork\js\account-business.js
jQuery(document).ready(function($) {
function account_activity_business_load(type) {
$(".status").removeClass("d-none");
$.ajax({
url: app.ajaxUrl,
type: "post",
dataType: "text",
data: {
action: 'account_activity_load_list_' + type,
token: $("#nonce_token").val()
},
success: function(output) {
$(".tab-" + type + " .account-list-item").append(output);
$(".status").addClass("d-none");
}
});
}
function account_get_tab_number(type = "activity") {
$.ajax({
url: app.ajaxUrl,
type: "post",
dataType: "text",
data: {
action: 'account_activity_get_tab_number',
type: type,
token: $("#nonce_token").val()
},
success: function(output) {
if (output != "error") {
const obj = JSON.parse(output, function(key, value) {
$(".account-load-btn[data-id='" + key + "'] span").text(value);
});
}
}
});
}
// ==
account_activity_business_load("deal");
account_get_tab_number();
// ==
$('body').on('click', '#change-acc', function(e) {
const $this = $(this);
const btnScan = $('#scan-voucher');
const type = $this.attr('data-type');
const userId = $this.attr('data-id');
const tab_active = $(".tab-active").data("tab-id");
$.ajax({
url: app.ajaxUrl,
type: "post",
dataType: "text",
data: {
action: 'change_business_activity',
token: $('#nonce_token').val(),
type: type,
id: userId,
},
beforeSend: function() {
$(".account-list-item:not('.news') .col-6").not(".col-addnew").remove();
$this.find('svg').addClass('spin');
},
success: function(response) {
$(".menu-tab-account .li").not(".active").find("a").addClass("load");
$this.find('svg').removeClass('spin');
btnScan.toggle();
if (type == 'activity') {
// xử lý data business
account_get_tab_number("business");
$this.attr('data-type', 'business').find('span').text(app.business);
$(".account-list-item .col-addnew").removeClass("d-none");
if (tab_active != "news") {
account_activity_business_load(tab_active);
}
$(".menu-tab-account ul li:last-child,.tab-news").addClass("d-none");
$(".col-activity-event").remove();
return;
}
$this.attr('data-type', 'activity').find('span').text(app.activity);
$(".account-list-item .col-addnew").addClass("d-none");
account_get_tab_number();
if (tab_active != "news") {
account_activity_business_load(tab_active);
}
$(".menu-tab-account ul li:last-child,.tab-news").removeClass("d-none");
},
error: function() {
$this.find('svg').removeClass('spin');
alert('Error');
}
})
return false;
});
// ==
var exclude_img = [];
var input_btn = 0;
var xp = -1;
$(document).on("click", ".add-image-btn", function(e) {
let select = $(this).closest("form");
input_btn++;
select.find(".list-input-file").append("<input type='file' name='upload_files[]' id='filenumber" + input_btn + "' class='img_file upload_files' accept='.gif,.jpg,.jpeg,.png,' multiple/>");
$("#filenumber" + input_btn).click();
});
$(document).on("change", ".upload_files", function(e) {
let select = $(this).closest("form")
files = e.target.files;
filesLength = files.length;
for (var i = 0; i < filesLength; i++) {
var f = files[i];
var res_ext = files[i].name.split(".");
var img_or_video = res_ext[res_ext.length - 1];
var fileReader = new FileReader();
fileReader.name = f.name;
fileReader.onload = function(e) {
xp++;
var file = e.target;
select.find(".images-box").append("<div class='box preview-image'><div class='images-preview' style='background-image: url(" + e.target.result + ")'><button type='button' data-id='" + xp + "' class='remove-img btn-close' title='Remove'></button></div></div>");
};
fileReader.readAsDataURL(f);
}
});
$(document).on("click", ".images-box .remove-img", function() {
$(this).closest(".box").remove();
exclude_img.push($(this).attr("data-id"));
});
// ==
var is_busy = false;
var add_deal_form = "#add-deal-form ";
$(add_deal_form).submit(function() {
if (is_busy == true) return;
is_busy = true;
var formData = new FormData(document.getElementById("add-deal-form"));
formData.append('action', 'add_deal');
formData.append('exclude_img', exclude_img);
$.ajax({
url: app.ajaxUrl,
type: "post",
contentType: false,
processData: false,
data: formData,
success: function(output) {
is_busy = false;
if (output == "success") {
$("#alert-success").modal("show");
$("#add_deal_form").remove();
}
}
});
return false;
});
$(document).on("click", ".deal.add-favorite", function() {
if (is_busy == true) return;
is_busy = true;
let select = $(this);
$.ajax({
url: app.ajaxUrl,
type: "post",
dataType: "text",
data: {
action: 'deal_favorite_process',
id: $(this).data("id"),
token: $(this).data("token"),
},
success: function(output) {
$(".status").addClass("d-none");
is_busy = false;
if (output == "success") {
select.toggleClass("added");
}
}
})
});
$(document).on("click", ".event.add-favorite", function() {
if (is_busy == true) return;
is_busy = true;
let select = $(this);
let page = $(this).data("page");
let id = $(this).data("id");
$.ajax({
url: app.ajaxUrl,
type: "post",
dataType: "text",
data: {
action: 'event_favorite_process',
id: id,
type: $(this).data("type"),
page: $(this).data("page"),
token: $(this).data("token"),
},
success: function(output) {
is_busy = false;
let result = $.parseJSON(output);
if (result[0] == "success") {
if (page == "single") {
$(".event.add-favorite").not(select).removeClass("added");
$(".n-o-going").text(result[1]);
$(".n-o-maybe").text(result[2]);
}
$(select).toggleClass("added");
}
}
})
});
$(".account-load-btn").click(function() {
if ($(this).hasClass("load")) {
let data_type = $(this).data("id");
let account_type = $("#change-acc").data("type");
account_activity_business_load(data_type);
$(this).removeClass("load");
}
})
});