1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/Models/EntityModel.php

274 lines
6.5 KiB
PHP
Raw Normal View History

2015-03-18 00:39:03 +01:00
<?php namespace App\Models;
2015-03-16 22:45:25 +01:00
2015-03-26 04:52:42 +01:00
use Auth;
use Eloquent;
2016-09-15 12:41:09 +02:00
use Illuminate\Database\QueryException;
2015-03-26 04:52:42 +01:00
use Utils;
2016-08-07 21:42:32 +02:00
use Validator;
2015-03-26 04:52:42 +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;
/**
* @param null $context
* @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-06-07 20:13:29 +02:00
if (method_exists($className, 'trashed')){
$lastEntity = $className::whereAccountId($entity->account_id)->withTrashed();
2016-03-23 20:41:05 +01:00
} else {
$lastEntity = $className::whereAccountId($entity->account_id);
2016-03-23 20:41:05 +01:00
}
2016-05-26 14:50:23 +02:00
2015-03-16 22:45:25 +01:00
2016-09-15 12:41:09 +02:00
if (static::$hasPublicId) {
$lastEntity = $lastEntity->orderBy('public_id', 'DESC')
->first();
if ($lastEntity) {
$entity->public_id = $lastEntity->public_id + 1;
} else {
$entity->public_id = 1;
}
2015-03-16 22:45:25 +01:00
}
return $entity;
}
/**
* @param $publicId
* @return mixed
*/
2015-03-16 22:45:25 +01:00
public static function getPrivateId($publicId)
{
$className = get_called_class();
2016-03-02 14:36:42 +01:00
return $className::scope($publicId)->withTrashed()->value('id');
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
* @return mixed
*/
2015-03-16 22:45:25 +01:00
public function scopeScope($query, $publicId = false, $accountId = false)
{
if (!$accountId) {
$accountId = Auth::user()->account_id;
}
2015-06-03 19:55:48 +02:00
$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);
}
}
2016-05-26 14:50:23 +02:00
if (Auth::check() && ! Auth::user()->hasPermission('view_all')) {
$query->where(Utils::pluralizeEntityType($this->getEntityType()) . '.user_id', '=', Auth::user()->id);
2016-05-26 14:50:23 +02:00
}
return $query;
}
/**
* @param $query
* @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
* @return string
*/
2016-05-01 22:55:13 +02:00
public static function getClassName($entityType)
{
return 'App\\Models\\' . ucwords(Utils::toCamelCase($entityType));
}
/**
* @param $entityType
* @return string
*/
2016-05-01 22:55:13 +02:00
public static function getTransformerName($entityType)
{
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) {
if (strstr($field, '_id') && !$this->$field) {
$this->$field = null;
}
}
}
// converts "App\Models\Client" to "client_id"
/**
* @return string
*/
2015-10-28 20:22:07 +01:00
public function getKeyField()
{
$class = get_class($this);
$parts = explode('\\', $class);
$name = $parts[count($parts)-1];
return strtolower($name) . '_id';
}
2016-08-07 21:42:32 +02:00
/**
* @param $data
* @param $entityType
* @return bool|string
*/
2016-08-08 16:45:37 +02:00
public static function validate($data, $entityType, $entity = false)
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';
2016-08-07 21:42:32 +02:00
$requestClass = sprintf('App\\Http\\Requests\\%s%sAPIRequest', ucwords($action), ucwords($entityType));
if ( ! class_exists($requestClass)) {
$requestClass = sprintf('App\\Http\\Requests\\%s%sRequest', ucwords($action), ucwords($entityType));
}
$request = new $requestClass();
$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);
if ( ! $request->authorize()) {
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',
'credits' => 'credit-card',
'quotes' => 'file-text-o',
'tasks' => 'clock-o',
'expenses' => 'file-image-o',
'vendors' => 'building',
'settings' => 'cog',
'self-update' => 'download',
2016-08-31 21:10:41 +02:00
];
return array_get($icons, $entityType);
}
2015-03-16 22:45:25 +01:00
}