1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 12:42:36 +01:00
invoiceninja/app/Models/EntityModel.php

478 lines
12 KiB
PHP
Raw Permalink Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Models;
2015-03-16 22:45:25 +01:00
use Str;
2015-03-26 04:52:42 +01:00
use Auth;
use Eloquent;
use Utils;
2016-08-07 21:42:32 +02:00
use Validator;
2015-03-26 04:52:42 +01:00
/**
2017-01-30 20:40:43 +01:00
* Class EntityModel.
*/
2015-03-16 22:45:25 +01:00
class EntityModel extends Eloquent
{
/**
* @var bool
*/
2015-03-16 22:45:25 +01:00
public $timestamps = true;
2016-09-15 12:41:09 +02:00
/**
* @var bool
*/
protected static $hasPublicId = true;
/**
* @var array
*/
2015-03-16 22:45:25 +01:00
protected $hidden = ['id'];
/**
* @var bool
*/
2016-06-02 21:03:59 +02:00
public static $notifySubscriptions = true;
2016-11-17 15:29:02 +01:00
/**
* @var array
*/
public static $statuses = [
STATUS_ACTIVE,
STATUS_ARCHIVED,
STATUS_DELETED,
];
/**
* @param null $context
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2015-10-25 08:13:06 +01:00
public static function createNew($context = null)
2015-03-16 22:45:25 +01:00
{
$className = get_called_class();
$entity = new $className();
2015-10-25 08:13:06 +01:00
if ($context) {
2016-06-02 21:03:59 +02:00
$user = $context instanceof User ? $context : $context->user;
$account = $context->account;
2015-03-16 22:45:25 +01:00
} elseif (Auth::check()) {
2016-06-02 21:03:59 +02:00
$user = Auth::user();
$account = Auth::user()->account;
2015-03-16 22:45:25 +01:00
} else {
Utils::fatalError();
}
2016-06-02 21:03:59 +02:00
$entity->user_id = $user->id;
$entity->account_id = $account->id;
// store references to the original user/account to prevent needing to reload them
$entity->setRelation('user', $user);
$entity->setRelation('account', $account);
2016-12-09 09:43:20 +01:00
if (static::$hasPublicId) {
$entity->public_id = static::getNextPublicId($entity->account_id);
}
return $entity;
}
private static function getNextPublicId($accountId)
{
$className = get_called_class();
2017-01-30 17:05:31 +01:00
if (method_exists($className, 'trashed')) {
$lastEntity = $className::whereAccountId($accountId)->withTrashed();
2016-03-23 20:41:05 +01:00
} else {
$lastEntity = $className::whereAccountId($accountId);
2016-03-23 20:41:05 +01:00
}
2016-05-26 14:50:23 +02:00
$lastEntity = $lastEntity->orderBy('public_id', 'DESC')->first();
2016-09-15 12:41:09 +02:00
if ($lastEntity) {
return $lastEntity->public_id + 1;
} else {
return 1;
2015-03-16 22:45:25 +01:00
}
}
/**
* @param $publicId
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2015-03-16 22:45:25 +01:00
public static function getPrivateId($publicId)
{
if (! $publicId) {
return null;
}
2015-03-16 22:45:25 +01:00
$className = get_called_class();
2018-04-11 22:18:01 +02:00
if (method_exists($className, 'trashed')) {
return $className::scope($publicId)->withTrashed()->value('id');
} else {
return $className::scope($publicId)->value('id');
2018-04-30 22:00:22 +02:00
}
2015-03-16 22:45:25 +01:00
}
/**
* @return string
*/
2015-03-16 22:45:25 +01:00
public function getActivityKey()
{
return '[' . $this->getEntityType().':'.$this->public_id.':'.$this->getDisplayName() . ']';
2015-03-16 22:45:25 +01:00
}
2016-08-07 21:42:32 +02:00
public function entityKey()
{
return $this->public_id . ':' . $this->getEntityType();
}
2016-08-31 21:10:41 +02:00
public function subEntityType()
{
return $this->getEntityType();
}
public function isEntityType($type)
{
return $this->getEntityType() === $type;
}
2015-03-16 22:45:25 +01:00
/*
public function getEntityType()
{
return '';
}
public function getNmae()
{
return '';
}
*/
/**
* @param $query
* @param bool $publicId
* @param bool $accountId
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2015-03-16 22:45:25 +01:00
public function scopeScope($query, $publicId = false, $accountId = false)
{
2017-11-16 20:02:36 +01:00
// If 'false' is passed as the publicId return nothing rather than everything
if (func_num_args() > 1 && ! $publicId && ! $accountId) {
2017-11-03 10:24:54 +01:00
$query->where('id', '=', 0);
return $query;
}
2018-03-05 15:40:10 +01:00
if (! $accountId) {
$accountId = Auth::user()->account_id;
}
$query->where($this->getTable() .'.account_id', '=', $accountId);
2015-03-16 22:45:25 +01:00
if ($publicId) {
if (is_array($publicId)) {
$query->whereIn('public_id', $publicId);
} else {
$query->wherePublicId($publicId);
}
}
2019-02-25 11:42:14 +01:00
if (Auth::check() && method_exists($this, 'getEntityType')
&& ! Auth::user()->hasPermission('view_' . $this->getEntityType())
&& $this->getEntityType() != ENTITY_TAX_RATE
2019-02-25 14:25:53 +01:00
&& $this->getEntityType() != ENTITY_DOCUMENT
2019-02-25 11:42:14 +01:00
&& $this->getEntityType() != ENTITY_INVITATION) {
$query->where(Utils::pluralizeEntityType($this->getEntityType()) . '.user_id', '=', Auth::user()->id);
2016-05-26 14:50:23 +02:00
}
return $query;
}
2018-03-14 11:21:50 +01:00
public function scopeWithActiveOrSelected($query, $id = false)
{
return $query->withTrashed()
->where(function ($query) use ($id) {
$query->whereNull('deleted_at')
->orWhere('id', '=', $id);
});
}
/**
* @param $query
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2016-02-24 21:58:42 +01:00
public function scopeWithArchived($query)
{
return $query->withTrashed()->where('is_deleted', '=', false);
}
/**
* @return mixed
*/
2015-07-02 22:21:29 +02:00
public function getName()
{
return $this->public_id;
}
/**
* @return mixed
*/
public function getDisplayName()
{
return $this->getName();
}
/**
* @param $entityType
2017-01-30 20:40:43 +01:00
*
* @return string
*/
2016-05-01 22:55:13 +02:00
public static function getClassName($entityType)
{
2017-01-30 17:05:31 +01:00
if (! Utils::isNinjaProd()) {
2016-12-08 14:41:35 +01:00
if ($module = \Module::find($entityType)) {
return "Modules\\{$module->getName()}\\Models\\{$module->getName()}";
}
}
2016-11-18 14:31:43 +01:00
if ($entityType == ENTITY_QUOTE || $entityType == ENTITY_RECURRING_INVOICE) {
$entityType = ENTITY_INVOICE;
}
2016-05-01 22:55:13 +02:00
return 'App\\Models\\' . ucwords(Utils::toCamelCase($entityType));
}
/**
* @param $entityType
2017-01-30 20:40:43 +01:00
*
* @return string
*/
2016-05-01 22:55:13 +02:00
public static function getTransformerName($entityType)
{
2017-01-30 17:05:31 +01:00
if (! Utils::isNinjaProd()) {
2016-12-09 09:43:20 +01:00
if ($module = \Module::find($entityType)) {
return "Modules\\{$module->getName()}\\Transformers\\{$module->getName()}Transformer";
}
}
2016-05-01 22:55:13 +02:00
return 'App\\Ninja\\Transformers\\' . ucwords(Utils::toCamelCase($entityType)) . 'Transformer';
}
2016-05-26 14:50:23 +02:00
2015-10-28 20:22:07 +01:00
public function setNullValues()
{
2015-10-28 20:22:07 +01:00
foreach ($this->fillable as $field) {
2017-01-30 20:40:43 +01:00
if (strstr($field, '_id') && ! $this->$field) {
2015-10-28 20:22:07 +01:00
$this->$field = null;
}
}
}
// converts "App\Models\Client" to "client_id"
2017-01-30 20:40:43 +01:00
/**
* @return string
*/
2015-10-28 20:22:07 +01:00
public function getKeyField()
{
$class = get_class($this);
$parts = explode('\\', $class);
2017-01-30 20:40:43 +01:00
$name = $parts[count($parts) - 1];
2015-10-28 20:22:07 +01:00
return strtolower($name) . '_id';
}
2016-08-07 21:42:32 +02:00
/**
* @param $data
* @param $entityType
2017-01-30 20:49:42 +01:00
* @param mixed $entity
* TODO Remove $entityType parameter
2016-08-07 21:42:32 +02:00
* @return bool|string
*/
public static function validate($data, $entityType = false, $entity = false)
2016-08-07 21:42:32 +02:00
{
if (! $entityType) {
$className = get_called_class();
$entityBlank = new $className();
$entityType = $entityBlank->getEntityType();
}
2016-08-07 21:42:32 +02:00
// Use the API request if it exists
2016-08-08 16:45:37 +02:00
$action = $entity ? 'update' : 'create';
$requestClass = sprintf('App\\Http\\Requests\\%s%sAPIRequest', ucwords($action), Str::studly($entityType));
2017-01-30 17:05:31 +01:00
if (! class_exists($requestClass)) {
$requestClass = sprintf('App\\Http\\Requests\\%s%sRequest', ucwords($action), Str::studly($entityType));
2016-08-07 21:42:32 +02:00
}
$request = new $requestClass();
2017-01-30 17:05:31 +01:00
$request->setUserResolver(function () {
return Auth::user();
});
2016-08-08 16:45:37 +02:00
$request->setEntity($entity);
2016-08-07 21:42:32 +02:00
$request->replace($data);
2017-01-30 17:05:31 +01:00
if (! $request->authorize()) {
2016-08-07 21:42:32 +02:00
return trans('texts.not_allowed');
}
$validator = Validator::make($data, $request->rules());
if ($validator->fails()) {
return $validator->messages()->first();
} else {
return true;
}
}
2016-08-31 21:10:41 +02:00
public static function getIcon($entityType)
{
$icons = [
'dashboard' => 'tachometer',
'clients' => 'users',
2016-09-23 16:00:47 +02:00
'products' => 'cube',
2016-08-31 21:10:41 +02:00
'invoices' => 'file-pdf-o',
'payments' => 'credit-card',
'recurring_invoices' => 'files-o',
2017-06-26 15:25:11 +02:00
'recurring_expenses' => 'files-o',
2016-08-31 21:10:41 +02:00
'credits' => 'credit-card',
'quotes' => 'file-text-o',
2018-01-31 12:51:18 +01:00
'proposals' => 'th-large',
2016-08-31 21:10:41 +02:00
'tasks' => 'clock-o',
'expenses' => 'file-image-o',
'vendors' => 'building',
'settings' => 'cog',
'self-update' => 'download',
2017-01-22 11:09:29 +01:00
'reports' => 'th-list',
2017-11-30 12:57:39 +01:00
'projects' => 'briefcase',
2016-08-31 21:10:41 +02:00
];
return array_get($icons, $entityType);
}
2017-04-04 15:57:33 +02:00
public function loadFromRequest()
{
foreach (static::$requestFields as $field) {
if ($value = request()->$field) {
$this->$field = strpos($field, 'date') ? Utils::fromSqlDate($value) : $value;
}
}
}
// isDirty return true if the field's new value is the same as the old one
public function isChanged()
{
foreach ($this->fillable as $field) {
if ($this->$field != $this->getOriginal($field)) {
return true;
}
}
return false;
}
2016-11-18 14:31:43 +01:00
2018-01-31 17:16:48 +01:00
public static function getFormUrl($entityType)
{
if (in_array($entityType, [ENTITY_PROPOSAL_CATEGORY, ENTITY_PROPOSAL_SNIPPET, ENTITY_PROPOSAL_TEMPLATE])) {
return str_replace('_', 's/', Utils::pluralizeEntityType($entityType));
} else {
return Utils::pluralizeEntityType($entityType);
}
}
2016-11-20 15:08:36 +01:00
public static function getStates($entityType = false)
2016-11-18 14:31:43 +01:00
{
$data = [];
foreach (static::$statuses as $status) {
$data[$status] = trans("texts.{$status}");
}
return $data;
}
2016-11-20 15:08:36 +01:00
public static function getStatuses($entityType = false)
{
return [];
}
public static function getStatesFor($entityType = false)
{
$class = static::getClassName($entityType);
return $class::getStates($entityType);
}
public static function getStatusesFor($entityType = false)
{
$class = static::getClassName($entityType);
return $class::getStatuses($entityType);
}
2016-12-26 20:43:53 +01:00
public function statusClass()
{
return '';
}
public function statusLabel()
{
return '';
}
public function save(array $options = [])
{
try {
return parent::save($options);
} catch (\Illuminate\Database\QueryException $exception) {
// check if public_id has been taken
if ($exception->getCode() == 23000 && static::$hasPublicId) {
$nextId = static::getNextPublicId($this->account_id);
if ($nextId != $this->public_id) {
$this->public_id = $nextId;
2017-07-02 15:28:05 +02:00
if (env('MULTI_DB_ENABLED')) {
if ($this->contact_key) {
$this->contact_key = strtolower(str_random(RANDOM_KEY_LENGTH));
} elseif ($this->invitation_key) {
$this->invitation_key = strtolower(str_random(RANDOM_KEY_LENGTH));
}
}
return $this->save($options);
}
}
throw $exception;
}
}
2018-03-14 09:48:16 +01:00
public function equalTo($obj)
{
if (empty($obj->id)) {
return false;
}
return $this->id == $obj->id && $this->getEntityType() == $obj->entityType;
}
2018-05-03 20:46:34 +02:00
/**
* @param $method
* @param $params
*/
public function __call($method, $params)
{
2018-05-03 20:46:34 +02:00
if (count(config('modules.relations'))) {
$entityType = $this->getEntityType();
if ($entityType) {
$config = implode('.', ['modules.relations.' . $entityType, $method]);
if (config()->has($config)) {
$function = config()->get($config);
return $function($this);
}
}
}
2018-05-03 20:46:34 +02:00
return parent::__call($method, $params);
}
2015-03-16 22:45:25 +01:00
}