2017-01-30 21:40:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
2016-04-28 15:16:33 +03:00
|
|
|
|
2016-08-31 22:10:41 +03:00
|
|
|
use App\Libraries\HistoryUtils;
|
2019-01-30 22:00:26 +11:00
|
|
|
use App\Models\Contact;
|
2016-12-08 22:51:53 +02:00
|
|
|
use App\Models\EntityModel;
|
2017-01-30 21:40:43 +02:00
|
|
|
use Input;
|
|
|
|
|
use Utils;
|
2016-04-28 15:16:33 +03:00
|
|
|
|
2017-01-30 18:05:31 +02:00
|
|
|
class EntityRequest extends Request
|
|
|
|
|
{
|
2016-04-28 15:16:33 +03:00
|
|
|
protected $entityType;
|
|
|
|
|
private $entity;
|
|
|
|
|
|
2016-06-07 21:13:29 +03:00
|
|
|
public function entity()
|
2016-04-28 15:16:33 +03:00
|
|
|
{
|
|
|
|
|
if ($this->entity) {
|
|
|
|
|
return $this->entity;
|
|
|
|
|
}
|
2016-05-01 14:31:10 +03:00
|
|
|
|
2017-08-16 10:28:40 +03:00
|
|
|
$class = EntityModel::getClassName($this->entityType);
|
|
|
|
|
|
2016-05-09 20:35:50 +03:00
|
|
|
// The entity id can appear as invoices, invoice_id, public_id or id
|
|
|
|
|
$publicId = false;
|
2016-07-06 21:35:16 +03:00
|
|
|
$field = $this->entityType . '_id';
|
2017-01-30 18:05:31 +02:00
|
|
|
if (! empty($this->$field)) {
|
2016-07-06 21:35:16 +03:00
|
|
|
$publicId = $this->$field;
|
|
|
|
|
}
|
2017-01-30 18:05:31 +02:00
|
|
|
if (! $publicId) {
|
2016-07-06 21:35:16 +03:00
|
|
|
$field = Utils::pluralizeEntityType($this->entityType);
|
2017-01-30 18:05:31 +02:00
|
|
|
if (! empty($this->$field)) {
|
2016-07-06 21:35:16 +03:00
|
|
|
$publicId = $this->$field;
|
2016-06-07 21:13:29 +03:00
|
|
|
}
|
2016-05-09 20:35:50 +03:00
|
|
|
}
|
2019-01-30 22:00:26 +11:00
|
|
|
if (! $publicId) {
|
|
|
|
|
$field = $this->entityType;
|
|
|
|
|
if (! empty($this->$field)) {
|
|
|
|
|
$publicId = $this->$field;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-30 18:05:31 +02:00
|
|
|
if (! $publicId) {
|
2016-05-09 20:35:50 +03:00
|
|
|
$publicId = Input::get('public_id') ?: Input::get('id');
|
|
|
|
|
}
|
2018-01-31 16:40:47 +02:00
|
|
|
|
2017-01-30 18:05:31 +02:00
|
|
|
if (! $publicId) {
|
2016-04-28 15:16:33 +03:00
|
|
|
return null;
|
2016-06-07 21:13:29 +03:00
|
|
|
}
|
|
|
|
|
|
2019-01-30 22:00:26 +11:00
|
|
|
//Support Client Portal Scopes
|
|
|
|
|
$accountId = false;
|
|
|
|
|
|
|
|
|
|
if(Input::get('account_id'))
|
|
|
|
|
$accountId = Input::get('account_id');
|
|
|
|
|
elseif($contact = Contact::getContactIfLoggedIn())
|
|
|
|
|
$accountId = $contact->account->id;
|
|
|
|
|
|
2016-12-08 22:51:53 +02:00
|
|
|
if (method_exists($class, 'trashed')) {
|
2019-01-30 22:00:26 +11:00
|
|
|
$this->entity = $class::scope($publicId, $accountId)->withTrashed()->firstOrFail();
|
2016-12-08 22:51:53 +02:00
|
|
|
} else {
|
2019-01-30 22:00:26 +11:00
|
|
|
$this->entity = $class::scope($publicId, $accountId)->firstOrFail();
|
|
|
|
|
|
2016-12-08 22:51:53 +02:00
|
|
|
}
|
2016-11-05 06:20:33 +11:00
|
|
|
|
2016-04-28 15:16:33 +03:00
|
|
|
return $this->entity;
|
|
|
|
|
}
|
2016-05-01 14:31:10 +03:00
|
|
|
|
2016-08-08 17:45:37 +03:00
|
|
|
public function setEntity($entity)
|
|
|
|
|
{
|
|
|
|
|
$this->entity = $entity;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-01 14:31:10 +03:00
|
|
|
public function authorize()
|
|
|
|
|
{
|
|
|
|
|
if ($this->entity()) {
|
2016-08-31 22:10:41 +03:00
|
|
|
if ($this->user()->can('view', $this->entity())) {
|
|
|
|
|
HistoryUtils::trackViewed($this->entity());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-05-01 14:31:10 +03:00
|
|
|
} else {
|
2019-01-30 22:00:26 +11:00
|
|
|
return $this->user()->can('createEntity', $this->entityType);
|
2016-05-01 14:31:10 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function rules()
|
|
|
|
|
{
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2016-04-28 15:16:33 +03:00
|
|
|
}
|