2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Models;
|
2016-12-29 22:23:36 -08:00
|
|
|
use App\Presenters\Presentable;
|
2016-03-25 01:18:05 -07:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2016-09-29 22:20:49 -07:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2016-10-31 21:00:30 -07:00
|
|
|
use Response;
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-04-07 13:21:09 -07:00
|
|
|
/**
|
|
|
|
* Model for the Actionlog (the table that keeps a historical log of
|
|
|
|
* checkouts, checkins, and updates).
|
|
|
|
*
|
|
|
|
* @version v1.0
|
|
|
|
*/
|
2016-12-29 22:23:36 -08:00
|
|
|
class Actionlog extends SnipeModel
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-12-29 22:23:36 -08:00
|
|
|
protected $presenter = "App\Presenters\ActionlogPresenter";
|
2016-03-25 01:18:05 -07:00
|
|
|
use SoftDeletes;
|
2016-12-29 22:23:36 -08:00
|
|
|
use Presentable;
|
2016-03-25 01:18:05 -07:00
|
|
|
protected $dates = [ 'deleted_at' ];
|
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
protected $table = 'action_logs';
|
2016-03-25 01:18:05 -07:00
|
|
|
public $timestamps = true;
|
2016-09-06 19:39:42 -07:00
|
|
|
protected $fillable = [ 'created_at', 'item_type','user_id','item_id','action_type','note','target_id', 'target_type' ];
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-09-29 22:20:49 -07:00
|
|
|
// Overridden from Builder to automatically add the company
|
|
|
|
public static function boot()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-09-29 22:20:49 -07:00
|
|
|
parent::boot();
|
2016-12-29 14:02:18 -08:00
|
|
|
static::creating(function (Actionlog $actionlog) {
|
2016-09-29 22:20:49 -07:00
|
|
|
// If the admin is a superadmin, let's see if the target instead has a company.
|
|
|
|
if (Auth::user() && Auth::user()->isSuperUser()) {
|
2016-10-12 18:45:32 -07:00
|
|
|
if ($actionlog->target) {
|
|
|
|
$actionlog->company_id = $actionlog->target->company_id;
|
2016-12-29 14:02:18 -08:00
|
|
|
} elseif ($actionlog->item) {
|
2016-10-12 18:45:32 -07:00
|
|
|
$actionlog->company_id = $actionlog->item->company_id;
|
|
|
|
}
|
2016-12-29 14:02:18 -08:00
|
|
|
} elseif (Auth::user() && Auth::user()->company) {
|
2016-09-29 22:20:49 -07:00
|
|
|
$actionlog->company_id = Auth::user()->company_id;
|
|
|
|
}
|
|
|
|
});
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-09-06 19:39:42 -07:00
|
|
|
// Eloquent Relationships below
|
|
|
|
public function item()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-09-06 19:39:42 -07:00
|
|
|
return $this->morphTo('item')->withTrashed();
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-12-29 14:02:18 -08:00
|
|
|
public function company()
|
|
|
|
{
|
|
|
|
return $this->hasMany('\App\Models\Company', 'id', 'company_id');
|
2016-11-11 20:30:26 -08:00
|
|
|
}
|
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
public function itemType()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-09-20 00:45:25 -07:00
|
|
|
|
2016-12-29 14:02:18 -08:00
|
|
|
if ($this->item_type == AssetModel::class) {
|
2016-09-15 19:58:27 -07:00
|
|
|
return "model";
|
|
|
|
}
|
2016-09-06 19:39:42 -07:00
|
|
|
return camel_case(class_basename($this->item_type));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2017-05-23 14:31:14 -07:00
|
|
|
public function targetType()
|
|
|
|
{
|
|
|
|
if ($this->target_type == User::class) {
|
|
|
|
return "user";
|
|
|
|
}
|
|
|
|
return camel_case(class_basename($this->target_type));
|
|
|
|
}
|
|
|
|
|
2016-12-29 14:02:18 -08:00
|
|
|
public function parseItemRoute()
|
|
|
|
{
|
2016-12-15 20:52:39 -08:00
|
|
|
if ($this->itemType() == "asset") {
|
|
|
|
$itemroute = 'assets';
|
|
|
|
} elseif ($this->itemType() == "accessory") {
|
|
|
|
$itemroute = 'accessories';
|
|
|
|
} elseif ($this->itemType()=="consumable") {
|
|
|
|
$itemroute = 'consumables';
|
2016-12-29 14:02:18 -08:00
|
|
|
} elseif ($this->itemType()=="license") {
|
2016-12-15 20:52:39 -08:00
|
|
|
$itemroute = 'licenses';
|
|
|
|
} elseif ($this->itemType()=="component") {
|
|
|
|
$itemroute = 'components';
|
|
|
|
} else {
|
|
|
|
$itemroute = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $itemroute;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
public function uploads()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-09-06 19:39:42 -07:00
|
|
|
return $this->morphTo('item')
|
|
|
|
->where('action_type', '=', 'uploaded')
|
2016-03-25 01:18:05 -07:00
|
|
|
->withTrashed();
|
|
|
|
}
|
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
public function userlog()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-09-06 19:39:42 -07:00
|
|
|
return $this->target();
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
public function user()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-09-06 19:39:42 -07:00
|
|
|
return $this->belongsTo(User::class, 'user_id')
|
2016-03-25 01:18:05 -07:00
|
|
|
->withTrashed();
|
|
|
|
}
|
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
public function target()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-12-30 11:44:47 -08:00
|
|
|
return $this->morphTo('target')->withTrashed();
|
2016-08-02 05:06:17 -07:00
|
|
|
}
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
public function childlogs()
|
|
|
|
{
|
|
|
|
return $this->hasMany('\App\Models\ActionLog', 'thread_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function parentlog()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('\App\Models\ActionLog', 'thread_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the file exists, and if it does, force a download
|
|
|
|
**/
|
2016-10-31 21:00:30 -07:00
|
|
|
public function get_src($type = 'assets', $fieldname = 'filename')
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-10-31 21:00:30 -07:00
|
|
|
$file = config('app.private_uploads') . '/' . $type . '/' . $this->{$fieldname};
|
2016-03-25 01:18:05 -07:00
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
2016-10-31 21:00:30 -07:00
|
|
|
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
/**
|
|
|
|
* Get the parent category name
|
|
|
|
*/
|
|
|
|
public function logaction($actiontype)
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->action_type = $actiontype;
|
|
|
|
|
|
|
|
if ($this->save()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getListingOfActionLogsChronologicalOrder
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
* @author Vincent Sposato <vincent.sposato@gmail.com>
|
|
|
|
* @version v1.0
|
|
|
|
*/
|
|
|
|
public function getListingOfActionLogsChronologicalOrder()
|
|
|
|
{
|
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
return $this->all()
|
2016-03-25 01:18:05 -07:00
|
|
|
->where('action_type', '!=', 'uploaded')
|
2016-09-06 19:39:42 -07:00
|
|
|
->orderBy('item_id', 'asc')
|
2016-03-25 01:18:05 -07:00
|
|
|
->orderBy('created_at', 'asc')
|
|
|
|
->get();
|
|
|
|
}
|
2016-11-11 20:30:26 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Query builder scope to search on text for complex Bootstrap Tables API
|
|
|
|
*
|
|
|
|
* @param Illuminate\Database\Query\Builder $query Query builder instance
|
|
|
|
* @param text $search Search term
|
|
|
|
*
|
|
|
|
* @return Illuminate\Database\Query\Builder Modified query builder
|
|
|
|
*/
|
|
|
|
public function scopeTextSearch($query, $search)
|
|
|
|
{
|
|
|
|
$search = explode(' OR ', $search);
|
|
|
|
|
|
|
|
return $query->where(function ($query) use ($search) {
|
|
|
|
|
|
|
|
foreach ($search as $search) {
|
|
|
|
$query->where(function ($query) use ($search) {
|
|
|
|
$query->whereHas('company', function ($query) use ($search) {
|
|
|
|
$query->where('companies.name', 'LIKE', '%'.$search.'%');
|
|
|
|
});
|
|
|
|
})->orWhere('action_type', 'LIKE', '%'.$search.'%')
|
|
|
|
->orWhere('note', 'LIKE', '%'.$search.'%');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|