<?php require_once 'viewmodels/Profile_ViewModel.php'; require_once 'viewmodels/Error_ViewModel.php'; require_once 'data/UserSession.php'; require_once 'data/data.php'; require_once 'utils/http.php'; require_once 'utils/string.php'; $session = UserSession::getInstance(); $data = MorpheusPetsData::getInstance(); // Get logged in user $loggedInUser = $session->getLoggedInUser(); // Username searched $usernameSearched = StringUtils::sanitize($_GET['search']); //Get profile's user $profileUser = $data->getUserByUserName($usernameSearched); if (!is_null($profileUser)) { $profileId = $profileUser->getId(); HTTPUtils::my_http_redirect("my_profile.php?profileId={$profileId}"); } else { $ret = $data->getLikeUsers($usernameSearched); if (!empty($ret)) { $viewModel = new Profile_ViewModel($loggedInUser, $profileUser); $viewModel->renderSearchResults($ret, $usernameSearched); } else { //Setup view model $viewModel = new Error_ViewModel(); $viewModel->renderFailSearch($usernameSearched); } }
<?php require_once 'viewmodels/PetViewer_ViewModel.php'; require_once 'viewmodels/Error_ViewModel.php'; require_once 'data/UserSession.php'; require_once 'data/data.php'; // GET keys $pet_id_key = "pet_id"; $session = UserSession::getInstance(); $data = MorpheusPetsData::getInstance(); $pet_id = null; $pet_to_show = null; // Get a pet if a pet id was specified if (isset($_GET[$pet_id_key])) { $pet_id = intval($_GET[$pet_id_key]); $pet_to_show = $data->getPet($pet_id); } if ($pet_to_show !== null) { // Create view model $view_model = new PetViewer_ViewModel($pet_to_show); // Add logged in user if such exists if ($session->isUserLoggedIn()) { $logged_in_user = $session->getLoggedInUser(); $view_model->setLoggedInUser($logged_in_user); } $view_model->renderPet(); } else { // Pet does not exist $view_model = new Error_ViewModel(); $view_model->renderPetNotExist($pet_id); }
<?php require_once 'viewmodels/Battle_ViewModel.php'; require_once 'viewmodels/Error_ViewModel.php'; require_once 'data/UserSession.php'; require_once 'data/data.php'; require_once 'utils/http.php'; $session = UserSession::getInstance(); $data = MorpheusPetsData::getInstance(); // Get logged in user $loggedInUser = $session->getLoggedInUser(); // Get profile id if (!is_null($loggedInUser)) { // Setup view model $viewModel = new Battle_ViewModel($loggedInUser); $viewModel->renderBattle(); } else { //Setup view model $viewModel = new Error_ViewModel(); $viewModel->renderUserNotExist($loggedInUser->getID()); }
HTTPUtils::my_http_redirect('index.php'); } // POST keys $pet_id_key = 'pet_id'; // Form inputs $form_pet_id = empty($_POST[$pet_id_key]) ? null : intval(StringUtils::sanitize($_POST[$pet_id_key])); $data = MorpheusPetsData::getInstance(); // Get logged in user $logged_in_user = $session->getLoggedInUser(); // Get pet to edit $pet_to_edit = $form_pet_id === null ? null : $data->getPet($form_pet_id); // Check if pet was found if ($pet_to_edit === null) { // Pet does not exist $view_model = new Error_ViewModel(); $view_model->renderPetNotExist($form_pet_id); } elseif (!PetUtils::userCanEditPet($logged_in_user, $pet_to_edit)) { // Pet does not belong to logged in user $view_model = new Error_ViewModel(); $view_model->renderEditPetNotAllowed($pet_to_edit); } else { // Delete pet if ($data->deletePet($pet_to_edit->getId())) { // Redirect to profile page HTTPUtils::my_http_redirect("my_profile.php?profileId=" . $logged_in_user->getId()); } else { // Pet could not be deleted $view_model = new Error_ViewModel(); $view_model->renderDeletePetFailed($pet_to_edit); } }
} // POST keys $pet_id_key = 'pet_id'; // Form inputs $form_pet_id = empty($_POST[$pet_id_key]) ? null : intval(StringUtils::sanitize($_POST[$pet_id_key])); $data = MorpheusPetsData::getInstance(); // Get logged in user $logged_in_user = $session->getLoggedInUser(); // Get pet to edit $pet_to_edit = $form_pet_id === null ? null : $data->getPet($form_pet_id); // Check if pet was found if ($pet_to_edit === null) { // Pet does not exist $view_model = new Error_ViewModel(); $view_model->renderPetNotExist($form_pet_id); } elseif (!PetUtils::userCanEditPet($logged_in_user, $pet_to_edit)) { // Pet does not belong to logged in user $view_model = new Error_ViewModel(); $view_model->renderEditPetNotAllowed($pet_to_edit); } else { // Pet exists and can be edited // Get species list $species_list = $data->getAllSpecies(); // Setup view model $viewModel = new PetEditor_ViewModel($logged_in_user); $viewModel->setPetToEdit($pet_to_edit); foreach ($species_list as $species) { $viewModel->addSelectableSpecies($species); } $viewModel->renderEditPet(); }
<?php require_once 'viewmodels/Error_ViewModel.php'; require_once 'data/UserSession.php'; require_once 'data/data.php'; require_once 'utils/http.php'; require_once 'utils/string.php'; $session = UserSession::getInstance(); $data = MorpheusPetsData::getInstance(); // Get logged in user $loggedInUser = $session->getLoggedInUser(); $edited_description = StringUtils::sanitize($_POST['description-text']); $profileId = $_POST['profile-user']; $profileUser = $data->getUser($profileId); if ($loggedInUser->getId() == $profileUser->getId() && !is_null($loggedInUser)) { $data->updateUser($profileUser, $edited_description); HTTPUtils::my_http_redirect("my_profile.php?profileId={$profileId}"); } else { //Setup view model $viewModel = new Error_ViewModel(); $viewModel->renderUserNotExist($profileId); }
$active_pets = []; foreach ($chosen_petIds as $pet_id) { array_push($active_pets, $data->getPet($pet_id)); } // Check if logged in user is the profile user, if the user is logged in, or they chose 3 active pets if ($loggedInUser->getId() == $profileUser->getId() && !is_null($loggedInUser) && sizeof($active_pets) == 3) { /** @var array $pet_collection */ $pet_collection = $data->getAllPetsForUser($profileId); /** @var Pet $pet */ foreach ($pet_collection as $pet) { if (in_array($pet, $active_pets)) { // If the pet is meant to be active $pet->setActive(true); } else { $pet->setActive(false); } $data->updatePet($pet); } HTTPUtils::my_http_redirect("my_profile.php?profileId={$profileId}"); } else { //Setup view model $viewModel = new Error_ViewModel(); if (sizeof($active_pets) != 3) { $viewModel->renderEditActivePetsNotAllowed($profileUser->getId()); } } } else { //Setup view model $viewModel = new Error_ViewModel(); $viewModel->renderNoPetsToSet(); }