optimized query && category icon

This commit is contained in:
vedatakd 2021-01-23 13:18:59 +03:00
parent 94323aaac1
commit 5473dd7006
25 changed files with 369 additions and 130 deletions

View File

@ -1,26 +0,0 @@
<?php
use Anomaly\Streams\Platform\Database\Migration\Migration;
class VisiosoftModuleAdvsCreateUserStatusField extends Migration
{
protected $delete = false;
protected $stream = [
'slug' => 'advs',
];
protected $fields = [
"user_status" => [
"type" => "anomaly.field_type.relationship",
"config" => [
"related" => \Visiosoft\AdvsModule\Status\StatusModel::class,
"mode" => "lookup",
]
],
];
protected $assignments = [
'user_status',
];
}

View File

@ -1,12 +1,10 @@
<?php namespace Visiosoft\AdvsModule\Adv\Event;
use Visiosoft\AdvsModule\Adv\AdvModel;
class ChangedStatusAd
{
private $ad;
public function __construct(AdvModel $ad)
public function __construct($ad)
{
$this->ad = $ad;
}

View File

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

View File

@ -51,7 +51,9 @@ class AdvTableBuilder extends TableBuilder
* @var array|string
*/
protected $actions = [
'delete',
'delete' => [
'handler' => \Visiosoft\AdvsModule\Adv\Table\Handler\Delete::class,
],
'approve' => [
'handler' => \Visiosoft\AdvsModule\Adv\Table\Handler\Approve::class,
'class' => 'btn btn-success'

View File

@ -15,14 +15,16 @@ class Approve extends ActionHandler
foreach ($selected as $id) {
$defaultAdPublishTime = $settingRepository->value('visiosoft.module.advs::default_published_time');
$ad = $model->newQuery()->find($id);
if ($ad = $model->newQuery()->find($id)) {
$ad->update([
'status' => 'approved',
'finish_at' => date('Y-m-d H:i:s', strtotime(date('Y-m-d H:i:s') . ' + ' . $defaultAdPublishTime . ' day')),
'publish_at' => date('Y-m-d H:i:s')
]);
event(new ChangedStatusAd($ad));//Create Notify
event(new ChangedStatusAd($ad));//Create Notify
}
}
if ($selected) {

View File

@ -0,0 +1,52 @@
<?php namespace Visiosoft\AdvsModule\Adv\Table\Handler;
use Anomaly\SettingsModule\Setting\Contract\SettingRepositoryInterface;
use Anomaly\Streams\Platform\Model\EloquentModel;
use Anomaly\Streams\Platform\Ui\Table\Component\Action\ActionHandler;
use Anomaly\Streams\Platform\Ui\Table\TableBuilder;
use Visiosoft\AdvsModule\Adv\Event\ChangedStatusAd;
use Visiosoft\AdvsModule\Adv\Event\DeletedAd;
use Visiosoft\AdvsModule\Adv\Table\AdvTableBuilder;
class Delete extends ActionHandler
{
public function handle(TableBuilder $builder, array $selected)
{
$count = 0;
$model = $builder->getTableModel();
/* @var EloquentModel $entry */
foreach ($selected as $id) {
$entry = $model->find($id);
$deletable = true;
if ($entry instanceof EloquentModel) {
$deletable = $entry->isDeletable();
}
if ($entry && $deletable && $entry->delete()) {
$builder->fire('row_deleted', compact('builder', 'model', 'entry'));
event(new DeletedAd($entry));
$count++;
}
}
if ($count) {
$builder->fire('rows_deleted', compact('count', 'builder', 'model'));
}
if ($selected && $count > 0) {
$this->messages->success(trans('streams::message.delete_success', compact('count')));
}
if ($selected && $count === 0) {
$this->messages->warning(trans('streams::message.delete_success', compact('count')));
}
}
}

View File

@ -957,7 +957,9 @@ class AdvsController extends PublicController
}
$this->adv_model->statusAds($id, $type);
event(new ChangedStatusAd($ad));//Create Notify
if ($type === 'approved') {
$message = trans('visiosoft.module.advs::message.approve_status_change');
} elseif ($type === 'sold') {

View File

@ -1,7 +1,10 @@
<ul class="categories-list px-0 mr-2 d-block d-sm-none">
<li class="categories-list-li category-icon text-truncate category-{{ main_category.slug }}">
<a href="{{ url_route('adv_list_seo', [main_category.slug]) }}" class="main-category">
<img src="{{ catIcon(main_category.icon.path) }}" alt="{{ main_category.name }}" class="img-responsive">
{% set icon = img(url('local://category_icon/'~main_category.id~'.png')) %}
{% set categoryIcon = (icon.data()) ? icon.url() : img('visiosoft.theme.base::images/default-categories-icon.png').url %}
<img src="{{ categoryIcon }}" alt="{{ main_category.name }}" class="img-responsive">
<span>{{ main_category.name }}</span>
<p class="p-0 sub-categories">
{% for subcats in cats.subcats %}

View File

@ -1,6 +1,10 @@
<ul class="categories-list p-0 d-none d-sm-block">
<li class="categories-list-li category-icon category-{{ main_category.slug }}">
<img src="{{ catIcon(main_category.icon.path) }}" alt="{{ main_category.name }}" class="img-responsive">
{% set icon = img(url('local://category_icon/'~main_category.id~'.png')) %}
{% set categoryIcon = (icon.data()) ? icon.url() : img('visiosoft.theme.base::images/default-categories-icon.png').url %}
<img src="{{ categoryIcon }}" alt="{{ main_category.name }}" class="img-responsive">
<a href="{{ url_route('adv_list_seo', [main_category.slug]) }}" class="main-category">
<b>{{ main_category.name }}</b>
{% if showAdsCount %}

View File

@ -1,6 +1,6 @@
{{ asset_add("scripts.js", "streams::js/table/table.js") }}
{% if not actions.empty() %}
{% if not actions.isEmpty() %}
{{ asset_add("scripts.js", "streams::js/table/actions.js") }}
{% endif %}
@ -9,13 +9,12 @@
{% endif %}
<div class="{{ table.options.container_class ?: 'container-fluid' }}">
{{ view("streams::table/partials/filters", {'table': table}) }}
<div class="card card-custom gutter-b table-responsive">
<div class="card-body pb-0">
{{ view("streams::table/partials/filters", {'table': table}) }}
{{ view("streams::table/partials/views", {'table': table}) }}
{{ view(table.options.heading ?: "streams::table/partials/heading", {'table': table}) }}
</div>
<div class="card-body pb-0">
{% if app.request.get('cat') != null %}
<ol class="breadcrumb">
@ -36,10 +35,12 @@
</div>
{% endif %}
</div>
<div class="card-body">
{% if count(table.rows) %}
{% if not table.rows.isEmpty() %}
{% block card %}
{{ form_open({ 'url': url_full() }) }}
<div class="card">
{{ form_open({ 'url': table.options.url ?: url_full() }) }}
<div class="table-stack">
<table
class="
@ -60,6 +61,8 @@
</table>
</div>
{{ form_close() }}
</div>
{% endblock %}
{% else %}
@ -72,7 +75,5 @@
{% endblock %}
{% endif %}
</div>
</div>
</div>

View File

@ -25,9 +25,7 @@ class CalculateAdsCount
$query = DB::table('cats_category')
->select('id', 'level');
if ($this->category_id) {
$category = $query->where('id', $this->category_id)->first();
if ($this->category_id and $category = $query->where('id', $this->category_id)->first()) {
$this->calculateCategory($category->id, $category->level);
} else {
$result = $query->where('count_at', '<', $formatted_date)

View File

@ -0,0 +1,24 @@
<?php namespace Visiosoft\CatsModule\Category\Listener;
use Visiosoft\AdvsModule\Adv\Event\ChangedStatusAd;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
use Visiosoft\CatsModule\Category\Listener\Traits\CalculateAdTrait;
class CalculatedTotalForChangedAdStatus
{
use CalculateAdTrait;
protected $categoryRepository;
public function __construct(CategoryRepositoryInterface $categoryRepository)
{
return $this->categoryRepository = $categoryRepository;
}
public function handle(ChangedStatusAd $event)
{
$ad_detail = $event->getAdDetail()->toArray();
$this->calculateAdAction($ad_detail);
}
}

View File

@ -0,0 +1,24 @@
<?php namespace Visiosoft\CatsModule\Category\Listener;
use Visiosoft\AdvsModule\Adv\Event\DeletedAd;
use Visiosoft\CatsModule\Category\Contract\CategoryRepositoryInterface;
use Visiosoft\CatsModule\Category\Listener\Traits\CalculateAdTrait;
class CalculatedTotalForDeletedAd
{
use CalculateAdTrait;
protected $categoryRepository;
public function __construct(CategoryRepositoryInterface $categoryRepository)
{
return $this->categoryRepository = $categoryRepository;
}
public function handle(DeletedAd $event)
{
$ad_detail = $event->getAdDetail()->toArray();
$this->calculateAdAction($ad_detail);
}
}

View File

@ -1,13 +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;
use Visiosoft\CatsModule\Category\Listener\Traits\CalculateAdTrait;
class CalculatedTotalForNewAd
{
use DispatchesJobs;
use CalculateAdTrait;
protected $categoryRepository;
public function __construct(CategoryRepositoryInterface $categoryRepository)
@ -19,17 +19,6 @@ class CalculatedTotalForNewAd
{
$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) {
$this->dispatch(new CalculateAdsCount($category_id));
}
$this->calculateAdAction($ad_detail);
}
}

View File

@ -0,0 +1,26 @@
<?php namespace Visiosoft\CatsModule\Category\Listener\Traits;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Visiosoft\CatsModule\Category\Command\CalculateAdsCount;
trait CalculateAdTrait
{
use DispatchesJobs;
public function calculateAdAction($ad_detail)
{
$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) {
$this->dispatch(new CalculateAdsCount($category_id));
}
}
}

View File

@ -1,15 +1,20 @@
<?php namespace Visiosoft\CatsModule;
use Anomaly\Streams\Platform\Addon\AddonServiceProvider;
use Visiosoft\AdvsModule\Adv\Event\ChangedStatusAd;
use Visiosoft\AdvsModule\Adv\Event\CreatedAd;
use Visiosoft\AdvsModule\Adv\Event\DeletedAd;
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\CalculatedTotalForChangedAdStatus;
use Visiosoft\CatsModule\Category\Listener\CalculatedTotalForDeletedAd;
use Visiosoft\CatsModule\Category\Listener\CalculatedTotalForEditedAdCategory;
use Visiosoft\CatsModule\Category\Listener\CalculatedTotalForNewAd;
use Visiosoft\CatsModule\Category\Table\Handler\Delete;
class CatsModuleServiceProvider extends AddonServiceProvider
{
@ -103,6 +108,12 @@ class CatsModuleServiceProvider extends AddonServiceProvider
EditedAdCategory::class => [
CalculatedTotalForEditedAdCategory::class,
],
ChangedStatusAd::class => [
CalculatedTotalForChangedAdStatus::class,
],
DeletedAd::class => [
CalculatedTotalForDeletedAd::class,
],
];
/**

View File

@ -1,8 +1,13 @@
<?php namespace Visiosoft\CatsModule\Http\Controller\Admin;
use Anomaly\FilesModule\File\FileSanitizer;
use Anomaly\FilesModule\File\FileUploader;
use Anomaly\FilesModule\Folder\Contract\FolderRepositoryInterface;
use Anomaly\Streams\Platform\Model\Cats\CatsCategoryEntryTranslationsModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use League\Flysystem\MountManager;
use Visiosoft\CatsModule\Category\CategoryModel;
use Visiosoft\CatsModule\Category\Command\CalculateAdsCount;
use Visiosoft\CatsModule\Category\Command\CalculateCategoryLevel;
@ -54,9 +59,11 @@ class CategoryController extends AdminController
return $table->render();
}
public function create(CategoryFormBuilder $form, Request $request)
public function create(FileUploader $uploader, FolderRepositoryInterface $folderRepository, MountManager $manager)
{
if ($this->request->action == "save") {
$all = $this->request->all();
$id = $all['parent_category'];
$parent_id = $all['parent_category'];
@ -114,13 +121,13 @@ class CategoryController extends AdminController
$category = $this->categoryRepository->create(array_merge($translatableEntries, [
'slug' => $all['slug'],
'parent_category' => $all['parent_category'] === "" ? null : $all['parent_category'],
'icon' => $all['icon'],
'seo_keyword' => $all['seo_keyword'],
'seo_description' => $all['seo_description'],
]));
$this->createIconFile($category->getId());
$this->dispatch(new CalculateCategoryLevel($category->id));
$this->dispatch(new CalculateCategoryLevel($category->getId()));
} else {
for ($i = 0; $i < count($isMultiCat[0]); $i++) {
@ -130,12 +137,13 @@ class CategoryController extends AdminController
$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->createIconFile($category->getId());
$this->dispatch(new CalculateCategoryLevel($category->getId()));
}
};
@ -177,6 +185,9 @@ class CategoryController extends AdminController
if ($form->hasFormErrors()) {
return $this->redirect->back();
}
$this->createIconFile($id);
$parent = $request->parent_category;
if ($parent != "") {
return $this->redirect->to('/admin/cats?cat=' . $parent);
@ -232,4 +243,24 @@ class CategoryController extends AdminController
return redirect('admin/cats');
}
public function createIconFile($category_id)
{
$folderRepository = app(FolderRepositoryInterface::class);
$manager = app(MountManager::class);
if ($file = $this->request->file('icon') and $folder = $folderRepository->findBySlug('category_icon')) {
$type = explode('.', $file->getClientOriginalName());
$type = end($type);
$file_location = $folder->getDisk()->getSlug() . '://' . $folder->getSlug() . '/' . FileSanitizer::clean($category_id . "." . $type);
if (Storage::exists($file_location)) {
Storage::delete($file_location);
}
$manager->put($file_location, file_get_contents($file->getRealPath()));
}
}
}

View File

@ -0,0 +1,22 @@
{
"name": "visiosoft/input_file-field_type",
"type": "streams-addon",
"authors": [
{
"name": "Visiosoft, LTD.",
"email": "support@visiosoft.com.tr",
"homepage": "https://visiosoft.com.tr/",
"role": "Owner"
},
{
"name": "Vedat Akdoğan",
"email": "vedat@visiosoft.com.tr",
"role": "Developer"
}
],
"autoload": {
"psr-4": {
"Visiosoft\\InputFileFieldType\\": "src/"
}
}
}

View File

@ -0,0 +1,7 @@
<?php
return [
'title' => 'Input File',
'name' => 'Input File Field Type',
'description' => ''
];

View File

@ -0,0 +1,5 @@
<?php
return [
'choose_file' => 'Choose File',
];

View File

@ -0,0 +1,5 @@
<div class="form-group pb-1">
<input type="file" id="input_file" name="{{ field_type.getInputName() }}" {{ html_attributes(field_type.attributes) }} {{ field_type.disabled ? 'disabled' }} {{ field_type.required ? 'required' }} {{ field_type.readonly ? 'readonly' }} accept="image/png">
</div>
{#Todo:: Check Accept Input File Field Type#}

View File

@ -0,0 +1,8 @@
<?php namespace Visiosoft\InputFileFieldType;
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
class InputFileFieldType extends FieldType
{
protected $inputView = 'visiosoft.field_type.input_file::input';
}

View File

@ -0,0 +1,19 @@
<?php namespace Visiosoft\InputFileFieldType;
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypePresenter;
use Collective\Html\HtmlBuilder;
class InputFileFieldTypePresenter extends FieldTypePresenter
{
protected $html;
public function __construct(HtmlBuilder $html, $object)
{
$this->html = $html;
parent::__construct($object);
}
}

View File

@ -190,5 +190,20 @@ class DatabaseSeeder extends Seeder
],
]);
};
//Create Category Icon Folder
if (!$this->folders->findBySlug('category_icon')) {
$disk = $this->disks->findBySlug('local');
$this->folders->create([
'en' => [
'name' => 'Category Icon',
'description' => 'A folder for Category Icon.',
],
'slug' => 'category_icon',
'disk' => $disk
]);
};
}
}