forked from OFFLINE-GmbH/oc-responsive-images-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
166 lines (147 loc) · 5.74 KB
/
Plugin.php
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php namespace OFFLINE\ResponsiveImages;
use Backend\FormWidgets\FileUpload;
use Cms\Classes\Theme;
use Illuminate\Support\Facades\Event;
use October\Rain\Exception\ApplicationException;
use OFFLINE\ResponsiveImages\Classes\Focuspoint\File as FocusFile;
use OFFLINE\ResponsiveImages\Classes\SVG\SVGInliner;
use OFFLINE\ResponsiveImages\Console\ConvertCommand;
use OFFLINE\ResponsiveImages\Console\GenerateResizedImages;
use OFFLINE\ResponsiveImages\Console\Clear;
use System\Classes\PluginBase;
use System\Models\File;
use System\Traits\AssetMaker;
use URL;
/**
* ResponsiveImages Plugin Information File
*/
class Plugin extends PluginBase
{
use AssetMaker;
/**
* Boot method, called right before the request route.
*
* @return array
*/
public function boot()
{
$this->app['Illuminate\Contracts\Http\Kernel']
->pushMiddleware('OFFLINE\ResponsiveImages\Classes\ResponsiveImagesMiddleware');
Event::listen('backend.page.beforeDisplay', function ($controller, $action, $params) {
$controller->addJs('/plugins/offline/responsiveimages/widgets/fileupload/assets/js/focuspoint-tool.js');
});
File::extend(function (File $file) {
$file->addDynamicMethod('focus', function ($width, $height, $options = []) use ($file) {
return FocusFile::fromFileModel($file)->focus($width, $height, $options);
});
});
FileUpload::extend(function (FileUpload $widget) {
$isEnabled = (bool)($widget->config->focuspoint ?? false);
if ($isEnabled !== true) {
return;
}
$widget->addViewPath('plugins/offline/responsiveimages/widgets/fileupload/partials');
$widget->addDynamicMethod('onSaveAttachmentConfigFocuspoint', function () use ($widget) {
$original = $widget->onSaveAttachmentConfig();
if (is_array($original) === false || array_key_exists('displayName', $original) === false) {
return $original;
}
try {
list($model, $attribute) = $widget->resolveModelAttribute($widget->valueFrom);
$fileModel = $model->makeRelation($attribute);
if (($fileId = post('file_id')) && ($file = $fileModel::find($fileId))) {
$file->offline_responsiveimages_focus_x_axis = post('offline_responsiveimages_focus_x_axis');
$file->offline_responsiveimages_focus_y_axis = post('offline_responsiveimages_focus_y_axis');
$file->title = post('title');
$file->description = post('description');
$file->save();
return ['displayName' => $file->title ?: $file->file_name];
}
throw new ApplicationException('Unable to find file, it may no longer exist');
} catch (\Throwable $ex) {
return json_encode(['error' => $ex->getMessage()]);
}
});
});
}
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'offline.responsiveimages::lang.plugin.name',
'description' => 'offline.responsiveimages::lang.plugin.description',
'author' => 'offline.responsiveimages::lang.plugin.author',
'homepage' => 'https://github.com/OFFLINE-GmbH/oc-responsive-images-plugin',
'icon' => 'icon-file-image-o',
];
}
/**
* Registers any back-end permissions.
*
* @return array
*/
public function registerPermissions()
{
return [
'offline.responsiveimages.manage_settings' => [
'tab' => 'offline.responsiveimages::lang.plugin.name',
'label' => 'offline.responsiveimages::lang.plugin.manage_settings_permission',
],
];
}
/**
* Registers any back-end settings.
*
* @return array
*/
public function registerSettings()
{
return [
'config' => [
'label' => 'offline.responsiveimages::lang.plugin.name',
'description' => 'offline.responsiveimages::lang.plugin.manage_settings',
'category' => 'system::lang.system.categories.cms',
'icon' => 'icon-file-image-o',
'class' => 'Offline\ResponsiveImages\Models\Settings',
'order' => 500,
'keywords' => 'responsive images',
'permissions' => ['offline.responsiveimages.manage_settings'],
],
];
}
public function register()
{
$this->registerConsoleCommand('responsiveimages:generate', GenerateResizedImages::class);
$this->registerConsoleCommand('responsiveimages:convert', ConvertCommand::class);
$this->registerConsoleCommand('responsiveimages:clear', Clear::class);
}
public function registerMarkupTags()
{
return [
'functions' => [
'svg' => function ($path, $vars = []) {
$themeDir = Theme::getActiveThemeCode();
return (new SVGInliner($themeDir))->inline($path, $vars);
},
],
];
}
/**
* Returns the extra report widgets.
*
* @return array
*/
public function registerReportWidgets()
{
return [
'OFFLINE\ResponsiveImages\ReportWidgets\ClearCache' => [
'label' => 'offline.responsiveimages::lang.reportwidgets.clearcache.label',
'context' => 'dashboard'
]
];
}
}