Completed
Push — master ( 416cb1...222de5 )
by Anton
03:44
created

Tables::fillMenuTable()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 22
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 22
rs 8.6738
cc 6
eloc 8
nc 6
nop 0
1
<?php
2
3
namespace Modules\Install\Utils {
4
5
	use Modules\Entitizer, Arr, DB, Language, Template;
6
7
	abstract class Tables {
8
9
		# Fill pages table
10
11
		private static function fillPagesTable() {
12
13
			# Count pages
14
15
			if (!(DB::select(TABLE_PAGES, 'COUNT(id) as count') && (DB::last()->rows === 1))) return false;
16
17
			if (intval(DB::last()->row()['count']) > 0) return true;
18
19
			# Insert initial pages
20
21
			$pages = [];
22
23
			$pages[] = ['visibility' => VISIBILITY_PUBLISHED,
24
25
				'hash' => Arr::encode(['index', 0]),
26
27
				'name' => 'index', 'title' => Language::get('INSTALL_PAGE_INDEX_TITLE'),
28
29
				'contents' => Template::block(Language::get('INSTALL_PAGE_INDEX_CONTENTS'))->contents(),
30
31
				'time_created' => REQUEST_TIME, 'time_modified' => REQUEST_TIME];
32
33
			for ($i = 1; $i <= 3; $i++) $pages[] = ['visibility' => VISIBILITY_PUBLISHED,
34
35
				'hash' => Arr::encode([('page-' . $i), 0]),
36
37
				'name' => ('page-' . $i), 'title' => (Language::get('INSTALL_PAGE_DEMO_TITLE') . ' ' . $i),
38
39
				'contents' => Template::block(Language::get('INSTALL_PAGE_DEMO_CONTENTS'))->contents(),
40
41
				'time_created' => REQUEST_TIME, 'time_modified' => REQUEST_TIME];
42
43
			# ------------------------
44
45
			return (DB::insert(TABLE_PAGES, $pages, true) && DB::last()->status);
46
		}
47
48
		# Fill menu table
49
50
		private static function fillMenuTable() {
51
52
			# Count menuitems
53
54
			if (!(DB::select(TABLE_MENU, 'COUNT(id) as count') && (DB::last()->rows === 1))) return false;
55
56
			if (intval(DB::last()->row()['count']) > 0) return true;
57
58
			# Insert initial menuitems
59
60
			$menu = [];
61
62
			for ($i = 1; $i <= 3; $i++) $menu[] = [
63
64
				'position' => ($i - 1), 'slug' => ('page-' . $i),
65
66
				'text' => (Language::get('INSTALL_PAGE_DEMO_TITLE') . ' ' . $i)];
67
68
			# ------------------------
69
70
			return (DB::insert(TABLE_MENU, $menu, true) && DB::last()->status);
71
		}
72
73
		# Create tables
74
75
		public static function create() {
76
77
			$definitions = [];
78
79
			$definitions[] = Entitizer\Definition::get(ENTITY_TYPE_PAGE);
80
81
			$definitions[] = Entitizer\Definition::get(ENTITY_TYPE_MENUITEM);
82
83
			$definitions[] = Entitizer\Definition::get(ENTITY_TYPE_VARIABLE);
84
85
			$definitions[] = Entitizer\Definition::get(ENTITY_TYPE_WIDGET);
86
87
			$definitions[] = Entitizer\Definition::get(ENTITY_TYPE_USER);
88
89
			$definitions[] = Entitizer\Definition::get(ENTITY_TYPE_USER_SECRET);
90
91
			$definitions[] = Entitizer\Definition::get(ENTITY_TYPE_USER_SESSION);
92
93
			foreach ($definitions as $definition) if (!$definition->createTable()) return false;
94
95
			# ------------------------
96
97
			return true;
98
		}
99
100
		# Fill tables
101
102
		public static function fill() {
103
104
			return (self::fillPagesTable() && self::fillMenuTable());
105
		}
106
	}
107
}
108