Esempio n. 1
0
 public static function addThread($sender_id, $receiver_id, $message)
 {
     // Get max thread_id
     $thread_id = self::max('thread_id') + 1;
     if (empty($thread_id) || empty($sender_id) || empty($receiver_id) || empty($message)) {
         return;
     }
     DB::beginTransaction();
     try {
         // Insert sender to recipient table
         if (!MessageRecipients::addSender($thread_id, $sender_id)) {
             throw new \Exception('Can\'t sent message from user');
         }
         // Insert receiver to
         if (!MessageRecipients::addReceiver($thread_id, $receiver_id)) {
             throw new \Exception('Can\'t sent message to receiver');
         }
         // Insert new message thread
         $insert = new self();
         $insert->thread_id = $thread_id;
         $insert->sender_id = $sender_id;
         $insert->message = $message;
         $result = $insert->save();
         if (!$result) {
             throw new \Exception('Can\'t sent message');
         }
         // Add notifications
         $notify = Notification::add($receiver_id, $result->id, $sender_id, 'messages', 'new_message');
         if (!$notify) {
             throw new \Exception('Can\'t add message notification');
         }
     } catch (Exception $ex) {
         DB::rollback();
         throw $ex;
     }
     DB::commit();
 }