1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Cadmium\System\Modules\Install |
5
|
|
|
* @author Anton Romanov |
6
|
|
|
* @copyright Copyright (c) 2015-2017, Anton Romanov |
7
|
|
|
* @link http://cadmium-cms.com |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Modules\Install\Utils { |
11
|
|
|
|
12
|
|
|
use Modules\Entitizer, DB, Language, Template; |
13
|
|
|
|
14
|
|
|
abstract class Tables { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Get the list of entities definitions |
18
|
|
|
* |
19
|
|
|
* @param $reverse tells to return the definitions in a reverse order (necessary for removing database tables) |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
private static function getDefinitions(bool $reverse) : array { |
23
|
|
|
|
24
|
|
|
$definitions = []; |
25
|
|
|
|
26
|
|
|
$definitions[] = Entitizer::getDefinition(TABLE_PAGES); |
27
|
|
|
|
28
|
|
|
$definitions[] = Entitizer::getDefinition(TABLE_MENU); |
29
|
|
|
|
30
|
|
|
$definitions[] = Entitizer::getDefinition(TABLE_VARIABLES); |
31
|
|
|
|
32
|
|
|
$definitions[] = Entitizer::getDefinition(TABLE_WIDGETS); |
33
|
|
|
|
34
|
|
|
$definitions[] = Entitizer::getDefinition(TABLE_USERS); |
35
|
|
|
|
36
|
|
|
$definitions[] = Entitizer::getDefinition(TABLE_USERS_SECRETS); |
37
|
|
|
|
38
|
|
|
$definitions[] = Entitizer::getDefinition(TABLE_USERS_SESSIONS); |
39
|
|
|
|
40
|
|
|
# ------------------------ |
41
|
|
|
|
42
|
|
|
return ($reverse ? array_reverse($definitions) : $definitions); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Fill in a relations table |
47
|
|
|
* |
48
|
|
|
* @param $table a basic table |
49
|
|
|
* @param $max_id a number of entries to insert |
50
|
|
|
* |
51
|
|
|
* @return bool : true on success or false on failure |
52
|
|
|
*/ |
53
|
|
|
|
54
|
|
|
private static function fillRelationsTable(string $table, int $max_id) : bool { |
55
|
|
|
|
56
|
|
|
$relations = []; |
57
|
|
|
|
58
|
|
|
for ($id = 1; $id <= $max_id; $id++) { |
59
|
|
|
|
60
|
|
|
$relations[] = ['ancestor' => $id, 'descendant' => $id, 'depth' => 0]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
# ------------------------ |
64
|
|
|
|
65
|
|
|
return (DB::insert($table, $relations, true, true) && DB::getLast()->status); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Fill in the pages table with the default values |
70
|
|
|
* |
71
|
|
|
* @return bool : true on success or false on failure |
72
|
|
|
*/ |
73
|
|
|
|
74
|
|
|
private static function fillPagesTable() : bool { |
75
|
|
|
|
76
|
|
|
# Count pages |
77
|
|
|
|
78
|
|
|
$count = DB::count(TABLE_PAGES); |
79
|
|
|
|
80
|
|
View Code Duplication |
if (false === $count) return false; else if ($count > 0) return true; |
|
|
|
|
81
|
|
|
|
82
|
|
|
# Process dataset |
83
|
|
|
|
84
|
|
|
$pages = [['id' => 1, 'visibility' => VISIBILITY_PUBLISHED, |
85
|
|
|
|
86
|
|
|
'locked' => false, 'slug' => 'index', 'name' => 'index', |
87
|
|
|
|
88
|
|
|
'title' => Language::get('INSTALL_PAGE_INDEX_TITLE'), |
89
|
|
|
|
90
|
|
|
'contents' => Template::createBlock(Language::get('INSTALL_PAGE_INDEX_CONTENTS'))->getContents(), |
91
|
|
|
|
92
|
|
|
'time_created' => REQUEST_TIME, 'time_modified' => REQUEST_TIME]]; |
93
|
|
|
|
94
|
|
|
for ($id = 2; $id <= 4; $id++) $pages[] = ['id' => $id, 'visibility' => VISIBILITY_PUBLISHED, |
95
|
|
|
|
96
|
|
|
'locked' => false, 'slug' => ('page-' . ($id - 1)), 'name' => ('page-' . ($id - 1)), |
97
|
|
|
|
98
|
|
|
'title' => (Language::get('INSTALL_PAGE_DEMO_TITLE') . ' ' . ($id - 1)), |
99
|
|
|
|
100
|
|
|
'contents' => Template::createBlock(Language::get('INSTALL_PAGE_DEMO_CONTENTS'))->getContents(), |
101
|
|
|
|
102
|
|
|
'time_created' => REQUEST_TIME, 'time_modified' => REQUEST_TIME]; |
103
|
|
|
|
104
|
|
|
# Process insertion |
105
|
|
|
|
106
|
|
View Code Duplication |
if (!(DB::insert(TABLE_PAGES, $pages, true) && DB::getLast()->status)) return false; |
|
|
|
|
107
|
|
|
|
108
|
|
|
self::fillRelationsTable(TABLE_PAGES_RELATIONS, count($pages)); |
109
|
|
|
|
110
|
|
|
# ------------------------ |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Fill in the menu table with the default values |
117
|
|
|
* |
118
|
|
|
* @return bool : true on success or false on failure |
119
|
|
|
*/ |
120
|
|
|
|
121
|
|
|
private static function fillMenuTable() : bool { |
122
|
|
|
|
123
|
|
|
# Count menuitems |
124
|
|
|
|
125
|
|
|
$count = DB::count(TABLE_MENU); |
126
|
|
|
|
127
|
|
View Code Duplication |
if (false === $count) return false; else if ($count > 0) return true; |
|
|
|
|
128
|
|
|
|
129
|
|
|
# Process dataset |
130
|
|
|
|
131
|
|
|
$menu = []; |
132
|
|
|
|
133
|
|
|
for ($id = 1; $id <= 3; $id++) $menu[] = [ |
134
|
|
|
|
135
|
|
|
'id' => $id, 'active' => true, 'position' => ($id - 1), 'slug' => ('page-' . $id), |
136
|
|
|
|
137
|
|
|
'text' => (Language::get('INSTALL_PAGE_DEMO_TITLE') . ' ' . $id)]; |
138
|
|
|
|
139
|
|
|
# Process insertion |
140
|
|
|
|
141
|
|
View Code Duplication |
if (!(DB::insert(TABLE_MENU, $menu, true) && DB::getLast()->status)) return false; |
|
|
|
|
142
|
|
|
|
143
|
|
|
self::fillRelationsTable(TABLE_MENU_RELATIONS, count($menu)); |
144
|
|
|
|
145
|
|
|
# ------------------------ |
146
|
|
|
|
147
|
|
|
return true; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Create the tables |
152
|
|
|
* |
153
|
|
|
* @return bool : true if all of the tables were successfully created, otherwise false |
154
|
|
|
*/ |
155
|
|
|
|
156
|
|
|
public static function create() : bool { |
157
|
|
|
|
158
|
|
|
$definitions = self::getDefinitions(false); |
159
|
|
|
|
160
|
|
|
foreach ($definitions as $definition) if (!$definition->createTables()) return false; |
161
|
|
|
|
162
|
|
|
# ------------------------ |
163
|
|
|
|
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Remove the tables |
169
|
|
|
* |
170
|
|
|
* @return bool : true if all of the tables were successfully removed, otherwise false |
171
|
|
|
*/ |
172
|
|
|
|
173
|
|
|
public static function remove() : bool { |
174
|
|
|
|
175
|
|
|
$definitions = self::getDefinitions(true); |
176
|
|
|
|
177
|
|
|
foreach ($definitions as $definition) if (!$definition->removeTables()) return false; |
178
|
|
|
|
179
|
|
|
# ------------------------ |
180
|
|
|
|
181
|
|
|
return true; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Fill in the tables with the default values |
186
|
|
|
* |
187
|
|
|
* @return bool : true if all of the tables were successfully filled in, otherwise false |
188
|
|
|
*/ |
189
|
|
|
|
190
|
|
|
public static function fill() : bool { |
191
|
|
|
|
192
|
|
|
return (self::fillPagesTable() && self::fillMenuTable()); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.