snipe-it/app/Models/Actionlog.php

288 lines
7.9 KiB
PHP
Raw Normal View History

2016-03-25 01:18:05 -07:00
<?php
2016-03-25 01:18:05 -07:00
namespace App\Models;
use App\Models\Traits\Searchable;
use App\Presenters\Presentable;
use Carbon;
2021-06-10 13:19:27 -07:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;
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
*/
class Actionlog extends SnipeModel
2016-03-25 01:18:05 -07:00
{
2021-06-10 13:17:44 -07:00
use HasFactory;
protected $presenter = \App\Presenters\ActionlogPresenter::class;
2016-03-25 01:18:05 -07:00
use SoftDeletes;
use Presentable;
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', 'stored_eula'];
2016-03-25 01:18:05 -07:00
use Searchable;
/**
* The attributes that should be included when searching the model.
*
* @var array
*/
protected $searchableAttributes = ['action_type', 'note', 'log_meta','user_id'];
/**
* The relations and their attributes that should be included when searching the model.
*
* @var array
*/
protected $searchableRelations = [
'company' => ['name'],
'user' => ['first_name','last_name','username'],
];
/**
* Override from Builder to automatically add the company
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public static function boot()
2016-03-25 01:18:05 -07:00
{
parent::boot();
static::creating(function (self $actionlog) {
// If the admin is a superadmin, let's see if the target instead has a company.
if (Auth::user() && Auth::user()->isSuperUser()) {
if ($actionlog->target) {
$actionlog->company_id = $actionlog->target->company_id;
2016-12-29 14:02:18 -08:00
} elseif ($actionlog->item) {
$actionlog->company_id = $actionlog->item->company_id;
}
2016-12-29 14:02:18 -08:00
} elseif (Auth::user() && Auth::user()->company) {
$actionlog->company_id = Auth::user()->company_id;
}
});
2016-03-25 01:18:05 -07:00
}
/**
* Establishes the actionlog -> item relationship
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function item()
2016-03-25 01:18:05 -07:00
{
return $this->morphTo('item')->withTrashed();
2016-03-25 01:18:05 -07:00
}
/**
* Establishes the actionlog -> company relationship
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
2016-12-29 14:02:18 -08:00
public function company()
{
return $this->hasMany(\App\Models\Company::class, 'id', 'company_id');
}
/**
* Establishes the actionlog -> item type relationship
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function itemType()
2016-03-25 01:18:05 -07:00
{
2016-12-29 14:02:18 -08: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
}
/**
* Establishes the actionlog -> target type relationship
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
2017-05-23 14:31:14 -07:00
public function targetType()
{
if ($this->target_type == User::class) {
return 'user';
2017-05-23 14:31:14 -07:00
}
2017-05-23 14:31:14 -07:00
return camel_case(class_basename($this->target_type));
}
2016-12-15 20:52:39 -08:00
/**
* Establishes the actionlog -> uploads relationship
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
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();
}
/**
* Establishes the actionlog -> userlog relationship
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function userlog()
2016-03-25 01:18:05 -07:00
{
return $this->target();
2016-03-25 01:18:05 -07:00
}
/**
* Establishes the actionlog -> user relationship
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
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();
}
/**
* Establishes the actionlog -> target relationship
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function target()
2016-03-25 01:18:05 -07:00
{
return $this->morphTo('target')->withTrashed();
2016-08-02 05:06:17 -07:00
}
/**
* Establishes the actionlog -> location relationship
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function location()
{
return $this->belongsTo(\App\Models\Location::class, 'location_id')->withTrashed();
2017-08-25 18:40:20 -07:00
}
2016-03-25 01:18:05 -07:00
/**
* Check if the file exists, and if it does, force a download
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0]
* @return string | false
*/
public function get_src($type = 'assets', $fieldname = 'filename')
2016-03-25 01:18:05 -07:00
{
if ($this->filename != '') {
$file = config('app.private_uploads').'/'.$type.'/'.$this->{$fieldname};
2018-05-02 14:13:06 -07:00
return $file;
}
2018-05-02 14:13:06 -07:00
return false;
2016-03-25 01:18:05 -07:00
}
/**
* Saves the log record with the action type
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v3.0]
* @return bool
*/
2016-03-25 01:18:05 -07:00
public function logaction($actiontype)
{
$this->action_type = $actiontype;
if ($this->save()) {
return true;
} else {
return false;
}
}
/**
* Calculate the number of days until the next audit
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0]
* @return int
*/
public function daysUntilNextAudit($monthInterval = 12, $asset = null)
{
$now = Carbon::now();
2017-08-25 18:40:20 -07:00
$last_audit_date = $this->created_at;
$next_audit = $last_audit_date->addMonth($monthInterval);
$next_audit_days = $now->diffInDays($next_audit);
2017-08-25 18:40:20 -07:00
// Override the default setting for interval if the asset has its own next audit date
if (($asset) && ($asset->next_audit_date)) {
$override_default_next = \Carbon::parse($asset->next_audit_date);
$next_audit_days = $override_default_next->diffInDays($now);
2017-08-25 18:40:20 -07:00
}
return $next_audit_days;
}
/**
* Calculate the date of the next audit
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0]
* @return \Datetime
*/
public function calcNextAuditDate($monthInterval = 12, $asset = null)
{
2017-08-25 18:40:20 -07:00
$last_audit_date = Carbon::parse($this->created_at);
// If there is an asset-specific next date already given,
if (($asset) && ($asset->next_audit_date)) {
2017-08-28 17:20:20 -07:00
return \Carbon::parse($asset->next_audit_date);
2017-08-25 18:40:20 -07:00
}
2017-08-28 17:20:20 -07:00
return \Carbon::parse($last_audit_date)->addMonths($monthInterval)->toDateString();
2017-08-25 18:40:20 -07:00
}
2016-03-25 01:18:05 -07:00
/**
* Gets action logs in chronological order, excluding uploads
*
* @author Vincent Sposato <vincent.sposato@gmail.com>
* @since v1.0
* @return \Illuminate\Database\Eloquent\Collection
*/
2016-03-25 01:18:05 -07:00
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();
}
}