Completed
Branch 0.3.0 (b16461)
by Anton
04:03
created

Tables::fillMenuTable()   C

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 10

Duplication

Lines 1
Ratio 3.57 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 1
loc 28
rs 5.6721
cc 6
eloc 10
nc 6
nop 0
1
<?php
2
3
namespace Modules\Install\Utils {
4
5
	use Modules\Entitizer, Modules\Informer, DB, Language, Template;
6
7
	abstract class Tables {
8
9
		# Fill specific relations table
10
11
		private static function fillRelationsTable(string $table, int $max_id) {
12
13
			$relations = [];
14
15
			for ($id = 1; $id <= $max_id; $id++) {
16
17
				$relations[] = ['ancestor' => $id, 'descendant' => $id, 'depth' => 0];
18
			}
19
20
			# ------------------------
21
22
			return (DB::insert($table, $relations, true, true) && DB::last()->status);
23
		}
24
25
		# Fill pages table
26
27
		private static function fillPagesTable() {
28
29
			# Count pages
30
31
			$count = Informer::countEntries(TABLE_PAGES, true);
32
33 View Code Duplication
			if (false === $count) return false; else if ($count > 0) return true;
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
34
35
			# Process dataset
36
37
			$pages = [['id' => 1, 'visibility' => VISIBILITY_PUBLISHED,
38
39
				'locked' => false, 'slug' => 'index', 'name' => 'index',
40
41
				'title' => Language::get('INSTALL_PAGE_INDEX_TITLE'),
42
43
				'contents' => Template::block(Language::get('INSTALL_PAGE_INDEX_CONTENTS'))->contents(),
44
45
				'time_created' => REQUEST_TIME, 'time_modified' => REQUEST_TIME]];
46
47
			for ($id = 2; $id <= 4; $id++) $pages[] = ['id' => $id, 'visibility' => VISIBILITY_PUBLISHED,
48
49
				'locked' => false, 'slug' => ('page-' . ($id - 1)), 'name' => ('page-' . ($id - 1)),
50
51
				'title' => (Language::get('INSTALL_PAGE_DEMO_TITLE') . ' ' . ($id - 1)),
52
53
				'contents' => Template::block(Language::get('INSTALL_PAGE_DEMO_CONTENTS'))->contents(),
54
55
				'time_created' => REQUEST_TIME, 'time_modified' => REQUEST_TIME];
56
57
			# Process insertion
58
59
			if (!(DB::insert(TABLE_PAGES, $pages, true) && DB::last()->status)) return false;
60
61
			self::fillRelationsTable(TABLE_PAGES_RELATIONS, 4);
62
63
			# ------------------------
64
65
			return true;
66
		}
67
68
		# Fill menu table
69
70
		private static function fillMenuTable() {
71
72
			# Count menuitems
73
74
			$count = Informer::countEntries(TABLE_MENU, true);
75
76 View Code Duplication
			if (false === $count) return false; else if ($count > 0) return true;
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
77
78
			# Process dataset
79
80
			$menu = [];
81
82
			for ($id = 1; $id <= 3; $id++) $menu[] = [
83
84
				'id' => $id, 'active' => true, 'position' => ($id - 1), 'slug' => ('page-' . $id),
85
86
				'text' => (Language::get('INSTALL_PAGE_DEMO_TITLE') . ' ' . $id)];
87
88
			# Process insertion
89
90
			if (!(DB::insert(TABLE_MENU, $menu, true) && DB::last()->status)) return false;
91
92
			self::fillRelationsTable(TABLE_MENU_RELATIONS, 3);
93
94
			# ------------------------
95
96
			return true;
97
		}
98
99
		# Create tables
100
101
		public static function create() {
102
103
			$definitions = [];
104
105
			$definitions[] = Entitizer::definition(TABLE_PAGES);
106
107
			$definitions[] = Entitizer::definition(TABLE_MENU);
108
109
			$definitions[] = Entitizer::definition(TABLE_VARIABLES);
110
111
			$definitions[] = Entitizer::definition(TABLE_WIDGETS);
112
113
			$definitions[] = Entitizer::definition(TABLE_USERS);
114
115
			$definitions[] = Entitizer::definition(TABLE_USERS_SECRETS);
116
117
			$definitions[] = Entitizer::definition(TABLE_USERS_SESSIONS);
118
119
			foreach ($definitions as $definition) if (!$definition->createTable()) return false;
120
121
			# ------------------------
122
123
			return true;
124
		}
125
126
		# Fill tables
127
128
		public static function fill() {
129
130
			return (self::fillPagesTable() && self::fillMenuTable());
131
		}
132
	}
133
}
134