asdlkif
This commit is contained in:
@@ -10,6 +10,15 @@ class MessageController extends Controller
|
||||
Auth::checkAuthentication();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the request is an AJAX request
|
||||
*/
|
||||
private function isAjaxRequest()
|
||||
{
|
||||
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
|
||||
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to a specific user via URL parameters
|
||||
* URL format: message/send/{receiver_id}/{subject}/{message}
|
||||
@@ -23,6 +32,13 @@ class MessageController extends Controller
|
||||
$message = isset($_POST['message']) ? $_POST['message'] : null;
|
||||
|
||||
if (!$receiver_id || !$message) {
|
||||
// Return JSON for AJAX requests
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['success' => false, 'message' => 'Receiver and message are required']);
|
||||
return;
|
||||
}
|
||||
|
||||
Session::add('feedback_negative', 'Receiver and message are required');
|
||||
Redirect::to('message');
|
||||
return;
|
||||
@@ -32,6 +48,18 @@ class MessageController extends Controller
|
||||
$sender_id = Session::get('user_id');
|
||||
$success = MessageModel::sendToUser($sender_id, $receiver_id, $subject, $message);
|
||||
|
||||
// Return JSON for AJAX requests
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
if ($success) {
|
||||
echo json_encode(['success' => true, 'message' => 'Message sent successfully']);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Failed to send message']);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Regular request handling
|
||||
if ($success) {
|
||||
Session::add('feedback_positive', 'Message sent successfully');
|
||||
} else {
|
||||
@@ -150,6 +178,64 @@ class MessageController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle reply to a message
|
||||
*/
|
||||
public function reply()
|
||||
{
|
||||
// Always return JSON for this endpoint
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Start output buffering to catch any accidental output
|
||||
ob_start();
|
||||
|
||||
try {
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid request method']);
|
||||
exit();
|
||||
}
|
||||
|
||||
$receiver_id = isset($_POST['receiver_id']) ? $_POST['receiver_id'] : null;
|
||||
$message = isset($_POST['message']) ? $_POST['message'] : null;
|
||||
|
||||
if (!$receiver_id || !$message) {
|
||||
echo json_encode(['success' => false, 'message' => 'Receiver and message are required']);
|
||||
exit();
|
||||
}
|
||||
|
||||
$sender_id = Session::get('user_id');
|
||||
if (!$sender_id) {
|
||||
echo json_encode(['success' => false, 'message' => 'Not logged in']);
|
||||
exit();
|
||||
}
|
||||
|
||||
// Send the message (using sendToUser without subject)
|
||||
$success = MessageModel::sendToUser($sender_id, $receiver_id, 'Re: Message', $message);
|
||||
|
||||
if ($success) {
|
||||
echo json_encode(['success' => true, 'message' => 'Reply sent successfully']);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Failed to send reply']);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
// Catch any PHP errors
|
||||
echo json_encode(['success' => false, 'message' => 'Server error: ' . $e->getMessage()]);
|
||||
}
|
||||
|
||||
// Clean any output buffer and exit
|
||||
ob_end_clean();
|
||||
exit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Show global chat interface
|
||||
*/
|
||||
public function global()
|
||||
{
|
||||
// Redirect to main messages page with global chat hash
|
||||
Redirect::to('message#load-global');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the messenger interface
|
||||
*/
|
||||
@@ -189,13 +275,95 @@ class MessageController extends Controller
|
||||
return;
|
||||
}
|
||||
|
||||
// Redirect to main messages page with conversation hash
|
||||
Redirect::to('message#load-conversation-' . $other_user_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get conversation messages as JSON (AJAX endpoint)
|
||||
*/
|
||||
public function getConversationMessages()
|
||||
{
|
||||
$user_id = Session::get('user_id');
|
||||
$url_parts = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
|
||||
$other_user_id = isset($url_parts[2]) ? $url_parts[2] : null;
|
||||
|
||||
if (!$other_user_id) {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['success' => false, 'message' => 'Missing user ID']);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get messages
|
||||
$messages = MessageModel::getMessagesWithUser($user_id, $other_user_id);
|
||||
|
||||
$this->View->render('message/conversation', array(
|
||||
'messages' => $messages,
|
||||
'other_user' => $other_user
|
||||
));
|
||||
// Mark messages as read when loading the conversation
|
||||
MessageModel::markAsRead($user_id, $other_user_id);
|
||||
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(['success' => true, 'messages' => $messages]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get global chat messages as JSON (AJAX endpoint)
|
||||
*/
|
||||
public function getGlobalMessages()
|
||||
{
|
||||
// Always return JSON for this endpoint
|
||||
header('Content-Type: application/json');
|
||||
|
||||
$messages = MessageModel::getGlobalMessages();
|
||||
echo json_encode(['success' => true, 'messages' => $messages]);
|
||||
|
||||
// Stop any further execution
|
||||
exit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Send message to global chat
|
||||
*/
|
||||
public function sendToGlobal()
|
||||
{
|
||||
// Always return JSON for this endpoint
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Start output buffering to catch any accidental output
|
||||
ob_start();
|
||||
|
||||
try {
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid request method']);
|
||||
exit();
|
||||
}
|
||||
|
||||
$message = isset($_POST['message']) ? $_POST['message'] : null;
|
||||
$sender_id = Session::get('user_id');
|
||||
|
||||
if (!$message) {
|
||||
echo json_encode(['success' => false, 'message' => 'Message is required']);
|
||||
exit();
|
||||
}
|
||||
|
||||
if (!$sender_id) {
|
||||
echo json_encode(['success' => false, 'message' => 'Not logged in']);
|
||||
exit();
|
||||
}
|
||||
|
||||
$success = MessageModel::sendToGlobal($sender_id, $message);
|
||||
|
||||
if ($success) {
|
||||
echo json_encode(['success' => true, 'message' => 'Message sent to global chat']);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Failed to send message']);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
// Catch any PHP errors
|
||||
echo json_encode(['success' => false, 'message' => 'Server error: ' . $e->getMessage()]);
|
||||
}
|
||||
|
||||
// Clean any output buffer and exit
|
||||
ob_end_clean();
|
||||
exit();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
/**
|
||||
* The note controller: Just an example of simple create, read, update and delete (CRUD) actions.
|
||||
*/
|
||||
require_once __DIR__ . '/../libs/SimpleMarkdown.php';
|
||||
|
||||
class NoteController extends Controller
|
||||
{
|
||||
/**
|
||||
@@ -29,6 +31,23 @@ class NoteController extends Controller
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get note as JSON (AJAX endpoint)
|
||||
*/
|
||||
public function getNote($note_id)
|
||||
{
|
||||
$note = NoteModel::getNote($note_id);
|
||||
|
||||
header('Content-Type: application/json');
|
||||
if ($note) {
|
||||
// Add markdown version
|
||||
$note->note_html = SimpleMarkdown::parse($note->note_text);
|
||||
echo json_encode(['success' => true, 'note' => $note]);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Note not found']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method controls what happens when you move to /dashboard/create in your app.
|
||||
* Creates a new note. This is usually the target of form submit actions.
|
||||
@@ -36,7 +55,18 @@ class NoteController extends Controller
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
NoteModel::createNote(Request::post('note_text'));
|
||||
$success = NoteModel::createNote(Request::post('note_text'));
|
||||
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
if ($success) {
|
||||
echo json_encode(['success' => true, 'message' => 'Note created successfully']);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Failed to create note']);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Redirect::to('note');
|
||||
}
|
||||
|
||||
@@ -59,7 +89,18 @@ class NoteController extends Controller
|
||||
*/
|
||||
public function editSave()
|
||||
{
|
||||
NoteModel::updateNote(Request::post('note_id'), Request::post('note_text'));
|
||||
$success = NoteModel::updateNote(Request::post('note_id'), Request::post('note_text'));
|
||||
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
if ($success) {
|
||||
echo json_encode(['success' => true, 'message' => 'Note updated successfully']);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Failed to update note']);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Redirect::to('note');
|
||||
}
|
||||
|
||||
@@ -71,7 +112,27 @@ class NoteController extends Controller
|
||||
*/
|
||||
public function delete($note_id)
|
||||
{
|
||||
NoteModel::deleteNote($note_id);
|
||||
$success = NoteModel::deleteNote($note_id);
|
||||
|
||||
if ($this->isAjaxRequest()) {
|
||||
header('Content-Type: application/json');
|
||||
if ($success) {
|
||||
echo json_encode(['success' => true, 'message' => 'Note deleted successfully']);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => 'Failed to delete note']);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
Redirect::to('note');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the request is an AJAX request
|
||||
*/
|
||||
private function isAjaxRequest()
|
||||
{
|
||||
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
|
||||
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user