snipe-it/app/Models/Actionlog.php

131 lines
3.2 KiB
PHP
Raw Normal View History

2016-03-25 01:18:05 -07:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;
2016-03-25 01:18:05 -07:00
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
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
*/
class Actionlog extends Model
2016-03-25 01:18:05 -07:00
{
use SoftDeletes;
protected $dates = [ 'deleted_at' ];
protected $table = 'action_logs';
2016-03-25 01:18:05 -07:00
public $timestamps = true;
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
// Overridden from Builder to automatically add the company
public static function boot()
2016-03-25 01:18:05 -07:00
{
parent::boot();
static::creating( function (Actionlog $actionlog) {
// If the admin is a superadmin, let's see if the target instead has a company.
if (Auth::user() && Auth::user()->isSuperUser()) {
$actionlog->company_id = $actionlog->target->company_id;
} else if (Auth::user() && Auth::user()->company) {
$actionlog->company_id = Auth::user()->company_id;
}
});
2016-03-25 01:18:05 -07:00
}
// Eloquent Relationships below
public function item()
2016-03-25 01:18:05 -07:00
{
return $this->morphTo('item')->withTrashed();
2016-03-25 01:18:05 -07:00
}
public function itemType()
2016-03-25 01:18:05 -07:00
{
2016-09-20 00:45:25 -07:00
if($this->item_type == AssetModel::class) {
return "model";
}
return camel_case(class_basename($this->item_type));
2016-03-25 01:18:05 -07:00
}
public function uploads()
2016-03-25 01:18:05 -07:00
{
return $this->morphTo('item')
->where('action_type', '=', 'uploaded')
2016-03-25 01:18:05 -07:00
->withTrashed();
}
public function userlog()
2016-03-25 01:18:05 -07:00
{
return $this->target();
2016-03-25 01:18:05 -07:00
}
public function user()
2016-03-25 01:18:05 -07:00
{
return $this->belongsTo(User::class, 'user_id')
2016-03-25 01:18:05 -07:00
->withTrashed();
}
public function target()
2016-03-25 01:18:05 -07:00
{
return $this->morphTo('target');
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-03-25 17:20:28 -07:00
public function get_src($type = 'assets')
2016-03-25 01:18:05 -07:00
{
2016-03-25 17:20:28 -07:00
$file = config('app.private_uploads') . '/' . $type . '/' . $this->filename;
2016-03-25 01:18:05 -07:00
return $file;
}
/**
* 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()
{
return $this->all()
2016-03-25 01:18:05 -07:00
->where('action_type', '!=', 'uploaded')
->orderBy('item_id', 'asc')
2016-03-25 01:18:05 -07:00
->orderBy('created_at', 'asc')
->get();
}
}