public function profile($sUsername) { //Get if the user is logged in or not $bLoggedIn = \Session::get("loggedin"); $data = Backend::LoginData(); //In default the profile's vistor is not the owner of it. $data['profileOwner'] = 'no'; if (isset($data['logged']) && $data['logged'] == 'yes') { $dVisitorId = $data['id_user']; $sUser = $data['username']; /* * Check if this page is for the logged in uesr * to show him more options. */ if ($sUsername == \Session::get('username')) { $data['profileOwner'] = 'yes'; } } else { $data['logged'] = 'no'; $data['user_type'] = "visitor"; } /* * Get the username and put it in data */ if (!empty($sUser)) { $data['username'] = $sUser; } /* * Get the Id of the logged in user */ if (!empty($dVisitorId)) { $data['visitor_id'] = $dVisitorId; } /* * Check if the user is a normal user 'Customers' */ $aUserData = DB::table('users')->where(array('username' => $sUsername))->get(array("id_user", "username", "first_name", "last_name", "email", "id_country", "gender", "age", "user_bio", "user_mobile", "user_status", "user_type", "photo")); if (empty($aUserData)) { /* * Being here means that the username is not for a normal user, * Check restauarant's table to check if the username is for a restaurant. */ $aParams = array('username', 'id_restaurant', 'id_country', 'restaurant_name', 'email', 'logo', 'telephone', 'twitter_account', 'bio', 'price_range', 'cuisines', 'opening_days', 'smoking_allowed', 'provide_ordering', 'website', 'rating', 'location'); $aUserData = DB::table('restaurants')->where(array('username' => $sUsername))->get($aParams); if (empty($aUserData)) { /* * If it came here means the username is not in the system * redirect the viewer to the home directory, * or maybe 'page not(404)found page' */ return redirect()->intended('/')->with(array('data' => $data)); } $oRestarant = $aUserData[0]; if (isset($oRestarant->cuisines)) { $sCuisineName = DB::table("cuisines")->where(array("id_cuisine" => $oRestarant->cuisines))->get(array("name")); if (!empty($sCuisineName)) { $oRestarant->cuisines = $sCuisineName[0]->name; } } $data['reviews_count'] = DB::table("reviews")->where(array("id_restaurant" => $oRestarant->id_restaurant))->count(array("*")); $oRestarant->opening_days = Backend::checkifOpen($oRestarant->opening_days); $oRestarant->location = json_decode($oRestarant->location); //username is for a restaurant, Redirect it to the restaurant's page. return view('pages.restaurantpage')->with(array('data' => $data, 'restaurant' => $oRestarant)); } /* * Get the user rating */ $aUserRating = DB::table('user_rating')->where(array('id_user' => $aUserData[0]->id_user))->get(array('likes_count')); /* * Get the user followers count */ $aUserFollower = DB::table("users_relationships")->where(array("id_other_user" => $aUserData[0]->id_user, "relationship_status" => "following"))->count(); $aUser['followers'] = $aUserFollower; /* * Get the user country */ $sCountry = DB::table("countries")->where(array('id_country' => $aUserData[0]->id_country))->get(array('country_name')); /* * Count how many reviews was written by the user */ $dReviewsCount = DB::table("reviews")->where(array("id_user" => $aUserData[0]->id_user))->count(array("*")); $aActivtiesList = DB::table("activities")->where(array("id_user" => $aUserData[0]->id_user))->orderBy("date_inserted", "desc")->get(array("*")); //Parse Activities $aActivties = array(); $aTmpActivity = array(); foreach ($aActivtiesList as $oActivity) { switch ($oActivity->type) { case "review": $aTmp = DB::table("reviews")->where(array("id_review" => $oActivity->id_other))->get(array("*")); if (count($aTmp) != 0) { $aRestaurantName = DB::table("restaurants")->where(array("id_restaurant" => $aTmp[0]->id_restaurant))->get(array("restaurant_name", "username")); $aTmpActivity['type'] = $oActivity->type; $aTmpActivity['other_name'] = $aRestaurantName[0]->restaurant_name; $aTmpActivity['content'] = $aTmp[0]->body; $aTmpActivity['rating'] = $aTmp[0]->rating; $aTmpActivity['date'] = $oActivity->date_inserted; $aTmpActivity['username'] = $aRestaurantName[0]->username; $aActivties[] = $aTmpActivity; } break; } } /* * Put the reviews count to 0 if there's no reviews */ if (!empty($dReviewsCount)) { $aUser['reviews_count'] = $dReviewsCount; } else { $aUser['reviews_count'] = 0; } /* * Get the user age */ $dInterval = Backend::getAge($aUserData[0]->age); $aUserData[0]->age = $dInterval; if (!empty($sCountry[0])) { //Set the user's country name $aUser['country'] = $sCountry[0]->country_name; } if (!empty($aUserRating)) { //Set the user rating $aUser['rating'] = $aUserRating[0]->likes_count; } $aUser['user'] = $aUserData[0]; /* * Show the normal user profile page, pass the needed data with the page */ return view('pages.profilepage')->with(array('data' => $data, 'aUser' => $aUser, 'aActivities' => $aActivties)); }