forked from trilbymedia/grav-plugin-flex-directory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflex-directory.php
188 lines (166 loc) · 6.91 KB
/
flex-directory.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
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Plugin\FlexDirectory\Controllers\AdminController;
use Grav\Plugin\FlexDirectory\Controllers\SiteController;
use Grav\Plugin\FlexDirectory\FlexDirectory;
use RocketTheme\Toolbox\Event\Event;
/**
* Class FlexDirectoryPlugin
* @package Grav\Plugin
*/
class FlexDirectoryPlugin extends Plugin
{
/** @var AdminController|SiteController */
protected $controller;
protected $version;
protected $directory;
/**
* @return array
*
* The getSubscribedEvents() gives the core a list of events
* that the plugin wants to listen to. The key of each
* array section is the event that the plugin listens to
* and the value (in the form of an array) contains the
* callable (or function) as well as the priority. The
* higher the number the higher the priority.
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onPageInitialized' => ['onPageInitialized', 0],
];
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized()
{
require_once __DIR__ . '/vendor/autoload.php';
if ($this->isAdmin()) {
$this->enable([
'onTwigTemplatePaths' => ['onTwigAdminTemplatePaths', 0],
'onAdminMenu' => ['onAdminMenu', 0],
'onDataTypeExcludeFromDataManagerPluginHook' => ['onDataTypeExcludeFromDataManagerPluginHook', 0],
'onAdminControllerInit' => ['onAdminControllerInit', 0],
]);
/** @var AdminController controller */
$this->controller = new AdminController($this);
} else {
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
]);
/** @var SiteController controller */
$this->controller = new SiteController($this);
}
$config = $this->config->get('plugins.flex-directory');
$blueprints = $config['directories'] ?: [];
if (is_string($blueprints)) {
$blueprints = FlexDirectory::getAllFromFolder($blueprints);
}
// Add to DI container
$this->grav['flex_directory'] = function () use ($blueprints) {
$list = [];
foreach ($blueprints as $blueprint) {
$list[basename($blueprint, '.yaml')] = $blueprint;
}
return new FlexDirectory($list);
};
}
public function onPageInitialized()
{
if ($this->controller->isActive()) {
$this->grav['page']->path($this->controller->getPath());
$this->grav['page']->media();
$this->controller->execute();
$this->controller->redirect();
}
}
public function onAdminControllerInit(Event $event)
{
$eventController = $event['controller'];
$eventController->blacklist_views[] = $this->name;
}
/**
* Add Flex Directory to admin menu
*/
public function onAdminMenu()
{
$this->grav['twig']->plugins_hooked_nav['PLUGIN_FLEX_DIRECTORY.TITLE'] = [
'route' => $this->name,
'icon' => 'fa-list',
'badge' => [
'count' => $this->grav['flex_directory']->count()
]
];
}
/**
* Exclude Flex Directory data from the Data Manager plugin
*/
public function onDataTypeExcludeFromDataManagerPluginHook()
{
$this->grav['admin']->dataTypesExcludedFromDataManagerPlugin[] = 'directory';
}
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
if (!$this->controller->isActive()) return;
$extra_site_twig_path = $this->config->get('plugins.flex-directory.extra_site_twig_path');
$extra_path = $extra_site_twig_path ? $this->grav['locator']->findResource($extra_site_twig_path) : null;
if ($extra_path) {
$this->grav['twig']->twig_paths[] = $extra_path;
}
array_unshift($this->grav['twig']->twig_paths, __DIR__ . '/templates');
}
/**
* Add plugin templates path
*/
public function onTwigAdminTemplatePaths()
{
if (!$this->controller->isActive()) return;
$extra_admin_twig_path = $this->config->get('plugins.flex-directory.extra_admin_twig_path');
$extra_path = $extra_admin_twig_path ? $this->grav['locator']->findResource($extra_admin_twig_path) : null;
if ($extra_path) {
$this->grav['twig']->twig_paths[] = $extra_path;
}
array_unshift($this->grav['twig']->twig_paths, __DIR__ . '/admin/templates');
}
/**
* Set needed variables to display direcotry.
*/
public function onTwigSiteVariables()
{
if ($this->isAdmin()) {
// Twig shortcuts
$this->grav['twig']->twig_vars['location'] = $this->controller->getLocation();
$this->grav['twig']->twig_vars['action'] = $this->controller->getAction();
$this->grav['twig']->twig_vars['task'] = $this->controller->getTask();
$this->grav['twig']->twig_vars['target'] = $this->controller->getTarget();
if ($this->controller->isActive()) {
$this->grav['twig']->twig_vars['context'] = $this->grav['page'];
$this->grav['twig']->twig_vars['context_mediaUri'] = $this->controller->getUri();
$this->grav['twig']->twig_vars['context_route'] = $this->controller->getRoute();
}
// CSS / JS Assets
$this->grav['assets']->addCss('plugin://flex-directory/css/admin.css');
$this->grav['assets']->addCss('plugin://admin/themes/grav/css/codemirror/codemirror.css');
if ($this->controller->getLocation() === 'flex-directory' && $this->controller->getAction() === 'list') {
$this->grav['assets']->addCss('plugin://flex-directory/css/filter.formatter.css');
$this->grav['assets']->addCss('plugin://flex-directory/css/theme.default.css');
$this->grav['assets']->addJs('plugin://flex-directory/js/jquery.tablesorter.min.js');
$this->grav['assets']->addJs('plugin://flex-directory/js/widgets/widget-storage.min.js');
$this->grav['assets']->addJs('plugin://flex-directory/js/widgets/widget-filter.min.js');
$this->grav['assets']->addJs('plugin://flex-directory/js/widgets/widget-pager.min.js');
}
} else {
if ($this->config->get('plugins.flex-directory.built_in_css')) {
$this->grav['assets']->addCss('plugin://flex-directory/css/site.css');
}
$this->grav['assets']->addJs('plugin://flex-directory/js/list.min.js');
}
}
}