* @copyright Copyright (C) 2018-2025, Frédéric Tempez * @license CC Attribution-NonCommercial-NoDerivatives 4.0 International * @link http://zwiicms.fr/ */ class chat extends common { const VERSION = '0.1'; const REALNAME = 'Chat'; const DELETE = true; const UPDATE = '0.0'; const DATADIRECTORY = ''; // Contenu localisé inclus par défaut (page.json et module.json) public static $actions = [ //'add' => self::GROUP_MODERATOR, //'config' => self::GROUP_MODERATOR, //'edit' => self::GROUP_MODERATOR, 'index' => self::GROUP_VISITOR, 'getFormattedMessages' => self::GROUP_VISITOR, ]; public static $messages = []; public function index(): void { if ($this->isPost()) { $message = filter_input(INPUT_POST, 'message'); if ($message) { $timestamp = time(); $userId = $this->getUser('id'); $chatData = $this->getData(['module', $this->getUrl(2), 'chat']) ?? []; $chatData[$timestamp] = [ 'user_id' => $userId, 'message' => $message ]; $this->setData(['module', $this->getUrl(2), 'chat'], $chatData); if ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest' ) { echo json_encode(['success' => true]); exit; } } } self::$messages = $this->getFormattedMessages(); // Valeurs en sortie $this->addOutput([ 'title' => 'ChatRoom', 'view' => 'index' ]); } private function getFormattedMessages() { $chatData = $this->getData(['module', $this->getUrl(2), 'chat']) ?? []; $messages = []; krsort($chatData); foreach ($chatData as $timestamp => $data) { $messages[] = [ 'timestamp' => $timestamp, 'user_id' => $data['user_id'], 'username' => $this->getUser('id'), 'message' => $data['message'] ]; } return $messages; } }