refactored cats repository and model && update Count for create ad and edit category

This commit is contained in:
vedatakd 2021-01-21 18:21:33 +03:00
parent bbe6879ad5
commit 57ee0c8b2d
37 changed files with 493 additions and 554 deletions

View File

@ -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();
},
],
],

View File

@ -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);
}
}

View File

@ -39,7 +39,7 @@
'neighborhoods':neighborhoods,
'villages':villages,
'param':param,
'categoryId':categoryId,
'category':category,
'cityId':cityId
})|raw }}

View File

@ -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 %}

View File

@ -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)

View File

@ -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;
}
}

View File

@ -0,0 +1,23 @@
<?php namespace Visiosoft\AdvsModule\Adv\Event;
class EditedAd
{
private $ad;
private $before_editing;
public function __construct($before_editing, $ad)
{
$this->ad = $ad;
$this->before_editing = $before_editing;
}
public function getAdDetail()
{
return $this->ad;
}
public function getBeforeEditingDetail()
{
return $this->before_editing;
}
}

View File

@ -0,0 +1,23 @@
<?php namespace Visiosoft\AdvsModule\Adv\Event;
class EditedAdCategory
{
private $ad;
private $before_editing;
public function __construct($before_editing_ad_params, $ad)
{
$this->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;
}
}

View File

@ -1,7 +1,7 @@
<?php namespace Visiosoft\AdvsModule\Adv\Event;
class priceChange
class PriceChange
{
public function __construct($request)
{

View File

@ -1,7 +1,7 @@
<?php namespace Visiosoft\AdvsModule\Adv\Event;
class showAdPhone
class ShowAdPhone
{
public function __construct($request)
{

View File

@ -1,7 +1,7 @@
<?php namespace Visiosoft\AdvsModule\Adv\Event;
class viewAd
class ViewAd
{
public function __construct($request)
{

View File

@ -0,0 +1,32 @@
<?php namespace Visiosoft\AdvsModule\Adv\Listener;
use Visiosoft\AdvsModule\Adv\Contract\AdvRepositoryInterface;
use Visiosoft\CatsModule\Category\Events\DeletedCategory;
class CategoryDeleted
{
public $advRepository;
public function __construct(AdvRepositoryInterface $advRepository)
{
$this->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);
}
}
}

View File

@ -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;
}
}
],
],

View File

@ -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(
[

View File

@ -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',

View File

@ -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);
}

View File

@ -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']);
}
}

View File

@ -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);
}

View File

@ -1,5 +1,5 @@
(function (window, document) {
// Go!
})(window, document);
// (function (window, document) {
//
// // Go!
//
// })(window, document);

View File

@ -39,7 +39,7 @@
</ul>
{{ addBlock('base/categoriesRow')|raw }}
<div class="col-12 p-0 m-0">
{% 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" %}

View File

@ -10,45 +10,37 @@
</div>
<div id="category" class="collapse show overflow-auto" aria-labelledby="categoryHeading" style="max-height: 300px;">
<div class="list-group">
{% 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 %}
<a href="{% if(viewType != "map") %}
{{ appendRequestURL(request_query(),url_route('adv_list_seo', [catId.slug, citySlug]),{},['page']) }}
{% else %}
{{ appendRequestURL(request_query(),url_route('advs_map_list'),{'cat':id},['page']) }}
{% endif %}" class="list-group-item list-group-item-action text-truncate">
<a href="{{ url }}" class="list-group-item list-group-item-action text-truncate">
<i class="fas fa-dot-circle"></i>
{{ name }}
{{ maincat.name }}
</a>
{% for subcat in params.subCats %}
<div class="list-group pl-3 bg-light">
{% if subcat.parent_category_id == maincat['id'] %}
{% set subCatId = entries('cats', 'category').find(subcat.id) %}
<a href="{% if(viewType != "map") %}
{{ appendRequestURL(request_query(),url_route('adv_list_seo', [subCatId.slug, citySlug]),{},['page']) }}
{% else %}
{{ appendRequestURL(request_query(),url_route('advs_map_list'),{'cat':subcat.id},['page']) }}
{% endif %}" class="list-group-item list-group-item-action text-truncate">
{{ subcat.name }}
</a>
{% endif %}
</div>
{% endfor %}
{% endfor %}
{% for subcat in params.subCats %}
<div class="list-group pl-3 bg-light">
{% 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 %}
<a href="{{ url }}" class="list-group-item list-group-item-action text-truncate">
{{ subcat.name }}
</a>
</div>
{% endfor %}
</div>
</div>

View File

@ -19,8 +19,8 @@
<div class="card-body pb-0">
{% if app.request.get('cat') != null %}
<ol class="breadcrumb">
{% for parnt in category_parents_name(app.request.get('cat'))|reverse %}
<li class="breadcrumb-item"><b>{{ parnt }}</b></li>
{% for parent in category_parents_name(app.request.get('cat'))|reverse %}
<li class="breadcrumb-item"><b>{{ parent.name }}</b></li>
{% endfor %}
</ol>
<div id="buttons">
@ -37,7 +37,7 @@
{% endif %}
</div>
<div class="card-body">
{% if not table.rows.empty() %}
{% if count(table.rows) %}
{% block card %}
{{ form_open({ 'url': url_full() }) }}
<div class="table-stack">

View File

@ -1,16 +1,14 @@
<?php namespace Visiosoft\CatsModule\Category;
use Anomaly\Streams\Platform\Entry\EntryCollection;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
class CategoryCollection extends EntryCollection
{
public function mainCategories()
public function getMainCategories()
{
return $this->filter(
function ($category) {
return (is_null($category->parent_category_id));
}
);
$category_repository = app(CategoryRepositoryInterface::class);
return $category_repository->getMainCategories();
}
}

View File

@ -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();
}
}

