1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Models/EntityModel.php
2016-07-07 11:44:15 +03:00

194 lines
4.3 KiB
PHP

<?php namespace App\Models;
use Auth;
use Eloquent;
use Utils;
/**
* Class EntityModel
*/
class EntityModel extends Eloquent
{
/**
* @var bool
*/
public $timestamps = true;
/**
* @var array
*/
protected $hidden = ['id'];
/**
* @var bool
*/
public static $notifySubscriptions = true;
/**
* @param null $context
* @return mixed
*/
public static function createNew($context = null)
{
$className = get_called_class();
$entity = new $className();
if ($context) {
$user = $context instanceof User ? $context : $context->user;
$account = $context->account;
} elseif (Auth::check()) {
$user = Auth::user();
$account = Auth::user()->account;
} else {
Utils::fatalError();
}
$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);
if (method_exists($className, 'trashed')){
$lastEntity = $className::whereAccountId($entity->account_id)->withTrashed();
} else {
$lastEntity = $className::whereAccountId($entity->account_id);
}
$lastEntity = $lastEntity->orderBy('public_id', 'DESC')
->first();
if ($lastEntity) {
$entity->public_id = $lastEntity->public_id + 1;
} else {
$entity->public_id = 1;
}
return $entity;
}
/**
* @param $publicId
* @return mixed
*/
public static function getPrivateId($publicId)
{
$className = get_called_class();
return $className::scope($publicId)->withTrashed()->value('id');
}
/**
* @return string
*/
public function getActivityKey()
{
return '[' . $this->getEntityType().':'.$this->public_id.':'.$this->getDisplayName() . ']';
}
/*
public function getEntityType()
{
return '';
}
public function getNmae()
{
return '';
}
*/
/**
* @param $query
* @param bool $publicId
* @param bool $accountId
* @return mixed
*/
public function scopeScope($query, $publicId = false, $accountId = false)
{
if (!$accountId) {
$accountId = Auth::user()->account_id;
}
$query->where($this->getTable() .'.account_id', '=', $accountId);
if ($publicId) {
if (is_array($publicId)) {
$query->whereIn('public_id', $publicId);
} else {
$query->wherePublicId($publicId);
}
}
if (Auth::check() && ! Auth::user()->hasPermission('view_all')) {
$query->where(Utils::pluralizeEntityType($this->getEntityType()) . '.user_id', '=', Auth::user()->id);
}
return $query;
}
/**
* @param $query
* @return mixed
*/
public function scopeWithArchived($query)
{
return $query->withTrashed()->where('is_deleted', '=', false);
}
/**
* @return mixed
*/
public function getName()
{
return $this->public_id;
}
/**
* @return mixed
*/
public function getDisplayName()
{
return $this->getName();
}
/**
* @param $entityType
* @return string
*/
public static function getClassName($entityType)
{
return 'App\\Models\\' . ucwords(Utils::toCamelCase($entityType));
}
/**
* @param $entityType
* @return string
*/
public static function getTransformerName($entityType)
{
return 'App\\Ninja\\Transformers\\' . ucwords(Utils::toCamelCase($entityType)) . 'Transformer';
}
public function setNullValues()
{
foreach ($this->fillable as $field) {
if (strstr($field, '_id') && !$this->$field) {
$this->$field = null;
}
}
}
// converts "App\Models\Client" to "client_id"
/**
* @return string
*/
public function getKeyField()
{
$class = get_class($this);
$parts = explode('\\', $class);
$name = $parts[count($parts)-1];
return strtolower($name) . '_id';
}
}