public function acceptFriendRequest($token, $requestId) { $user = new User(); $meId = $user->getMeId($token); try { $requestData = Capsule::table('friendrequests')->select('userFrom', 'userTo')->where('id', $requestId)->first(); if ($requestData->userFrom == $meId) { $newFriendId = $requestData->userTo; } else { $newFriendId = $requestData->userFrom; } } catch (\Exception $e) { throw new \Exception('Wystąpił błąd przy pobieraniu przyjaciół'); } if ($user->isUserMyFriend($newFriendId, $token)) { throw new \Exception('Ten użytkownik jest już Twoim znajomym'); } try { Capsule::table('friendrequests')->where('id', $requestId)->where('userFrom', $meId)->orWhere('userTo', $meId)->where('accepted', 0)->update(array('accepted' => 1)); Capsule::table('friends')->insert(array('userA' => $meId, 'userB' => $newFriendId)); } catch (\Exception $e) { throw new \Exception('Wystąpił błąd przy pobieraniu przyjaciół'); } $newFriendData = $user->getUser($newFriendId, $token); return $newFriendData; }
public function newTopic($userId, $token) { $user = new User(); $meId = $user->getMeId($token); $conversation = $this->isConversationBetweenUsers($userId, $meId); if (!$conversation) { try { $conversation = Capsule::table('messagestopics')->insertGetId(array('userFrom' => $meId, 'userTo' => $userId, 'name' => '')); } catch (\Exception $e) { throw new \Exception('Problem z inicjacją czatu'); } } return $conversation; }
public function getNotifications($token) { $user = new User(); $userId = $user->getMeId($token); $array = array(); try { $notifications = Capsule::table('friendrequests')->select('*')->where('userTo', $userId)->where('accepted', 0)->get(); } catch (\Exception $e) { throw new \Exception('Problem z probraniem wiadmości'); } foreach ($notifications as $notification) { if ($notification->userFrom == $userId) { $notification->firstName = $user->getUser($notification->userTo, $token)->firstName; } array_push($array, $notification); } return $array; }
public function deleteRegister($token, $id) { $user = new User(); if ($this->getNumberOfRegisterDocuments($id) > 0) { throw new \Exception("Nie można usunąć rejestru, gdyż znajdują się w nim wprowadzone dokumenty."); } if (!$user->isUserHasRole($token, 'delete:all:register')) { if (!$user->isUserHasRole($token, 'delete:my:register')) { throw new \Exception("Brak uprawnień"); } else { if (!$user->isUserBelongsToUnit($user->getMeId($token), $this->getRegisterUnitId($id))) { throw new \Exception("Brak uprawnień"); } } } try { Capsule::table('registers')->where('id', $id)->delete(); } catch (\Exception $e) { throw new \Exception('Problem z usunięciem rejestru'); } return true; }