From 57ee0c8b2dad855e65e72c18ce057e8e15d689d1 Mon Sep 17 00:00:00 2001 From: vedatakd Date: Thu, 21 Jan 2021 18:21:33 +0300 Subject: [PATCH] refactored cats repository and model && update Count for create ad and edit category --- .../resources/config/settings/settings.php | 2 +- .../advs-module/resources/js/edit_cats.js | 3 +- .../views/list/partials/list-filter.twig | 2 +- .../resources/views/new-ad/edit-cat.twig | 2 +- .../advs-module/src/Adv/AdvRepository.php | 21 +- .../advs-module/src/Adv/Event/EditAd.php | 10 +- .../advs-module/src/Adv/Event/EditedAd.php | 23 ++ .../src/Adv/Event/EditedAdCategory.php | 23 ++ .../{priceChange.php => PriceChange.php} | 2 +- .../{showAdPhone.php => ShowAdPhone.php} | 2 +- .../src/Adv/Event/{viewAd.php => ViewAd.php} | 2 +- .../src/Adv/Listener/CategoryDeleted.php | 32 +++ .../src/Adv/Table/AdvTableColumns.php | 8 +- .../src/Adv/Table/AdvTableFilters.php | 2 +- .../src/AdvsModuleServiceProvider.php | 3 +- .../src/Http/Controller/AjaxController.php | 2 +- .../src/Http/Controller/advsController.php | 226 ++++++++++-------- .../src/OptionHandler/CategoriesOptions.php | 2 +- .../resources/js/theme/initialize.js | 10 +- .../resources/views/partials/categories.twig | 2 +- .../views/ads-list/partials/list-filter.twig | 64 +++-- .../resources/views/table/table.twig | 6 +- .../src/Category/CategoryCollection.php | 10 +- .../src/Category/CategoryCriteria.php | 11 +- .../src/Category/CategoryModel.php | 185 +------------- .../src/Category/CategoryPresenter.php | 19 +- .../src/Category/CategoryRepository.php | 171 ++++++------- ...Level2Cats.php => getCategoriesLevel2.php} | 4 +- .../Category/Contract/CategoryInterface.php | 26 +- .../Contract/CategoryRepositoryInterface.php | 18 +- .../CalculatedTotalForEditedAdCategory.php | 57 +++++ .../Listener/CalculatedTotalForNewAd.php | 36 +++ .../Category/Table/CategoryTableBuilder.php | 2 +- .../src/Category/Table/Handler/Delete.php | 2 +- .../cats-module/src/CatsModulePlugin.php | 25 +- .../src/CatsModuleServiceProvider.php | 15 +- .../Controller/Admin/CategoryController.php | 17 +- 37 files changed, 493 insertions(+), 554 deletions(-) create mode 100644 addons/default/visiosoft/advs-module/src/Adv/Event/EditedAd.php create mode 100644 addons/default/visiosoft/advs-module/src/Adv/Event/EditedAdCategory.php rename addons/default/visiosoft/advs-module/src/Adv/Event/{priceChange.php => PriceChange.php} (92%) rename addons/default/visiosoft/advs-module/src/Adv/Event/{showAdPhone.php => ShowAdPhone.php} (92%) rename addons/default/visiosoft/advs-module/src/Adv/Event/{viewAd.php => ViewAd.php} (94%) create mode 100644 addons/default/visiosoft/advs-module/src/Adv/Listener/CategoryDeleted.php rename addons/default/visiosoft/cats-module/src/Category/Command/{getLevel2Cats.php => getCategoriesLevel2.php} (74%) create mode 100644 addons/default/visiosoft/cats-module/src/Category/Listener/CalculatedTotalForEditedAdCategory.php create mode 100644 addons/default/visiosoft/cats-module/src/Category/Listener/CalculatedTotalForNewAd.php diff --git a/addons/default/visiosoft/advs-module/resources/config/settings/settings.php b/addons/default/visiosoft/advs-module/resources/config/settings/settings.php index e5bca4a16..32f70c98f 100644 --- a/addons/default/visiosoft/advs-module/resources/config/settings/settings.php +++ b/addons/default/visiosoft/advs-module/resources/config/settings/settings.php @@ -377,7 +377,7 @@ return [ 'type' => 'anomaly.field_type.checkboxes', 'config' => [ 'options' => function (\Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface $categoryRepository) { - return $categoryRepository->mainCats()->pluck('name', 'id')->all(); + return $categoryRepository->getMainCategories()->pluck('name', 'id')->all(); }, ], ], diff --git a/addons/default/visiosoft/advs-module/resources/js/edit_cats.js b/addons/default/visiosoft/advs-module/resources/js/edit_cats.js index 5f77f428f..cc4ece057 100644 --- a/addons/default/visiosoft/advs-module/resources/js/edit_cats.js +++ b/addons/default/visiosoft/advs-module/resources/js/edit_cats.js @@ -83,6 +83,7 @@ $(document).ready(function () { function selectedValue() { return $('.cat-select').on('change', function () { + console.log(234234) var value = $(this).val(); var all_category_box = $('.category-row').find('.category-box'); var level = parseInt($(this).attr('data-level')) + 1; @@ -126,4 +127,4 @@ function scroolToSelect(fields) { $([document.documentElement, document.body]).animate({ scrollTop: $(fields[fields.length - 1]).offset().top + 300 }, 1000); -} +} \ No newline at end of file diff --git a/addons/default/visiosoft/advs-module/resources/views/list/partials/list-filter.twig b/addons/default/visiosoft/advs-module/resources/views/list/partials/list-filter.twig index 24015510f..2c2362a4f 100644 --- a/addons/default/visiosoft/advs-module/resources/views/list/partials/list-filter.twig +++ b/addons/default/visiosoft/advs-module/resources/views/list/partials/list-filter.twig @@ -39,7 +39,7 @@ 'neighborhoods':neighborhoods, 'villages':villages, 'param':param, - 'categoryId':categoryId, + 'category':category, 'cityId':cityId })|raw }} diff --git a/addons/default/visiosoft/advs-module/resources/views/new-ad/edit-cat.twig b/addons/default/visiosoft/advs-module/resources/views/new-ad/edit-cat.twig index 328e9fe23..ae5fa76c3 100644 --- a/addons/default/visiosoft/advs-module/resources/views/new-ad/edit-cat.twig +++ b/addons/default/visiosoft/advs-module/resources/views/new-ad/edit-cat.twig @@ -38,4 +38,4 @@ {{ asset_add("scripts.js", "visiosoft.module.advs::js/edit_cats.js") }} {{ asset_style("visiosoft.module.advs::css/edit_category.css") }} -{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/addons/default/visiosoft/advs-module/src/Adv/AdvRepository.php b/addons/default/visiosoft/advs-module/src/Adv/AdvRepository.php index 0970d1336..164fe6d8e 100644 --- a/addons/default/visiosoft/advs-module/src/Adv/AdvRepository.php +++ b/addons/default/visiosoft/advs-module/src/Adv/AdvRepository.php @@ -9,6 +9,7 @@ use Intervention\Image\Facades\Image; use Visiosoft\AdvsModule\Adv\Contract\AdvRepositoryInterface; use Anomaly\Streams\Platform\Entry\EntryRepository; use Visiosoft\CatsModule\Category\CategoryModel; +use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface; use Visiosoft\LocationModule\City\CityModel; use Visiosoft\LocationModule\Country\CountryModel; use Visiosoft\LocationModule\District\DistrictModel; @@ -88,16 +89,11 @@ class AdvRepository extends EntryRepository implements AdvRepositoryInterface } } if ($category) { - $cat = new CategoryModel(); - if ($category) { - if ($category->parent_category_id == null) { - $catLevel = 1; - } else { - $catLevel = $cat->getCatLevel($category->id); - } - $catLevel = "cat" . $catLevel; - $query = $query->where($catLevel, $category->id); - } + $category_repository = app(CategoryRepositoryInterface::class); + + $catLevel = $category_repository->getLevelById($category->id); + $catLevel = "cat" . $catLevel; + $query = $query->where($catLevel, $category->id); } if (!empty($param['user'])) { $query = $query->where('advs_advs.created_by_id', $param['user']); @@ -499,8 +495,9 @@ class AdvRepository extends EntryRepository implements AdvRepositoryInterface ->paginate(setting_value('visiosoft.module.advs::popular_ads_limit', setting_value('streams::per_page'))); } - public function getName($id){ - return $this->find($id)->name; + public function getName($id) + { + return $this->find($id)->name; } public function approveAds($adsIDs) diff --git a/addons/default/visiosoft/advs-module/src/Adv/Event/EditAd.php b/addons/default/visiosoft/advs-module/src/Adv/Event/EditAd.php index ed5646b47..dee96cc62 100644 --- a/addons/default/visiosoft/advs-module/src/Adv/Event/EditAd.php +++ b/addons/default/visiosoft/advs-module/src/Adv/Event/EditAd.php @@ -3,15 +3,13 @@ class EditAd { - public function __construct($request, $settings, $adv) + public function __construct($ad) { - $this->request = $request; - $this->settings = $settings; - $this->adv = $adv; + $this->ad = $ad; } - public function getRequest() + public function getAd() { - return $this; + return $this->ad; } } diff --git a/addons/default/visiosoft/advs-module/src/Adv/Event/EditedAd.php b/addons/default/visiosoft/advs-module/src/Adv/Event/EditedAd.php new file mode 100644 index 000000000..f3fb3d6b4 --- /dev/null +++ b/addons/default/visiosoft/advs-module/src/Adv/Event/EditedAd.php @@ -0,0 +1,23 @@ +ad = $ad; + $this->before_editing = $before_editing; + } + + public function getAdDetail() + { + return $this->ad; + } + + public function getBeforeEditingDetail() + { + return $this->before_editing; + } +} diff --git a/addons/default/visiosoft/advs-module/src/Adv/Event/EditedAdCategory.php b/addons/default/visiosoft/advs-module/src/Adv/Event/EditedAdCategory.php new file mode 100644 index 000000000..6015f7b99 --- /dev/null +++ b/addons/default/visiosoft/advs-module/src/Adv/Event/EditedAdCategory.php @@ -0,0 +1,23 @@ +ad = $ad; + $this->before_editing_ad_params = $before_editing_ad_params; + } + + public function getAdDetail() + { + return $this->ad; + } + + public function getBeforeEditingParams() + { + return $this->before_editing_ad_params; + } +} diff --git a/addons/default/visiosoft/advs-module/src/Adv/Event/priceChange.php b/addons/default/visiosoft/advs-module/src/Adv/Event/PriceChange.php similarity index 92% rename from addons/default/visiosoft/advs-module/src/Adv/Event/priceChange.php rename to addons/default/visiosoft/advs-module/src/Adv/Event/PriceChange.php index 26be17cfb..631047e45 100644 --- a/addons/default/visiosoft/advs-module/src/Adv/Event/priceChange.php +++ b/addons/default/visiosoft/advs-module/src/Adv/Event/PriceChange.php @@ -1,7 +1,7 @@ advRepository = $advRepository; + } + + public function handle(DeletedCategory $event) + { + $category = $event->getCategory(); + + $catLevelNum = ($category->parent_category_id) ? count($event->getParents()) : 1; + + $catLevelText = "cat" . $catLevelNum; + + $advs = $this->advRepository->newQuery()->where($catLevelText, $category->id)->get(); + foreach ($advs as $adv) { + $nullableCats = array(); + for ($i = $catLevelNum; $i <= 10; $i++) { + $nullableCats['cat' . $i] = null; + } + $adv->update($nullableCats); + } + } +} \ No newline at end of file diff --git a/addons/default/visiosoft/advs-module/src/Adv/Table/AdvTableColumns.php b/addons/default/visiosoft/advs-module/src/Adv/Table/AdvTableColumns.php index 64f303c65..5eaf015c1 100644 --- a/addons/default/visiosoft/advs-module/src/Adv/Table/AdvTableColumns.php +++ b/addons/default/visiosoft/advs-module/src/Adv/Table/AdvTableColumns.php @@ -3,6 +3,7 @@ use Anomaly\Streams\Platform\Entry\Contract\EntryInterface; use Anomaly\Streams\Platform\Entry\EntryModel; use Visiosoft\CatsModule\Category\CategoryModel; +use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface; class AdvTableColumns { @@ -35,10 +36,11 @@ class AdvTableColumns } return $value; }, - 'category' => function (EntryInterface $entry, CategoryModel $categoryModel) { - $category = $categoryModel->getCat($entry->cat1); - if (!is_null($category)) + 'category' => function (EntryInterface $entry, CategoryRepositoryInterface $categoryRepository) { + $category = $categoryRepository->find($entry->cat1); + if ($category){ return $category->name; + } } ], ], diff --git a/addons/default/visiosoft/advs-module/src/Adv/Table/AdvTableFilters.php b/addons/default/visiosoft/advs-module/src/Adv/Table/AdvTableFilters.php index dea4ecdfc..909f3ebe6 100644 --- a/addons/default/visiosoft/advs-module/src/Adv/Table/AdvTableFilters.php +++ b/addons/default/visiosoft/advs-module/src/Adv/Table/AdvTableFilters.php @@ -14,7 +14,7 @@ class AdvTableFilters { $cities = $cityRepository->all()->pluck('name', 'id')->all(); - $categories = $categoryRepository->mainCats()->pluck('name', 'id')->all(); + $categories = $categoryRepository->getMainCategories()->pluck('name', 'id')->all(); $builder->setFilters( [ diff --git a/addons/default/visiosoft/advs-module/src/AdvsModuleServiceProvider.php b/addons/default/visiosoft/advs-module/src/AdvsModuleServiceProvider.php index 304b2f03a..9430e04e1 100644 --- a/addons/default/visiosoft/advs-module/src/AdvsModuleServiceProvider.php +++ b/addons/default/visiosoft/advs-module/src/AdvsModuleServiceProvider.php @@ -254,8 +254,9 @@ class AdvsModuleServiceProvider extends AddonServiceProvider ProductoptionsValueRepositoryInterface::class => ProductoptionsValueRepository::class, ]; - public function boot(AddonCollection $addonCollection, FileModel $fileModel) + public function boot(AddonCollection $addonCollection, FileModel $fileModel,CategoryRepositoryInterface $categoryRepository) { + $settings_url = [ 'general_settings' => [ 'title' => 'visiosoft.module.advs::button.general_settings', diff --git a/addons/default/visiosoft/advs-module/src/Http/Controller/AjaxController.php b/addons/default/visiosoft/advs-module/src/Http/Controller/AjaxController.php index b3ee086e2..11a208e21 100644 --- a/addons/default/visiosoft/advs-module/src/Http/Controller/AjaxController.php +++ b/addons/default/visiosoft/advs-module/src/Http/Controller/AjaxController.php @@ -54,7 +54,7 @@ class AjaxController extends PublicController { $datas = []; $catModel = new CategoryModel(); - $datas['category'] = $catModel->searchKeyword($request->q, $request->selected); + $datas['category'] = $catModel->searchKeyword($request->q); return response()->json($datas); } diff --git a/addons/default/visiosoft/advs-module/src/Http/Controller/advsController.php b/addons/default/visiosoft/advs-module/src/Http/Controller/advsController.php index a4bda2e53..2f98cb5de 100644 --- a/addons/default/visiosoft/advs-module/src/Http/Controller/advsController.php +++ b/addons/default/visiosoft/advs-module/src/Http/Controller/advsController.php @@ -16,9 +16,12 @@ use Visiosoft\AdvsModule\Adv\AdvModel; use Visiosoft\AdvsModule\Adv\Contract\AdvRepositoryInterface; use Visiosoft\AdvsModule\Adv\Event\ChangedStatusAd; use Visiosoft\AdvsModule\Adv\Event\CreatedAd; -use Visiosoft\AdvsModule\Adv\Event\priceChange; -use Visiosoft\AdvsModule\Adv\Event\showAdPhone; -use Visiosoft\AdvsModule\Adv\Event\viewAd; +use Visiosoft\AdvsModule\Adv\Event\EditAd; +use Visiosoft\AdvsModule\Adv\Event\EditedAd; +use Visiosoft\AdvsModule\Adv\Event\EditedAdCategory; +use Visiosoft\AdvsModule\Adv\Event\PriceChange; +use Visiosoft\AdvsModule\Adv\Event\ShowAdPhone; +use Visiosoft\AdvsModule\Adv\Event\ViewAd; use Visiosoft\AdvsModule\Adv\Form\AdvFormBuilder; use Visiosoft\AdvsModule\Option\Contract\OptionRepositoryInterface; use Visiosoft\AdvsModule\OptionConfiguration\Contract\OptionConfigurationRepositoryInterface; @@ -148,10 +151,9 @@ class AdvsController extends PublicController $isActiveDopings = $this->adv_model->is_enabled('dopings'); // Search by category slug - $categoryId = null; if ($category) { // Slug - $categoryId = $this->category_repository->findBy('slug', $category); - if (!$categoryId) { + $category = $this->category_repository->findBy('slug', $category); + if (!$category) { $this->messages->error(trans('visiosoft.module.advs::message.category_not_exist')); return redirect('/'); } @@ -159,19 +161,19 @@ class AdvsController extends PublicController unset($param['cat']); return redirect(fullLink( $param, - route('adv_list_seo', [$categoryId->slug]) + route('adv_list_seo', [$category->slug]) )); } } elseif (isset($param['cat']) && !empty($param['cat'])) { // Only Param - $categoryId = $this->category_repository->find($param['cat']); - if (!$categoryId) { + $category = $this->category_repository->find($param['cat']); + if (!$category) { $this->messages->error(trans('visiosoft.module.advs::message.category_not_exist')); return redirect('/'); } unset($param['cat']); return redirect(fullLink( $param, - route('adv_list_seo', [$categoryId->slug]) + route('adv_list_seo', [$category->slug]) )); } @@ -190,7 +192,7 @@ class AdvsController extends PublicController unset($param['city']); return redirect(fullLink( $param, - route('adv_list_seo', [$categoryId->slug, $cityId->slug]) + route('adv_list_seo', [$category->slug, $cityId->slug]) )); } elseif ($isOneCity) { // Param and slug $cityId = $this->cityRepository->find($param['city'][0]); @@ -198,13 +200,13 @@ class AdvsController extends PublicController unset($param['city']); return redirect(fullLink( $param, - route('adv_list_seo', [$categoryId->slug, $cityId->slug]) + route('adv_list_seo', [$category->slug, $cityId->slug]) )); } } elseif ($city && $isMultipleCity) { // Slug and multiple param cities return redirect(fullLink( $param, - route('adv_list_seo', [$categoryId->slug]), + route('adv_list_seo', [$category->slug]), array() )); } elseif ($city) { @@ -212,14 +214,14 @@ class AdvsController extends PublicController unset($param['city']); return redirect(fullLink( $param, - route('adv_list_seo', [$categoryId->slug]) + route('adv_list_seo', [$category->slug]) )); } else { // Only slug $cityId = $this->cityRepository->findBy('slug', $city); if (!$cityId) { return redirect(fullLink( $param, - route('adv_list_seo', [$categoryId->slug]) + route('adv_list_seo', [$category->slug]) ), 301); } } @@ -228,7 +230,7 @@ class AdvsController extends PublicController $isActiveCustomFields = $this->adv_model->is_enabled('customfields'); $advs = $this->adv_repository->searchAdvs( - 'list', $param, $customParameters, null, $categoryId, $cityId, false + 'list', $param, $customParameters, null, $category, $cityId, false ); if ($isActiveDopings) { @@ -264,25 +266,26 @@ class AdvsController extends PublicController $return_values = $cfRepository->getSeenList($advs); $return_values = $cfRepository - ->getSeenWithCategory($return_values['advs'], $return_values['seenList'], $categoryId); + ->getSeenWithCategory($return_values['advs'], $return_values['seenList'], $category); $advs = $return_values['advs']; $seenList = $return_values['seenList']; } - if ($categoryId) { - $mainCats = $this->category_model->getMains($categoryId->id); - $current_cat = $this->category_model->getCat($categoryId->id); - $mainCats[] = [ - 'id' => $current_cat->id, - 'val' => $current_cat->name, - 'slug' => $current_cat->slug, - ]; - $subCats = $this->category_repository->getSubCatById($categoryId->id); + if ($category) { + $mainCats = $this->category_repository->getParentCategoryById($category->id); + $subCats = $this->category_repository->getCategoryById($category->id); + + //if there is no subcategory + if (count($subCats) < 1) { + //fetch subcategories of the last category + $subCats = $this->category_repository->getCategoryById($mainCats[1]['id']); + unset($mainCats[0]);//remove last category + } $allCats = false; } else { - $mainCats = $this->category_repository->mainCats(); + $mainCats = $this->category_repository->getMainCategories(); $allCats = true; } @@ -379,11 +382,11 @@ class AdvsController extends PublicController $viewType = $this->requestHttp->cookie('viewType'); - list('catText' => $catText, 'user' => $user) = $this->handleSeo($categoryId, $mainCats, $cityId); + list('catText' => $catText, 'user' => $user) = $this->handleSeo($category, $mainCats, $cityId); $compact = compact('advs', 'countries', 'mainCats', 'subCats', 'checkboxes', 'param', 'user', 'featured_advs', 'viewType', 'topfields', 'selectDropdown', 'selectRange', 'selectImage', 'ranges', - 'seenList', 'radio', 'categoryId', 'cityId', 'allCats', 'catText', 'cFArray'); + 'seenList', 'radio', 'category', 'cityId', 'allCats', 'catText', 'cFArray'); return $this->viewTypeBasedRedirect($viewType, $compact); } @@ -395,10 +398,9 @@ class AdvsController extends PublicController $catText = ''; if ($category) { - $seo_keywords = $this->category_model->getMeta_keywords($category->id); - $seo_description = $this->category_model->getMeta_description($category->id); - - $metaTitle = $this->category_model->getMeta_title($category->id); + $seo_keywords = $category->getMetaKeywords(); + $seo_description = $category->getMetaDescription(); + $metaTitle = $category->name; $metaDesc = $seo_description; $this->template->set('meta_keywords', implode(', ', $seo_keywords)); @@ -509,7 +511,7 @@ class AdvsController extends PublicController for ($i = 1; $i <= 10; $i++) { $cat = "cat" . $i; if ($adv->$cat != null) { - $item = $this->category_repository->getItem($adv->$cat); + $item = $this->category_repository->find($adv->$cat); if (!is_null($item)) { $categories['cat' . $i] = [ 'name' => $item->name, @@ -541,7 +543,7 @@ class AdvsController extends PublicController $options = $this->optionRepository->findAllBy('adv_id', $id); - $this->event->dispatch(new viewAd($adv));//view ad + $this->event->dispatch(new ViewAd($adv));//view ad if (substr($adv->cover_photo, 0, 4) === "http") { $coverPhoto = $adv->cover_photo; @@ -607,7 +609,7 @@ class AdvsController extends PublicController for ($i = 1; $i <= 10; $i++) { $cat = "cat" . $i; if ($adv->$cat != null) { - $item = $this->category_repository->getItem($adv->$cat); + $item = $this->category_repository->find($adv->$cat); if (!is_null($item)) { $categories['cat' . $i] = [ 'name' => $item->name, @@ -670,7 +672,7 @@ class AdvsController extends PublicController public function getCats($id) { - return $this->category_repository->getSubCatById($id); + return $this->category_repository->getCategoryById($id); } public function getCatsForNewAd($id) @@ -688,14 +690,14 @@ class AdvsController extends PublicController return $cats; } - public function create(Request $request, AdvFormBuilder $formBuilder, CategoryRepositoryInterface $repository) + public function create(AdvFormBuilder $formBuilder, CategoryRepositoryInterface $repository) { if (!Auth::user()) { redirect('/login?redirect=' . url()->current())->send(); } $isActive = new AdvModel(); - $cats = $request->toArray(); + $cats = $this->request->toArray(); unset($cats['_token']); $end = count($cats); @@ -706,7 +708,7 @@ class AdvsController extends PublicController for ($i = 0; $i < $end; $i++) { $plus1 = $i + 1; - $cat = $repository->getSingleCat($cats['cat' . $plus1]); + $cat = $repository->find($cats['cat' . $plus1]); $cats_d['cat' . $plus1] = $cat->name; } if ($isActive->is_enabled('customfields')) { @@ -720,37 +722,31 @@ class AdvsController extends PublicController public function store ( AdvFormBuilder $form, - MessageBag $messages, - Request $request, - SettingRepositoryInterface $settings, - AdvRepositoryInterface $advRepository, - CategoryRepositoryInterface $categoryRepository, - Dispatcher $events, - AdvModel $advModel, AdressRepositoryInterface $address ) { - if (!Auth::user()) { - redirect('/login?redirect=' . url()->current())->send(); - } - $messages->pull('error'); - if ($request->action == "update") { - $error = $form->build($request->update_id)->validate()->getFormErrors()->getMessages(); + if ($this->request->action == "update") { + $error = $form->build($this->request->update_id)->validate()->getFormErrors()->getMessages(); if (!empty($error)) { return $this->redirect->back(); } - /* Update Adv */ - $adv = AdvsAdvsEntryModel::find($request->update_id); + /* Update Adv */ + $before_editing = $this->adv_repository->find($this->request->update_id); + + $adv = $before_editing; + + $is_new_create = ($adv->slug == "") ? true : false; //Set Old Price - $old_price = ($adv->slug == "") ? $request->price : $adv->price; + $old_price = ($adv->slug == "") ? $this->request->price : $adv->price; $adv->old_price = $old_price; $allowPendingAdCreation = false; - if ($advModel->is_enabled('packages') and $adv->slug == "") { - $cat = app('Visiosoft\PackagesModule\Http\Controller\PackageFEController')->AdLimitForNewAd($request); + + if (is_module_installed('visiosoft.module.packages') and $is_new_create) { + $cat = app('Visiosoft\PackagesModule\Http\Controller\PackageFEController')->AdLimitForNewAd($this->request); if (!is_null($cat)) { if (array_key_exists('allowPendingAds', $cat)) { $allowPendingAdCreation = $cat['allowPendingAds']; @@ -761,55 +757,64 @@ class AdvsController extends PublicController } // Create options - $deletedOptions = $request->deleted_options; - $newOptions = $request->new_options; + $deletedOptions = $this->request->deleted_options; + $newOptions = $this->request->new_options; + if (!empty($deletedOptions)) { - $deletedOptions = explode(',', $request->deleted_options); + $deletedOptions = explode(',', $this->request->deleted_options); $this->optionRepository->newQuery() ->whereIn('id', $deletedOptions) - ->where('adv_id', $request->update_id) + ->where('adv_id', $this->request->update_id) ->delete(); } + if (!empty($newOptions)) { - $newOptions = explode(',', $request->new_options); + $newOptions = explode(',', $this->request->new_options); foreach ($newOptions as $option) { $this->optionRepository->create([ 'name' => $option, - 'adv_id' => $request->update_id, + 'adv_id' => $this->request->update_id, ]); } } + //Get Categories Settings $get_categories_status = false; if ($get_categories = setting_value('visiosoft.module.advs::get_categories') and $get_categories = in_array($adv->cat1, $get_categories)) { $get_categories_status = true; } - $adv->is_get_adv = ($request->is_get_adv and $get_categories_status) ? true : false; + $adv->is_get_adv = ($this->request->is_get_adv and $get_categories_status) ? true : false; $adv->save(); + + //Todo Move To Module //Cloudinary Module - $isActiveCloudinary = $advModel->is_enabled('cloudinary'); - if ($isActiveCloudinary) { - + if (is_module_installed('visiosoft.module.cloudinary')) { $CloudinaryModel = new VideoModel(); - $CloudinaryModel->updateRequest($request); + $CloudinaryModel->updateRequest($this->request); - if ($request->url != "") { + if ($this->request->url != "") { $adv->save(); } } - if ($this->adv_model->is_enabled('customfields')) { - app('Visiosoft\CustomfieldsModule\Http\Controller\cfController')->store($adv, $request); + + + //Todo Create Event + if (is_module_installed('visiosoft.module.customfields')) { + app('Visiosoft\CustomfieldsModule\Http\Controller\cfController')->store($adv, $this->request); } - // Auto approve + //Todo Create Event + // Auto Approve $autoApprove = true; + if ($allowPendingAdCreation) { $adLogExists = app('Visiosoft\PackagesModule\AdvsLog\Contract\AdvsLogRepositoryInterface') ->findByAdID($adv->id); $autoApprove = $adLogExists ? false : true; } + if (setting_value('visiosoft.module.advs::auto_approve') && $autoApprove) { $defaultAdPublishTime = setting_value('visiosoft.module.advs::default_published_time'); $adv->update([ @@ -819,9 +824,9 @@ class AdvsController extends PublicController ]); } - $form->render($request->update_id); - $adv = $this->adv_repository->find($request->update_id); + $form->render($this->request->update_id); + //Create Adress if ($this->request->address_id != "") { $address = $address->find($this->request->address_id); $adv->country_id = $address->country_id; @@ -831,57 +836,72 @@ class AdvsController extends PublicController $adv->village = null; $adv->save(); } + + $post = $form->getPostData(); - $post['id'] = $request->update_id; - $events->dispatch(new priceChange($post));//price history - if ($request->url == "") { - $advRepository->cover_image_update($adv); + $post['id'] = $this->request->update_id; + + //Price Change Event + $this->event->dispatch(new PriceChange($post)); + + //Cover Image URL + if ($this->request->url == "") { + $this->adv_repository->cover_image_update($adv); } + if ($form->hasFormErrors()) { - $cats = $request->toArray(); + $cats = $this->request->toArray(); $cats_d = array(); foreach ($cats as $para => $value) { if (substr($para, 0, 3) === "cat") { $id = $cats[$para]; - $cat = $categoryRepository->getSingleCat($id); + $cat = $this->category_repository->find($id); if ($cat != null) { $cats_d[$para] = $cat->name; } } } - return redirect('/advs/edit_advs/' . $request->update_id)->with('cats_d', $cats_d)->with('request', $request); + return redirect('/advs/edit_advs/' . $this->request->update_id) + ->with('cats_d', $cats_d) + ->with('request', $this->request); } - event(new CreatedAd($adv)); - return redirect(route('advs_preview', [$request->update_id])); + + if ($is_new_create) { + event(new CreatedAd($adv)); + } else { + event(new EditedAd($before_editing, $adv)); + } + + return redirect(route('advs_preview', [$this->request->update_id])); } /* New Create Adv */ - $request->publish_at = date('Y-m-d H:i:s'); - $all = $request->all(); + $this->request->publish_at = date('Y-m-d H:i:s'); + $all = $this->request->all(); - $packageEnabled = $advModel->is_enabled('packages'); - if ($packageEnabled) { + if (is_module_installed('visiosoft.module.packages')) { unset($all['pack_id']); } - $new = AdvModel::query()->create($all); + $adv = $this->adv_repository->create($all); - if ($packageEnabled + if (is_module_installed('visiosoft.module.packages') && \request()->pack_id && setting_value('visiosoft.module.packages::allow_pending_ad_creation')) { $package = app('Visiosoft\PackagesModule\Package\Contract\PackageRepositoryInterface') ->find(\request()->pack_id); if ($package->price) { app('Visiosoft\PackagesModule\Http\Controller\PackageFEController') - ->packageAddCart(\request()->pack_id, $new->id); + ->packageAddCart(\request()->pack_id, $adv->id); } } - $events->dispatch(new EditAd($advModel)); - return redirect('/advs/edit_advs/' . $new->id); + $this->event->dispatch(new EditAd($adv)); + + return redirect('/advs/edit_advs/' . $adv->id); } public function edit($id) @@ -905,7 +925,7 @@ class AdvsController extends PublicController for ($i = 1; $i <= 10; $i++) { if ($adv[$cat . $i]) { - $name = $this->category_repository->getSingleCat($adv[$cat . $i]); + $name = $this->category_repository->find($adv[$cat . $i]); if ($name) { $cats_d['cat' . $i] = $name->name; $cats['cat' . $i] = $name->id; @@ -989,7 +1009,7 @@ class AdvsController extends PublicController if (!Auth::user()) { redirect('/login?redirect=' . url()->current())->send(); } - $main_cats = $this->category_repository->mainCats(); + $main_cats = $this->category_repository->getMainCategories(); return $this->view->make('visiosoft.module.advs::new-ad/post-cat', compact('main_cats')); } @@ -997,13 +1017,14 @@ class AdvsController extends PublicController public function editCategoryForAd($id) { $adv = $this->adv_model->userAdv(true)->find($id); + $before_editing_ad_params = $adv->toArray(); if (is_null($adv)) { abort(403); } - if ($this->requestHttp->action == 'update') { - $params = $this->requestHttp->all(); + if ($this->request->action == 'update') { + $params = $this->request->all(); unset($params['action']); for ($i = 1; $i <= 10; $i++) { @@ -1013,6 +1034,7 @@ class AdvsController extends PublicController } $adv->update($params); + $this->event->dispatch(new EditedAdCategory($before_editing_ad_params,$adv)); $this->messages->success(trans('visiosoft.module.advs::message.updated_category_msg')); return redirect('/advs/edit_advs/' . $id); } @@ -1023,9 +1045,9 @@ class AdvsController extends PublicController } - public function mapJson(Request $request, AdvRepositoryInterface $repository) + public function mapJson(AdvRepositoryInterface $repository) { - $param = $request->toArray(); + $param = $this->request->toArray(); $customParameters = array(); $advModel = new AdvModel(); @@ -1056,11 +1078,11 @@ class AdvsController extends PublicController return $this->redirect->back(); } - public function sold($id, Request $request, AdvModel $advModel) + public function sold($id, AdvModel $advModel) { - if ($request->sold == 'sold') { + if ($this->request->sold == 'sold') { $advModel->find($id)->update(['status' => 'sold']); - } elseif ($request->sold = 'not-sold') { + } elseif ($this->request->sold = 'not-sold') { $advModel->find($id)->update(['status' => 'approved']); } } diff --git a/addons/default/visiosoft/advs-module/src/OptionHandler/CategoriesOptions.php b/addons/default/visiosoft/advs-module/src/OptionHandler/CategoriesOptions.php index ce3958242..dcf8211d1 100644 --- a/addons/default/visiosoft/advs-module/src/OptionHandler/CategoriesOptions.php +++ b/addons/default/visiosoft/advs-module/src/OptionHandler/CategoriesOptions.php @@ -14,7 +14,7 @@ class CategoriesOptions public function handle(SelectFieldType $fieldType) { - $categories = $this->categoryRepository->mainCats(); + $categories = $this->categoryRepository->getMainCategories(); $options = $categories->pluck('name', 'id')->all(); $fieldType->setOptions($options); } diff --git a/addons/default/visiosoft/base-theme/resources/js/theme/initialize.js b/addons/default/visiosoft/base-theme/resources/js/theme/initialize.js index be480dac4..a52c5dc3e 100644 --- a/addons/default/visiosoft/base-theme/resources/js/theme/initialize.js +++ b/addons/default/visiosoft/base-theme/resources/js/theme/initialize.js @@ -1,5 +1,5 @@ -(function (window, document) { - - // Go! - -})(window, document); +// (function (window, document) { +// +// // Go! +// +// })(window, document); diff --git a/addons/default/visiosoft/base-theme/resources/views/partials/categories.twig b/addons/default/visiosoft/base-theme/resources/views/partials/categories.twig index 8b2d09a62..84650bc0b 100644 --- a/addons/default/visiosoft/base-theme/resources/views/partials/categories.twig +++ b/addons/default/visiosoft/base-theme/resources/views/partials/categories.twig @@ -39,7 +39,7 @@ {{ addBlock('base/categoriesRow')|raw }}
- {% set cats = getLevel2Cats() %} + {% set cats = getCategoriesLevel2() %} {% for main_category in cats.maincats %} {% include "visiosoft.theme.base::partials/categories-web" %} {% include "visiosoft.theme.base::partials/categories-mobile" %} diff --git a/addons/default/visiosoft/cats-module/resources/views/ads-list/partials/list-filter.twig b/addons/default/visiosoft/cats-module/resources/views/ads-list/partials/list-filter.twig index 81d0a953b..a241200d4 100644 --- a/addons/default/visiosoft/cats-module/resources/views/ads-list/partials/list-filter.twig +++ b/addons/default/visiosoft/cats-module/resources/views/ads-list/partials/list-filter.twig @@ -10,45 +10,37 @@
- {% for maincat in params.mainCats %} - {% set name = maincat['val'] %} - {% set id = maincat['id'] %} - {% set parent_category = true %} - {% if app.request.get('cat') is null or app.request.get('cat') == "" %} - {% if params.categoryId is null %} - {% set name = maincat.name %} - {% set id = maincat.id %} - {% set parent_category = false %} - {% endif %} + + {% set citySlug = null %} + {% set pathInfo = app.request.pathinfo|split('/') %} + {% if pathInfo|length is same as(4) %} + {% set citySlug = pathInfo[3] %} + {% endif %} + + + {% for maincat in params.mainCats|reverse %} + {% set url = appendRequestURL(request_query(),url_route('adv_list_seo', [maincat.slug, citySlug]),{},['page']) %} + {% if(viewType == "map") %} + {% set url = appendRequestURL(request_query(),url_route('advs_map_list'),{'cat':maincat.id},['page']) %} {% endif %} - {% set catId = entries('cats', 'category').find(id) %} - {% set citySlug = null %} - {% set pathInfo = app.request.pathinfo|split('/') %} - {% if pathInfo|length is same as(4) %} - {% set citySlug = pathInfo[3] %} - {% endif %} - + + - {{ name }} + {{ maincat.name }} - {% for subcat in params.subCats %} -
- {% if subcat.parent_category_id == maincat['id'] %} - {% set subCatId = entries('cats', 'category').find(subcat.id) %} - - {{ subcat.name }} - - {% endif %} -
- {% endfor %} + {% endfor %} + + {% for subcat in params.subCats %} +
+ {% set subCatId = entries('cats', 'category').find(subcat.id) %} + {% set url = appendRequestURL(request_query(),url_route('adv_list_seo', [subCatId.slug, citySlug]),{},['page']) %} + {% if(viewType == "map") %} + {% set url = appendRequestURL(request_query(),url_route('advs_map_list'),{'cat':subcat.id},['page']) %} + {% endif %} + + {{ subcat.name }} + +
{% endfor %}
diff --git a/addons/default/visiosoft/cats-module/resources/views/table/table.twig b/addons/default/visiosoft/cats-module/resources/views/table/table.twig index 5fc3bacf8..3063c04db 100644 --- a/addons/default/visiosoft/cats-module/resources/views/table/table.twig +++ b/addons/default/visiosoft/cats-module/resources/views/table/table.twig @@ -19,8 +19,8 @@
{% if app.request.get('cat') != null %}
@@ -37,7 +37,7 @@ {% endif %}
- {% if not table.rows.empty() %} + {% if count(table.rows) %} {% block card %} {{ form_open({ 'url': url_full() }) }}
diff --git a/addons/default/visiosoft/cats-module/src/Category/CategoryCollection.php b/addons/default/visiosoft/cats-module/src/Category/CategoryCollection.php index 8e3ced75b..2dcaf9f18 100644 --- a/addons/default/visiosoft/cats-module/src/Category/CategoryCollection.php +++ b/addons/default/visiosoft/cats-module/src/Category/CategoryCollection.php @@ -1,16 +1,14 @@ filter( - function ($category) { - return (is_null($category->parent_category_id)); - } - ); + $category_repository = app(CategoryRepositoryInterface::class); + return $category_repository->getMainCategories(); } } diff --git a/addons/default/visiosoft/cats-module/src/Category/CategoryCriteria.php b/addons/default/visiosoft/cats-module/src/Category/CategoryCriteria.php index 66c1390d1..b29907903 100644 --- a/addons/default/visiosoft/cats-module/src/Category/CategoryCriteria.php +++ b/addons/default/visiosoft/cats-module/src/Category/CategoryCriteria.php @@ -19,14 +19,7 @@ class CategoryCriteria extends EntryCriteria $this->categoryRepository = $categoryRepository; } - public function getMainCats() { - $mainCats = $this->categoryRepository->mainCats(); - - foreach ($mainCats as $cat) { - $subCount = $this->categoryRepository->newQuery()->where('parent_category_id', $cat->id)->count(); - $cat->hasChild = !!$subCount; - } - - return $mainCats; + public function getMainCategories() { + return $this->categoryRepository->getMainCategories(); } } diff --git a/addons/default/visiosoft/cats-module/src/Category/CategoryModel.php b/addons/default/visiosoft/cats-module/src/Category/CategoryModel.php index ccf3769cd..4eef167cc 100644 --- a/addons/default/visiosoft/cats-module/src/Category/CategoryModel.php +++ b/addons/default/visiosoft/cats-module/src/Category/CategoryModel.php @@ -1,196 +1,19 @@ where('cats_category.id', $id) - ->whereRaw('deleted_at IS NULL') - ->first(); + return $this->seo_keyword; } - public function getParentCats($id, $type = null, $noMainCat = true) + public function getMetaDescription() { - $cat = $this->getCat($id); - $catNames = array(); - $cat_ids = array(); - $catNames[] = $cat->name; - $cat_ids[] = $cat->id; - $subCat = $cat->parent_category_id; - if ($subCat != null) { - for ($i = 0; $i < 10; $i++) { - $parCat = $this->getCat($subCat); - if (isset($parCat)) { - if ($parCat->parent_category_id == "") { - if ($type == "add_main") - $catNames[] = $parCat->name; - if ($noMainCat) { - break; - } - } - $catNames[] = $parCat->name; - $cat_ids[] = $parCat->id; - $subCat = $parCat->parent_category_id; - } - } - } - if ($type == 'category_ids') { - return CategoryModel::query() - ->whereIn('cats_category.id', $cat_ids) - ->whereRaw('deleted_at IS NULL') - ->orderBy('cats_category.id', 'asc') - ->get(); - } - if ($type == "parent_id") { - $cat_ids = array_reverse($cat_ids); - return $cat_ids[0]; - } - return $catNames; - } - - public function getCatLevel($id) - { - //count parent and itself - return count($this->getParentCats($id)) + 1; - } - - public function getParentsCount($id) - { - $parentCats = array(); - $currentId = $id; - do { - $cat = $this->getCat($currentId); - $catParent = $cat->parent_category_id; - if ($catParent) { - $currentId = $catParent; - $parentCats[] = $catParent; - } - } while ($catParent); - return count($parentCats); - } - - public function getSubCategories($id, $get = null) - { - $sub_categories = $this->where('parent_category_id', $id)->get(); - if ($get == 'id') { - $list_categories_id = array(); - foreach ($sub_categories as $item_category) { - $list_categories_id[] = $item_category->id; - } - return $list_categories_id; - } - return $sub_categories; - } - - public function getAllSubCategories($id) - { - $sub = $this->getSubCategories($id, 'id'); - for ($i = 0; $i <= count($sub) - 1; $i++) { - $sub = array_merge($sub, $this->getSubCategories($sub[$i], 'id')); - } - return $sub; - } - - public function deleteSubCategories($id) - { - $subCategories = $this->getAllSubCategories($id); - if (count($subCategories)) { - $this->newQuery()->whereIn('id', $subCategories)->delete(); - } - - return true; - } - - public function searchKeyword($keyword, $selected = null) - { - $data = []; - $cats = DB::table('cats_category'); - if ($selected != null) { - if (strpos($selected, "-") !== false) { - $selected = explode('-', $selected); - $cats = $cats->whereNotIn('cats_category.id', $selected); - } else { - $cats = $cats->where('cats_category.id', '!=', $selected); - } - } - $cats = $cats->where('name', 'like', $keyword . '%') - ->whereRaw('deleted_at IS NULL'); - - $cats = $cats->leftJoin('cats_category_translations', function ($join) { - $join->on('cats_category.id', '=', 'cats_category_translations.entry_id'); - $join->whereIn('cats_category_translations.locale', [config('app.locale'), setting_value('streams::default_locale'), 'en']);//active lang - }); - $cats = $cats->select('cats_category.*', 'cats_category_translations.name as name'); - $cats = $cats->orderBy('id', 'DESC') - ->groupBy(['cats_category.id']) - ->get(); - foreach ($cats as $cat) { - $link = ''; - $parents = $this->getParentCats($cat->id, null, false); - krsort($parents); - foreach ($parents as $key => $parent) { - if ($key == 0) { - $link .= $parent . ''; - } else { - $link .= $parent . ' > '; - } - } - $data[] = array( - 'id' => $cat->id, - 'name' => $cat->name, - 'parents' => $link, - 'slug' => $cat->slug - ); - } - return $data; - } - - public function getMainCategory() - { - return $this->where('parent_category_id', NULL)->get(); - } - - public function getMeta_keywords($cat_id) - { - return $this->find($cat_id)->seo_keyword; - } - - public function getMeta_description($cat_id) - { - return $this->find($cat_id)->seo_description; - } - - public function getMeta_title($cat_id) - { - return $this->find($cat_id)->name; - } - - public function getMains($id) - { - $categories = array(); - $z = 1; - for ($i = 1; $i <= $z; $i++) { - if ($main = $this->newQuery()->where('id', $id)->first()) { - $new = array(); - $new['id'] = $main->id; - $new['val'] = $main->name; - $new['slug'] = $main->slug; - $categories[] = $new; - if ($main->parent_category_id != null) { - $id = $main->parent_category_id; - $z++; - } - } - } - $categories = array_reverse($categories); - unset($categories[count($categories) - 1]); - return $categories; + return $this->seo_description; } public function getParent() diff --git a/addons/default/visiosoft/cats-module/src/Category/CategoryPresenter.php b/addons/default/visiosoft/cats-module/src/Category/CategoryPresenter.php index 3d1791434..37ab0d61e 100644 --- a/addons/default/visiosoft/cats-module/src/Category/CategoryPresenter.php +++ b/addons/default/visiosoft/cats-module/src/Category/CategoryPresenter.php @@ -1,30 +1,19 @@ find($id); return $category->name; } - public function getname($id) + public function getParentCategoryById($id) { - $cat = CatsCategoryEntryModel::query()->find($id); - return $cat->name; - } - - public function getMains($id) - { - $category_model = new CategoryModel(); - return $category_model->getMains($id); + $category_repository = app(CategoryRepositoryInterface::class); + return $category_repository->getParentCategoryById($id); } } diff --git a/addons/default/visiosoft/cats-module/src/Category/CategoryRepository.php b/addons/default/visiosoft/cats-module/src/Category/CategoryRepository.php index 9d3af96a8..c3b01bcb5 100644 --- a/addons/default/visiosoft/cats-module/src/Category/CategoryRepository.php +++ b/addons/default/visiosoft/cats-module/src/Category/CategoryRepository.php @@ -1,148 +1,117 @@ model = $model; $this->advRepository = $advRepository; } - public function findById($id) + public function getMainCategories() { - return $this->model->orderBy('created_at', 'DESC')->where('cats_category.id', $id)->first(); + return $this->newQuery() + ->where('parent_category_id', null) + ->orderBy('sort_order') + ->get(); } - public function mainCats() - { - return $this->model->where('parent_category_id', null)->orderBy('sort_order')->get(); - } - public function getLevel2Cats() + public function getCategoriesLevel2() { $dBName = 'default_cats_category'; - $dBNamet = $dBName.'_translations'; + $dBNamet = $dBName . '_translations'; - $catsDB = DB::table((DB::raw($dBName.' c1'))) - ->select( - DB::raw('c1.id'), - DB::raw('c1.slug'), - DB::raw('c1.count'), - DB::raw('c1.parent_category_id'), - DB::raw('t1.name'), + $catsDB = DB::table((DB::raw($dBName . ' c1'))) + ->select( + DB::raw('c1.id'), + DB::raw('c1.slug'), + DB::raw('c1.count'), + DB::raw('c1.parent_category_id'), + DB::raw('t1.name'), - DB::raw('c2.id as c2_id'), - DB::raw('c2.slug as c2_slug'), - DB::raw('c2.count as c2_count'), - DB::raw('c2.parent_category_id as c2_parent_category_id'), - DB::raw('t2.name as c2_name') - ) - ->leftJoin((DB::raw($dBName.' c2')), DB::raw('c2.parent_category_id'), '=', DB::raw('c1.id')) - ->leftJoin((DB::raw($dBNamet.' t1')), DB::raw('c1.id'), '=', DB::raw('t1.entry_id')) - ->leftJoin((DB::raw($dBNamet.' t2')), DB::raw('c2.id'), '=', DB::raw('t2.entry_id')) - ->where(DB::raw('t1.locale'), Request()->session()->get('_locale', setting_value('streams::default_locale'))) - ->where(DB::raw('t2.locale'), Request()->session()->get('_locale', setting_value('streams::default_locale'))) - ->where(DB::raw("c1.deleted_at"),NULL) - ->where(DB::raw("c2.deleted_at"),NULL) - ->whereNull(DB::raw("c1.parent_category_id")) - ->orderBy(DB::raw("c1.sort_order")) - ->orderBy(DB::raw("c2.sort_order")) - ->get(); + DB::raw('c2.id as c2_id'), + DB::raw('c2.slug as c2_slug'), + DB::raw('c2.count as c2_count'), + DB::raw('c2.parent_category_id as c2_parent_category_id'), + DB::raw('t2.name as c2_name') + ) + ->leftJoin((DB::raw($dBName . ' c2')), DB::raw('c2.parent_category_id'), '=', DB::raw('c1.id')) + ->leftJoin((DB::raw($dBNamet . ' t1')), DB::raw('c1.id'), '=', DB::raw('t1.entry_id')) + ->leftJoin((DB::raw($dBNamet . ' t2')), DB::raw('c2.id'), '=', DB::raw('t2.entry_id')) + ->where(DB::raw('t1.locale'), Request()->session()->get('_locale', setting_value('streams::default_locale'))) + ->where(DB::raw('t2.locale'), Request()->session()->get('_locale', setting_value('streams::default_locale'))) + ->where(DB::raw("c1.deleted_at"), NULL) + ->where(DB::raw("c2.deleted_at"), NULL) + ->whereNull(DB::raw("c1.parent_category_id")) + ->orderBy(DB::raw("c1.sort_order")) + ->orderBy(DB::raw("c2.sort_order")) + ->get(); $cats = collect([]); $cats->subcats = $catsDB; $cats->maincats = $catsDB->unique('id'); return $cats; } - public function getItem($cat) + public function getCategoryById($id) { - return $this->model->where('cats_category.id', $cat)->first(); - } - - public function getCatById($id) - { - return $this->model->where('cats_category.id', $id)->where('deleted_at', null)->orderBy('sort_order')->get(); - } - - public function getSubCatById($id) - { - $cats = $this->model->newQuery() + return $this->newQuery() ->where('parent_category_id', $id) - ->get(); - - foreach ($cats as $cat) { - $subCount = $this->model->newQuery()->where('parent_category_id', $cat->id)->count(); - $cat->hasChild = !!$subCount; - } - - return $cats; - } - - public function getSingleCat($id) - { - return CatsCategoryEntryModel::query()->where('cats_category.id', $id)->first(); + ->where('deleted_at', null) + ->orderBy('sort_order')->get(); } public function findBySlug($slug) { - return $this->model->orderBy('created_at', 'DESC')->where('slug', $slug)->first(); + return $this->newQuery() + ->where('slug', $slug) + ->first(); } - public function getCategories() + public function getParentCategoryById($id) { - return $this->model->orderBy('sort_order')->get(); - } - - public function removeCatFromAds($category) - { - $catLevelNum = 1; - if (!is_null($category->parent_category_id)) { - $catLevelNum = $this->model->getCatLevel($category->id); - } - $catLevelText = "cat" . $catLevelNum; - - $advs = $this->advRepository->newQuery()->where($catLevelText, $category->id)->get(); - foreach ($advs as $adv) { - $nullableCats = array(); - for ($i = $catLevelNum; $i <= 10; $i++) { - $nullableCats['cat' . $i] = null; + if ($category = $this->find($id)) { + $parents_count = ($category->parent_category_id) ? 1 : 0; + $parents[] = $category; + for ($i = 0; $i < $parents_count; $i++) { + if ($category = $this->find($category->parent_category_id)) { + $parents[] = $category; + $parents_count++; + } } - $adv->update($nullableCats); + + return $parents; } + return null; } - public function DeleteCategories($id) + public function getLevelById($id) { - if (!is_null($category = $this->find($id))) { - // Remove deleted category from ads - $this->removeCatFromAds($category); - - // Delete the category - $this->model->find($id)->delete(); - - // Delete the subcategories - $this->model->deleteSubCategories($id); - } + return count($this->getParentCategoryById($id)); } -} + + public function getCategoriesByName($keyword) + { + $cats = DB::table('cats_category'); + + $cats = $cats->where('name', 'like', $keyword . '%') + ->whereRaw('deleted_at IS NULL'); + + $cats = $cats->leftJoin('cats_category_translations', function ($join) { + $join->on('cats_category.id', '=', 'cats_category_translations.entry_id'); + $join->whereIn('cats_category_translations.locale', [config('app.locale'), setting_value('streams::default_locale'), 'en']);//active lang + }) + ->select('cats_category.*', 'cats_category_translations.name as name') + ->orderBy('id', 'DESC')->groupBy(['cats_category.id'])->get(); + + return $cats; + } +} \ No newline at end of file diff --git a/addons/default/visiosoft/cats-module/src/Category/Command/getLevel2Cats.php b/addons/default/visiosoft/cats-module/src/Category/Command/getCategoriesLevel2.php similarity index 74% rename from addons/default/visiosoft/cats-module/src/Category/Command/getLevel2Cats.php rename to addons/default/visiosoft/cats-module/src/Category/Command/getCategoriesLevel2.php index d35413978..06bf3e951 100644 --- a/addons/default/visiosoft/cats-module/src/Category/Command/getLevel2Cats.php +++ b/addons/default/visiosoft/cats-module/src/Category/Command/getCategoriesLevel2.php @@ -3,10 +3,10 @@ namespace Visiosoft\CatsModule\Category\Command; use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface; -class getLevel2Cats +class getCategoriesLevel2 { public function handle(CategoryRepositoryInterface $repo) { - return $repo->getLevel2Cats(); + return $repo->getCategoriesLevel2(); } } diff --git a/addons/default/visiosoft/cats-module/src/Category/Contract/CategoryInterface.php b/addons/default/visiosoft/cats-module/src/Category/Contract/CategoryInterface.php index 0e6e89b7b..8cd765413 100644 --- a/addons/default/visiosoft/cats-module/src/Category/Contract/CategoryInterface.php +++ b/addons/default/visiosoft/cats-module/src/Category/Contract/CategoryInterface.php @@ -4,31 +4,9 @@ use Anomaly\Streams\Platform\Entry\Contract\EntryInterface; interface CategoryInterface extends EntryInterface { - public function getCat($id); + public function getMetaKeywords(); - public function getParentCats($id, $type = null); - - public function getCatLevel($id); - - public function getParentsCount($id); - - public function getSubCategories($id, $get = null); - - public function getAllSubCategories($id); - - public function deleteSubCategories($id); - - public function searchKeyword($keyword, $selected = null); - - public function getMainCategory(); - - public function getMeta_keywords($cat_id); - - public function getMeta_description($cat_id); - - public function getMeta_title($cat_id); - - public function getMains($id); + public function getMetaDescription(); public function getParent(); } diff --git a/addons/default/visiosoft/cats-module/src/Category/Contract/CategoryRepositoryInterface.php b/addons/default/visiosoft/cats-module/src/Category/Contract/CategoryRepositoryInterface.php index f9907814e..edc7d2303 100644 --- a/addons/default/visiosoft/cats-module/src/Category/Contract/CategoryRepositoryInterface.php +++ b/addons/default/visiosoft/cats-module/src/Category/Contract/CategoryRepositoryInterface.php @@ -4,23 +4,15 @@ use Anomaly\Streams\Platform\Entry\Contract\EntryRepositoryInterface; interface CategoryRepositoryInterface extends EntryRepositoryInterface { - public function findById($id); + public function getMainCategories(); - public function mainCats(); + public function getCategoriesLevel2(); - public function getLevel2Cats(); - - public function getItem($cat); - - public function getCatById($id); - - public function getSubCatById($id); - - public function getSingleCat($id); + public function getCategoryById($id); public function findBySlug($slug); - public function getCategories(); + public function getParentCategoryById($id); - public function DeleteCategories($id); + public function getLevelById($id); } diff --git a/addons/default/visiosoft/cats-module/src/Category/Listener/CalculatedTotalForEditedAdCategory.php b/addons/default/visiosoft/cats-module/src/Category/Listener/CalculatedTotalForEditedAdCategory.php new file mode 100644 index 000000000..9399febbb --- /dev/null +++ b/addons/default/visiosoft/cats-module/src/Category/Listener/CalculatedTotalForEditedAdCategory.php @@ -0,0 +1,57 @@ +categoryRepository = $categoryRepository; + } + + public function handle(EditedAdCategory $event) + { + $ad_detail = $event->getAdDetail()->toArray(); + $before_editing_ad = $event->getBeforeEditingParams(); + + //Categories New Ad + $category_fields_new_ad = preg_grep('/^cat/i', array_keys($ad_detail)); + $category_fields_new_ad = array_combine($category_fields_new_ad, $category_fields_new_ad); + + foreach ($category_fields_new_ad as $key => $field) { + $category_fields_new_ad[$key] = $ad_detail[$key]; + } + $category_fields_new = array_filter($category_fields_new_ad); + + //Categories Before Editing Ad + $category_fields_old_ad = preg_grep('/^cat/i', array_keys($before_editing_ad)); + $category_fields_old_ad = array_combine($category_fields_old_ad, $category_fields_old_ad); + + foreach ($category_fields_old_ad as $key => $field) { + $category_fields_old_ad[$key] = $before_editing_ad[$key]; + } + $category_fields_old = array_filter($category_fields_old_ad); + + //Update previous category Count + foreach ($category_fields_old as $category_id) { + if ($category = $this->categoryRepository->find($category_id)) { + $category->setAttribute('count', $category->count - 1); + $category->setAttribute('count_at', now()); + $category->save(); + } + } + + //Update New Category Count + foreach ($category_fields_new as $category_id) { + if ($category = $this->categoryRepository->find($category_id)) { + $category->setAttribute('count', $category->count + 1); + $category->setAttribute('count_at', now()); + $category->save(); + } + } + } +} \ No newline at end of file diff --git a/addons/default/visiosoft/cats-module/src/Category/Listener/CalculatedTotalForNewAd.php b/addons/default/visiosoft/cats-module/src/Category/Listener/CalculatedTotalForNewAd.php new file mode 100644 index 000000000..a6784ae6d --- /dev/null +++ b/addons/default/visiosoft/cats-module/src/Category/Listener/CalculatedTotalForNewAd.php @@ -0,0 +1,36 @@ +categoryRepository = $categoryRepository; + } + + public function handle(CreatedAd $event) + { + $ad_detail = $event->getAdDetail()->toArray(); + + $category_fields = preg_grep('/^cat/i', array_keys($ad_detail)); + $category_fields = array_combine($category_fields, $category_fields); + + foreach ($category_fields as $key => $field) { + $category_fields[$key] = $ad_detail[$key]; + } + + $category_fields = array_filter($category_fields); + + foreach ($category_fields as $category_id) { + if ($category = $this->categoryRepository->find($category_id)) { + $category->setAttribute('count', $category->count + 1); + $category->setAttribute('count_at', now()); + $category->save(); + } + } + } +} \ No newline at end of file diff --git a/addons/default/visiosoft/cats-module/src/Category/Table/CategoryTableBuilder.php b/addons/default/visiosoft/cats-module/src/Category/Table/CategoryTableBuilder.php index 902f0cfd1..595b5c978 100644 --- a/addons/default/visiosoft/cats-module/src/Category/Table/CategoryTableBuilder.php +++ b/addons/default/visiosoft/cats-module/src/Category/Table/CategoryTableBuilder.php @@ -86,7 +86,7 @@ class CategoryTableBuilder extends TableBuilder * @var array */ protected $options = [ - + 'table_view' => 'visiosoft.module.cats::table/table' ]; /** diff --git a/addons/default/visiosoft/cats-module/src/Category/Table/Handler/Delete.php b/addons/default/visiosoft/cats-module/src/Category/Table/Handler/Delete.php index 0dea822bf..dc9e2da8a 100644 --- a/addons/default/visiosoft/cats-module/src/Category/Table/Handler/Delete.php +++ b/addons/default/visiosoft/cats-module/src/Category/Table/Handler/Delete.php @@ -13,7 +13,7 @@ class Delete extends ActionHandler { try { foreach ($selected as $id) { - $categoryRepository->DeleteCategories($id); + //Todo Delete category and Sub Categories } if ($selected) { diff --git a/addons/default/visiosoft/cats-module/src/CatsModulePlugin.php b/addons/default/visiosoft/cats-module/src/CatsModulePlugin.php index ff2905ce8..25a712f94 100644 --- a/addons/default/visiosoft/cats-module/src/CatsModulePlugin.php +++ b/addons/default/visiosoft/cats-module/src/CatsModulePlugin.php @@ -3,14 +3,19 @@ use Anomaly\Streams\Platform\Addon\Plugin\Plugin; use Anomaly\Streams\Platform\Image\Command\MakeImageInstance; -use Visiosoft\CatsModule\Category\Command\getLevel2Cats; -use Visiosoft\CatsModule\Category\CategoryModel; -use Visiosoft\CatsModule\Category\CategoryRepository; +use Visiosoft\CatsModule\Category\Command\getCategoriesLevel2; use Visiosoft\CatsModule\Category\Command\GetCategoryName; use Visiosoft\CatsModule\Category\Command\GetCategoryDetail; +use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface; class CatsModulePlugin extends Plugin { + protected $categoryRepository; + + public function __construct(CategoryRepositoryInterface $categoryRepository) + { + $this->categoryRepository = $categoryRepository; + } /** * @return array @@ -41,14 +46,12 @@ class CatsModulePlugin extends Plugin ), new \Twig_SimpleFunction( 'category_parents_name', function ($id) { - $category_model = new CategoryModel(); - return $category_model->getParentCats($id, 'add_main'); + return $this->categoryRepository->getParentCategoryById($id); } ), new \Twig_SimpleFunction( 'getParentsCount', function ($id) { - $category_model = new CategoryModel(); - return $category_model->getParentsCount($id); + return $this->categoryRepository->getParentCategoryById($id) - 1; } ), new \Twig_SimpleFunction( 'catIcon', @@ -56,16 +59,16 @@ class CatsModulePlugin extends Plugin if ($path == "") { return $this->dispatch(new MakeImageInstance('visiosoft.theme.base::images/default-categories-icon.png', 'img'))->url(); } else { - return url('files/'.$path); + return url('files/' . $path); } } ), new \Twig_SimpleFunction( - 'getLevel2Cats', + 'getCategoriesLevel2', function () { - if (!$getLevel2Cats = $this->dispatch(new getLevel2Cats())) { + if (!$getCategoriesLevel2 = $this->dispatch(new getCategoriesLevel2())) { return 0; } - return $getLevel2Cats; + return $getCategoriesLevel2; } ) ]; diff --git a/addons/default/visiosoft/cats-module/src/CatsModuleServiceProvider.php b/addons/default/visiosoft/cats-module/src/CatsModuleServiceProvider.php index deb880a22..bc70385f1 100644 --- a/addons/default/visiosoft/cats-module/src/CatsModuleServiceProvider.php +++ b/addons/default/visiosoft/cats-module/src/CatsModuleServiceProvider.php @@ -1,11 +1,15 @@ [ - // Visiosoft\CatsModule\Listener\ExampleListener::class, - //], + CreatedAd::class => [ + CalculatedTotalForNewAd::class, + ], + EditedAdCategory::class => [ + CalculatedTotalForEditedAdCategory::class, + ], ]; /** @@ -193,7 +200,7 @@ class CatsModuleServiceProvider extends AddonServiceProvider 'category' => [ 'buttons' => [ 'new_category' => [ - 'href' => '/admin/cats/create?parent='.$request->cat + 'href' => '/admin/cats/create?parent=' . $request->cat ], ], ] diff --git a/addons/default/visiosoft/cats-module/src/Http/Controller/Admin/CategoryController.php b/addons/default/visiosoft/cats-module/src/Http/Controller/Admin/CategoryController.php index 3d44cf98c..cc727c9b6 100644 --- a/addons/default/visiosoft/cats-module/src/Http/Controller/Admin/CategoryController.php +++ b/addons/default/visiosoft/cats-module/src/Http/Controller/Admin/CategoryController.php @@ -6,6 +6,7 @@ use DateTime; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; +use Visiosoft\AdvsModule\Adv\Event\CreatedCategory; use Visiosoft\CatsModule\Category\CategoryModel; use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface; use Visiosoft\CatsModule\Category\Form\CategoryFormBuilder; @@ -36,7 +37,7 @@ class CategoryController extends AdminController if ($this->request->action == "delete") { $CategoriesModel = new CategoryModel(); foreach ($this->request->id as $item) { - $CategoriesModel->deleteSubCategories($item); + //Todo Delete sub Categories } } if (!isset($request->cat) || $request->cat == "") { @@ -113,7 +114,7 @@ class CategoryController extends AdminController } } if (empty($isMultiCat)) { - $this->categoryRepository->create(array_merge($translatableEntries, [ + $category = $this->categoryRepository->create(array_merge($translatableEntries, [ 'slug' => $all['slug'], 'parent_category' => $all['parent_category'] === "" ? null : $all['parent_category'], 'icon' => $all['icon'], @@ -135,6 +136,8 @@ class CategoryController extends AdminController } }; + $this->catLevelCalc(); + // $this->categoryRepository->create(array_merge($translatableEntries, [ // 'slug' => $all['slug'], // 'parent_category' => $all['parent_category'], @@ -186,9 +189,9 @@ class CategoryController extends AdminController public function delete(CategoryRepositoryInterface $categoryRepository, Request $request, CategoryModel $categoryModel, $id) { - $categoryRepository->DeleteCategories($id); + //Todo Delete Category and Sub Categories if ($request->parent != "") { - $subCats = $categoryRepository->getSubCatById($request->parent); + $subCats = $categoryRepository->getCategoryById($request->parent); if (count($subCats)) { return redirect('admin/cats?cat=' . $request->parent)->with('success', ['Category and related sub-categories deleted successfully.']); } @@ -205,7 +208,7 @@ class CategoryController extends AdminController $parentCat = $this->categoryRepository->find($parentCatId); if (is_null($parentCat) && !is_null($parentCatId)) { $this->categoryEntryTranslationsModel->where('entry_id', $cat->id)->delete(); - $this->categoryRepository->DeleteCategories($cat->id); + //Todo Delete Category and Sub Categories $deletedCatsCount++; } } @@ -250,8 +253,8 @@ class CategoryController extends AdminController ->get(); foreach ($result as $key => $data) { $id = $data->id; - $CategoriesModel = new CategoryModel(); - $level = $CategoriesModel->getCatLevel($id); + $category_repository = app(CategoryRepositoryInterface::class); + $level = $category_repository->getLevelById($id); DB::table('cats_category')->where('id',$id)->update(array( 'level'=>$level,