1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 14:12:44 +01:00
invoiceninja/app/Models/EntityModel.php

120 lines
2.8 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;
use Utils;
2015-03-16 22:45:25 +01:00
class EntityModel extends Eloquent
{
public $timestamps = true;
protected $hidden = ['id'];
public static function createNew($parent = false)
{
$className = get_called_class();
$entity = new $className();
if ($parent) {
$entity->user_id = $parent instanceof User ? $parent->id : $parent->user_id;
$entity->account_id = $parent->account_id;
} elseif (Auth::check()) {
$entity->user_id = Auth::user()->id;
$entity->account_id = Auth::user()->account_id;
} else {
Utils::fatalError();
}
$lastEntity = $className::withTrashed()->scope(false, $entity->account_id)->orderBy('public_id', 'DESC')->first();
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();
return $className::scope($publicId)->pluck('id');
}
public function getActivityKey()
{
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
public function getName()
{
return $this->public_id;
}
public function getDisplayName()
{
return $this->getName();
}
2015-07-02 22:21:29 +02:00
// Remap ids to public_ids and show name
public function toPublicArray()
{
$data = $this->toArray();
foreach ($this->attributes as $key => $val) {
if (strpos($key, '_id')) {
list($field, $id) = explode('_', $key);
if ($field == 'account') {
// do nothing
} else {
$entity = @$this->$field;
if ($entity) {
$data["{$field}_name"] = $entity->getName();
}
}
}
}
$data = Utils::hideIds($data);
return $data;
}
public function isBeingDeleted()
{
return $this->is_deleted && !$this->getOriginal('is_deleted');
}
2015-03-16 22:45:25 +01:00
}