-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMapTheory.php
More file actions
94 lines (75 loc) · 2.88 KB
/
MapTheory.php
File metadata and controls
94 lines (75 loc) · 2.88 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
<?php
namespace Grav\Plugin;
use Grav\Common\Cache;
use Grav\Plugin\Utils;
use Grav\Common\Grav;
require_once(__DIR__ . "/Utils.php");
class MapTheory extends \Grav\Common\Twig\TwigExtension
{
public function getName()
{
return 'MapTheory';
}
public function getFunctions() : array
{
return [
new \Twig_SimpleFunction('phpSaveMapT', [$this, 'SaveMapT']),
new \Twig_SimpleFunction('phpDeleteMapT', [$this, 'DeleteMapT']),
];
}
const MAPTHEORY_DATA_URL = "/data/maptheory";
public static function SaveMapT(){
if ($_SERVER["REQUEST_METHOD"] !== "POST") return;
if (empty($_POST['group']) || empty($_POST['date'])) return;
if(empty($_FILES["PDF"]["tmp_name"])){
Utils::return_ERROR("Nahrání PDF souboru se nezdařilo.");
}
// init vars
$mapt_group = $_POST['group'];
$mapt_file_name = $_POST['date'] . ".pdf" ;
$page = Grav::instance()['page']->find(self::MAPTHEORY_DATA_URL);
$mapt_save_path = $page->path() .'/'. $mapt_group;
//get frontmatter
$frontmatter = (array)$page->header();
// add maptheory to frontmatter
if(isset($frontmatter['maptheory'][$mapt_group]) && in_array ($mapt_file_name , $frontmatter['maptheory'][$mapt_group]) ){
Utils::return_ERROR("Už je nahrana mapova teorie se stejnym datem a skupinou");
}
else{
$frontmatter['maptheory'][$mapt_group][] = $mapt_file_name;
arsort($frontmatter['maptheory'][$mapt_group]);
}
// save pdf
Utils::save_PDF($mapt_save_path, $mapt_file_name, $makeThumbnail=False);
// save page
$page->header($frontmatter);
$page->save();
Utils::log("MapTheory | saved | {$mapt_file_name}");
Cache::clearCache('cache-only');
}
public static function DeleteMapT(){
if ($_SERVER["REQUEST_METHOD"] !== "POST") return;
if (empty($_POST['group']) || empty($_POST['name'])) return;
// init vars
$mapt_name = $_POST['name'];
$mapt_group = $_POST['group'];
$page = Grav::instance()['page']->find(self::MAPTHEORY_DATA_URL);
$mapt_file_path = $page->path() .'/'. $mapt_group . '/' . $mapt_name;
// get frontmatter
$frontmatter = (array)$page->header();
// remove maptheory from frontmatter
if (($key = array_search($mapt_name, $frontmatter['maptheory'][$mapt_group])) !== false) {
unset($frontmatter['maptheory'][$mapt_group][$key]);
}
// delete pdf
if(file_exists($mapt_file_path)){
unlink($mapt_file_path);
}
// save page
$page->header($frontmatter);
$page->save();
Utils::log("MapTheory | deleted | {$mapt_name}");
Cache::clearCache('cache-only');
}
}
?>