1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tracking\Http\Controllers\Metrics; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Routing\Controller; |
7
|
|
|
use Tracking\Providers\Metrics\LogParser; |
8
|
|
|
use Tracking\Models\Metrics\LarametricsModel; |
9
|
|
|
use DB; |
10
|
|
|
use Carbon\Carbon; |
11
|
|
|
|
12
|
|
|
class ModelController extends Controller |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
public function index() |
16
|
|
|
{ |
17
|
|
|
$modelChanges = LarametricsModel::groupBy('model') |
18
|
|
|
->select('model', DB::raw('count(*) as total')) |
19
|
|
|
->get() |
20
|
|
|
->keyBy('model'); |
21
|
|
|
|
22
|
|
|
$modelsAmounts = array(); |
23
|
|
|
|
24
|
|
|
$earliestModel = LarametricsModel::orderBy('created_at', 'desc') |
25
|
|
|
->first(); |
26
|
|
|
|
27
|
|
|
foreach(\Illuminate\Support\Facades\Config::get('larametrics.modelsWatched') as $model) { |
28
|
|
|
$modelsAmounts[$model] = array( |
29
|
|
|
'count' => $model::count(), |
30
|
|
|
'changes' => isset($modelChanges[$model]) ? $modelChanges[$model]['total'] : 0 |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return view( |
35
|
|
|
'rica.larametrics::models.index', [ |
36
|
|
|
'modelsAmounts' => $modelsAmounts, |
37
|
|
|
'pageTitle' => 'Database Models', |
38
|
|
|
'pageSubtitle' => 'Data shown is only by models being watched by Larametrics', |
39
|
|
|
'watchLength' => $earliestModel ? $earliestModel->created_at->diffInDays(Carbon::now()) : 0 |
40
|
|
|
] |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function show($model) |
45
|
|
|
{ |
46
|
|
|
if(is_numeric($model)) { |
47
|
|
|
$larametricsModel = LarametricsModel::find($model); |
48
|
|
|
|
49
|
|
|
$modelPrimaryKey = (new $larametricsModel->model)->getKeyName(); |
50
|
|
|
|
51
|
|
|
return view( |
52
|
|
|
'rica.larametrics::models.show', [ |
53
|
|
|
'model' => $larametricsModel, |
54
|
|
|
'pageTitle' => $larametricsModel->model, |
55
|
|
|
'modelPrimaryKey' => $modelPrimaryKey |
56
|
|
|
] |
57
|
|
|
); |
58
|
|
|
} else { |
59
|
|
|
$appModel = str_replace('+', '\\', $model); |
60
|
|
|
|
61
|
|
|
$models = LarametricsModel::where('model', $appModel) |
62
|
|
|
->orderBy('created_at', 'desc') |
63
|
|
|
->get(); |
64
|
|
|
|
65
|
|
|
$earliestModel = LarametricsModel::orderBy('created_at', 'desc') |
66
|
|
|
->first(); |
67
|
|
|
|
68
|
|
|
$modelPrimaryKey = (new $appModel)->getKeyName(); |
69
|
|
|
|
70
|
|
|
return view( |
71
|
|
|
'rica.larametrics::models.model', [ |
72
|
|
|
'models' => $models, |
73
|
|
|
'pageTitle' => $appModel, |
74
|
|
|
'watchLength' => $earliestModel ? $earliestModel->created_at->diffInDays(Carbon::now()) : 0, |
75
|
|
|
'modelPrimaryKey' => $modelPrimaryKey |
76
|
|
|
] |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function revert(LarametricsModel $model) |
82
|
|
|
{ |
83
|
|
|
$original = json_decode($model->original, true); |
|
|
|
|
84
|
|
|
$revertModel = $model->model::find($original['id']); |
85
|
|
|
|
86
|
|
|
if($model->method === 'created') { |
87
|
|
|
|
88
|
|
|
if($revertModel) { |
89
|
|
|
$revertModel->delete(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} elseif($model->method === 'deleted') { |
93
|
|
|
|
94
|
|
|
unset($original['id']); |
95
|
|
|
$model->model::create($original); |
96
|
|
|
|
97
|
|
|
} else { |
98
|
|
|
|
99
|
|
|
if($revertModel) { |
100
|
|
|
unset($original['updated_at']); |
101
|
|
|
$revertModel->update($original); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return redirect()->route('rica.larametrics::models.index'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.