View File

@ -1,196 +1,19 @@
<?php namespace Visiosoft\CatsModule\Category;
use Illuminate\Support\Facades\DB;
use Visiosoft\CatsModule\Category\Contract\CategoryInterface;
use Anomaly\Streams\Platform\Model\Cats\CatsCategoryEntryModel;
class CategoryModel extends CatsCategoryEntryModel implements CategoryInterface
{
public function getCat($id)
public function getMetaKeywords()
{
return CategoryModel::query()
->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()

View File

@ -1,30 +1,19 @@
<?php namespace Visiosoft\CatsModule\Category;
use Anomaly\Streams\Platform\Entry\EntryPresenter;
use Anomaly\Streams\Platform\Model\Cats\CatsCategoryEntryModel;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
class CategoryPresenter extends EntryPresenter
{
public function getAdvsListUrl($attributes)
{
return \route('visiosoft.module.advs::list', "cat=" . $attributes);
}
public function getCategoryName($id)
{
$category = $this->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);
}
}

View File

@ -1,148 +1,117 @@
<?php namespace Visiosoft\CatsModule\Category;
use Anomaly\Streams\Platform\Model\Cats\CatsCategoryEntryModel;
use Visiosoft\AdvsModule\Adv\Contract\AdvRepositoryInterface;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
use Anomaly\Streams\Platform\Entry\EntryRepository;
use Illuminate\Support\Facades\DB;
use Eloquent\Support\Collection;
class CategoryRepository extends EntryRepository implements CategoryRepositoryInterface
{
/**
* The entry model.
*
* @var CategoryModel
*/
protected $model;
protected $advRepository;
/**
* Create a new CategoryRepository instance.
*
* @param CategoryModel $model
* @param AdvRepositoryInterface $advRepository
*/
public function __construct(CategoryModel $model, AdvRepositoryInterface $advRepository)
{
$this->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;
}
}

View File

@ -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();
}
}

View File

@ -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();
}

View File

@ -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);
}

View File

@ -0,0 +1,57 @@
<?php namespace Visiosoft\CatsModule\Category\Listener;
use Visiosoft\AdvsModule\Adv\Event\CreatedAd;
use Visiosoft\AdvsModule\Adv\Event\EditedAdCategory;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
class CalculatedTotalForEditedAdCategory
{
protected $categoryRepository;
public function __construct(CategoryRepositoryInterface $categoryRepository)
{
return $this->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();
}
}
}
}

View File

@ -0,0 +1,36 @@
<?php namespace Visiosoft\CatsModule\Category\Listener;
use Visiosoft\AdvsModule\Adv\Event\CreatedAd;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
class CalculatedTotalForNewAd
{
protected $categoryRepository;
public function __construct(CategoryRepositoryInterface $categoryRepository)
{
return $this->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();
}
}
}
}

View File

@ -86,7 +86,7 @@ class CategoryTableBuilder extends TableBuilder
* @var array
*/
protected $options = [
'table_view' => 'visiosoft.module.cats::table/table'
];
/**

View File

@ -13,7 +13,7 @@ class Delete extends ActionHandler
{
try {
foreach ($selected as $id) {
$categoryRepository->DeleteCategories($id);
//Todo Delete category and Sub Categories
}
if ($selected) {

View File

@ -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;
}
)
];

View File

@ -1,11 +1,15 @@
<?php namespace Visiosoft\CatsModule;
use Anomaly\Streams\Platform\Addon\AddonServiceProvider;
use Visiosoft\AdvsModule\Adv\Event\CreatedAd;
use Visiosoft\AdvsModule\Adv\Event\EditedAdCategory;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
use Visiosoft\CatsModule\Category\CategoryRepository;
use Anomaly\Streams\Platform\Model\Cats\CatsCategoryEntryModel;
use Visiosoft\CatsModule\Category\CategoryModel;
use Illuminate\Routing\Router;
use Visiosoft\CatsModule\Category\Listener\CalculatedTotalForEditedAdCategory;
use Visiosoft\CatsModule\Category\Listener\CalculatedTotalForNewAd;
class CatsModuleServiceProvider extends AddonServiceProvider
{
@ -93,9 +97,12 @@ class CatsModuleServiceProvider extends AddonServiceProvider
* @type array|null
*/
protected $listeners = [
//Visiosoft\CatsModule\Event\ExampleEvent::class => [
// 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
],
],
]

View File

@ -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,