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 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 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 createCategories($categoryName, $isDrink = "1") { $cat = new MasterCategoryModel(); $cats = $cat->getWhere("name='{$categoryName}'"); if (count($cats) <= 0) { $c = new MasterCategoryModel(); $c->name = $categoryName; $c->status = Keys::$YES; $c->is_drink = $isDrink; return $c->save(); } else { return $cats[0]->id_category; } }