Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // TODO global.php MUST BE PLACED ONE FILE LEVEL ABOVE TO FUNCTION
- include_once '../global.php';
- // get the identifier for the page we want to load
- $action = $_GET['action'];
- // instantiate a controller and route it
- $sc = new Index();
- $sc->route($action);
- class Index{
- // route us to the appropriate class method for this action
- public function route($action) {
- switch($action) {
- // home page, login, logout
- case 'home':
- $this->home();
- $this->getTemp('London');
- break;
- case 'login':
- $this->login();
- break;
- case 'loginSubmit':
- $username = $_POST['username'];
- $password = $_POST['pw'];
- $this -> loginSubmit($username, $password);
- break;
- case 'logout':
- $this -> logout();
- break;
- case 'register':
- $this -> register();
- break;
- case 'confirm_reg':
- $this -> confirm_reg();
- break;
- // add location/people
- case 'add_loc':
- $this->add_loc();
- break;
- case 'confirm_loc':
- $this->confirm_loc();
- break;
- case 'add_person':
- $this->add_person();
- break;
- case 'confirm_person':
- $this->confirm_person();
- break;
- // display/edit people
- case 'view_person':
- $name = $_GET['name'];
- $this->view_person($name);
- break;
- case 'edit_person':
- $name = $_GET['name'];
- $this->edit_person($name);
- break;
- case 'edit_person_complete':
- $name = $_GET['name'];
- $this->edit_person_complete($name);
- break;
- case 'person_article_complete':
- $name = $_GET['name'];
- $this->person_article_complete($name);
- break;
- case 'person_delete':
- $name = $_GET['name'];
- $this->person_delete($name);
- break;
- // display/edit locations
- case 'view_loc':
- $name = $_GET['name'];
- $this->view_loc($name);
- break;
- case 'edit_loc':
- $name = $_GET['name'];
- $this->edit_loc($name);
- break;
- case 'edit_loc_complete':
- $name = $_GET['name'];
- $this->edit_loc_complete($name);
- break;
- case 'loc_article_complete':
- $name = $_GET['name'];
- $this->loc_article_complete($name);
- break;
- case 'loc_delete':
- $name = $_GET['name'];
- $this->loc_delete($name);
- break;
- // list view
- case 'list_locations':
- $this->list_locations();
- break;
- case 'list_people':
- $this->list_people();
- break;
- case 'list_community':
- $this->list_community();
- break;
- // profiles
- case 'view_profile':
- $name = $_GET['name'];
- $this -> view_profile($name);
- break;
- case 'edit_profile':
- $name = $_GET['name'];
- $this -> edit_profile($name);
- break;
- case 'edit_profile_complete':
- $name = $_GET['name'];
- $this -> edit_profile_complete($name);
- break;
- case 'follow':
- $name = $_GET['name'];
- $this -> follow($name);
- break;
- case 'unfollow':
- $name = $_GET['name'];
- $this -> unfollow($name);
- break;
- }
- }
- //////////////////////////////////////////////////////////////////////////////////
- public function home() {
- $pageTitle = 'Home';
- include_once SYSTEM_PATH.'/view/header.tpl';
- $test = $this->getTemp('Aliceville');
- include_once SYSTEM_PATH.'/view/home.tpl';
- // TODO: AJAX widget?
- // echo '<p style="margin-right: 10%; font-size:large;"> Current Weather in Aliceville: '.$test['description'].'</p>';
- // TODO: pull data and put into feed
- if(!isset($_SESSION['username'])){
- // add template here
- }
- include_once SYSTEM_PATH.'/view/footer.tpl';
- }
- public function login(){
- $pageTitle = 'Login';
- include_once SYSTEM_PATH.'/view/header.tpl';
- include_once SYSTEM_PATH.'/view/login.tpl';
- include_once SYSTEM_PATH.'/view/footer.tpl';
- }
- public function loginSubmit($un, $pw){
- $db = Db::instance(); // create db connection
- // build query
- $q = sprintf("SELECT * FROM User_Data WHERE username = %d;", $un);
- $result = $db->query($q); // execute query
- // make sure we found something
- if($result->num_rows == 0) {
- header('Location: '.BASE_URL.'/login');
- } else {
- $row = $result->fetch_assoc(); // get results as associative array
- $correctPassword = $row['password'];
- }
- if($pw == $correctPassword){
- $_SESSION['permissions'] = $row['permissions'];
- $_SESSION['username'] = $un;
- //add to activity feed
- $this->addActivity('login', 'login');
- header('Location: '.BASE_URL.'/community/'.$un);
- }
- else
- header('Location: '.BASE_URL.'/login');
- }
- public function logout(){
- // end session
- $this->addActivity('logout', 'logout');
- unset($_SESSION['username']);
- session_destroy();
- // return to home page
- header('Location: '.BASE_URL);
- }
- public function register(){
- $pageTitle = 'Register';
- include_once SYSTEM_PATH.'/view/header.tpl';
- include_once SYSTEM_PATH.'/view/signup.tpl';
- include_once SYSTEM_PATH.'/view/footer.tpl';
- }
- public function confirm_reg(){
- // get POST variables
- $username = $_POST['username_in'];
- $first_name = $_POST['first_name_in'];
- $last_name = $_POST['last_name_in'];
- $email = $_POST['email_in'];
- $password = $_POST['password_in'];
- $gender = $_POST['gender'];
- $permissions = 'user';
- // required fields
- if( empty($username) | empty($first_name) | empty($last_name) | empty($email) | empty($password) | empty($gender) ) {
- header('Location: '.BASE_URL.'/register/');
- }
- $user = new User();
- $user->username = $username;
- $user->first_name = $first_name;
- $user->last_name = $last_name;
- $user->email = $email;
- $user->password = $password;
- $user->gender = $gender;
- $user->permissions = $permissions;
- $memID = $user->u_save();
- header('Location: '.BASE_URL.'/login');
- }
- //////////////////////////////////////////////////////////////////////////////////
- public function add_person(){
- // can not add if not logged in
- if(!isset($_SESSION['username'])){
- header('Location: '.BASE_URL);
- }
- $pageTitle = "Add New Person";
- include_once SYSTEM_PATH.'/view/header.tpl';
- include_once SYSTEM_PATH.'/view/add_person.tpl';
- include_once SYSTEM_PATH.'/view/footer.tpl';
- }
- public function confirm_person(){
- // can not edit if not logged in
- if(!isset($_SESSION['username'])){
- header('Location: '.BASE_URL);
- }
- // get POST variables
- $first_name = $_POST['first_name_in']; // required
- $last_name = $_POST['last_name_in']; // required
- $date_captured = $_POST['dc_in'];
- $age = $_POST['age_in'];
- $profile_image = $_POST['profile_image_in'];
- $etc = $_POST['etc_in'];
- // name is required
- if( empty($name)) {
- header('Location: '.BASE_URL.'/add_person/');
- }
- $person = new Person();
- $person->first_name = $first_name;
- $person->last_name = $last_name;
- $person->date_captured = $date_captured;
- $person->age = $age;
- $person->profile_image = $profile_image;
- $person->etc = $etc;
- $memID = $person->p_save();
- header('Location: '.BASE_URL.'/people/'.$last_name);
- }
- public function add_loc(){
- // can not add if not logged in
- if(!isset($_SESSION['username'])){
- header('Location: '.BASE_URL);
- }
- $pageTitle = "Add New Location";
- // TODO post template here
- }
- public function confirm_loc(){
- // can not edit if not logged in
- if(!isset($_SESSION['username'])){
- header('Location: '.BASE_URL);
- }
- // get POST variables
- $name = $_POST['name_in']; // required
- $activities = $_POST['activities_in'];
- $staff = $_POST['staff_in'];
- $capacity = $_POST['capacity_in'];
- $cost = $_POST['cost_in'];
- $profile_image = $_POST['profile_image_in'];
- $brief_description = $_POST['brief_description'];
- // name is required
- if( empty($name)) {
- header('Location: '.BASE_URL.'/add_location/');
- }
- $place = new Location();
- $place->name = $name;
- $place->activities = $activities;
- $place->staff = $staff;
- $place->capacity = $capacity;
- $place->cost = $cost;
- $memID = $place->l_save();
- $name = ucwords(str_replace(" ", "_", $name));
- header('Location: '.BASE_URL.'/locations/'.$name);
- }
- //////////////////////////////////////////////////////////////////////////////////
- public function view_person($name){
- // check if valid, and post
- $person = Person::p_loadByID($name);
- if($person == null) {
- die('Invalid id');
- }
- $pageTitle = $person->first_name;
- $list = Article::getByTargetID($person->id, "People");
- include_once SYSTEM_PATH.'/view/header.tpl';
- include_once SYSTEM_PATH.'/view/person.tpl';
- include_once SYSTEM_PATH.'/view/footer.tpl';
- }
- public function edit_person($name){
- // can not edit if not logged in
- if(!isset($_SESSION['username'])){
- header('Location: '.BASE_URL);
- }
- // check if valid, and post
- $person = Person::p_loadByID($name);
- if($person == null) {
- die('Invalid name');
- }
- // title
- $pageTitle = "Edit $person->first_name";
- $list = Article::getByTargetID($person->id, "People");
- //templates
- include_once SYSTEM_PATH.'/view/header.tpl';
- include_once SYSTEM_PATH.'/view/edit_person.tpl';
- include_once SYSTEM_PATH.'/view/footer.tpl';
- }
- public function edit_person_complete($name){
- // can not edit if not logged in
- if(!isset($_SESSION['username'])){
- header('Location: '.BASE_URL);
- }
- // get POST variables
- $first_name = $_POST['first_name_in'];
- $last_name = $_POST['last_name_in'];
- $date_captured = $_POST['dc_in'];
- $age = $_POST['age_in'];
- $profile_image = $_POST['profile_image_in'];
- $etc = $_POST['etc_in'];
- $person = Person::p_loadByID($last_name);
- $person->first_name = $first_name;
- $person->date_captured = $date_captured;
- $person->age = $age;
- $person->profile_image = $profile_image;
- $person->etc = $etc;
- // Add to user's activity
- $this->addActivity($name, 'edit');
- $memID = $person->p_save();
- $name = ucwords(str_replace(" ", "_", $name));
- header('Location: '.BASE_URL.'/people/'.$name);
- }
- public function person_article_complete($name){
- // can not add article if not logged in
- if(!isset($_SESSION['username'])){
- header('Location: '.BASE_URL);
- }
- // get POST variables
- $target_id = $_POST['target_id_in'];
- $article_title = $_POST['article_title_in'];
- $article_text = $_POST['article_text_in'];
- $article_image = $_POST['article_image_in'];
- $article = new Article();
- $article->target_id = $target_id;
- $article->article_title = $article_title;
- $article->article_text = $article_text;
- $article->article_image = $article_image;
- $article->save("People");
- $name = ucwords(str_replace(" ", "_", $name));
- header('Location: '.BASE_URL.'/people/'.$name);
- }
- public function person_delete($name){
- // can not remove if not logged in
- if(!isset($_SESSION['username'])){
- header('Location: '.BASE_URL);
- }
- // check if valid
- $person = Person::loadByName($name);
- if($person == null) {
- die('Invalid family member name');
- }
- // remove all data sections
- $articles = Article::getByTargetID($person->id, "People");
- foreach($articles as $art){
- $art->remove();
- }
- // call remove function on member page
- $person->remove();
- $this->addActivity($name, 'delete');
- header('Location: '.BASE_URL.'/people');
- }
- //////////////////////////////////////////////////////////////////////////////////
- public function view_loc($name){
- // title
- $name = ucwords(str_replace("_", " ", $name));
- $pageTitle = $name;
- // check if valid, and post
- $place = Location::l_loadByName($name);
- if($place == null) {
- die('Invalid name');
- }
- include_once SYSTEM_PATH.'/view/header.tpl';
- include_once SYSTEM_PATH.'/view/location.tpl';
- include_once SYSTEM_PATH.'/view/footer.tpl';
- }
- public function edit_loc($name){
- // can not edit if not logged in
- if(!isset($_SESSION['username'])){
- header('Location: '.BASE_URL);
- }
- $name = ucwords(str_replace("_", " ", $name));
- $pageTitle = $name;
- // check if valid, and post
- $place = Location::l_loadByName($name);
- if($place == null) {
- die('Invalid name');
- }
- include_once SYSTEM_PATH.'/view/header.tpl';
- include_once SYSTEM_PATH.'/view/edit_location.tpl';
- include_once SYSTEM_PATH.'/view/footer.tpl';
- }
- public function edit_loc_complete($name){
- // can not edit if not logged in
- if(!isset($_SESSION['username'])){
- header('Location: '.BASE_URL);
- }
- // get POST variables
- $name = $_POST['name_in']; // required
- $activities = $_POST['activities_in'];
- $staff = $_POST['staff_in'];
- $capacity = $_POST['capacity_in'];
- $cost = $_POST['cost_in'];
- $profile_image = $_POST['profile_image_in'];
- $brief_description = $_POST['brief_description_in'];
- $name = ucwords(str_replace("_", " ", $name));
- $place = Location::l_loadByName($name);
- $place->name = $name;
- $place->activities = $activities;
- $place->staff = $staff;
- $place->capacity = $capacity;
- $place->cost = $cost;
- $place->profile_image = $profile_image;
- $place->brief_description = $brief_description;
- $memID = $place->l_save();
- $name = ucwords(str_replace(" ", "_", $name));
- header('Location: '.BASE_URL.'/locations/'.$name);
- }
- public function loc_article_complete($name){
- // can not add article if not logged in
- if(!isset($_SESSION['username'])){
- header('Location: '.BASE_URL);
- }
- // get POST variables
- $target_id = $_POST['target_id_in'];
- $article_title = $_POST['article_title_in'];
- $article_text = $_POST['article_text_in'];
- $article_image = $_POST['article_image_in'];
- $article = new Article();
- $article->target_id = $target_id;
- $article->article_title = $article_title;
- $article->article_text = $article_text;
- $article->article_image = $article_image;
- $article->save("Locations");
- $name = ucwords(str_replace(" ", "_", $name));
- header('Location: '.BASE_URL.'/people/'.$name);
- }
- public function loc_delete($name){
- // can not remove if not logged in
- if(!isset($_SESSION['username'])){
- header('Location: '.BASE_URL);
- }
- // check if valid
- $place = Location::loadByName($name);
- if($person == null) {
- die('Invalid family member name');
- }
- // remove all data sections
- $articles = Article::getByTargetID($person->id, "Locations");
- foreach($articles as $art){
- $art->remove();
- }
- // call remove function on member page
- $place->remove();
- $this->addActivity($name, 'delete');
- header('Location: '.BASE_URL.'/locations');
- }
- //////////////////////////////////////////////////////////////////////////////////
- public function list_people(){
- $pageTitle = "People";
- // get list of all persons and display
- $list = Person::getPeople();
- include_once SYSTEM_PATH.'/view/header.tpl';
- include_once SYSTEM_PATH.'/view/people.tpl';
- include_once SYSTEM_PATH.'/view/footer.tpl';
- }
- public function list_locations(){
- $pageTitle = "Locations";
- // get list of all Locations and display
- $list = Location::getLocations();
- include_once SYSTEM_PATH.'/view/header.tpl';
- include_once SYSTEM_PATH.'/view/locations.tpl';
- include_once SYSTEM_PATH.'/view/footer.tpl';
- }
- public function list_community(){
- $pageTitle = "Community";
- // get list of all persons and display
- $list = User::getUsers();
- include_once SYSTEM_PATH.'/view/header.tpl';
- include_once SYSTEM_PATH.'/view/community.tpl';
- include_once SYSTEM_PATH.'/view/footer.tpl';
- }
- //////////////////////////////////////////////////////////////////////////////////
- public function view_profile($name){
- // check if valid, and post
- $user = User::u_loadByName($name);
- $currentUser = $_SESSION['username'];
- if($user != null) {
- $logged_in = true;
- // TODO add content template
- } else {
- die('Invalid name');
- }
- $list = Follow::getByFollower($currentUser);
- $activity_feed = Activity::getActivitiesByUser($name);
- $following = false;
- foreach($list as $follow)
- {
- if ($follow->user_followee == $name)
- {
- $following = true;
- }
- }
- $pageTitle = $user->first_name."_".$user->last_name;
- include_once SYSTEM_PATH.'/view/header.tpl';
- include_once SYSTEM_PATH.'/view/profile.tpl';
- include_once SYSTEM_PATH.'/view/footer.tpl';
- }
- public function edit_profile($name){
- // check if valid, and post
- $user = User::u_loadByName($name);
- if($user != null) {
- // TODO add content template
- } else {
- die('Invalid name');
- }
- $pageTitle = "Edit Profile";
- include_once SYSTEM_PATH.'/view/header.tpl';
- include_once SYSTEM_PATH.'/view/edit_profile.tpl';
- include_once SYSTEM_PATH.'/view/footer.tpl';
- }
- public function edit_profile_complete($name){
- $user = User::u_loadByName($name);
- if($user == null) {
- die('Invalid name');
- }
- // TODO admin priviledges
- // can not edit if not logged in
- if($_SESSION['username'] != $user->username){
- header('Location: '.BASE_URL);
- }
- $corr_pw = $user->password;
- // get POST variables
- $old_pw = $_POST['old_pw_in'];
- $password = $_POST['new_pw_in'];
- $first_name = $_POST['first_name_in'];
- $last_name = $_POST['last_name_in'];
- $email = $_POST['email_in'];
- $gender = $_POST['gender'];
- echo $old_pw;
- echo $password;
- echo $first_name;
- echo $last_name;
- echo $email;
- echo $gender;
- if($old_pw != $corr_pw){
- header('Location: '.BASE_URL.'/community/'.$name.'/edit_profile');
- }
- if( empty($first_name) | empty($last_name) | empty($email) | empty($password) | empty($gender) ) {
- header('Location: '.BASE_URL.'/community/'.$name.'/edit_profile');
- }
- $user->first_name = $first_name;
- $user->last_name = $last_name;
- $user->email = $email;
- $user->password = $password;
- $user->gender = $gender;
- $memID = $user->u_update();
- $name = ucwords(str_replace(" ", "_", $name));
- $name = strtolower($name);
- $this->addActivity($name, 'edit');
- header('Location: '.BASE_URL.'/community/'.$name);
- }
- public function follow($name){
- /// get usernames for follower and folowee
- $follower = $_SESSION['username'];
- $followee = User::u_loadByName($name);
- $follow = new Follow();
- $follow->user_follower = $follower;
- $follow->user_followee = $followee->username;
- $follow->save();
- //TODO test this method
- // Add to user's activity
- $this->addActivity($name, 'follow');
- header('Location: '.BASE_URL.'/community/'.$name);
- }
- public function unfollow($name){
- // get usernames for follower and folowee
- $follower = $_SESSION['username'];
- $followee = User::u_loadByName($name);
- $follow = new Follow();
- $follow->user_follower = $follower;
- $follow->user_followee = $followee->username;
- $follow->remove();
- //TODO test this method
- // Add to user's activity
- $this->addActivity($name, 'unfollow');
- header('Location: '.BASE_URL.'/community/'.$name);
- }
- public function addActivity($page, $activity)
- {
- $act = new Activity();
- $act->user = $_SESSION['username'];
- $act->page = $page;
- $act->activity_type = $activity;
- $act->save();
- }
- public static function getTemp($cityName) {
- $query = urlencode($cityName);
- $endpoint = 'http://api.openweathermap.org/data/2.5/weather?q='.$query.'&APPID=567d44be5ca794937686b4c376c29fec';
- $contents = file_get_contents($endpoint);
- $json = json_decode($contents);
- $results = $json->{'weather'};
- //shuffle($results);
- $info = $results[0];
- $main = $info->{'main'}; // title of the image
- $description = $info->{'description'}; // thumbnail of the img
- $icon = $info->{'icon'}; // url to the img's item page
- $data = array(
- 'main' => $main,
- 'description' => $description,
- 'icon' => $icon
- );
- return $data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement