ad create , category create and ad category edit event fixed

This commit is contained in:
vedatakd 2021-01-22 15:59:13 +03:00
parent d0af08575e
commit 94323aaac1
9 changed files with 142 additions and 107 deletions

View File

@ -91,7 +91,7 @@ class AdvCriteria extends EntryCriteria
public function countAdsByCategoryId($catId, $level = 1)
{
return $this->advRepository->countByCat($catId, $level);
return $this->advRepository->getAdsCountByCategory($catId, $level);
}
public function getCurrentLocale()

View File

@ -423,7 +423,7 @@ class AdvRepository extends EntryRepository implements AdvRepositoryInterface
return $ads;
}
public function countByCat($catID, $level = 1)
public function getAdsCountByCategory($catID, $level = 1)
{
return DB::table('advs_advs')
->whereDate('finish_at', '>=', date("Y-m-d H:i:s"))

View File

@ -39,7 +39,7 @@ interface AdvRepositoryInterface extends EntryRepositoryInterface
public function getByCat($catID, $level = 1, $limit = 20);
public function countByCat($catID, $level = 1);
public function getAdsCountByCategory($catID, $level = 1);
public function getCategoriesWithAdID($id);

View File

@ -29,7 +29,6 @@ use Visiosoft\AdvsModule\Productoption\Contract\ProductoptionRepositoryInterface
use Visiosoft\AdvsModule\ProductoptionsValue\Contract\ProductoptionsValueRepositoryInterface;
use Visiosoft\CatsModule\Category\CategoryModel;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
use Visiosoft\CloudinaryModule\Video\VideoModel;
use Visiosoft\FavsModule\Http\Controller\FavsController;
use Visiosoft\LocationModule\City\CityModel;
use Visiosoft\LocationModule\City\CityRepository;
@ -529,17 +528,7 @@ class AdvsController extends PublicController
$features = app('Visiosoft\CustomfieldsModule\Http\Controller\cfController')->view($adv);
}
//Cloudinary Module
$adv->video_url = null;
if ($this->adv_model->is_enabled('cloudinary')) {
$CloudinaryModel = new VideoModel();
$Cloudinary = $CloudinaryModel->getVideo($id);
if (count($Cloudinary->get()) > 0) {
$adv->video_url = $Cloudinary->first()->toArray()['url'];
}
}
$options = $this->optionRepository->findAllBy('adv_id', $id);
@ -714,7 +703,7 @@ class AdvsController extends PublicController
if ($isActive->is_enabled('customfields')) {
$custom_fields = app('Visiosoft\CustomfieldsModule\Http\Controller\cfController')->create($categories);
}
//Cloudinary Module
return $this->view->make('visiosoft.module.advs::new-ad/new-create', compact(
'request', 'formBuilder', 'cats_d', 'custom_fields'));
}
@ -788,18 +777,6 @@ class AdvsController extends PublicController
$adv->save();
//Todo Move To Module
//Cloudinary Module
if (is_module_installed('visiosoft.module.cloudinary')) {
$CloudinaryModel = new VideoModel();
$CloudinaryModel->updateRequest($this->request);
if ($this->request->url != "") {
$adv->save();
}
}
//Todo Create Event
if (is_module_installed('visiosoft.module.customfields')) {
app('Visiosoft\CustomfieldsModule\Http\Controller\cfController')->store($adv, $this->request);
@ -938,18 +915,6 @@ class AdvsController extends PublicController
$options = $this->optionRepository->findAllBy('adv_id', $id);
//Cloudinary Module
$Cloudinary = null;
$isActiveCloudinary = $this->adv_model->is_enabled('cloudinary');
if ($isActiveCloudinary) {
$CloudinaryModel = new VideoModel();
$Cloudinary = $CloudinaryModel->getVideo($id)->get();
if (count($Cloudinary) > 0) {
$Cloudinary = $Cloudinary->first()->toArray();
}
}
$categories = array_keys($cats);
$custom_fields = array();
@ -960,7 +925,7 @@ class AdvsController extends PublicController
return $this->view->make(
'visiosoft.module.advs::new-ad/new-create',
compact('id', 'cats_d', 'cats', 'Cloudinary', 'adv', 'custom_fields', 'options')
compact('id', 'cats_d', 'cats', 'adv', 'custom_fields', 'options')
);
}

