public function setRestoCategories()
 {
     if (Efiwebsetting::getData('checkOAuth') == 'yes') {
         IMBAuth::checkOAuth();
     }
     $idRestaurant = Generic::mustCheck($_POST['id_restaurant'], Keys::$ERR_NOT_FOUND_ID_RESTAURANT);
     $category = Generic::mustCheck($_POST['categories'], Keys::$ERR_NOT_FOUND_CATEGORIES);
     //category yang di passing harus berupa JSON ARRAY
     $arrNewCategory = json_decode($category);
     foreach ($arrNewCategory as $newCategory) {
         echo $newCategory->category_name . "<br>";
     }
     die;
     $json = array();
     $json['status_code'] = 1;
     $id_restaurant = isset($_POST['id_restaurant']) ? addslashes($_POST['id_restaurant']) : "";
     if (!$id_restaurant) {
         Generic::errorMsg("Restaurant ID not Found");
     }
     $category = isset($_POST['categories']) ? $_POST['categories'] : "";
     if (!$category) {
         Generic::errorMsg("Category ID not found!");
     }
     $resto = new MasterRestaurantModel();
     $resto->getByID($id_restaurant);
     $arrOldCategories = explode(",", $resto->id_categories);
     $other = '[{"category_name":"OTHERS"}]';
     $other = json_decode($other);
     $jsonCategories = json_decode($category);
     foreach ($jsonCategories as $cat) {
         $jsonCategorieshlp[] = $cat->category_name;
     }
     if (!in_array("OTHERS", $jsonCategorieshlp)) {
         $jsonCategories[]['category_name'] = "OTHERS";
     }
     $objCategory = new MasterCategoryModel();
     $idCategories = array();
     foreach ($jsonCategories as $category) {
         $arrCategory = $objCategory->getWhere("name = '{$category->category_name}'");
         if (count($arrCategory) == 0) {
             if (strtoupper($category->category_name) != strtoupper("OTHERS")) {
                 $objCategory->name = strtoupper($category->category_name);
                 //                    echo $objCategory->name;
                 $objCategory->status = "1";
                 $objCategory->is_drink = "0";
                 $objCategory->save();
             }
         }
     }
     foreach ($jsonCategories as $category) {
         $arrCategory = $objCategory->getWhere("name = '{$category->category_name}'");
         foreach ($arrCategory as $val) {
             $idCategories[] = $val->id_category;
         }
     }
     //compare old categories to new categories
     //cari yang dulu ada sekarang ga ada
     //migrasi dish ke others
     $missingCategories = array_diff($arrOldCategories, $idCategories);
     foreach ($missingCategories as $category) {
         $d = new MasterDishModel();
         $arrDishes = $d->getWhere("id_category = '{$category}' AND id_restaurant = '{$id_restaurant}'");
         foreach ($arrDishes as $dish) {
             $dish->load = 1;
             $dish->id_category = "0";
             $dish->save();
         }
     }
     $objRestaurant = new MasterRestaurantModel();
     $arrResto = $objRestaurant->getWhere("id_restaurant = '{$id_restaurant}'");
     if (count($arrResto) == 0) {
         $json['status_code'] = 0;
         $json['status_message'] = "No ID Found";
         echo json_encode($json);
         die;
     }
     $idCategories = implode(",", $idCategories);
     $arrResto[0]->id_categories = $idCategories;
     $arrResto[0]->load = 1;
     $idSave = $arrResto[0]->save();
     if (!$idSave) {
         Generic::errorMsg(Lang::t('save failed'));
     } else {
         $json['status_code'] = 1;
         $json['results'] = "Success";
     }
     echo json_encode($json);
     die;
 }
 public static function checkDish($id_dish)
 {
     $objDish = new MasterDishModel();
     $arrDish = $objDish->getWhere("id_dish='{$id_dish}'");
     if (count($arrDish) > 0) {
         return true;
     } else {
         return false;
     }
 }
 public function loadDish()
 {
     if (Efiwebsetting::getData('checkOAuth') == 'yes') {
         IMBAuth::checkOAuth();
     }
     $cmd = isset($_GET['cmd']) ? addslashes($_GET['cmd']) : "";
     $json = array();
     $json['status_code'] = 1;
     if (!$cmd) {
         $json['status_code'] = 0;
         $json['status_message'] = "No ID Found";
         echo json_encode($json);
         die;
     }
     $id_user = isset($_GET['id']) ? addslashes($_GET['id']) : "";
     if (!$id_user) {
         $json['status_code'] = 0;
         $json['status_message'] = "No ID Found";
         echo json_encode($json);
         die;
     }
     $user = new UserModel();
     $arrUser = $user->getWhere("id_user={$id_user}");
     if ($cmd == "fav") {
         foreach ($arrUser as $o) {
             $dishSearch['fav_dishes_ids'] = $o->fav_dishes_ids;
         }
         $objectDish = new MasterDishModel();
         $objResto = new MasterRestaurantModel();
         $arrdishSearch = explode(",", $dishSearch[fav_dishes_ids]);
         foreach ($arrdishSearch as $val) {
             if ($where == "") {
                 $where = "{$objectDish->table_name}.id_dish={$val}";
             } else {
                 $where = $where . " or {$objectDish->table_name}.id_dish={$val}";
             }
         }
         $where = $where . " ORDER BY {$objectDish->table_name}.fav DESC";
         $where = $where;
         global $db;
         $arrDish = $objectDish->getWhere($where);
         foreach ($arrDish as $key => $dish) {
             $arrFavbyResto[] = $dish->id_restaurant;
             $arrGesDish[$dish->id_restaurant][] = MasterDish::getDish($dish->id_dish);
         }
         $arrFavbyResto = array_unique($arrFavbyResto);
         $sem = array();
         foreach ($arrFavbyResto as $resto) {
             $restoHelp = self::getRestaurant($resto);
             foreach ($arrGesDish as $key => $valdish) {
                 $dishhelp = array();
                 if ($key == $resto) {
                     $dishhelp = $valdish;
                     break;
                 }
             }
             $restoHelp['dish'] = $dishhelp;
             $sem['restaurant'][] = $restoHelp;
         }
         $json['results'] = $sem;
     } elseif ($cmd == "his") {
         foreach ($arrUser as $o) {
             $dishSearch['history_dishes_ids'] = $o->history_dishes_ids;
         }
         $arrdishSearch = explode(",", $dishSearch[history_dishes_ids]);
         $objectDish = new MasterDishModel();
         $objectRestaurant = new MasterRestaurantModel();
         $exp = explode(",", str_replace(" ", "", $objectDish->crud_webservice_allowed));
         $arrPicsToAddPhotoUrl = $objectDish->crud_add_photourl;
         foreach ($arrdishSearch as $val) {
             $arr = $objectDish->getWhere("id_dish=" . $val, "*");
             if (count($arr) != 0) {
                 $json['status_code'] = 1;
             } else {
                 $json['status_code'] = 0;
                 $json['status_message'] = "No ID Found";
                 echo json_encode($json);
                 die;
             }
             $sem = array();
             foreach ($arr as $o) {
                 foreach ($exp as $attr) {
                     //                    echo $attr;
                     if (in_array($attr, $arrPicsToAddPhotoUrl)) {
                         $sem[$attr] = _BPATH . _PHOTOURL . $o->{$attr};
                     } elseif ($attr == "id_restaurant") {
                         //                        echo "masuk";
                         //                        echo $o->$attr;
                         $sem[$attr] = stripslashes($o->{$attr});
                         $sem['restaurant'] = $this->getRestaurant($o->{$attr});
                     } else {
                         $sem[$attr] = stripslashes($o->{$attr});
                     }
                 }
                 //$sem['restaurant'] = $objectRestaurant->getRestaurant($val);
                 $json["results"][] = $sem;
             }
         }
     } elseif ($cmd == "receipthis") {
     } else {
         $json['status_code'] = 0;
         $json['status_message'] = "No ID Found";
         echo json_encode($json);
         die;
     }
     echo json_encode($json);
     die;
 }
 public function setRestoCategories()
 {
     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) {
         Generic::errorMsg("Restaurant ID not Found");
     }
     $category = isset($_POST['categories']) ? $_POST['categories'] : "";
     if (!$category) {
         Generic::errorMsg("Category ID not found!");
     }
     $resto = new MasterRestaurantModel();
     $resto->getByID($id_restaurant);
     $arrOldCategories = explode(",", $resto->id_categories);
     $jsonCategories = json_decode($category);
     $jsonCategorieshlp = array();
     foreach ($jsonCategories as $cat) {
         $jsonCategorieshlp[] = $cat->category_name;
     }
     if (!in_array("OTHERS", $jsonCategorieshlp)) {
         $jsonCategorieshlp[] = "OTHERS";
     }
     $jsonCategorieshlp = array_unique($jsonCategorieshlp);
     $objCategory = new MasterCategoryModel();
     $idCategories = array();
     foreach ($jsonCategorieshlp as $category) {
         $arrCategory = $objCategory->getWhere("name = '{$category}'");
         if (count($arrCategory) == 0 && !Generic::IsNullOrEmptyString($category)) {
             $oc = new MasterCategoryModel();
             $oc->name = strtoupper($category);
             $oc->status = "1";
             $oc->is_drink = "0";
             $oc->save();
         }
     }
     foreach ($jsonCategorieshlp as $category) {
         $arrCategory = $objCategory->getWhere("name = '{$category}'");
         foreach ($arrCategory as $val) {
             $idCategories[] = $val->id_category;
         }
     }
     //compare old categories to new categories
     //cari yang dulu ada sekarang ga ada
     //migrasi dish ke others
     $missingCategories = array_diff($arrOldCategories, $idCategories);
     foreach ($missingCategories as $category) {
         $d = new MasterDishModel();
         $arrDishes = $d->getWhere("id_category = '{$category}' AND id_restaurant = '{$id_restaurant}'");
         foreach ($arrDishes as $dish) {
             $dish->load = 1;
             $dish->id_category = "0";
             $dish->save();
         }
     }
     $objRestaurant = new MasterRestaurantModel();
     $arrResto = $objRestaurant->getWhere("id_restaurant = '{$id_restaurant}'");
     if (count($arrResto) == 0) {
         $json['status_code'] = 0;
         $json['status_message'] = "No ID Found";
         echo json_encode($json);
         die;
     }
     $idCategories = implode(",", $idCategories);
     $arrResto[0]->id_categories = $idCategories;
     $arrResto[0]->load = 1;
     $idSave = $arrResto[0]->save();
     if (!$idSave) {
         Generic::errorMsg(Lang::t('save failed'));
     } else {
         $json['status_code'] = 1;
         $json['results'] = "Success";
     }
     echo json_encode($json);
     die;
 }
    public function overwriteForm($return, $returnfull)
    {
        $r = new MasterRestaurantModel();
        $arrR = $r->getWhere("aktiv='1'");
        $arrHelp = array("0" => "Nothing Selected");
        foreach ($arrR as $resto) {
            $arrHelp[$resto->id_restaurant] = $resto->name;
        }
        $return['id_restaurant'] = new \Leap\View\InputSelect($arrHelp, "id_restaurant", "id_restaurant", $this->id_restaurant);
        if (!isset($this->id_restaurant)) {
            $this->id_restaurant = key($arrHelp);
        }
        $d = new MasterDishModel();
        $arrD = $d->getWhere("id_restaurant='{$this->id_restaurant}' AND status='1'");
        $arrHelpDish = array("0" => "Nothing Selected");
        foreach ($arrD as $dish) {
            $arrHelpDish[$dish->id_dish] = $dish->name;
        }
        $return['id_dish'] = new Leap\View\InputSelect($arrHelpDish, "id_dish", "id_dish", $this->id_dish);
        ?>
        <script>
            $("#id_restaurant").change(function () {
                var selVal = $("select#id_restaurant option:selected").val();
                if (selVal == 0 || selVal == "" || selVal == "0") {
                    $('#id_dish').empty();
                    $('#id_dish').append($("<option></option>")
                        .attr("value", 0)
                        .text("Nothing Selected"));
                    return;
                }
                $.ajax({
                    method: "GET",
                    url: _sppath + "RecommendationFood/getDishByRestoId",
                    data: {id_restaurant: selVal}
                }).done(function (msg) {
//                        console.log(msg);
                    $('#id_dish').empty();
                    var json_obj = $.parseJSON(msg);//parse JSON
                    for (var i in json_obj) {
                        $('#id_dish')
                            .append($("<option></option>")
                                .attr("value", json_obj[i].value)
                                .text(json_obj[i].text));
                        console.log(json_obj[i].text);
                    }
                });
            });
        </script>
        <?php 
        if (!isset($this->start)) {
            $dt = leap_mysqldate();
        } else {
            $dt = $this->start;
        }
        $return['start'] = new \Leap\View\InputText("date", "start", "start", $dt);
        if (!isset($this->end)) {
            $dt = leap_mysqldate();
        } else {
            $dt = $this->end;
        }
        $return['end'] = new \Leap\View\InputText("date", "end", "end", $dt);
        return $return;
    }
 public function reinitiateCategories()
 {
     $idRestaurant = Generic::mustCheck($_GET['id_restaurant'], "BOOO!");
     $catIdDefault = $this->createCategories("OTHERS");
     $md = new MasterDishModel();
     if ($idRestaurant == "all") {
         $arrDishes = $md->getAll();
     } else {
         $arrDishes = $md->getWhere("id_restaurant='{$idRestaurant}'");
     }
     echo "<h1>Default cat id : {$catIdDefault}</h1>";
     foreach ($arrDishes as $d) {
         $dish = new MasterDishModel();
         $dish->getByID($d->id_dish);
         echo "<h2>{$dish->id_dish} - {$dish->name}</h2>";
         echo "<h3>From {$dish->id_category}";
         if (Generic::IsNullOrEmptyString($dish->id_category)) {
             echo "changed to";
             $dish->id_category = $catIdDefault;
             $dish->save();
         } else {
             if (!$this->isCategoryIdValid($dish->id_category)) {
                 echo "changed to";
                 $dish->id_category = $catIdDefault;
                 $dish->save();
             } else {
                 echo "stay at";
             }
         }
         echo " {$dish->id_category}</h3>";
         $resto = new MasterRestaurantModel();
         $resto->getByID($dish->id_restaurant);
         echo "<h2>{$resto->id_restaurant} - {$resto->name}</h2>";
         echo "<h3>From {$resto->id_categories}";
         if (Generic::IsNullOrEmptyString($resto->id_categories)) {
             $resto->id_categories = $catIdDefault;
             $resto->save();
         } else {
             $arrCats = explode(',', $resto->id_categories);
             $arrCats[] = $dish->id_category;
             $arrCats = array_unique($arrCats);
             $resto->id_categories = implode(',', $arrCats);
             $resto->save();
         }
         echo " To {$resto->id_categories}</h3>";
     }
 }