Files
core/app/Models/ActionLog.php
T

81 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
/**
* Class ActionLog
*/
class ActionLog extends Model
{
protected $table = 'action_log';
public $timestamps = false;
protected $fillable = [
'type_id',
'user_id',
'parameters'
];
protected $guarded = [];
/**
* Query factory for loading action logs with their type (therefore with name and value)
*
* @return mixed
*/
public function getLogActionsQuery()
{
$actions = \DB::table('action_log')
->join('action_log_types', 'action_log_types.id', '=', 'action_log.type_id')
->orderBy('created_at', 'DESC');
return $actions;
}
/**
* Get last N user actions
*
* @param null $id
* @param int $limit
*
* @return array
*/
public function getUserActions($id = null, $limit = 15)
{
// if user is not specified, use logged one
if (is_null($id)) {
$id = Auth::user()->id;
}
// load last N actions of selected user
$actions = $this->getLogActionsQuery()
->limit($limit)
->where('user_id', $id)
->get();
// translate and return result
return $this->translateLogMessages($actions);
}
/**
* Method used for translating and filling parameters into value of log.
*
* @param $messages
*
* @return array
*/
public function translateLogMessages($messages)
{
$log = [];
// translate name and value of each record
foreach ($messages as $action) {
$action->name = trans($action->name);
$action->value = trans($action->value, (array)json_decode($action->parameters));
$log[] = $action;
}
return $log;
}
}