View File

@ -0,0 +1,55 @@
<?php namespace Visiosoft\CatsModule\Category\Command;
use DateTime;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\DB;
use Visiosoft\AdvsModule\Adv\Contract\AdvRepositoryInterface;
class CalculateAdsCount
{
use DispatchesJobs;
protected $category_id;
public function __construct($category_id = null)
{
$this->category_id = $category_id;
}
public function handle()
{
$date = new DateTime;
$date->modify('-30 minutes');
$formatted_date = $date->format('Y-m-d H:i:s');
$query = DB::table('cats_category')
->select('id', 'level');
if ($this->category_id) {
$category = $query->where('id', $this->category_id)->first();
$this->calculateCategory($category->id, $category->level);
} else {
$result = $query->where('count_at', '<', $formatted_date)
->orWhereNull('count_at')->get();
foreach ($result as $key => $category) {
$this->calculateCategory($category->id, $category->level);
}
}
}
public function calculateCategory($category_id, $level)
{
$advRepository = app(AdvRepositoryInterface::class);
if (!empty($level)) {
$count = $advRepository->getAdsCountByCategory($category_id, $level);
DB::table('cats_category')->where('id', $category_id)->update(array(
'count' => $count,
'count_at' => now(),
));
} else {
$this->dispatch(new CalculateCategoryLevel($category_id));
}
}
}

View File

@ -0,0 +1,53 @@
<?php namespace Visiosoft\CatsModule\Category\Command;
use DateTime;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\DB;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
class CalculateCategoryLevel
{
use DispatchesJobs;
protected $category_id;
public function __construct($category_id = null)
{
$this->category_id = $category_id;
}
public function handle()
{
if ($this->category_id) {
$this->calculateLevelByCategory($this->category_id);
} else {
$date = new DateTime;
$date->modify('-30 minutes');
$formatted_date = $date->format('Y-m-d H:i:s');
$result = DB::table('cats_category')
->select('id')
->where('level_at', '<', $formatted_date)
->where('level', '=', 0)
->orWhereNull('level_at')
->get();
foreach ($result as $key => $category) {
$this->calculateLevelByCategory($category->id);
}
}
}
public function calculateLevelByCategory($category_id)
{
$categoryRepository = app(CategoryRepositoryInterface::class);
$level = $categoryRepository->getLevelById($category_id);
DB::table('cats_category')->where('id', $category_id)
->update(array(
'level' => $level,
'level_at' => now(),
));
}
}

View File

@ -1,11 +1,14 @@
<?php namespace Visiosoft\CatsModule\Category\Listener;
use Visiosoft\AdvsModule\Adv\Event\CreatedAd;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Visiosoft\AdvsModule\Adv\Event\EditedAdCategory;
use Visiosoft\CatsModule\Category\Command\CalculateAdsCount;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
class CalculatedTotalForEditedAdCategory
{
use DispatchesJobs;
protected $categoryRepository;
public function __construct(CategoryRepositoryInterface $categoryRepository)
@ -38,20 +41,12 @@ class CalculatedTotalForEditedAdCategory
//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();
}
$this->dispatch(new CalculateAdsCount($category_id));
}
//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();
}
$this->dispatch(new CalculateAdsCount($category_id));
}
}
}

View File

@ -1,10 +1,13 @@
<?php namespace Visiosoft\CatsModule\Category\Listener;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Visiosoft\AdvsModule\Adv\Event\CreatedAd;
use Visiosoft\CatsModule\Category\Command\CalculateAdsCount;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
class CalculatedTotalForNewAd
{
use DispatchesJobs;
protected $categoryRepository;
public function __construct(CategoryRepositoryInterface $categoryRepository)
@ -26,11 +29,7 @@ class CalculatedTotalForNewAd
$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();
}
$this->dispatch(new CalculateAdsCount($category_id));
}
}
}

