2016-04-28 14:16:33 +02:00
|
|
|
<?php namespace App\Http\Requests;
|
|
|
|
|
|
|
|
use App\Http\Requests\Request;
|
|
|
|
use Input;
|
|
|
|
use Utils;
|
|
|
|
|
2016-05-01 13:31:10 +02:00
|
|
|
class EntityRequest extends Request {
|
2016-04-28 14:16:33 +02:00
|
|
|
|
|
|
|
protected $entityType;
|
|
|
|
private $entity;
|
|
|
|
|
|
|
|
public function entity()
|
|
|
|
{
|
|
|
|
if ($this->entity) {
|
|
|
|
return $this->entity;
|
|
|
|
}
|
2016-05-01 13:31:10 +02:00
|
|
|
|
2016-05-09 19:35:50 +02:00
|
|
|
// The entity id can appear as invoices, invoice_id, public_id or id
|
|
|
|
$publicId = false;
|
|
|
|
foreach (['_id', 's'] as $suffix) {
|
|
|
|
$field = $this->entityType . $suffix;
|
|
|
|
if ($this->$field) {
|
|
|
|
$publicId= $this->$field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( ! $publicId) {
|
|
|
|
$publicId = Input::get('public_id') ?: Input::get('id');
|
|
|
|
}
|
2016-04-28 14:16:33 +02:00
|
|
|
if ( ! $publicId) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = Utils::getEntityClass($this->entityType);
|
2016-05-01 21:30:39 +02:00
|
|
|
|
|
|
|
if (method_exists($class, 'withTrashed')) {
|
|
|
|
$this->entity = $class::scope($publicId)->withTrashed()->firstOrFail();
|
|
|
|
} else {
|
|
|
|
$this->entity = $class::scope($publicId)->firstOrFail();
|
|
|
|
}
|
2016-04-28 14:16:33 +02:00
|
|
|
|
|
|
|
return $this->entity;
|
|
|
|
}
|
2016-05-01 13:31:10 +02:00
|
|
|
|
|
|
|
public function authorize()
|
|
|
|
{
|
|
|
|
if ($this->entity()) {
|
|
|
|
return $this->user()->can('view', $this->entity());
|
|
|
|
} else {
|
|
|
|
return $this->user()->can('create', $this->entityType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2016-04-28 14:16:33 +02:00
|
|
|
}
|