-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.php
More file actions
93 lines (85 loc) · 2.87 KB
/
Plugin.php
File metadata and controls
93 lines (85 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php namespace Kpolicar\BackendTrafficCop;
use App;
use Backend;
use Backend\Widgets\Form as BackendForm;
use Carbon\Carbon;
use Kpolicar\BackendMenuPinnedPages\Models\PinnedPage;
use Kpolicar\BackendTrafficCop\Exceptions\ModelHasChangedException;
use October\Rain\Database\Model;
use October\Rain\Exception\ApplicationException;
use RainLab\Blog\Models\Post;
use System\Classes\PluginBase;
use Backend\Classes\Controller as BackendController;
use Backend\Models\User as BackendUser;
use BackendAuth;
/**
* BackendTrafficCop Plugin Information File
*/
class Plugin extends PluginBase
{
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'kpolicar.backendtrafficcop::lang.plugin.name',
'description' => 'kpolicar.backendtrafficcop::lang.plugin.description',
'author' => 'Klemen Janez Poličar',
'icon' => 'icon-random'
];
}
/**
* Register method, called when the plugin is first registered.
*
* @return void
*/
public function register()
{
App::error(function (ModelHasChangedException $e) {
return e(trans('kpolicar.backendtrafficcop::lang.popup.message'));
});
}
/**
* Boot method, called right before the request route.
*
* @return array
*/
public function boot()
{
if (!App::runningInBackend())
return;
BackendForm::extend(function($widget) {
$widget->addJs('/plugins/kpolicar/backendtrafficcop/assets/js/traffic-cop.js', ['defer' => true]);
});
\Event::listen('backend.form.extendFields', function (BackendForm $form) {
if (!$form->model->exists)
return;
$form->addFields([
'_retrieved_at' => [
'type' => 'text',
'readOnly' => true,
'containerAttributes' => ['js-retrieved-at' => ''],
'cssClass' => 'hidden',
]
]);
$form->getField('_retrieved_at')->value = now();
});
Model::extend(function (Model $model) {
$model->addDynamicProperty('_retrieved_at', null);
$model->addDynamicMethod('hasBeenSavedSinceRetrieval', function () use ($model) {
return $model->exists && Carbon::parse($model->_retrieved_at)->isBefore($model->updated_at);
});
if (!post('_kpolicar_backendtrafficcop_confirmed')) {
$model->bindEventOnce('model.saveInternal', function ($attributes, $options) use ($model) {
throw_if(
$model->hasBeenSavedSinceRetrieval(),
ModelHasChangedException::class
);
});
}
});
}
}