public function indexAction() { if ($this->request->isAjax() && $this->request->getPost('chat_list') == 'y') { $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW); } // $this->view->disable(); if ($this->session->has('user_id')) { $user_id = $this->session->get('user_id'); } ///////////////// Пагинация if (!empty($_POST['page'])) { $currentPage = $_POST["page"]; } else { $currentPage = 1; } $paginator = new Phalcon\Paginator\Adapter\Model(array("data" => Chat::find(array('order' => 'creation_date DESC')), "limit" => 10, "page" => $currentPage)); $page = $paginator->getPaginate(); foreach ($page->items as $chat) { $chats[$chat->id] = $chat->toArray(); $rt = $chat->messagechat->getLast(); if (!is_bool($rt)) { //$this->elements->var_print(is_bool($rt)); $chat_s[$chat->id] = $rt->text; // $chat_s[$chat->id] = false; } } // echo '<pre>'; print_r($mes); echo '</pre>'; $this->view->setVars(array('chat_col' => $chat->count(), 'chats' => $chats, 'chat_s' => $cha = isset($chat_s) ? $chat_s : false, 'page_num' => $page->current, 'page_total' => $page->total_pages)); }
/** * Display the specified resource. * * @param int $id * @return Response */ public function show($id) { $chat = Chat::find($id); if (!isset($chat)) { return Redirect::to('/community/chats')->with('flash_chat', 'That chat doesn\'t exist!')->with('alert_class', 'alert-danger'); } $users = $chat->user->all(); //If the user isn't part of the conversation, don't let them view it $inArray = false; foreach ($users as $user) { if ($user->id == Auth::user()->id) { $inArray = true; } } if (!$inArray) { return Redirect::to('/community/chats')->with('flash_chat', 'You don\'t have permission to view this chat')->with('alert_class', 'alert-danger'); } $replies = Reply::where('chat_id', '=', $chat->id)->get(); foreach ($replies as $reply) { $notification = Notification::where('user_id', '=', Auth::user()->id)->where('reply_id', '=', $reply->id)->firstOrFail(); $notification->has_read = 1; $notification->save(); } return View::make('readOneChat')->with(array('chat' => $chat, 'users' => $chat->getOtherUsers(), 'replies' => $replies)); }
public function retrieveTypingStatus() { $username = Input::get('username'); $chat = Chat::find(1); if ($chat->user1 == $username) { if ($chat->user2_is_typing) { return $chat->user2; } } else { if ($chat->user1_is_typing) { return $chat->user1; } } }
public function getChatWithUser($userId) { $result = null; if (isset($this->id) && (int) $this->id > 0) { $data = DB::select('select c.chat_id from chats_members c where c.chat_id in (select b.chat_id from chats_members b where b.chat_id in (select a.chat_id from chats_members a where a.user_id = ?) group by b.chat_id having count(b.user_id) = 2) and c.user_id = ?;', array($this->id, $userId)); if (count($data) > 0) { $result = Chat::find($data[0]->chat_id); } } return $result; }
/** * Update the chat info * * @param int|null $chatId Chat ID * @return Response */ public function update($chatId = null) { $user = Auth::user(); if ((int) $chatId > 0 && !is_null($chat = Chat::find((int) $chatId)) && $chat->hasMember(Auth::user()->id)) { if ((bool) Input::get('mark_as_read')) { $this->markAsRead($chat, $user); } if (strlen($newTopic = (string) Input::get('topic')) > 0) { $chat->topic = $newTopic; $chat->save(); foreach ($chat->getMembersTokens() as $token) { $state = new StateSender($token); $state->setChatAsUpdated((int) $chatId, (int) Auth::user()->id); $state->send(); } } if (Input::has('typing')) { foreach ($chat->getMembersTokens() as $token) { $state = new StateSender($token); $state->setChatAsTyping($chatId, Auth::user()->id, (bool) Input::get('typing')); $state->send(); } } if (Input::has('delivered')) { $this->delivered($chat); } return $this->respondNoContent(); } else { return $this->respondWithError('Chat doesn\'t exist'); } }
public function newMessages($chatId, $messageId, $size = 20) { $chat = Chat::find($chatId); if (!$chat) { return $this->respondNotFound(); } if (!Message::find($messageId)) { return $this->respondNotFound(); } return $this->respond(['messages' => $chat->messages()->where('id', '>', $messageId)->take($size)->get()->transform(function ($message) { return $message->getAsArray(); })]); }