Exemplo n.º 1
0
 /**
  * Creates or updates the message and returns it.
  *
  * @param WiseChatMessage $message
  *
  * @return WiseChatMessage
  * @throws Exception On validation error
  */
 public function save($message)
 {
     global $wpdb;
     // low-level validation:
     if ($message->getTime() === null) {
         throw new Exception('Time cannot be null');
     }
     if ($message->getUserId() === null) {
         throw new Exception('User ID cannot be null');
     }
     if ($message->getUserName() === null) {
         throw new Exception('Username cannot be null');
     }
     if ($message->getText() === null) {
         throw new Exception('Text cannot be null');
     }
     if ($message->getChannelName() === null) {
         throw new Exception('Channel name cannot be null');
     }
     // prepare user data:
     $columns = array('time' => $message->getTime(), 'admin' => $message->isAdmin() ? 1 : 0, 'user' => $message->getUserName(), 'chat_user_id' => $message->getUserId(), 'text' => $message->getText(), 'channel' => $message->getChannelName(), 'ip' => $message->getIp());
     // update or insert:
     if ($message->getId() !== null) {
         $columns['user_id'] = $message->getWordPressUserId();
         $wpdb->update($this->table, $columns, array('id' => $message->getId()), '%s', '%d');
     } else {
         if ($message->getWordPressUserId() > 0) {
             $columns['user_id'] = $message->getWordPressUserId();
         }
         $wpdb->insert($this->table, $columns);
         $message->setId($wpdb->insert_id);
     }
     return $message;
 }