2016-09-06 19:39:42 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2017-08-25 10:04:19 -07:00
|
|
|
use App\Notifications\AuditNotification;
|
2016-09-06 19:39:42 -07:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
trait Loggable
|
|
|
|
{
|
|
|
|
|
2016-09-19 23:51:45 -07:00
|
|
|
/**
|
2018-06-20 01:59:59 -07:00
|
|
|
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
2016-09-19 23:51:45 -07:00
|
|
|
* @since [v3.4]
|
|
|
|
* @return \App\Models\Actionlog
|
|
|
|
*/
|
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
public function log()
|
|
|
|
{
|
|
|
|
return $this->morphMany(Actionlog::class, 'item');
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:51:45 -07:00
|
|
|
/**
|
2018-06-20 01:59:59 -07:00
|
|
|
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
2016-09-19 23:51:45 -07:00
|
|
|
* @since [v3.4]
|
|
|
|
* @return \App\Models\Actionlog
|
|
|
|
*/
|
2019-03-14 01:09:16 -07:00
|
|
|
public function logCheckout($note, $target, $action_date = null)
|
2016-09-06 19:39:42 -07:00
|
|
|
{
|
|
|
|
$log = new Actionlog;
|
2017-08-31 11:14:21 -07:00
|
|
|
$log = $this->determineLogItemType($log);
|
2019-01-15 13:58:23 -08:00
|
|
|
if(Auth::user())
|
|
|
|
$log->user_id = Auth::user()->id;
|
2016-09-19 23:51:45 -07:00
|
|
|
|
2017-08-31 11:14:21 -07:00
|
|
|
if (!isset($target)) {
|
|
|
|
throw new Exception('All checkout logs require a target');
|
|
|
|
return;
|
2016-09-06 19:39:42 -07:00
|
|
|
}
|
2017-08-31 11:14:21 -07:00
|
|
|
$log->target_type = get_class($target);
|
|
|
|
$log->target_id = $target->id;
|
2016-09-19 23:51:45 -07:00
|
|
|
|
2017-09-25 21:40:43 -07:00
|
|
|
|
|
|
|
// Figure out what the target is
|
2018-03-25 13:46:57 -07:00
|
|
|
if ($log->target_type == Location::class) {
|
2017-08-31 11:14:21 -07:00
|
|
|
$log->location_id = $target->id;
|
2018-03-25 13:46:57 -07:00
|
|
|
} elseif ($log->target_type == Asset::class) {
|
2018-05-16 19:20:43 -07:00
|
|
|
$log->location_id = $target->location_id;
|
2016-12-27 16:24:41 -08:00
|
|
|
} else {
|
2017-08-31 11:14:21 -07:00
|
|
|
$log->location_id = $target->location_id;
|
2016-12-27 16:24:41 -08:00
|
|
|
}
|
2017-09-25 21:40:43 -07:00
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
$log->note = $note;
|
2019-03-13 21:36:32 -07:00
|
|
|
$log->action_date = $action_date;
|
2019-03-14 01:09:16 -07:00
|
|
|
|
|
|
|
if (!$log->action_date) {
|
|
|
|
$log->action_date = date('Y-m-d H:i:s');
|
|
|
|
}
|
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
$log->logaction('checkout');
|
|
|
|
|
|
|
|
return $log;
|
|
|
|
}
|
|
|
|
|
2017-08-31 11:14:21 -07:00
|
|
|
/**
|
|
|
|
* Helper method to determine the log item type
|
|
|
|
*/
|
|
|
|
private function determineLogItemType($log)
|
|
|
|
{
|
|
|
|
// We need to special case licenses because of license_seat vs license. So much for clean polymorphism :
|
|
|
|
if (static::class == LicenseSeat::class) {
|
|
|
|
$log->item_type = License::class;
|
|
|
|
$log->item_id = $this->license_id;
|
|
|
|
} else {
|
|
|
|
$log->item_type = static::class;
|
|
|
|
$log->item_id = $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $log;
|
|
|
|
}
|
2016-09-19 23:51:45 -07:00
|
|
|
/**
|
2018-06-20 01:59:59 -07:00
|
|
|
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
2016-09-19 23:51:45 -07:00
|
|
|
* @since [v3.4]
|
|
|
|
* @return \App\Models\Actionlog
|
|
|
|
*/
|
2019-03-13 21:36:32 -07:00
|
|
|
public function logCheckin($target, $note, $action_date)
|
2016-09-06 19:39:42 -07:00
|
|
|
{
|
|
|
|
$log = new Actionlog;
|
2016-11-17 15:54:29 -08:00
|
|
|
$log->target_type = get_class($target);
|
|
|
|
$log->target_id = $target->id;
|
2018-03-25 13:46:57 -07:00
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
if (static::class == LicenseSeat::class) {
|
|
|
|
$log->item_type = License::class;
|
|
|
|
$log->item_id = $this->license_id;
|
|
|
|
} else {
|
2018-05-16 19:20:43 -07:00
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
$log->item_type = static::class;
|
|
|
|
$log->item_id = $this->id;
|
2018-05-16 19:20:43 -07:00
|
|
|
|
|
|
|
if (static::class == Asset::class) {
|
|
|
|
if ($asset = Asset::find($log->item_id)) {
|
|
|
|
$asset->increment('checkin_counter', 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
}
|
2018-03-25 13:46:57 -07:00
|
|
|
|
|
|
|
|
2016-09-06 19:39:42 -07:00
|
|
|
$log->location_id = null;
|
|
|
|
$log->note = $note;
|
2019-03-13 21:36:32 -07:00
|
|
|
$log->action_date = $action_date;
|
2016-09-06 19:39:42 -07:00
|
|
|
$log->user_id = Auth::user()->id;
|
|
|
|
$log->logaction('checkin from');
|
|
|
|
|
|
|
|
return $log;
|
|
|
|
}
|
|
|
|
|
2017-08-25 10:04:19 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author A. Gianotto <snipe@snipe.net>
|
|
|
|
* @since [v4.0]
|
|
|
|
* @return \App\Models\Actionlog
|
|
|
|
*/
|
2018-04-24 02:54:54 -07:00
|
|
|
public function logAudit($note, $location_id, $filename = null)
|
2017-08-25 10:04:19 -07:00
|
|
|
{
|
|
|
|
$log = new Actionlog;
|
2017-08-31 21:30:38 -07:00
|
|
|
$location = Location::find($location_id);
|
2017-08-25 10:04:19 -07:00
|
|
|
if (static::class == LicenseSeat::class) {
|
|
|
|
$log->item_type = License::class;
|
|
|
|
$log->item_id = $this->license_id;
|
|
|
|
} else {
|
|
|
|
$log->item_type = static::class;
|
|
|
|
$log->item_id = $this->id;
|
|
|
|
}
|
2017-08-25 18:40:20 -07:00
|
|
|
$log->location_id = ($location_id) ? $location_id : null;
|
2017-08-25 10:04:19 -07:00
|
|
|
$log->note = $note;
|
|
|
|
$log->user_id = Auth::user()->id;
|
2018-04-24 02:54:54 -07:00
|
|
|
$log->filename = $filename;
|
2017-08-25 10:04:19 -07:00
|
|
|
$log->logaction('audit');
|
|
|
|
|
|
|
|
$params = [
|
|
|
|
'item' => $log->item,
|
2018-04-24 02:54:54 -07:00
|
|
|
'filename' => $log->filename,
|
2017-08-25 10:04:19 -07:00
|
|
|
'admin' => $log->user,
|
2017-08-31 21:30:38 -07:00
|
|
|
'location' => ($location) ? $location->name : '',
|
2017-08-25 10:04:19 -07:00
|
|
|
'note' => $note
|
|
|
|
];
|
|
|
|
Setting::getSettings()->notify(new AuditNotification($params));
|
|
|
|
|
|
|
|
return $log;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-25 18:40:20 -07:00
|
|
|
|
2016-10-12 18:45:32 -07:00
|
|
|
/**
|
2018-06-20 01:59:59 -07:00
|
|
|
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
2016-10-17 11:07:08 -07:00
|
|
|
* @since [v3.5]
|
2016-10-12 18:45:32 -07:00
|
|
|
* @return \App\Models\Actionlog
|
|
|
|
*/
|
|
|
|
public function logCreate($note = null)
|
|
|
|
{
|
2016-10-17 11:07:08 -07:00
|
|
|
$user_id = -1;
|
|
|
|
if (Auth::user()) {
|
|
|
|
$user_id = Auth::user()->id;
|
|
|
|
}
|
2016-10-12 18:45:32 -07:00
|
|
|
$log = new Actionlog;
|
|
|
|
if (static::class == LicenseSeat::class) {
|
|
|
|
$log->item_type = License::class;
|
|
|
|
$log->item_id = $this->license_id;
|
|
|
|
} else {
|
|
|
|
$log->item_type = static::class;
|
|
|
|
$log->item_id = $this->id;
|
|
|
|
}
|
|
|
|
$log->location_id = null;
|
|
|
|
$log->note = $note;
|
2016-10-17 11:07:08 -07:00
|
|
|
$log->user_id = $user_id;
|
2018-01-03 17:22:02 -08:00
|
|
|
$log->logaction('create');
|
2016-10-17 11:07:08 -07:00
|
|
|
$log->save();
|
2016-10-12 18:45:32 -07:00
|
|
|
return $log;
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:51:45 -07:00
|
|
|
/**
|
2018-06-20 01:59:59 -07:00
|
|
|
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
2016-09-19 23:51:45 -07:00
|
|
|
* @since [v3.4]
|
|
|
|
* @return \App\Models\Actionlog
|
|
|
|
*/
|
2016-09-06 19:39:42 -07:00
|
|
|
public function logUpload($filename, $note)
|
|
|
|
{
|
|
|
|
$log = new Actionlog;
|
|
|
|
if (static::class == LicenseSeat::class) {
|
|
|
|
$log->item_type = License::class;
|
|
|
|
$log->item_id = $this->license_id;
|
|
|
|
} else {
|
|
|
|
$log->item_type = static::class;
|
|
|
|
$log->item_id = $this->id;
|
|
|
|
}
|
|
|
|
$log->user_id = Auth::user()->id;
|
|
|
|
$log->note = $note;
|
|
|
|
$log->target_id = null;
|
2017-04-06 19:48:15 -07:00
|
|
|
$log->created_at = date("Y-m-d H:i:s");
|
2016-09-06 19:39:42 -07:00
|
|
|
$log->filename = $filename;
|
|
|
|
$log->logaction('uploaded');
|
|
|
|
|
|
|
|
return $log;
|
|
|
|
}
|
|
|
|
}
|