2013-12-04 18:20:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class EntityModel extends Eloquent
|
|
|
|
|
{
|
|
|
|
|
protected $softDelete = true;
|
2014-01-09 10:39:20 +00:00
|
|
|
public $timestamps = true;
|
2014-01-08 20:09:47 +00:00
|
|
|
|
2014-01-02 10:27:48 +02:00
|
|
|
protected $hidden = ['id', 'created_at', 'deleted_at', 'updated_at'];
|
|
|
|
|
|
2013-12-10 19:18:35 +02:00
|
|
|
public static function createNew($parent = false)
|
|
|
|
|
{
|
2013-12-04 18:20:14 +02:00
|
|
|
$className = get_called_class();
|
|
|
|
|
$entity = new $className();
|
|
|
|
|
|
2014-05-25 17:48:00 +03:00
|
|
|
if ($parent)
|
2014-01-09 13:44:55 +00:00
|
|
|
{
|
2013-12-10 19:18:35 +02:00
|
|
|
$entity->user_id = $parent->user_id;
|
|
|
|
|
$entity->account_id = $parent->account_id;
|
2014-01-09 13:44:55 +00:00
|
|
|
}
|
|
|
|
|
else if (Auth::check())
|
|
|
|
|
{
|
2014-01-09 11:24:03 +00:00
|
|
|
$entity->user_id = Auth::user()->id;
|
|
|
|
|
$entity->account_id = Auth::user()->account_id;
|
2014-01-09 13:44:55 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-01-02 01:12:33 +02:00
|
|
|
Utils::fatalError();
|
2013-12-10 19:18:35 +02:00
|
|
|
}
|
|
|
|
|
|
2013-12-11 13:11:59 +02:00
|
|
|
$lastEntity = $className::withTrashed()->scope(false, $entity->account_id)->orderBy('public_id', 'DESC')->first();
|
2013-12-04 18:20:14 +02:00
|
|
|
|
|
|
|
|
if ($lastEntity)
|
|
|
|
|
{
|
|
|
|
|
$entity->public_id = $lastEntity->public_id + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$entity->public_id = 1;
|
|
|
|
|
}
|
2013-12-10 19:18:35 +02:00
|
|
|
|
2013-12-04 18:20:14 +02:00
|
|
|
return $entity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getPrivateId($publicId)
|
|
|
|
|
{
|
|
|
|
|
$className = get_called_class();
|
|
|
|
|
return $className::scope($publicId)->pluck('id');
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-09 10:39:20 +00:00
|
|
|
public function getActivityKey()
|
2014-01-08 20:09:47 +00:00
|
|
|
{
|
|
|
|
|
return $this->getEntityType() . ':' . $this->public_id . ':' . $this->getName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
public function getEntityType()
|
|
|
|
|
{
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-04 18:20:14 +02:00
|
|
|
public function getNmae()
|
|
|
|
|
{
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2014-01-08 20:09:47 +00:00
|
|
|
*/
|
2013-12-04 18:20:14 +02:00
|
|
|
|
2013-12-10 19:18:35 +02:00
|
|
|
public function scopeScope($query, $publicId = false, $accountId = false)
|
2013-12-04 18:20:14 +02:00
|
|
|
{
|
2013-12-29 01:33:48 +02:00
|
|
|
if (!$accountId)
|
|
|
|
|
{
|
2013-12-10 19:18:35 +02:00
|
|
|
$accountId = Auth::user()->account_id;
|
|
|
|
|
}
|
2013-12-29 01:33:48 +02:00
|
|
|
|
2013-12-10 19:18:35 +02:00
|
|
|
$query->whereAccountId($accountId);
|
2013-12-04 18:20:14 +02:00
|
|
|
|
|
|
|
|
if ($publicId)
|
|
|
|
|
{
|
|
|
|
|
if (is_array($publicId))
|
|
|
|
|
{
|
|
|
|
|
$query->whereIn('public_id', $publicId);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$query->wherePublicId($publicId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
|
}
|
|
|
|
|
}
|