diff --git a/README.md b/README.md index 5cba8d0..f81d041 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,163 @@ -# categories -Pacote básico de categorias +## Categorias + +Pacote para implementar categorias em seus pacotes. + +## Instalação + +Adicione no seu composer.json + +```js + "require": { + "mixdinternet/categories": "0.1.*" + } +``` + +ou + +```js + composer require mixdinternet/categories +``` + +## Service Provider + +Abra o arquivo `config/app.php` e adicione + +`Mixdinternet\Categories\Providers\CategoriesServiceProvider::class` + +## Migrations + +``` + php artisan vendor:publish --provider="Mixdinternet\Categories\Providers\CategoriesServiceProvider" --tag="migrations"` + php artisan migrate +``` + +## Configurações + +É possivel a troca de icone e nomenclatura do pacote em `config/mcategories.php` + +``` + php artisan vendor:publish --provider="Mixdinternet\Categories\Providers\CategoriesServiceProvider" --tag="config"` +``` + +## Utilização + +* Acrescentar diretorios e arquivos ao pacote em que deseja implementar categorias + +Como exemplo usaremos um pacote com o nome Flavor.
+Após esses passos e necessario criar o diretorio Scopes dentro do pacote em que deseja implementar a categoria.
+E dentro deste diretorio Scopes um arquivo NomePacoteCategory.php
+Ex: FlavorsCategory.php que contera o seguinte codigo + + +``` + where('type', 'flavors'); + } + } +``` +Nesses trechos trocar de acordo com o nome do seu pacote + +``` +namespace Mixdinternet\Flavors\Scopes; +``` + +``` + class FlavorsCategory implements Scope + ``` + + ``` + $builder->where('type', 'flavors'); + ``` + + * Na pasta base do seu pacote criar o arquivo Category.php contendo o seguinte codigo + +``` +hasMany(Flavor::class); + } + } + ``` + + * Sera necessario alterar de acordo com o nome do seu pacote nos seguintes trechos + + trocar o namespace de acordo com o seu pacote + ``` + namespace Mixdinternet\Flavors; + ``` + + Incluir o arquivo anteriormente criado + ``` + use Mixdinternet\Flavors\Scopes\FlavorsCategory; + ``` + + + ``` + static::addGlobalScope(new FlavorsCategory()); + ``` + + Este e o relacionamento com o seu pacote + ``` +public function flavors() +{ + return $this->hasMany(Flavor::class); +} + ``` + + ### Arquivo de rotas do pacote + + Acrescentar em seu arquivo de rotas e substituir ``` {categoryType} ``` pelo nome de seu pacote. + +``` + ['web'], 'prefix' => config('admin.url'), 'as' => 'admin.flavors'], function () { + Route::group(['middleware' => ['auth.admin', 'auth.rules']], function () { + + Route::get('{categoryType}/categories/trash', ['uses' => '\Mixdinternet\Categories\Http\Controllers\CategoriesAdminController@index', 'as' => '.categories.trash']); + Route::post('{categoryType}/categories/restore/{id}', ['uses' => '\Mixdinternet\Categories\Http\Controllers\CategoriesAdminController@restore', 'as' => '.categories.restore']); + Route::resource('{categoryType}/categories', '\Mixdinternet\Categories\Http\Controllers\CategoriesAdminController', [ + 'names' => [ + 'index' => '.categories.index', + 'create' => '.categories.create', + 'store' => '.categories.store', + 'edit' => '.categories.edit', + 'update' => '.categories.update', + 'show' => '.categories.show', + ], 'except' => ['destroy']]); + Route::delete('{categoryType}/categories/destroy', ['uses' => '\Mixdinternet\Categories\Http\Controllers\CategoriesAdminController@destroy', 'as' => '.categories.destroy']); + + }); +}); + ``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4e9cb4c --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "mixdinternet/categories", + "description": "Pacote básico de categorias", + "keywords": ["laravel", "categorias", "categories"], + "license": "MIT", + "authors": [ + { + "name": "Irineu Martins Junior", + "email": "irineu@mixd.com.br", + "homepage": "http://www.mixd.com.br/", + "role": "Developer" + } + ], + "require": { + "php": ">=5.5.9", + "laravel/framework": "5.3.*" + }, + "autoload": { + "psr-4": { + "Mixdinternet\\Categories\\": "src/" + } + } +} diff --git a/src/Category.php b/src/Category.php new file mode 100755 index 0000000..3cfef56 --- /dev/null +++ b/src/Category.php @@ -0,0 +1,100 @@ + 'nome', + 'slug' => 'nome amigável', + 'description' => 'descrição', + ]; + + protected $dates = ['deleted_at']; + + protected $fillable = ['status', 'name', 'description', 'image', 'type']; + + public $translatable = ['name', 'description', 'slug']; + + public function sluggable() + { + return [ + 'slug' => [ + 'source' => 'name' + ] + ]; + } + + public function __construct(array $attributes = []) + { + $this->hasAttachedFile('image', [ + 'styles' => [ + 'crop' => function ($file, $imagine) { + $image = $imagine->open($file->getRealPath()); + if (request()->input('crop.image.w', 0) > 0 && request()->input('crop.image.y', 0) > 0) { + $image->crop(new \Imagine\Image\Point(request()->input('crop.image.x'), request()->input('crop.image.y')) + , new \Imagine\Image\Box(request()->input('crop.image.w'), request()->input('crop.image.h'))); + } + return $image; + } + ], + /*'default_url' => '/assets/img/avatar.png',*/ + ]); + + parent::__construct($attributes); + } + + public static function boot() + { + parent::boot(); + + static::bootStapler(); + } + + public function toArray() + { + return array_merge(parent::toArray(), [ + 'image' => $this->attachedFiles['image']->url() + , 'image_crop' => $this->attachedFiles['image']->url('crop') + ]); + } + + + public function scopeSort($query, $fields = []) + { + if (count($fields) <= 0) { + $fields = [ + 'status' => 'asc', + 'name' => 'asc' + ]; + } + + if (request()->has('field') && request()->has('sort')) { + $fields = [request()->get('field') => request()->get('sort')]; + } + + foreach ($fields as $field => $order) { + $query->orderBy($field, $order); + } + } + + public function scopeActive($query) + { + $query->where('status', 'active')->sort(); + } + + # revision + public function identifiableName() + { + return $this->name; + } +} diff --git a/src/Http/Controllers/CategoriesAdminController.php b/src/Http/Controllers/CategoriesAdminController.php new file mode 100755 index 0000000..7697073 --- /dev/null +++ b/src/Http/Controllers/CategoriesAdminController.php @@ -0,0 +1,114 @@ +categoryType = request()->segment(2); + view()->share('categoryType', $this->categoryType); + } + + public function index(Request $request) + { + session()->put('backUrl', request()->fullUrl()); + + $trash = ($request->segment(4) == 'trash') ? true : false; + + $query = Category::sort(); + $query->where('type', $this->categoryType); + ($trash) ? $query->onlyTrashed() : ''; + + $search = []; + $search['name'] = $request->input('name', ''); + $search['status'] = $request->input('status', ''); + + ($search['name']) ? $query->where('name', 'LIKE', '%' . $search['name'] . '%') : ''; + ($search['status']) ? $query->where('status', $search['status']) : ''; + + $categories = $query->paginate(50); + + $view['search'] = $search; + $view['categories'] = $categories; + $view['trash'] = $trash; + + return view('mixdinternet/categories::admin.index', $view); + } + + public function create(Category $category) + { + $view['category'] = $category; + + return view('mixdinternet/categories::admin.form', $view); + } + + public function store(CreateEditCategoriesRequest $request) + { + $data = $request->all(); + $data['type'] = $this->categoryType; + if (Category::create($data)) { + Flash::success('Item inserido com sucesso.'); + } else { + Flash::error('Falha no cadastro.'); + } + + return ($url = session()->get('backUrl')) ? redirect($url) : redirect()->route('admin.' . $this->categoryType . '.categories.index'); + } + + public function edit(Category $category) + { + $view['category'] = $category; + + return view('mixdinternet/categories::admin.form', $view); + } + + public function update(Category $category, CreateEditCategoriesRequest $request) + { + $data = $request->all(); + $data['type'] = $this->categoryType; + if ($category->update($data)) { + Flash::success('Item atualizado com sucesso.'); + } else { + Flash::error('Falha na atualização.'); + } + + return ($url = session()->get('backUrl')) ? redirect($url) : redirect()->route('admin.' . $this->categoryType . '.categories.index'); + } + + public function destroy(Request $request) + { + if (Category::destroy($request->input('id'))) { + Flash::success('Item removido com sucesso.'); + } else { + Flash::error('Falha na remoção.'); + } + + return ($url = session()->get('backUrl')) ? redirect($url) : redirect()->route('admin.categories.index'); + } + + public function restore($id) + { + $category = Category::onlyTrashed()->find($id); + + if (!$category) { + abort(404); + } + + if ($category->restore()) { + Flash::success('Item restaurado com sucesso.'); + } else { + Flash::error('Falha na restauração.'); + } + + return back(); + } +} \ No newline at end of file diff --git a/src/Http/Requests/CreateEditCategoriesRequest.php b/src/Http/Requests/CreateEditCategoriesRequest.php new file mode 100755 index 0000000..de18192 --- /dev/null +++ b/src/Http/Requests/CreateEditCategoriesRequest.php @@ -0,0 +1,20 @@ + 'required' + ]; + } + + public function authorize() + { + return true; + } +} \ No newline at end of file diff --git a/src/Providers/CategoriesServiceProvider.php b/src/Providers/CategoriesServiceProvider.php new file mode 100755 index 0000000..041da2f --- /dev/null +++ b/src/Providers/CategoriesServiceProvider.php @@ -0,0 +1,56 @@ +loadViews(); + + $this->loadMigrations(); + + $this->publish(); + + } + + public function register() + { + $this->loadConfigs(); + } + + + protected function loadMigrations() + { + $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); + } + + protected function loadViews() + { + $this->loadViewsFrom(__DIR__ . '/../resources/views', 'mixdinternet/categories'); + } + + protected function loadConfigs() + { + $this->mergeConfigFrom(__DIR__ . '/../config/maudit.php', 'maudit.alias'); + $this->mergeConfigFrom(__DIR__ . '/../config/mcategories.php', 'mcategories'); + } + + protected function publish() + { + $this->publishes([ + __DIR__ . '/../resources/views' => base_path('resources/views/vendor/mixdinternet/categories'), + ], 'views'); + + $this->publishes([ + __DIR__ . '/../database/migrations' => base_path('database/migrations'), + ], 'migrations'); + + $this->publishes([ + __DIR__ . '/../config' => base_path('config'), + ], 'config'); + } +} \ No newline at end of file diff --git a/src/config/maudit.php b/src/config/maudit.php new file mode 100644 index 0000000..e0e948e --- /dev/null +++ b/src/config/maudit.php @@ -0,0 +1,5 @@ + config('mcategories.name', 'Categorias') +]; \ No newline at end of file diff --git a/src/config/mcategories.php b/src/config/mcategories.php new file mode 100644 index 0000000..99ac7e8 --- /dev/null +++ b/src/config/mcategories.php @@ -0,0 +1,23 @@ + 'Categorias' + , 'icon' => 'fa fa-align-left' + , 'order' => 50 + + /* + para desabilitar a imagem passe false como valor + , 'image' => false + + para habilitar a imagem passe o array com o width e height + , 'image' => [ + 'width' => 640 + , 'height' => 480 + ] + */ + , 'image' => [ + 'width' => 640 + , 'height' => 480 + ] + +]; \ No newline at end of file diff --git a/src/database/migrations/2017_02_24_095800_create_categories_table.php b/src/database/migrations/2017_02_24_095800_create_categories_table.php new file mode 100755 index 0000000..26f8a5f --- /dev/null +++ b/src/database/migrations/2017_02_24_095800_create_categories_table.php @@ -0,0 +1,31 @@ +increments('id'); + $table->string('type')->default('articles'); + $table->string('status')->default('active'); + $table->boolean('star')->default(0); + $table->string('name', 255); + $table->string('image_file_name')->nullable(); + $table->integer('image_file_size')->nullable(); + $table->string('image_content_type')->nullable(); + $table->timestamp('image_updated_at')->nullable(); + $table->string('slug', 255); + $table->timestamps(); + $table->softDeletes(); + }); + } + + public function down() + { + Schema::drop('categories'); + } +} diff --git a/src/resources/views/admin/form.blade.php b/src/resources/views/admin/form.blade.php new file mode 100755 index 0000000..aefc794 --- /dev/null +++ b/src/resources/views/admin/form.blade.php @@ -0,0 +1,49 @@ +@extends('mixdinternet/admix::form') + +@section('title') + Gerenciar categorias +@endsection + +@section('form') + +@endsection \ No newline at end of file diff --git a/src/resources/views/admin/index.blade.php b/src/resources/views/admin/index.blade.php new file mode 100755 index 0000000..ef1ced9 --- /dev/null +++ b/src/resources/views/admin/index.blade.php @@ -0,0 +1,115 @@ +@extends('mixdinternet/admix::index') + +@section('title') + Listagem de categorias +@endsection + +@section('btn-insert') + @if((!checkRule('admin.' . $categoryType . '.categories.create')) && (!$trash)) + @include('mixdinternet/admix::partials.actions.btn.insert', ['route' => route('admin.' . $categoryType . '.categories.create')]) + @endif + @if((!checkRule('admin.' . $categoryType . '.categories.trash')) && (!$trash)) + @include('mixdinternet/admix::partials.actions.btn.trash', ['route' => route('admin.' . $categoryType . '.categories.trash')]) + @endif + @if($trash) + @include('mixdinternet/admix::partials.actions.btn.list', ['route' => route('admin.' . $categoryType . '.categories.index')]) + @endif +@endsection + +@section('btn-delete-all') + @if((!checkRule('admin.' . $categoryType . '.categories.destroy')) && (!$trash)) + @include('mixdinternet/admix::partials.actions.btn.delete-all', ['route' => route('admin.' . $categoryType . '.categories.destroy')]) + @endif +@endsection + +@section('search') + {!! Form::model($search, ['route' => ($trash) ? 'admin.' . $categoryType . '.categories.trash' : 'admin.' . $categoryType . '.categories.index', 'method' => 'get', 'id' => 'form-search' + , 'class' => '']) !!} +
+
+ {!! BootForm::select('status', 'Status', ['' => '-', 'active' => 'Ativo', 'inactive' => 'Inativo'], null + , ['class' => 'jq-select2']) !!} +
+
+ {!! BootForm::text('name', 'Nome') !!} +
+
+ +
+
+
+ + + + + +
+
+
+ {!! Form::close() !!} +@endsection + +@section('table') + @if (count($categories) > 0) + + + + @if((!checkRule('admin.' . $categoryType . '.categories.destroy')) && (!$trash)) + + @endif + + + + + + + + + @foreach ($categories as $category) + + @if((!checkRule('admin.' . $categoryType . '.categories.destroy')) && (!$trash)) + + @endif + + + + + + + @endforeach + +
+
+ + +
+
{!! columnSort('#', ['field' => 'id', 'sort' => 'asc']) !!}{!! columnSort('Nome', ['field' => 'name', 'sort' => 'asc']) !!}{!! columnSort('Status', ['field' => 'status', 'sort' => 'asc']) !!}{!! columnSort('Destaque', ['field' => 'star', 'sort' => 'asc']) !!}
+ @include('mixdinternet/admix::partials.actions.checkbox', ['row' => $category]) + {{ $category->id }}{{ $category->name }}@include('mixdinternet/admix::partials.label.status', ['status' => $category->status])@include('mixdinternet/admix::partials.label.yes-no', ['yesNo' => $category->star]) + @if((!checkRule('admin.' . $categoryType . '.categories.edit')) && (!$trash)) + @include('mixdinternet/admix::partials.actions.btn.edit', ['route' => route('admin.' . $categoryType . '.categories.edit', ['category' => $category->id])]) + @endif + @if((!checkRule('admin.' . $categoryType . '.categories.destroy')) && (!$trash)) + @include('mixdinternet/admix::partials.actions.btn.delete', ['route' => route('admin.' . $categoryType . '.categories.destroy'), 'id' => $category->id]) + @endif + @if($trash) + @include('mixdinternet/admix::partials.actions.btn.restore', ['route' => route('admin.' . $categoryType . '.categories.restore', ['categories' => $category->id]), 'id' => $category->id]) + @endif +
+ @else + @include('mixdinternet/admix::partials.nothing-found') + @endif +@endsection + +@section('pagination') + {!! $categories->appends(request()->except(['page']))->render() !!} +@endsection + +@section('pagination-showing') + @include('mixdinternet/admix::partials.pagination-showing', ['model' => $categories]) +@endsection \ No newline at end of file diff --git a/src/routes/web.php b/src/routes/web.php new file mode 100644 index 0000000..e703091 --- /dev/null +++ b/src/routes/web.php @@ -0,0 +1,16 @@ + '\Mixdinternet\Categories\Http\Controllers\CategoriesAdminController@index', 'as' => '.categories.trash']); + Route::post('downloads/categories/restore/{id}', ['uses' => '\Mixdinternet\Categories\Http\Controllers\CategoriesAdminController@restore', 'as' => '.categories.restore']); + Route::resource('downloads/categories', '\Mixdinternet\Categories\Http\Controllers\CategoriesAdminController', [ + 'names' => [ + 'index' => '.categories.index', + 'create' => '.categories.create', + 'store' => '.categories.store', + 'edit' => '.categories.edit', + 'update' => '.categories.update', + 'show' => '.categories.show', + ], 'except' => ['destroy']]); + Route::delete('downloads/categories/destroy', ['uses' => '\Mixdinternet\Categories\Http\Controllers\CategoriesAdminController@destroy', 'as' => '.categories.destroy']); +*/ \ No newline at end of file