2013-12-04 17:20:14 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class EntityModel extends Eloquent
|
|
|
|
{
|
|
|
|
protected $softDelete = true;
|
|
|
|
protected $hidden = array('id', 'created_at', 'updated_at', 'deleted_at');
|
|
|
|
|
2013-12-10 18:18:35 +01:00
|
|
|
public static function createNew($parent = false)
|
|
|
|
{
|
2013-12-04 17:20:14 +01:00
|
|
|
$className = get_called_class();
|
|
|
|
$entity = new $className();
|
|
|
|
|
2013-12-10 18:18:35 +01:00
|
|
|
if (Auth::check()) {
|
|
|
|
$entity->user_id = Auth::user()->id;
|
|
|
|
$entity->account_id = Auth::user()->account_id;
|
|
|
|
} else if ($parent) {
|
|
|
|
$entity->user_id = $parent->user_id;
|
|
|
|
$entity->account_id = $parent->account_id;
|
|
|
|
} else {
|
|
|
|
exit; // TODO_FIX
|
|
|
|
}
|
|
|
|
|
2013-12-11 12:11:59 +01:00
|
|
|
$lastEntity = $className::withTrashed()->scope(false, $entity->account_id)->orderBy('public_id', 'DESC')->first();
|
2013-12-04 17:20:14 +01:00
|
|
|
|
|
|
|
if ($lastEntity)
|
|
|
|
{
|
|
|
|
$entity->public_id = $lastEntity->public_id + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$entity->public_id = 1;
|
|
|
|
}
|
2013-12-10 18:18:35 +01:00
|
|
|
|
2013-12-04 17:20:14 +01:00
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getPrivateId($publicId)
|
|
|
|
{
|
|
|
|
$className = get_called_class();
|
|
|
|
return $className::scope($publicId)->pluck('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNmae()
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2013-12-10 18:18:35 +01:00
|
|
|
public function scopeScope($query, $publicId = false, $accountId = false)
|
2013-12-04 17:20:14 +01:00
|
|
|
{
|
2013-12-29 00:33:48 +01:00
|
|
|
if (!$accountId)
|
|
|
|
{
|
2013-12-10 18:18:35 +01:00
|
|
|
$accountId = Auth::user()->account_id;
|
|
|
|
}
|
2013-12-29 00:33:48 +01:00
|
|
|
|
2013-12-10 18:18:35 +01:00
|
|
|
$query->whereAccountId($accountId);
|
2013-12-04 17:20:14 +01:00
|
|
|
|
|
|
|
if ($publicId)
|
|
|
|
{
|
|
|
|
if (is_array($publicId))
|
|
|
|
{
|
|
|
|
$query->whereIn('public_id', $publicId);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$query->wherePublicId($publicId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
}
|