Skip to content

Commit

Permalink
Fix #15
Browse files Browse the repository at this point in the history
  • Loading branch information
gravitano committed Nov 19, 2014
1 parent 19ea7b2 commit f066805
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/Pingpong/Admin/AdminServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class AdminServiceProvider extends ServiceProvider {
'Pingpong\Menus\MenusServiceProvider',
'Pingpong\Trusty\TrustyServiceProvider',
'Pingpong\Admin\Providers\ConsoleServiceProvider',
'Pingpong\Admin\Providers\ErrorServiceProvider',
];

/**
Expand Down
16 changes: 13 additions & 3 deletions src/Pingpong/Admin/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,22 @@ protected function installPackage()

if ($this->confirm('Do you want publish configuration files from pingpong/admin package ?'))
{
$this->call('config:publish', ['package' => 'pingpong/admin']);
if( ! $this->option('bench'))
{
$this->call('config:publish', ['package' => 'pingpong/admin']);
}
}

if ($this->confirm('Do you want publish assets from pingpong/admin package ?'))
{
$this->call('asset:publish', ['package' => 'pingpong/admin']);
if($this->option('bench'))
{
$this->call('asset:publish', ['--bench' => 'pingpong/admin']);
}
else
{
$this->call('asset:publish', ['package' => 'pingpong/admin']);
}
}

if ($this->confirm('Do you want create the app/menus.php file ?'))
Expand Down Expand Up @@ -142,7 +152,7 @@ protected function installMenus()
}
else
{
$this->line("File already exists at path : {$file}");
$this->error("File already exists at path : {$file}");
}
}

Expand Down
20 changes: 8 additions & 12 deletions src/Pingpong/Admin/Controllers/CategoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ public function create()
*/
public function store()
{
$category = Category::create($this->inputAll());
app('Pingpong\Admin\Validation\Category\Create')
->validate($data = $this->inputAll());

if ($category->save())
{
return $this->redirect('categories.index');
}
$category = Category::create($data);

return \Redirect::back()->withInput()->withErrors($category->getErrors());
return $this->redirect('categories.index');
}

/**
Expand Down Expand Up @@ -105,14 +103,12 @@ public function update($id)
{
try
{
$category = Category::findOrFail($id);
app('Pingpong\Admin\Validation\Category\Update')
->validate($data = $this->inputAll());

$category->update($this->inputAll());
$category = Category::findOrFail($id);

if ( ! $category->save())
{
return \Redirect::back()->withErrors($category->getErrors())->withInput();
}
$category->update($data);

return $this->redirect('categories.index');
}
Expand Down
10 changes: 2 additions & 8 deletions src/Pingpong/Admin/Entities/Category.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
<?php namespace Pingpong\Admin\Entities;

use Pingpong\Presenters\Model;

class Category extends Model {

/**
* @var array
*/
protected $fillable = ['name', 'slug', 'description'];

/**
* @var array
*/
protected $rules = [
'name' => 'required',
'slug' => 'required|unique:categories,slug',
];

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
Expand Down
91 changes: 91 additions & 0 deletions src/Pingpong/Admin/Providers/ErrorServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php namespace Pingpong\Admin\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Response;
use Pingpong\Validator\Exceptions\ValidationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class ErrorServiceProvider extends ServiceProvider {

/**
* @var array
*/
protected $handlers = [
'NotFound',
'FormValidation',
'ModelNotFound',
];

/**
* Boot the provider.
*
* @return void
*/
public function boot()
{
$this->registerErrorHandlers();
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
//
}

/**
* Register the required files.
*
* @return void
*/
protected function registerErrorHandlers()
{
foreach ($this->handlers as $handler)
{
$this->{'register' . $handler . 'Handler'}();
}
}

public function registerNotFoundHandler()
{
$this->app->error(function (NotFoundHttpException $e)
{
if ($this->app['request']->ajax())
{
return Response::json(['code' => 404, 'message' => 'Not Found'], 404);
}

return Response::view('404', [], 404);
});
}

public function registerFormValidationHandler()
{
$this->app->error(function (ValidationException $e)
{
if ($this->app['request']->ajax())
{
$errors = $e->getErrors()->toArray();

$default = ['status' => false];

return Response::json($default + compact('errors'));
}

return $this->app['redirect']->back()->withInput()->withErrors($e->getErrors());
});
}

public function registerModelNotFoundHandler()
{
$this->app->error(function (ModelNotFoundException $e)
{
return Response::view('404', [], 404);
});
}

}
14 changes: 14 additions & 0 deletions src/Pingpong/Admin/Validation/Category/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php namespace Pingpong\Admin\Validation\Category;

use Pingpong\Validator\Validator;

class Create extends Validator {

public function rules()
{
return [
'name' => 'required',
'slug' => 'required|unique:categories,slug',
];
}
}
15 changes: 15 additions & 0 deletions src/Pingpong/Admin/Validation/Category/Update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php namespace Pingpong\Admin\Validation\Category;

use Pingpong\Validator\Validator;
use Illuminate\Support\Facades\Request;

class Update extends Validator {

public function rules()
{
return [
'name' => 'required',
'slug' => 'required|unique:categories,slug,'. Request::segment(3),
];
}
}

0 comments on commit f066805

Please sign in to comment.