-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliveform.php
More file actions
110 lines (89 loc) · 2.89 KB
/
liveform.php
File metadata and controls
110 lines (89 loc) · 2.89 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace Grav\Plugin;
use \Grav\Common\Plugin;
use \Grav\Common\Grav;
use \Symfony\Component\Yaml\Yaml;
use RocketTheme\Toolbox\Event\Event;
class LiveFormPlugin extends Plugin
{
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onThemeInitialized' => ['onThemeInitialized', 0],
'onGetPageTemplates' => ['onGetPageTemplates', 0],
];
}
/**
* Initialize configuration
*/
public function onThemeInitialized()
{
if ($this->isAdmin()) {
return;
}
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
]);
}
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = 'plugins://liveform/templates';
}
/**
* Load our custom JS & CSS files on Page Init.
*/
public function onTwigSiteVariables()
{
if ( !(pathinfo($this->grav['page']->name(), PATHINFO_FILENAME) == 'liveform') ) {
return;
}
# Get the Assets object
$assets = $this->grav['assets'];
# Initialize our bits variable
$liveform_bits = [];
foreach ( ['css','js'] as $k => $v) {
# Scan the parent directories for this plugin, and build our collection of JS Files
$assetsDir = new \FilesystemIterator('plugins://liveform/'.$v);
foreach ($assetsDir as $asset) {
if ($asset->isFile()) {
$liveform_bits[] = $asset->getPathname();
}
}
if ($v == 'js') {
# Build our custom inline JS string from the imported live.yaml file
# Dependent on the imports plugin
$rules = $this->grav['page']->header()->imports['live'];
$inlineJS = 'var formRules = {';
foreach ($rules as $formname => $ruleset) {
$ruleset = $ruleset['actions'];
$inlineJS .= '"' . $formname . '": ' . json_encode($ruleset);
if (!($formname === end($rules))) {
$inlineJS .= ",";
}
}
$inlineJS .= "};";
# add our inline JS object
$assets->addInlineJs($inlineJS,110);
}
}
# Define and Add our Collection
$assets->registerCollection('liveform', $liveform_bits);
$assets->add('liveform', 100);
}
/**
* Add page template types.
*/
public function onGetPageTemplates(Event $event)
{
/** @var Types $types */
$types = $event->types;
$types->scanTemplates('plugins://liveform/templates');
}
}