-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathWeeklyPlan.php
More file actions
141 lines (111 loc) · 4.77 KB
/
WeeklyPlan.php
File metadata and controls
141 lines (111 loc) · 4.77 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
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
<?php
namespace Grav\Plugin;
use Grav\Common\Cache;
use Grav\Plugin\Utils;
use Grav\Common\Grav;
require_once(__DIR__ . "/Utils.php");
class WeeklyPlan extends \Grav\Common\Twig\TwigExtension
{
public function getName()
{
return 'WeeklyPlan';
}
public function getFunctions() : array
{
return [
new \Twig_SimpleFunction('phpSavePlan2', [$this, 'SavePlan2']),
new \Twig_SimpleFunction('phpLoadPlanFromTemplate', [$this, 'LoadPlanFromTemplate']),
new \Twig_SimpleFunction('phpSavePlan2Template', [$this, 'SavePlan2Template']),
new \Twig_SimpleFunction('phpCreatePlanTemplate', [$this, 'CreatePlanTemplate']),
new \Twig_SimpleFunction('phpDeletePlanTemplate', [$this, 'DeletePlanTemplate']),
new \Twig_SimpleFunction('phpSetDefaultTemplate', [$this, 'SetDefaultTemplate']),
];
}
const PLAN_URL = "/auth/plan2/";
const PLAN_TEMPLATES_URL = "/auth/plan2/templates";
static function SavePlan2Template(){
if ($_SERVER["REQUEST_METHOD"] !== "POST") return;
if (empty($_POST["template"])) return;
$page = Grav::instance()['page']->find(self::PLAN_TEMPLATES_URL);
$frontmatter = (array)$page->header();
$frontmatter["defaultTemplate"] = $_POST["defaultTemplate"];
$frontmatter["templates"] = $_POST["template"];
$page->header($frontmatter);
$page->save();
Utils::log("PlanTemplates | edited ");
Cache::clearCache('cache-only');
}
static function edit_events_groups($events) {
foreach($events as $id => $groups) {
$year = substr($id, 0, 4);
$page_url = "/data/events/{$year}/{$id}";
$page = Grav::instance()['page']->find($page_url);
$frontmatter = (array)$page->header();
$frontmatter["taxonomy"]["skupina"] = $groups;
$page->header($frontmatter);
$page->save();
}
}
public static function SavePlan2(){
if ($_SERVER["REQUEST_METHOD"] !== "POST") return;
if (empty($_POST["plan"])) return;
$page = Grav::instance()['page']->find(self::PLAN_URL);
$frontmatter = (array)$page->header();
$frontmatter["plan"] = $_POST["plan"];
if (!empty($_POST["events"])) {
self::edit_events_groups($_POST["events"]);
}
$page->header($frontmatter);
$page->save();
Utils::log("Plan | edited");
Cache::clearCache('cache-only');
}
public static function LoadPlanFromTemplate(){
if ($_SERVER["REQUEST_METHOD"] !== "POST") return;
if (empty($_POST["week"]) || empty($_POST["template"])) return;
$page = Grav::instance()['page']->find(self::PLAN_URL);
$plan_frontmatter = (array)$page->header();
$template_frontmatter = (array)$page->find(self::PLAN_TEMPLATES_URL)->header();
$week = $_POST["week"];
$template = $_POST["template"];
$plan_frontmatter["plan"][$week] = $template_frontmatter["templates"][$template];
$page->header($plan_frontmatter);
$page->save();
Utils::log("Plan | loaded from template | {$_POST["week"]} => {$_POST["template"]}");
Cache::clearCache('cache-only');
}
public static function DeletePlanTemplate() {
if ($_SERVER["REQUEST_METHOD"] !== "POST") return;
if (empty($_POST["deletedTemplate"])) return;
$del_template = $_POST["deletedTemplate"];
$page = Grav::instance()['page']->find(self::PLAN_TEMPLATES_URL);
$frontmatter = (array)$page->header();
unset($frontmatter["templates"][$del_template]);
$page->header($frontmatter);
$page->save();
Utils::log("PlanTemplate | deleted | {$del_template}");
Cache::clearCache('cache-only');
}
public static function CreatePlanTemplate() {
if ($_SERVER["REQUEST_METHOD"] !== "POST") return;
if (empty($_POST["templateName"])) return;
$template_name = Utils::trim_all($_POST["templateName"]);
if ($template_name === "plan") return;
$page = Grav::instance()['page']->find(self::PLAN_TEMPLATES_URL);
$frontmatter = (array)$page->header();
$counter = 0;
$new_template_name = $template_name;
while (array_key_exists($new_template_name, $frontmatter["templates"])) {
$counter++;
$new_template_name = $template_name . "(". $counter .")";
}
$frontmatter["templates"][$new_template_name] = array();
$page->header($frontmatter);
$page->save();
Utils::log("PlanTemplate | created | {$template_name}");
Cache::clearCache('cache-only');
header('Content-type:application/json;charset=utf-8');
echo json_encode($new_template_name);
}
}
?>