forked from baumrock/RockMigrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MagicPages.module.php
228 lines (201 loc) · 7.18 KB
/
MagicPages.module.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
<?php
namespace RockMigrations;
use ProcessWire\HookEvent;
use ProcessWire\Module;
use ProcessWire\PageArray;
use ProcessWire\Paths;
use ProcessWire\RockMigrations;
use ProcessWire\WireData;
use ReflectionClass;
class MagicPages extends WireData implements Module
{
private $readyClasses;
private $filePaths = [];
public static function getModuleInfo()
{
return [
'title' => 'MagicPages',
'version' => '1.0.8',
'summary' => 'Autoload module to support MagicPages',
'autoload' => true,
'singular' => true,
'icon' => 'magic',
'requires' => [
'RockMigrations>=1.6.3',
],
'installs' => [],
];
}
public function __construct()
{
$this->wire->addHookAfter("ProcessWire::init", function () {
$this->readyClasses = $this->wire(new PageArray());
if ($this->wire->config->useMagicClasses === false) return;
if ($this->wire->config->useMagicClasses === 0) return;
foreach ($this->wire->templates as $tpl) {
$p = $this->wire->pages->newPage(['template' => $tpl]);
if (!property_exists($p, "isMagicPage")) continue;
if (!$p->isMagicPage) continue;
if (method_exists($p, 'init')) $p->init();
if (method_exists($p, 'ready')) $this->readyClasses->add($p);
$this->rockmigrations()->watch($p, method_exists($p, 'migrate'));
$this->addMagicMethods($p);
}
});
}
public function init()
{
$this->wire->addHookAfter("ProcessPageEdit::buildForm", $this, "addPageAssets");
}
public function ready()
{
$ready = $this->readyClasses ?: []; // prevents error on uninstall
foreach ($ready as $p) $p->ready();
}
/**
* Add magic methods to this page object
* @param Page $magicPage
* @return void
*/
public function addMagicMethods($magicPage)
{
if (method_exists($magicPage, "editForm")) {
$this->wire->addHookAfter("ProcessPageEdit::buildForm", function ($event) use ($magicPage) {
$page = $event->object->getPage();
if ($page->className !== $magicPage->className) return;
$form = $event->return;
$page->editForm($form, $page);
});
}
if (method_exists($magicPage, "editFormContent")) {
$this->wire->addHookAfter("ProcessPageEdit::buildFormContent", function ($event) use ($magicPage) {
$page = $event->object->getPage();
if ($page->className !== $magicPage->className) return;
$form = $event->return;
$page->editFormContent($form, $page);
});
}
if (method_exists($magicPage, "editFormSettings")) {
$this->wire->addHookAfter("ProcessPageEdit::buildFormSettings", function ($event) use ($magicPage) {
$page = $event->object->getPage();
if ($page->className !== $magicPage->className) return;
$form = $event->return;
$page->editFormSettings($form, $page);
});
}
// execute onSaved on every save
// this will also fire when id=0
if (method_exists($magicPage, "onSaved")) {
$this->wire->addHookAfter("Pages::saved", function ($event) use ($magicPage) {
$page = $event->arguments(0);
if ($page->className !== $magicPage->className) return;
$page->onSaved();
});
}
// execute onSaveReady on every save
// this will also fire when id=0
if (method_exists($magicPage, "onSaveReady")) {
$this->wire->addHookAfter("Pages::saveReady", function ($event) use ($magicPage) {
$page = $event->arguments(0);
if ($page->className !== $magicPage->className) return;
$page->onSaveReady();
});
}
// execute onCreate on saveReady when id=0
if (method_exists($magicPage, "onCreate")) {
$this->wire->addHookAfter("Pages::saveReady", function ($event) use ($magicPage) {
$page = $event->arguments(0);
if ($page->id) return;
if ($page->className !== $magicPage->className) return;
$page->onCreate();
});
}
// execute onAdded on saved when id=0
if (method_exists($magicPage, "onAdded")) {
$this->wire->addHookAfter("Pages::added", function ($event) use ($magicPage) {
$page = $event->arguments(0);
if ($page->className !== $magicPage->className) return;
$page->onAdded();
});
}
// execute onTrashed hook
if (method_exists($magicPage, "onTrashed")) {
$this->wire->addHookAfter("Pages::trashed", function ($event) use ($magicPage) {
$page = $event->arguments(0);
if ($page->className !== $magicPage->className) return;
$page->onTrashed();
});
}
// form processing
if (method_exists($magicPage, "onProcessInput")) {
$this->wire->addHookAfter("InputfieldForm::processInput", function ($event) use ($magicPage) {
if ($event->process != "ProcessPageEdit") return;
$page = $event->process->getPage();
if ($page->className !== $magicPage->className) return;
$page->onProcessInput($event->return, $event->arguments(0));
});
}
}
/**
* Add assets having the same filename as the magic page
*
* FooPage.php would load FooPage.css/.js on page edit screen
*/
public function addPageAssets(HookEvent $event): void
{
$page = $event->process->getPage();
$path = $this->getFilePath($page);
$rm = $this->rockmigrations();
// is the asset stored in /site/classes ?
// then move it to /site/assets because /site/classes is blocked by htaccess
if (strpos($path, $this->wire->config->paths->classes) === 0) {
$cachePath = $this->wire->config->paths->assets . "MagicPages/assets/";
foreach (['css', 'js'] as $ext) {
$file = substr($path, 0, -3) . $ext;
$name = basename($file);
$cache = $cachePath . $name;
// delete file?
if (!is_file($file) and is_file($cache)) {
$this->wire->files->unlink($cache);
}
// create file?
if ($rm->filemtime($file) > $rm->filemtime($cache)) {
$this->wire->files->mkdir($cachePath, true);
$this->wire->files->copy($file, $cache);
}
// add asset to backend
if ($ext == 'css') $this->rockmigrations()->addStyles($cache);
elseif ($ext == 'js') $this->rockmigrations()->addScripts($cache);
}
}
// now we have an assets from a pageclass inside a module (for example)
else {
foreach (['css', 'js'] as $ext) {
// add asset to backend
$file = substr($path, 0, -3) . $ext;
if ($ext == 'css') $this->rockmigrations()->addStyles($file);
elseif ($ext == 'js') $this->rockmigrations()->addScripts($file);
}
}
}
/**
* Get filepath of file for given page
*/
public function getFilePath($page): string
{
// try to get filepath from cache
$tpl = (string)$page->template;
if ($tpl and array_key_exists($tpl, $this->filePaths)) {
return $this->filePaths[$tpl];
}
// otherwise get filepath from reflectionclass
$reflector = new ReflectionClass($page);
$filePath = Paths::normalizeSeparators($reflector->getFileName());
if ($tpl) $this->filePaths[$tpl] = $filePath;
return $filePath;
}
public function rockmigrations(): RockMigrations
{
return $this->wire->modules->get('RockMigrations');
}
}