1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Installer;
13
14 use ICanBoogie\ActiveRecord\Connection;
15 use ICanBoogie\ActiveRecord\ConnectionNotEstablished;
16 use ICanBoogie\ActiveRecord\RecordNotFound;
17 use ICanBoogie\Errors;
18
19 use Icybee\Modules\Pages\Page;
20 use Icybee\Modules\Sites\Site;
21 use Icybee\Modules\Users\User;
22 use ICanBoogie\I18n\FormattedString;
23
24 class InstallOperation extends \ICanBoogie\Operation
25 {
26 protected function validate(\ICanBoogie\Errors $errors)
27 {
28 return true;
29 }
30
31 protected function process()
32 {
33 $this->process_database();
34 $errors = $this->process_modules();
35
36 if ($errors->count())
37 {
38 $this->response->errors = $errors;
39
40 return;
41 }
42
43 try
44 {
45 $this->process_site();
46 }
47 catch (\Exception $e)
48 {
49 throw new \Exception("Unable to create site record: " . $e->getMessage());
50 }
51
52 try
53 {
54 $this->process_user();
55 }
56 catch (\Exception $e)
57 {
58 throw new \Exception("Unable to create user record: " . $e->getMessage());
59 }
60
61 if (!$this->process_config())
62 {
63 return;
64 }
65
66 $this->response->message = new FormattedString("Icybee has been successfuly installed.");
67
68 return true;
69 }
70
71 72 73
74 protected function process_database()
75 {
76 global $core;
77
78 $options = $core->session->install['database'];
79
80 $core->connections['primary'] = array
81 (
82 'dsn' => "mysql:dbname=" . $options['name'] . ";host=" . ($options['host'] ?: 'localhost'),
83 'username' => $options['username'],
84 'password' => $options['password'],
85
86 Connection::TABLE_NAME_PREFIX => $options['prefix'],
87 Connection::TIMEZONE => '+00:00'
88 );
89
90 $core->connections['primary'];
91 $core->connections['local'];
92 }
93
94 95 96 97 98
99 protected function process_modules()
100 {
101 global $core;
102
103 $modules = $core->modules;
104 $modules->index;
105
106 $ids = array();
107 $errors = new Errors();
108 $is_installed_errors = new Errors();
109
110 foreach ($modules->descriptors as $id => $descriptor)
111 {
112 $ids[] = $id;
113 $modules->enable($id);
114 }
115
116 $core();
117
118 foreach ($modules->descriptors as $id => $descriptor)
119 {
120 $module = $modules[$id];
121
122 $is_installed_errors->clear();
123
124 if (!$module->is_installed($is_installed_errors))
125 {
126 $module->install($errors);
127 }
128 }
129
130 $core->vars['enabled_modules'] = $ids;
131
132 \Icybee\Modules\Nodes\Module::create_default_routes();
133
134 return $errors;
135 }
136
137 138 139
140 protected function process_site()
141 {
142 global $core;
143
144 $site = $core->models['sites']->one;
145
146 if (!$site)
147 {
148 $site = new Site;
149 }
150
151 $options = $core->session->install['site'];
152
153 $site->title = $options['title'];
154 $site->language = $options['language'];
155 $site->timezone = $options['timezone'];
156 $site->email = $core->session->install['user']['email'];
157 $site->status = Site::STATUS_OK;
158
159 $site->save();
160
161 if (!$core->models['pages']->one)
162 {
163 $page = new Page();
164 $page->title = "Home";
165 $page->is_online = true;
166 $page->uid = 1;
167 $page->siteid = $site->siteid;
168 $page->save();
169 }
170 }
171
172 173 174
175 protected function process_user()
176 {
177 global $core;
178
179 try
180 {
181 $user = $core->models['users'][1];
182 }
183 catch (RecordNotFound $e)
184 {
185 $user = new User;
186 }
187
188 $options = $core->session->install['user'];
189
190 $user->username = $options['username'];
191 $user->email = $options['email'];
192 $user->password = $options['password'];
193 $user->language = $options['language'];
194
195 $user->save();
196 $user->login();
197 }
198
199 protected function process_config()
200 {
201 $errors = new Errors;
202 $requirements = InstallRequirements::get();
203
204 if ($requirements($errors))
205 {
206 return;
207 }
208
209 $this->response['content'] = $requirements->render();
210 }
211 }