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;
|
|
|
|
use Utils;
|
|
|
|
|
2015-03-16 22:45:25 +01:00
|
|
|
class EntityModel extends Eloquent
|
|
|
|
{
|
|
|
|
public $timestamps = true;
|
|
|
|
protected $hidden = ['id'];
|
|
|
|
|
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) {
|
|
|
|
$entity->user_id = $context instanceof User ? $context->id : $context->user_id;
|
|
|
|
$entity->account_id = $context->account_id;
|
2015-03-16 22:45:25 +01:00
|
|
|
} elseif (Auth::check()) {
|
|
|
|
$entity->user_id = Auth::user()->id;
|
|
|
|
$entity->account_id = Auth::user()->account_id;
|
|
|
|
} else {
|
|
|
|
Utils::fatalError();
|
|
|
|
}
|
|
|
|
|
2016-03-23 20:41:05 +01:00
|
|
|
if(method_exists($className, 'withTrashed')){
|
|
|
|
$lastEntity = $className::withTrashed()
|
|
|
|
->scope(false, $entity->account_id);
|
|
|
|
} else {
|
|
|
|
$lastEntity = $className::scope(false, $entity->account_id);
|
|
|
|
}
|
2016-05-26 14:50:23 +02:00
|
|
|
|
2016-03-23 20:41:05 +01:00
|
|
|
$lastEntity = $lastEntity->orderBy('public_id', 'DESC')
|
2015-11-01 23:10:20 +01:00
|
|
|
->first();
|
2015-03-16 22:45:25 +01:00
|
|
|
|
|
|
|
if ($lastEntity) {
|
|
|
|
$entity->public_id = $lastEntity->public_id + 1;
|
|
|
|
} else {
|
|
|
|
$entity->public_id = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function getActivityKey()
|
|
|
|
{
|
2015-08-10 17:48:41 +02:00
|
|
|
return '[' . $this->getEntityType().':'.$this->public_id.':'.$this->getDisplayName() . ']';
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
public function getEntityType()
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNmae()
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
2015-07-02 22:21:29 +02:00
|
|
|
|
2016-05-26 14:50:23 +02:00
|
|
|
public function scopeViewable($query)
|
|
|
|
{
|
|
|
|
if (Auth::check() && ! Auth::user()->hasPermission('view_all')) {
|
|
|
|
$query->where($this->getEntityType(). 's.user_id', '=', Auth::user()->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2016-02-24 21:58:42 +01:00
|
|
|
public function scopeWithArchived($query)
|
|
|
|
{
|
|
|
|
return $query->withTrashed()->where('is_deleted', '=', false);
|
|
|
|
}
|
|
|
|
|
2015-07-02 22:21:29 +02:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->public_id;
|
|
|
|
}
|
|
|
|
|
2015-08-10 17:48:41 +02:00
|
|
|
public function getDisplayName()
|
|
|
|
{
|
|
|
|
return $this->getName();
|
|
|
|
}
|
|
|
|
|
2016-05-01 22:55:13 +02:00
|
|
|
public static function getClassName($entityType)
|
|
|
|
{
|
|
|
|
return 'App\\Models\\' . ucwords(Utils::toCamelCase($entityType));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06 19:55:55 +02:00
|
|
|
{
|
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"
|
|
|
|
public function getKeyField()
|
|
|
|
{
|
|
|
|
$class = get_class($this);
|
|
|
|
$parts = explode('\\', $class);
|
|
|
|
$name = $parts[count($parts)-1];
|
|
|
|
return strtolower($name) . '_id';
|
2015-10-06 19:55:55 +02:00
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|