View File

@ -1,13 +1,11 @@
<?php namespace Visiosoft\CatsModule\Http\Controller\Admin;
use Anomaly\Streams\Platform\Image\Command\MakeImageInstance;
use Anomaly\Streams\Platform\Model\Cats\CatsCategoryEntryTranslationsModel;
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\Command\CalculateAdsCount;
use Visiosoft\CatsModule\Category\Command\CalculateCategoryLevel;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
use Visiosoft\CatsModule\Category\Form\CategoryFormBuilder;
use Visiosoft\CatsModule\Category\Table\CategoryTableBuilder;
@ -35,7 +33,6 @@ class CategoryController extends AdminController
public function index(CategoryTableBuilder $table, Request $request)
{
if ($this->request->action == "delete") {
$CategoriesModel = new CategoryModel();
foreach ($this->request->id as $item) {
//Todo Delete sub Categories
}
@ -121,23 +118,27 @@ class CategoryController extends AdminController
'seo_keyword' => $all['seo_keyword'],
'seo_description' => $all['seo_description'],
]));
$this->dispatch(new CalculateCategoryLevel($category->id));
} else {
for ($i = 0; $i < count($isMultiCat[0]); $i++) {
foreach ($isMultiCat as $cat) {
$translatableEntries = array_merge($translatableEntries, $cat[$i]);
}
$this->categoryRepository->create(array_merge($translatableEntries, [
$category = $this->categoryRepository->create(array_merge($translatableEntries, [
'slug' => $this->str->slug(reset($translatableEntries)['name'], '_'),
'parent_category' => $all['parent_category'] === "" ? null : $all['parent_category'],
'icon' => $all['icon'],
'seo_keyword' => $all['seo_keyword'],
'seo_description' => $all['seo_description'],
]));
$this->dispatch(new CalculateCategoryLevel($category->id));
}
};
$this->catLevelCalc();
// $this->categoryRepository->create(array_merge($translatableEntries, [
// 'slug' => $all['slug'],
// 'parent_category' => $all['parent_category'],
@ -214,54 +215,21 @@ class CategoryController extends AdminController
}
return redirect('admin/cats')->with('success', [$deletedCatsCount . ' categories has been deleted.']);
}
public function adCountCalc(AdvRepositoryInterface $advRepository)
{
$date = new DateTime;
$date2 = new DateTime;
$date->modify('-30 minutes');
$formatted_date = $date->format('Y-m-d H:i:s');
$result = DB::table('cats_category')
->select('id','level')
->where('count_at','<',$formatted_date)
->orWhereNull('count_at')
->get();
foreach ($result as $key => $data) {
$id = $data->id;
$level = $data->level;
if(!empty($level)) {
$count = $advRepository->countByCat($id, $level);
DB::table('cats_category')->where('id',$id)->update(array(
'count'=>$count,
'count_at'=>$date2,
));
}
}
public function adCountCalc()
{
$this->dispatch(new CalculateAdsCount());
$this->messages->success(trans('streams::message.edit_success', ['name' => trans('visiosoft.module.cats::addon.title')]));
return redirect('admin/cats');
}
public function catLevelCalc()
{
$date = new DateTime;
$date2 = new DateTime;
$date->modify('-30 minutes');
$formatted_date = $date->format('Y-m-d H:i:s');
$this->dispatch(new CalculateCategoryLevel());
$result = DB::table('cats_category')
->select('id')
->where('level_at','<',$formatted_date)
->where('level','=',0)
->orWhereNull('level_at')
->get();
foreach ($result as $key => $data) {
$id = $data->id;
$category_repository = app(CategoryRepositoryInterface::class);
$level = $category_repository->getLevelById($id);
DB::table('cats_category')->where('id',$id)->update(array(
'level'=>$level,
'level_at'=>$date2,
));
}
return redirect('admin/cats')->with('success', ['Updated']);
$this->messages->success(trans('streams::message.edit_success', ['name' => trans('visiosoft.module.cats::addon.title')]));
return redirect('admin/cats');
}
}