Commit f3dd5486 authored by Romain Duminil's avatar Romain Duminil

#50 Création d'une messagerie interne

parent e0ab5864
......@@ -11,6 +11,7 @@ require_once "CommunityProject_install.php";
require_once "UserRoleController.php";
require_once "ProjectCustomTypeCreator.php";
require_once "ShortcodeController.php";
require_once "tchat/Tchat.php";
require_once ABSPATH . "/wp-content/plugins/CommunityVoice/Settings.php";
class CommunityProject
......@@ -40,6 +41,7 @@ class CommunityProject
add_filter('screen_options_show_screen', array( $this, 'remove_screen_options_tab' ) );
new ShortcodeController();
new Tchat();
}
function remove_admin_bar() {
......
......@@ -4,6 +4,16 @@ class CommunityProject_install
{
function install()
{
$GLOBALS['wpdb']->query( "
CREATE TABLE `cv_tchat_project` (
`id` int(11) NOT NULL,
`post_id` int(11) NOT NULL,
`message` text NOT NULL,
`user_id` int(11) NOT NULL,
`date_message` date DEFAULT NULL
)"
);
add_option( "community_project_installed", "1" );
}
......
<?php
class Tchat
{
function __construct()
{
add_action( 'admin_enqueue_scripts', array( $this, 'load_script' ) );
add_action( 'wp_ajax_saveMessage', array( $this, 'saveMessage' ) );
add_action( 'wp_ajax_getMessages', array( $this, 'getMessages' ) );
add_action( "add_meta_boxes", array( $this, "add_tchat_meta_box" ) );
}
function load_script()
{
wp_register_script( 'ajax_tchat', plugin_dir_url( __FILE__ ) . 'tchat.js', array( 'jquery' ) );
wp_localize_script( 'ajax_tchat', 'tchatVar', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'tchat_nonce' ),
'userName'=> wp_get_current_user()->user_login,
) );
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'ajax_tchat' );
}
function saveMessage()
{
check_ajax_referer( 'tchat_nonce', 'nonce' );
global $wpdb;
if( isset( $_POST['message'] ) && ! empty( $_POST['message'] ) )
{
$wpdb->insert(
"{$wpdb->prefix}tchat_project",
array(
"post_id" => $_POST['post_id'],
"user_id" => get_current_user_id(),
"message" => $_POST['message'],
"date_message" => date('Y-m-d')
)
);
}
echo json_encode("success");
wp_die();
}
function add_tchat_meta_box()
{
add_meta_box(
'tchat',
'Chat',
array( $this, 'getMessages' ),
'project',
'normal',
'default',
[
'post_id' => get_post()->ID,
'user_id' => get_current_user_id(),
]
);
}
function getMessages( $post = null, $metabox = null )
{
global $wpdb;
$post_id = $metabox === null ? $_POST['post_id'] : $metabox['args']['post_id'];
$messages = $wpdb->get_results(
"SELECT * FROM {$wpdb->prefix}tchat_project WHERE post_id = '$post_id'"
, ARRAY_A);
ob_start();
include 'tchatView.php';
echo ob_get_clean();
wp_die();
}
}
const $ = jQuery;
$(document).ready(function ()
{
$("#sendMessageBtn").click(function(){
sendMessage();
});
var timer = setInterval(getMessages,5000);
});
function getMessages()
{
console.log("5s");
let message = $("#message");
$.post({
url : tchatVar.ajaxurl,
dataType: "text",
data : {
action : "getMessages",
nonce : tchatVar.nonce,
message : message.val(),
post_id: $("#sendMessageBtn").attr("data-id")
},
success : function ( response )
{
$("#tchatContent").remove();
$(".tchat").prepend($.parseHTML(response)[0].children.tchatContent);
},
error : function ( response )
{
console.log("error");
console.log(response);
}
});
}
function sendMessage()
{
let message = $("#message");
console.log("toto");
$.post({
url : tchatVar.ajaxurl,
dataType: "json",
data : {
action : "saveMessage",
nonce : tchatVar.nonce,
message : message.val(),
post_id: $("#sendMessageBtn").attr("data-id")
},
success : function ( response )
{
$("#tchatContent").append(
"<p>" + tchatVar.userName + " : " + message.val() + "</p>"
);
message.val('');
},
error : function ( response )
{
console.log("error");
console.log(response);
}
});
}
<div class="tchat">
<div class="" id="tchatContent">
<?php
foreach ( $messages as $message ):
?>
<p><?= get_user_by( 'ID', $message['user_id'] )->user_login . ' : ' . $message['message'] ?></p>
<?php
endforeach;
?>
</div>
<input type="text" name="message" id="message" value="">
<input type="button" value="Envoyer" id="sendMessageBtn" data-id="<?= get_post()->ID ?>">
</div>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment