public function searchDish()
 {
     if (Efiwebsetting::getData('checkOAuth') == 'yes') {
         IMBAuth::checkOAuth();
     }
     $resto = "resto";
     $json = array();
     $json['status_code'] = 1;
     $search = isset($_GET['name']) ? addslashes($_GET['name']) : "";
     $lat = isset($_GET['latitude']) ? addslashes($_GET['latitude']) : "";
     if ($lat == "" or !$lat) {
         $lat = MenuRevoConstants::$latitude;
     }
     if (!Generic::checkLatitude($lat)) {
         Generic::errorMsg("Latitude must be Numeric!");
     }
     $long = isset($_GET['longitude']) ? addslashes($_GET['longitude']) : "";
     if ($long == "" or !$long) {
         $long = MenuRevoConstants::$longitude;
     }
     if (!Generic::checklongitude($long)) {
         Generic::errorMsg("Longitude must be Numeric!");
     }
     $nearby = isset($_GET['nearby']) ? addslashes($_GET['nearby']) : 20;
     if ($nearby == '1') {
         $nearby = 20;
     } else {
         $nearby = 100000;
     }
     $arrSort['distance'] = 1;
     $nearby = " HAVING distance < " . $nearby;
     $favorite = isset($_GET['fav']) ? addslashes($_GET['fav']) : "0";
     if ($favorite == '1') {
         $arrSort['fav'] = $favorite;
     }
     $houropen = isset($_GET['houropen']) ? addslashes($_GET['houropen']) : "0";
     if ($houropen == '1') {
         $whereOption = "  CONVERT_TZ(NOW(),@@session.time_zone, '+00:00') >= (CONVERT_TZ(concat(CURRENT_DATE,' ',resto.time_open),@@session.time_zone, '+00:00' )) AND CONVERT_TZ(NOW(),@@session.time_zone, '+00:00') < (CONVERT_TZ(concat(CURRENT_DATE,' ',resto.time_close),@@session.time_zone, '+00:00' ))";
     }
     $cuisine = isset($_GET['id_cuisine']) ? addslashes($_GET['id_cuisine']) : "None";
     if (Cuisine::checkCuisineID($cuisine) != 0) {
         $arrWhere['id_cuisine'] = $cuisine;
     }
     $restotype = isset($_GET['id_restotype']) ? addslashes($_GET['id_restotype']) : "None";
     if (MasterRestaurantType::isRestoTypeByIDAvailable($restotype) != 0) {
         $arrWhere['restaurant_type'] = $restotype;
     }
     $disc_mr = isset($_GET['disc_mr']) ? addslashes($_GET['disc_mr']) : "None";
     if ($disc_mr == 1) {
         $arrWhereOr['disc_mr'] = $disc_mr;
     }
     $disc_cc = isset($_GET['disc_cc']) ? addslashes($_GET['disc_cc']) : "None";
     if ($disc_cc == 1) {
         $arrWhereOr['disc_cc'] = $disc_cc;
     }
     $disc_resto = isset($_GET['disc_resto']) ? addslashes($_GET['disc_resto']) : "None";
     if ($disc_resto == 1) {
         $arrWhereOr['disc_resto'] = $disc_resto;
     }
     $where = " WHERE dish.name LIKE '%{$search}%' ";
     if ($whereOption != "") {
         $where = $where . " AND " . $whereOption;
     }
     foreach ($arrWhere as $key => $val) {
         $where = $where . " AND {$resto}" . ".{$key}= '{$val}'";
     }
     foreach ($arrWhereOr as $key => $val) {
         $where = $where . " AND {$resto}" . ".{$key} != '0'";
     }
     $sort = $nearby . " ORDER BY ";
     foreach ($arrSort as $key => $val) {
         if ($key == "distance") {
             $sort = $sort . " {$key} ASC,";
         } elseif ($key == "fav") {
             $sort = $sort . " {$resto}" . ".{$key} DESC,";
         } elseif ($key == "houropen") {
             $sort = $sort . " {$resto}" . ".{$key} ASC,";
         }
     }
     $sort = substr($sort, 0, -1);
     $page = addslashes($_GET['page']);
     if ($page == "" || $page < 1) {
         $json['status_code'] = 0;
         $json['status_message'] = "No Page Found";
         echo json_encode($json);
         die;
     }
     $limit = addslashes($_GET['limit']);
     if ($limit == "" || $limit < 1) {
         $json['status_code'] = 0;
         $json['status_message'] = "Limit Error";
         echo json_encode($json);
         die;
     }
     $begin = ($page - 1) * $limit;
     global $db;
     $objRestaurant = new MasterRestaurantModel();
     $objDish = new MasterDishModel();
     $qdish = "SELECT dish.*, resto.name as nama_restaurant, SQRT(POW(69.1 * (resto.latitude - {$lat}), 2) + POW(69.1 * ({$long} - resto.longitude) * COS(resto.latitude / 57.3), 2)) AS distance" . " FROM  {$objDish->table_name} dish LEFT JOIN {$objRestaurant->table_name} resto ON dish.id_restaurant=resto.id_restaurant " . $where . $sort . " LIMIT {$begin},{$limit}";
     //        echo $qdish. "<br>";
     $arrDish = $db->query($qdish, 2);
     if (count($arrDish) == 0) {
         Generic::errorMsg("No Food found!");
     }
     $arrDishHelp = array();
     foreach ($arrDish as $dish) {
         $dishhlp = MasterDish::getDish($dish->id_dish);
         $dishhlp['distance'] = $dish->distance;
         $dishhlp['nama_restaurant'] = $dish->nama_restaurant;
         $arrDishHelp[] = $dishhlp;
     }
     //
     //
     //        $qResto = "SELECT *, SQRT(POW(69.1 * (latitude - $lat), 2) + POW(69.1 * ($long - longitude) * COS(latitude / 57.3), 2)) AS distance FROM {$objRestaurant->table_name} " . $where . $sort  ;
     ////        echo $qResto;
     //        $arrResto = $db->query($qResto, 2);
     //        $checkCountResto = false;
     //        $arrDishHelp = array();
     //        foreach ($arrResto as $restoID) {
     //
     //            $arrSearchDish = $objDish->getWhere("name LIKE '%$search%' AND availability='1' AND id_restaurant='$restoID->id_restaurant' LIMIT $begin,$limit");
     //
     //            if (count($arrSearchDish) > 0) {
     //                $checkCountResto = true;
     //                $jarak = $restoID->distance;
     //
     //                foreach ($arrSearchDish as $dish) {
     //                    $dish = MasterDish::getDish($dish->id_dish);
     //                    $dish['distance'] = $jarak;
     //                    $dish['nama_restaurant'] = MasterRestaurant::getRestoNameByIDResto($dish['id_restaurant']);
     //                    $arrDishHelp[] = $dish;
     //                }
     //
     //            }
     //
     //        }
     //
     //        if (!$checkCountResto) {
     //            Generic::errorMsg("No Food found!");
     //        }
     $json['results']['Dish'] = $arrDishHelp;
     echo json_encode($json);
     die;
 }
 public function searchResto()
 {
     if (Efiwebsetting::getData('checkOAuth') == 'yes') {
         IMBAuth::checkOAuth();
     }
     $json = array();
     $json['status_code'] = 1;
     $search = isset($_GET['name']) ? addslashes($_GET['name']) : "";
     $lat = isset($_GET['latitude']) ? addslashes($_GET['latitude']) : "";
     if ($lat != "") {
         if (!Generic::checkLatitude($lat)) {
             Generic::errorMsg("Latitude must be Numeric!");
         }
     } else {
         Generic::errorMsg("Latitude is empty");
     }
     $long = isset($_GET['longitude']) ? addslashes($_GET['longitude']) : "";
     if ($long != "") {
         if (!Generic::checklongitude($long)) {
             Generic::errorMsg("Longitude must be Numeric!");
         }
     } else {
         Generic::errorMsg("Longitude is empty");
     }
     //
     $nearby = isset($_GET['nearby']) ? addslashes($_GET['nearby']) : 20;
     if ($nearby == '1') {
         $nearby = 20;
     } else {
         $nearby = 100000;
     }
     //        echo "near: " . $nearby;
     $arrSort['distance'] = 1;
     $nearby = " HAVING distance < " . $nearby;
     $favorite = isset($_GET['fav']) ? addslashes($_GET['fav']) : "0";
     if ($favorite == '1') {
         $arrSort['fav'] = $favorite;
     }
     $houropen = isset($_GET['houropen']) ? addslashes($_GET['houropen']) : "0";
     if ($houropen == '1') {
         $whereOption = " AND CONVERT_TZ(NOW(),@@session.time_zone, '+00:00') >= (CONVERT_TZ(concat(CURRENT_DATE,' ',time_open),@@session.time_zone, '+00:00' )) AND CONVERT_TZ(NOW(),@@session.time_zone, '+00:00') < (CONVERT_TZ(concat(CURRENT_DATE,' ',time_close),@@session.time_zone, '+00:00' ))";
         //            CONVERT_TZ(NOW(),@@session.time_zone, '+00:00') >= (CONVERT_TZ(concat(CURRENT_DATE,' ',time_open),@@session.time_zone, '+00:00' )) AND CONVERT_TZ(NOW(),@@session.time_zone, '+00:00') < (CONVERT_TZ(concat(CURRENT_DATE,' ',time_close),@@session.time_zone, '+00:00' ))
         //            $whereOption = " AND (CURTIME() BETWEEN time_open AND time_close) ";
     }
     $cuisine = isset($_GET['id_cuisine']) ? addslashes($_GET['id_cuisine']) : "None";
     if (Cuisine::checkCuisineID($cuisine) != 0) {
         $arrWhere['id_cuisine'] = $cuisine;
     }
     $restotype = isset($_GET['id_restotype']) ? addslashes($_GET['id_restotype']) : "None";
     if (MasterRestaurantType::isRestoTypeByIDAvailable($restotype) != 0) {
         $arrWhere['restaurant_type'] = $restotype;
     }
     $disc_mr = isset($_GET['disc_mr']) ? addslashes($_GET['disc_mr']) : "None";
     if ($disc_mr == 1) {
         $arrWhereOr['disc_mr'] = $disc_mr;
     }
     $disc_cc = isset($_GET['disc_cc']) ? addslashes($_GET['disc_cc']) : "None";
     if ($disc_cc == 1) {
         $arrWhereOr['disc_cc'] = $disc_cc;
     }
     $disc_resto = isset($_GET['disc_resto']) ? addslashes($_GET['disc_resto']) : "None";
     if ($disc_resto == 1) {
         $arrWhereOr['disc_resto'] = $disc_resto;
     }
     $where = " WHERE name LIKE '%{$search}%' AND aktiv='1' AND show_in_apps='1'";
     if ($whereOption != "") {
         $where = $where . $whereOption;
     }
     foreach ($arrWhere as $key => $val) {
         $where = $where . " AND {$key}= '{$val}'";
     }
     foreach ($arrWhereOr as $key => $val) {
         $where = $where . " AND {$key} != '0'";
     }
     //        pr($where);
     //        die();
     $sort = " ORDER BY ";
     foreach ($arrSort as $key => $val) {
         if ($key == "distance") {
             $sort = $sort . " {$key} ASC,";
         } elseif ($key == "fav") {
             $sort = $sort . " {$key} DESC,";
         } elseif ($key == "houropen") {
             $sort = $sort . " {$key} ASC,";
         }
     }
     $sort = substr($sort, 0, -1);
     $page = addslashes($_GET['page']);
     if ($page == "" || $page < 1) {
         $json['status_code'] = 0;
         $json['status_message'] = "No Page Found";
         echo json_encode($json);
         die;
     }
     $limit = addslashes($_GET['limit']);
     if ($limit == "" || $limit < 1) {
         $json['status_code'] = 0;
         $json['status_message'] = "Limit Error";
         echo json_encode($json);
         die;
     }
     $begin = ($page - 1) * $limit;
     global $db;
     $objRestaurant = new MasterRestaurantModel();
     $q = "SELECT *, SQRT(POW(69.1 * (latitude - {$lat}), 2) + POW(69.1 * ({$long} - longitude) * COS(latitude / 57.3), 2)) AS distance FROM {$objRestaurant->table_name} " . $where . $sort . " LIMIT {$begin},{$limit}";
     //        echo $q;
     $arrResto = $db->query($q, 2);
     Generic::checkCountWithMsg($arrResto, "Can't find the Restaurant");
     $json['results']['restaurant'] = array();
     foreach ($arrResto as $resto) {
         $resto_help = User::getRestaurant($resto->id_restaurant);
         $resto_help['distance'] = $resto->distance;
         $sem[] = $resto_help;
     }
     $json['results']['restaurant'] = $sem;
     echo json_encode($json);
     die;
 }