Skip to content

Commit

Permalink
pacote inicial de categorias
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Tamagawa committed May 26, 2017
1 parent 2a7d68f commit d9c2b72
Show file tree
Hide file tree
Showing 12 changed files with 715 additions and 2 deletions.
165 changes: 163 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.<br>
Após esses passos e necessario criar o diretorio Scopes dentro do pacote em que deseja implementar a categoria. <br>
E dentro deste diretorio Scopes um arquivo NomePacoteCategory.php <br>
Ex: FlavorsCategory.php que contera o seguinte codigo


```
<?php
namespace Mixdinternet\Flavors\Scopes;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
class FlavorsCategory implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function apply(Builder $builder, Model $model)
{
$builder->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

```
<?php
namespace Mixdinternet\Flavors;
use Mixdinternet\Categories\Category as BaseCategory;
use Mixdinternet\Flavors\Scopes\FlavorsCategory;
class Category extends BaseCategory
{
public static function boot()
{
static::addGlobalScope(new FlavorsCategory());
}
public function flavors()
{
return $this->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.

```
<?php
Route::group(['middleware' => ['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']);
});
});
```
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"homepage": "http://www.mixd.com.br/",
"role": "Developer"
}
],
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.3.*"
},
"autoload": {
"psr-4": {
"Mixdinternet\\Categories\\": "src/"
}
}
}
100 changes: 100 additions & 0 deletions src/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace Mixdinternet\Categories;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Cviebrock\EloquentSluggable\Sluggable;
use Venturecraft\Revisionable\RevisionableTrait;

class Category extends Model
{
use SoftDeletes, RevisionableTrait, Sluggable;

protected $revisionCreationsEnabled = true;

protected $revisionFormattedFieldNames = [
'name' => '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;
}
}
Loading

0 comments on commit d9c2b72

Please sign in to comment.