From a5c1f7c57572081130a0a48500cbfe28b8cdea62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 13 Dec 2011 23:02:00 +0100 Subject: [PATCH] Filling ChatForm with life --- file/lib/form/ChatForm.class.php | 92 +++++++++++++++++++++++++++++++- file/lib/page/ChatPage.class.php | 3 ++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/file/lib/form/ChatForm.class.php b/file/lib/form/ChatForm.class.php index f83a6e6..1981c90 100644 --- a/file/lib/form/ChatForm.class.php +++ b/file/lib/form/ChatForm.class.php @@ -1,5 +1,12 @@ readUserData(); + parent::readData(); + } + + /** + * @see \wcf\form\AbstractForm::readFormParameters() + */ + public function readFormParameters() { + parent::readFormParameters(); + + if (isset($_REQUEST['text'])) $this->message = StringUtil::trim($_REQUEST['text']); + } + + /** + * Reads user data. + */ + public function readUserData() { + // TODO: Move this into ChatUtil + $ush = UserStorageHandler::getInstance(); + $packageID = PackageDependencyHandler::getPackageID('timwolla.wcf.chat'); + + // load storage + $ush->loadStorage(array(WCF::getUser()->userID), $packageID); + $data = $ush->getStorage(array(WCF::getUser()->userID), 'color', $packageID); + + if ($data[WCF::getUser()->userID] === null) { + // set defaults + $data[WCF::getUser()->userID] = array(1 => 0xFF0000, 2 => 0x00FF00); // TODO: Change default values + $ush->update(WCF::getUser()->userID, 'color', serialize($data[WCF::getUser()->userID]), $packageID); + } + else { + // load existing data + $data[WCF::getUser()->userID] = unserialize($data[WCF::getUser()->userID]); + } + + $this->userData['color'] = $data[WCF::getUser()->userID]; + + $data = $ush->getStorage(array(WCF::getUser()->userID), 'roomID', $packageID); + $this->userData['roomID'] = $data[WCF::getUser()->userID]; + } + + /** + * @see \wcf\form\AbstractForm::validate() + */ + public function validate() { + parent::validate(); + if ($this->message === '') { + throw new UserInputException('text'); + } + if ($this->userData['roomID'] === null) { + throw new PermissionDeniedException(); + } + } + + /** + * @see \wcf\form\AbstractForm::save() + */ + public function save() { + parent::save(); + + $messageAction = new chat\message\ChatMessageAction(array(), 'create', array( + 'data' => array( + 'roomID' => $this->userData['roomID'], + 'sender' => WCF::getUser()->userID, + 'username' => WCF::getUser()->username, + 'time' => TIME_NOW, + 'type' => chat\message\ChatMessage::TYPE_NORMAL, + 'message' => $this->message, + 'color1' => $this->userData['color'][1], + 'color2' => $this->userData['color'][2] + ) + )); + $messageAction->executeAction(); + + $this->saved(); + } } diff --git a/file/lib/page/ChatPage.class.php b/file/lib/page/ChatPage.class.php index 730d6da..9f92903 100644 --- a/file/lib/page/ChatPage.class.php +++ b/file/lib/page/ChatPage.class.php @@ -148,6 +148,7 @@ public function readRoom() { * Reads user data. */ public function readUserData() { + // TODO: Move this into ChatUtil $ush = UserStorageHandler::getInstance(); $packageID = PackageDependencyHandler::getPackageID('timwolla.wcf.chat'); @@ -166,6 +167,8 @@ public function readUserData() { } $this->userData['color'] = $data[WCF::getUser()->userID]; + + $ush->update(WCF::getUser()->userID, 'roomID', $this->room->roomID, $packageID); } /**