-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPlugin.php
254 lines (228 loc) · 8.08 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php namespace Graker\PhotoAlbums;
use Backend;
use System\Classes\PluginBase;
use Event;
use Backend\Widgets\Form;
use Lang;
use Graker\PhotoAlbums\Widgets\PhotoSelector;
use Graker\PhotoAlbums\Classes\MenuItemsProvider;
/**
* PhotoAlbums Plugin Information File
*/
class Plugin extends PluginBase
{
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'graker.photoalbums::lang.plugin.name',
'description' => 'graker.photoalbums::lang.plugin.description',
'author' => 'Graker',
'icon' => 'icon-camera-retro',
'homepage' => 'https://github.com/graker/oc-photoalbums-plugin',
];
}
/**
* Registers any front-end components implemented in this plugin.
*
* @return array
*/
public function registerComponents()
{
return [
'Graker\PhotoAlbums\Components\Photo' => 'singlePhoto',
'Graker\PhotoAlbums\Components\Album' => 'photoAlbum',
'Graker\PhotoAlbums\Components\AlbumList' => 'albumList',
'Graker\PhotoAlbums\Components\RandomPhotos' => 'randomPhotos',
];
}
/**
* Registers any back-end permissions used by this plugin.
* At the moment there's one permission allowing overall management of albums and photos
*
* @return array
*/
public function registerPermissions()
{
return [
'graker.photoalbums.manage_albums' => [
'label' => 'graker.photoalbums::lang.plugin.manage_albums',
'tab' => 'graker.photoalbums::lang.plugin.tab',
],
'graker.photoalbums.access_settings' => [
'label' => 'graker.photoalbums::lang.plugin.access_permission',
'tab' => 'graker.photoalbums::lang.plugin.tab',
],
];
}
/**
* Registers back-end navigation items for this plugin.
*
* @return array
*/
public function registerNavigation()
{
return [
'photoalbums' => [
'label' => 'graker.photoalbums::lang.plugin.tab',
'url' => Backend::url('graker/photoalbums/albums'),
'icon' => 'icon-camera-retro',
'permissions' => ['graker.photoalbums.manage_albums'],
'order' => 500,
'sideMenu' => [
'upload_photos' => [
'label' => 'graker.photoalbums::lang.plugin.upload_photos',
'icon' => 'icon-upload',
'url' => Backend::url('graker/photoalbums/upload/form'),
'permissions' => ['graker.photoalbums.manage_albums'],
],
'new_album' => [
'label' => 'graker.photoalbums::lang.plugin.new_album',
'icon' => 'icon-plus',
'url' => Backend::url('graker/photoalbums/albums/create'),
'permissions' => ['graker.photoalbums.manage_albums'],
],
'albums' => [
'label' => 'graker.photoalbums::lang.plugin.albums',
'icon' => 'icon-copy',
'url' => Backend::url('graker/photoalbums/albums'),
'permissions' => ['graker.photoalbums.manage_albums'],
],
'new_photo' => [
'label' => 'graker.photoalbums::lang.plugin.new_photo',
'icon' => 'icon-plus-square-o',
'url' => Backend::url('graker/photoalbums/photos/create'),
'permissions' => ['graker.photoalbums.manage_albums'],
],
'photos' => [
'label' => 'graker.photoalbums::lang.plugin.photos',
'icon' => 'icon-picture-o',
'url' => Backend::url('graker/photoalbums/photos'),
'permissions' => ['graker.photoalbums.manage_albums'],
],
],
],
];
}
/**
*
* Registers plugin's settings
*
* @return array
*/
public function registerSettings()
{
return [
'settings' => [
'label' => 'graker.photoalbums::lang.plugin.name',
'description' => 'graker.photoalbums::lang.plugin.settings_description',
'icon' => 'icon-camera-retro',
'class' => 'Graker\PhotoAlbums\Models\Settings',
'order' => 100,
'permissions' => ['graker.photoalbums.access_settings'],
]
];
}
/**
*
* Custom column types definition
*
* @return array
*/
public function registerListColumnTypes() {
return [
'is_front' => [$this, 'evalIsFrontListColumn'],
'image' => [$this, 'evalImageListColumn'],
];
}
/**
*
* Special column to show photo set to be album's front in album's relations list
*
* @param $value
* @param $column
* @param $record
* @return string
*/
public function evalIsFrontListColumn($value, $column, $record) {
return ($value == $record->id) ? Lang::get('graker.photoalbums::lang.plugin.bool_positive') : '';
}
/**
*
* Column to render image thumb for Photo model
*
* @param $value
* @param $column
* @param $record
* @return string
*/
function evalImageListColumn($value, $column, $record) {
if ($record->has('image')) {
$thumb = $record->image->getThumb(
isset($column->config['width']) ? $column->config['width'] : 200,
isset($column->config['height']) ? $column->config['height'] : 200,
['mode' => 'auto']
);
} else {
// in case the file attachment was manually deleted for some reason
$thumb = '';
}
return "<img src=\"$thumb\" />";
}
/**
* boot() implementation
* - Register listener to markdown.parse
* - Add button to blog post form to insert photos from albums
*/
public function boot() {
Event::listen('markdown.parse', 'Graker\PhotoAlbums\Classes\MarkdownPhotoInsert@parse');
$this->extendBlogPostForm();
$this->registerMenuItems();
}
/**
* Extends Blog post form by adding a new button: Insert photo from albums
*/
protected function extendBlogPostForm() {
Event::listen('backend.form.extendFields', function (Form $widget) {
// attach to post forms only
$controller = $widget->getController();
if (!($controller instanceof \RainLab\Blog\Controllers\Posts)) {
return ;
}
if (!($widget->model instanceof \RainLab\Blog\Models\Post)) {
return ;
}
// add PhotoSelector widget to Post controller
$photo_selector = new PhotoSelector($controller);
$photo_selector->alias = 'photoSelector';
$photo_selector->bindToController();
// add javascript extending Markdown editor with new button
$widget->addJs('/plugins/graker/photoalbums/assets/js/extend-markdown-editor.js');
});
}
/**
* Listen to events from RainLab.Pages plugin to register and resolve new menu items
*/
protected function registerMenuItems() {
// register items
Event::listen('pages.menuitem.listTypes', function() {
return MenuItemsProvider::listTypes();
});
// return item type info
Event::listen('pages.menuitem.getTypeInfo', function($type) {
if (in_array($type, array_keys(MenuItemsProvider::listTypes()))) {
return MenuItemsProvider::getMenuTypeInfo($type);
}
});
// resolve item
Event::listen('pages.menuitem.resolveItem', function($type, $item, $url, $theme) {
if (in_array($type, array_keys(MenuItemsProvider::listTypes()))) {
return MenuItemsProvider::resolveMenuItem($type, $item, $url, $theme);
}
});
}
}