-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatterns.php
More file actions
130 lines (110 loc) · 4.38 KB
/
Patterns.php
File metadata and controls
130 lines (110 loc) · 4.38 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
<?php
namespace MerapiPanel\Module\Website {
use Exception;
use MerapiPanel\Box;
use MerapiPanel\Box\Module\__Fragment;
use MerapiPanel\Box\Module\Entity\Module;
use MerapiPanel\Database\DB;
use MerapiPanel\Views\View;
use PDO;
use Symfony\Component\Filesystem\Path;
class Patterns extends __Fragment
{
protected $module;
function onCreate(Module $module)
{
$this->module = $module;
}
private function getProvidePatterns()
{
$result = [];
foreach ($this->module->getLoader()->getListModule() as $module => $moduledir) {
foreach (glob(Path::join($moduledir, "data", "patterns", "*.json")) as $file) {
$raw = file_get_contents($file);
$pattern = json_decode($raw, 1);
if (!is_array($pattern)) continue;
$name = $module . "/" . basename($file, ".json");
if (!isset($pattern['components'])) continue;
$result[] = [
"label" => "Unamed",
"media" => "Pattern",
...$pattern,
"name" => $name,
"removable" => false
];
}
}
return $result;
}
function listpops()
{
$data = $this->getProvidePatterns();
// Fetch patterns from the database
$SQL = "SELECT * FROM patterns";
$stmt = DB::instance()->query($SQL);
$dbPatterns = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Merge database patterns with the existing $data
foreach ($dbPatterns as $pattern) {
$pattern['components'] = !is_array($pattern['components']) ? json_decode($pattern['components'], true) : [];
$findKey = array_search($pattern['name'], array_column($data, "name"));
if ($findKey !== false) {
// Merge existing data with database data
$data[$findKey] = [
...$data[$findKey],
...$pattern
];
} else {
// Add new database pattern to the $data array
$data[] = [
...$pattern,
"removable" => true
];
}
}
$data = array_map(function ($pattern) {
if (isset($pattern['media'])) return $pattern;
$pattern['html'] = View::minimizeHTML(Box::module("Editor")->Blocks->render($pattern['components']));
return $pattern;
}, $data);
return $data;
}
function getpop($name)
{
$SQL = "SELECT * FROM patterns WHERE name =?";
$stmt = DB::instance()->prepare($SQL);
$stmt->execute([$name]);
if ($pattern = $stmt->fetch(PDO::FETCH_ASSOC)) {
$pattern['components'] = json_decode($pattern['components'] ?? '[]', 1);
return $pattern;
}
[$moduleName, $patternName] = explode("/", $name);
$module = Box::module($moduleName)->data->patterns->get("$patternName.json");
return $module->getContent();
}
function removepop($name)
{
if (empty($name)) throw new Exception("Could't delete pattern without name", 401);
$SQL = "DELETE FROM patterns WHERE name =?";
$stmt = DB::instance()->prepare($SQL);
return $stmt->execute([$name]);
}
function addpop($name, $label, $components = [])
{
if (empty($components)) $components = "[]";
$components = !is_string($components) ? json_encode($components) : $components;
$SQL = "REPLACE INTO patterns (name, label, components) VALUES (:name, :label, :components)";
$stmt = DB::instance()->prepare($SQL);
$stmt->execute([
"name" => $name,
"label" => $label,
"components" => $components
]);
return [
"name" => $name,
"media" => null,
"label" => $label,
"components" => json_decode($components, 1)
];
}
}
}