public function getHomeResults()
 {
     $master = new MasterRestaurant();
     $master->getHomeResults();
     //
     //        if (Efiwebsetting::getData('checkOAuth') == 'yes')
     //            IMBAuth::checkOAuth();
     //        $searchType = Generic::mustCheck($_POST['search_type'], "No Type");
     //        $searchTerm = Generic::mustCheck($_POST['search_term'], "No Term");
     //
     //        $isResto = !($searchType == "5" || $searchType == "6");
 }
 public function updateDish()
 {
     if (Efiwebsetting::getData('checkOAuth') == 'yes') {
         IMBAuth::checkOAuth();
     }
     $json = array();
     $json['status_code'] = 1;
     $id_restaurant = isset($_POST['id_restaurant']) ? addslashes($_POST['id_restaurant']) : "";
     if (!$id_restaurant) {
         $json['status_code'] = 0;
         $json['status_message'] = "No ID Found";
         echo json_encode($json);
         die;
     }
     $jsonDish = isset($_POST['dishes']) ? $_POST['dishes'] : "";
     if (!$jsonDish) {
         $json['status_code'] = 0;
         $json['status_message'] = "No ID Found";
         echo json_encode($json);
         die;
     }
     $jsonArrayDish = json_decode($jsonDish);
     $objDish = new MasterDishModel();
     $sem = array();
     $semCheck = array();
     foreach ($jsonArrayDish as $key => $value) {
         $arrDish = $objDish->getWhere("id_dish='{$value->id_dish}'");
         if (count($arrDish) > 0) {
             foreach ($value as $key => $dish) {
                 if (in_array($key, $objDish->crud_add_photourl)) {
                     $picname = MasterRestaurant::savePic($dish);
                     $arrDish[0]->{$key} = $picname;
                 } elseif ($key == "category_name") {
                     $objCategory = new MasterCategoryModel();
                     $arrCategory = $objCategory->getWhere("name='{$dish}'");
                     if (count($arrCategory) == 0) {
                         $objCategory->name = strtoupper($dish);
                         $objCategory->status = "1";
                         $objCategory->save();
                     }
                     $arrCategory = $objCategory->getWhere("name='{$dish}'");
                     $arrDish[0]->id_category = $arrCategory[0]->id_category;
                 } else {
                     $arrDish[0]->{$key} = $dish;
                 }
                 $arrDish[0]->id_restaurant = $id_restaurant;
             }
             $arrDish[0]->load = 1;
             $arrDish[0]->save();
             $sem[] = "id_dish: " . $value->id_dish . " or " . $value->name . " are  updated";
             $semCheck[] = 1;
         } else {
             $sem[] = "id_dish: " . $value->id_dish . " or " . $value->name . " not found";
             $semCheck[] = 0;
         }
     }
     //        pr($semCheck);
     if (!in_array(1, $semCheck)) {
         $json['status_code'] = 0;
     } else {
         $json['status_code'] = 1;
     }
     $json['results'] = $sem;
     echo json_encode($json);
     die;
 }
 public function getHomeResults()
 {
     $idHome = Generic::mustCheck($_GET['id_home'], "ID Not Found");
     $currentLat = !Generic::IsNullOrEmptyString($_GET['latitude']) ? $_GET['latitude'] : "";
     $currentLong = !Generic::IsNullOrEmptyString($_GET['longitude']) ? $_GET['longitude'] : "";
     $home = new HomeModel();
     $home->getByID($idHome);
     if ($home->status != "1") {
         Generic::errorMsg("Not Active Home");
     }
     $page = addslashes($_GET['page']);
     if ($page == "" || $page < 1) {
         Generic::errorMsg("No Page Found");
     }
     $limit = addslashes($_GET['limit']);
     if ($limit == "" || $limit < 1) {
         Generic::errorMsg("Limit Error");
     }
     $begin = ($page - 1) * $limit;
     $extraQ = " LIMIT {$begin},{$limit}";
     $key = "key";
     $results = array();
     $searchType = $home->search_type;
     $searchTerm = $home->search_term;
     $query = $home->query;
     switch ($searchType) {
         case "0":
             break;
         case "1":
             $resto = new MasterRestaurantModel();
             $results = $resto->getWhere("restaurant_type='{$searchTerm}' AND show_in_apps='1' ORDER BY fav DESC {$extraQ}");
             $exp = explode(",", str_replace(" ", "", $resto->crud_webservice_allowed));
             $arrPicsToAddPhotoUrl = $resto->crud_add_photourl;
             $key = "restaurant";
             break;
         case "2":
             $resto = new MasterRestaurantModel();
             $results = $resto->getWhere("FIND_IN_SET('{$searchTerm}', id_categories) AND show_in_apps='1' ORDER BY fav DESC {$extraQ}");
             $exp = explode(",", str_replace(" ", "", $resto->crud_webservice_allowed));
             $arrPicsToAddPhotoUrl = $resto->crud_add_photourl;
             $key = "restaurant";
             break;
         case "3":
             $resto = new MasterRestaurantModel();
             $results = $resto->getWhere("id_cuisine='{$searchTerm}' AND show_in_apps='1' ORDER BY fav DESC {$extraQ}");
             $exp = explode(",", str_replace(" ", "", $resto->crud_webservice_allowed));
             $arrPicsToAddPhotoUrl = $resto->crud_add_photourl;
             $key = "restaurant";
             break;
         case "4":
             $resto = new MasterRestaurantModel();
             $results = $resto->getWhere("name LIKE '%{$query}%' AND show_in_apps='1' ORDER BY fav DESC {$extraQ}");
             $exp = explode(",", str_replace(" ", "", $resto->crud_webservice_allowed));
             $arrPicsToAddPhotoUrl = $resto->crud_add_photourl;
             $key = "restaurant";
             break;
         case "5":
             $dish = new MasterDishModel();
             $results = $dish->getWhere("'{$searchTerm}' IN (dish_tags) ORDER BY id_dish DESC {$extraQ}");
             $exp = explode(",", str_replace(" ", "", $dish->crud_webservice_allowed));
             $arrPicsToAddPhotoUrl = $dish->crud_add_photourl;
             $key = "Dish";
             break;
         case "6":
             $dish = new MasterDishModel();
             $results = $dish->getWhere("name LIKE '%{$query}%' ORDER BY id_dish DESC {$extraQ}");
             $exp = explode(",", str_replace(" ", "", $dish->crud_webservice_allowed));
             $arrPicsToAddPhotoUrl = $dish->crud_add_photourl;
             $key = "Dish";
             break;
         case "8888":
             break;
         default:
             break;
     }
     $json["results"]["latitude"] = $currentLat;
     $json["results"]["longitude"] = $currentLong;
     $json["results"][$key] = array();
     foreach ($results as $o) {
         $sem = array();
         foreach ($exp as $attr) {
             if (in_array($attr, $arrPicsToAddPhotoUrl)) {
                 $sem[$attr] = Generic::insertImageUrl($o->{$attr});
             } else {
                 $sem[$attr] = stripslashes($o->{$attr});
             }
         }
         if ($searchType == "5" || $searchType == "6") {
             $sem['nama_restaurant'] = MasterRestaurant::getRestoNameByIDResto($o->id_restaurant);
         }
         if ($currentLat != "" && $currentLong != "") {
             $sem["distance"] = sqrt(pow(69.09999999999999 * ($o->latitude - $currentLat), 2) + pow(69.09999999999999 * ($currentLong - $o->longitude) * cos($o->latitude / 57.3), 2));
         } else {
             $sem["distance"] = 0;
         }
         //            $json["results"]["search_type"] = Generic::homeSearchType($searchType);
         $json["results"][$key][] = $sem;
     }
     $json['status_code'] = 1;
     echo json_encode($json);
     die;
 }
 public function userUpdateProfile()
 {
     if (Efiwebsetting::getData('checkOAuth') == 'yes') {
         IMBAuth::checkOAuth();
     }
     $idUser = Generic::mustCheck($_POST['id_user'], "ID user required");
     $fullName = Generic::mustCheck($_POST['full_name'], "Empty Full Name");
     $userName = Generic::mustCheck($_POST['user_name'], "Empty User Name");
     $email = Generic::mustCheck($_POST['email'], "Empty Email");
     $password = Generic::mustCheck($_POST['pwd'], "Empty Password");
     $idCuisine = Generic::mustCheck($_POST['pref_cuisine'], "Cuisine Not Selected");
     $birthday = Generic::mustCheck($_POST['birthday'], "Empty Birthday");
     $phoneNo = Generic::mustCheck($_POST['phone_no'], "Empty Phone Number");
     $fbId = Generic::IsNullOrEmptyString($_POST['fb_id']) ? "" : $_POST['fb_id'];
     $latitude = Generic::IsNullOrEmptyString($_POST['lat']) ? "" : $_POST['lat'];
     $longitude = Generic::IsNullOrEmptyString($_POST['long']) ? "" : $_POST['long'];
     $district = Generic::IsNullOrEmptyString($_POST['district']) ? "" : $_POST['district'];
     $city = Generic::IsNullOrEmptyString($_POST['city']) ? "" : $_POST['city'];
     $pic = Generic::IsNullOrEmptyString($_POST['pic']) ? "" : MasterRestaurant::savePic($_POST['pic']);
     ///START OF LOVELY VALIDATION TIME
     if (!Generic::isValidUserName($userName)) {
         Generic::errorMsg("Username must only contains alphanumeric (and/or) \\'.\\', \\'-\\', \\'_\\'");
     }
     if (!Generic::isValidUserNameLength($userName)) {
         Generic::errorMsg("Max username length is 20");
     }
     if (!Generic::isValidEmail($email)) {
         Generic::errorMsg("Email format not valid");
     }
     if (!Generic::isValidPassword($password)) {
         Generic::errorMsg("Password must contain minimum 5 character and no whitespace");
     }
     $user = new UserModel();
     $user->getByID($idUser);
     if ($birthday != $user->birthday) {
         Generic::errorMsg("Birthday cannot be changed");
     }
     if ($userName != $user->user_name) {
         Generic::errorMsg("Username cannot be changed");
     }
     if (!Generic::IsNullOrEmptyString($user->fb_id) && $fbId != $user->fb_id) {
         Generic::errorMsg("Facebook cannot be changed");
     } else {
         $user->fb_id = $fbId;
     }
     ///END OF LOVELY VALIDATION TIME, HOW SAD :(
     $user->full_name = $fullName;
     $user->email = $email;
     $user->password = $password;
     $user->id_cuisine = $idCuisine;
     $user->phone_no = $phoneNo;
     if (!Generic::IsNullOrEmptyString($pic)) {
         $user->pic = $pic;
     }
     if (!Generic::IsNullOrEmptyString($latitude)) {
         $user->latitude = $latitude;
         $user->last_lat = $latitude;
     }
     if (!Generic::IsNullOrEmptyString($longitude)) {
         $user->longitude = $longitude;
         $user->last_long = $longitude;
     }
     if (!Generic::IsNullOrEmptyString($district)) {
         $user->district = $district;
         $user->last_district = $district;
     }
     if (!Generic::IsNullOrEmptyString($city)) {
         $user->city = $city;
         $user->last_city = $city;
     }
     $user->status = "1";
     $uid = $user->save();
     if (!$uid) {
         Generic::errorMsg("Failed Creating User");
     } else {
         $json['status_code'] = 1;
         $json['results']['fb_id'] = $user->fb_id;
         $json['results']['id_user'] = $idUser;
         $json['results']['user_name'] = $user->user_name;
         $json['results']['full_name'] = $user->full_name;
         $json['results']['email'] = $user->email;
         $json['results']['pic'] = Generic::insertImageUrl($user->pic);
         echo json_encode($json);
         die;
     }
 }
 public function loadRestoDishNearBy()
 {
     if (Efiwebsetting::getData('checkOAuth') == 'yes') {
         IMBAuth::checkOAuth();
     }
     $json = array();
     $json['status_code'] = 1;
     $distance = isset($_GET['distance']) ? addslashes($_GET['distance']) : 20;
     if (!$distance) {
         Generic::errorMsg("Not Distance");
     }
     $lat = isset($_GET['latitude']) ? addslashes($_GET['latitude']) : "";
     if (!$lat) {
         Generic::errorMsg("No Lat");
     }
     $long = isset($_GET['longitude']) ? addslashes($_GET['longitude']) : "";
     if (!$long) {
         Generic::errorMsg("No longitude");
     }
     $page = addslashes($_GET['page']);
     if ($page == "" || $page < 1) {
         Generic::errorMsg("No Page Found");
     }
     $limit = addslashes($_GET['limit']);
     if ($limit == "" || $limit < 1) {
         Generic::errorMsg("Limit Error!");
     }
     $begin = ($page - 1) * $limit;
     $objRestaurant = new MasterRestaurantModel();
     $objDish = new MasterDishModel();
     global $db;
     $q = "SELECT *,SQRT(POW(69.1 * (latitude - {$lat}), 2) + POW(69.1 * ({$long} - longitude) * COS(latitude / 57.3), 2)) AS distance\nFROM {$objRestaurant->table_name} HAVING distance < {$distance} ORDER BY distance";
     $arrResto = $db->query($q, 2);
     MasterDish::checkCount($arrResto);
     $exp = explode(",", str_replace(" ", "", $objRestaurant->crud_webservice_allowed));
     //tmbh untuk add photo url
     $arrPicsToAddPhotoUrl = $objRestaurant->crud_add_photourl;
     $arrPicsDishToAddPhotoUrl = $objRestaurant->crud_add_photourl;
     $json['results']['Dish'] = array();
     $arrDishHelp = array();
     $qdish = "SELECT objDish.*, SQRT(POW(69.1 * (objDish.latitude - {$lat}), 2) + POW(69.1 * ({$long} - objDish.longitude) * COS(objDish.latitude / 57.3), 2)) AS distance" . " FROM (SELECT menurevo__ms_dish.*, menurevo__ms_restaurant.latitude, menurevo__ms_restaurant.longitude FROM menurevo__ms_dish LEFT JOIN menurevo__ms_restaurant ON menurevo__ms_dish.id_restaurant = menurevo__ms_restaurant.id_restaurant ORDER BY menurevo__ms_dish.fav DESC)as objDish " . "GROUP BY objDish.id_restaurant HAVING distance < 20 ORDER BY distance ASC LIMIT {$begin},{$limit}";
     //            echo $qdish;
     $arrDish = $db->query($qdish, 2);
     $arrDishHelp = array();
     foreach ($arrDish as $dish) {
         $jarak = $dish->distance;
         $dish = MasterDish::getDish($dish->id_dish);
         $dish['distance'] = $jarak;
         $dish['nama_restaurant'] = MasterRestaurant::getRestoNameByIDResto($dish['id_restaurant']);
         $arrDishHelp[] = $dish;
     }
     $json['results']['Dish'] = $arrDishHelp;
     echo json_encode($json);
     die;
 }