This commit is contained in:
vedatakd 2019-10-18 11:34:10 +03:00
parent bf2a56b401
commit 8a945995ef
4 changed files with 41 additions and 18 deletions

View File

@ -14,32 +14,34 @@ class AjaxController extends PublicController
public function locations(Request $request) public function locations(Request $request)
{ {
$datas = []; $datas = [];
if($request->level == 1){ if ($request->level == 1) {
$datas = CityModel::where('parent_country_id', $request->cat)->get(); $datas = CityModel::where('parent_country_id', $request->cat)->get();
}else if($request->level == 2){ } else if ($request->level == 2) {
$datas = DistrictModel::where('parent_city_id', $request->cat)->get(); $datas = DistrictModel::where('parent_city_id', $request->cat)->get();
}else if($request->level == 3){ } else if ($request->level == 3) {
$datas = NeighborhoodModel::where('parent_district_id', $request->cat)->get(); $datas = NeighborhoodModel::where('parent_district_id', $request->cat)->get();
}else if($request->level == 4){ } else if ($request->level == 4) {
$datas = VillageModel::where('parent_neighborhood_id', $request->cat)->get(); $datas = VillageModel::where('parent_neighborhood_id', $request->cat)->get();
} }
return response()->json($datas); return response()->json($datas);
} }
public function categories(Request $request) public function categories(Request $request)
{ {
$datas = []; $datas = [];
if($request->level == 0){ if ($request->level == 0) {
$datas = CategoryModel::whereNull('parent_category_id')->get(); $datas = CategoryModel::whereNull('parent_category_id')->get();
}else{ } else {
$datas = CategoryModel::where('parent_category_id', $request->cat)->get(); $datas = CategoryModel::where('parent_category_id', $request->cat)->get();
} }
return response()->json($datas); return response()->json($datas);
} }
public function keySearch(Request $request) public function keySearch(Request $request)
{ {
$datas = []; $datas = [];
$catModel = new CategoryModel(); $catModel = new CategoryModel();
$datas['category'] = $catModel->searchKeyword($request->q); $datas['category'] = $catModel->searchKeyword($request->q, $request->selected);
return response()->json($datas); return response()->json($datas);
} }

View File

@ -12,7 +12,7 @@ class CategoryModel extends CatsCategoryEntryModel implements CategoryInterface
return CategoryModel::query()->where('cats_category.id', $id)->first(); return CategoryModel::query()->where('cats_category.id', $id)->first();
} }
public function getParentCats($id, $type = null, $subCatDeepCount = 5) public function getParentCats($id, $type = null, $subCatDeepCount = 7)
{ {
$cat = $this->getCat($id); $cat = $this->getCat($id);
$catNames = array(); $catNames = array();
@ -21,9 +21,9 @@ class CategoryModel extends CatsCategoryEntryModel implements CategoryInterface
$cat_ids[] = $cat->id; $cat_ids[] = $cat->id;
$subCat = $cat->parent_category_id; $subCat = $cat->parent_category_id;
if ($subCat != null) { if ($subCat != null) {
for ($i = 0; $i < $subCatDeepCount; $i++) { for ($i = 0; $i < 7; $i++) {
$parCat = $this->getCat($subCat); $parCat = $this->getCat($subCat);
if ($parCat == null) { if ($parCat->parent_category_id == "") {
break; break;
} }
$catNames[] = $parCat->name; $catNames[] = $parCat->name;
@ -77,16 +77,26 @@ class CategoryModel extends CatsCategoryEntryModel implements CategoryInterface
return true; return true;
} }
public function searchKeyword($keyword) public function searchKeyword($keyword, $selected = null)
{ {
$data = []; $data = [];
$cats = DB::table('cats_category_translations') $cats = DB::table('cats_category');
->select('cats_category.id', 'cats_category_translations.name', 'cats_category.parent_category_id') if ($selected != null) {
->where('name', 'like', $keyword . '%') if (strpos($selected, "-") !== false) {
->join('cats_category', 'cats_category_translations.entry_id', '=', 'cats_category.id') $selected = explode('-', $selected);
->orderBy('cats_category_translations.id', 'DESC') $cats = $cats->whereNotIn('cats_category.id', $selected);
->get(); } else {
$cats = $cats->where('cats_category.id', '!=', $selected);
}
}
$cats = $cats->where('name', 'like', $keyword . '%');
$cats = $cats->leftJoin('cats_category_translations', function ($join) {
$join->on('cats_category.id', '=', 'cats_category_translations.entry_id');
$join->where('cats_category_translations.locale', '=', Request()->session()->get('_locale'));
});
$cats = $cats->orderBy('cats_category_translations.id', 'DESC')
->get();
foreach ($cats as $cat) { foreach ($cats as $cat) {
$link = ''; $link = '';
$parents = $this->getParentCats($cat->id, null, 2); $parents = $this->getParentCats($cat->id, null, 2);
@ -101,6 +111,7 @@ class CategoryModel extends CatsCategoryEntryModel implements CategoryInterface
$data[] = array( $data[] = array(
'id' => $cat->id, 'id' => $cat->id,
'name' => $cat->name, 'name' => $cat->name,
'locale' => $cat->locale,
'parents' => $link 'parents' => $link
); );
} }

View File

@ -23,12 +23,14 @@ class CategoryRepository extends EntryRepository implements CategoryRepositoryIn
{ {
$this->model = $model; $this->model = $model;
} }
public function findById($id) public function findById($id)
{ {
return $this->model->orderBy('created_at', 'DESC')->where('cats_category.id', $id)->first(); return $this->model->orderBy('created_at', 'DESC')->where('cats_category.id', $id)->first();
} }
public function mainCats(){ public function mainCats()
{
return $this->model->where('parent_category_id', null)->where('deleted_at', null)->get(); return $this->model->where('parent_category_id', null)->where('deleted_at', null)->get();
} }
@ -51,8 +53,14 @@ class CategoryRepository extends EntryRepository implements CategoryRepositoryIn
{ {
return CatsCategoryEntryModel::query()->where('cats_category.id', $id)->first(); return CatsCategoryEntryModel::query()->where('cats_category.id', $id)->first();
} }
public function findBySlug($slug) public function findBySlug($slug)
{ {
return $this->model->orderBy('created_at', 'DESC')->where('slug', $slug)->first(); return $this->model->orderBy('created_at', 'DESC')->where('slug', $slug)->first();
} }
public function getCategories()
{
return $this->model->get();
}
} }

View File

@ -17,4 +17,6 @@ interface CategoryRepositoryInterface extends EntryRepositoryInterface
public function getSingleCat($id); public function getSingleCat($id);
public function findBySlug($slug); public function findBySlug($slug);
public function getCategories();
} }