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\I18n;
17
18 class DatabaseOperation extends Operation
19 {
20 protected function get_controls()
21 {
22 return array
23 (
24 self::CONTROL_FORM => true
25 )
26
27 + parent::get_controls();
28 }
29
30 protected function get_form()
31 {
32 return new DatabaseForm;
33 }
34
35 protected function validate(\ICanBoogie\Errors $errors)
36 {
37 $request = $this->request;
38
39 $name = $request['name'];
40 $username = $request['username'];
41 $password = $request['password'];
42 $host = $request['host'];
43 $prefix = $request['prefix'];
44
45 try
46 {
47 $connection = new Connection
48 (
49 "mysql:dbname=$name;host=$host", $username, $password, array
50 (
51 Connection::TABLE_NAME_PREFIX => $prefix
52 )
53 );
54 }
55 catch (\PDOException $e)
56 {
57 $code = $e->getCode();
58
59 if ($code == 1049)
60 {
61 $errors['name'] = I18n\t('panel.database.error.name');
62 }
63 else if ($code == 1045)
64 {
65 $errors['username'] = I18n\t('panel.database.error.username_password');
66 $errors['password'] = true;
67 }
68 else if ($code == 2005)
69 {
70 $errors['host'] = I18n\t('panel.database.error.host');
71 }
72 else
73 {
74 throw $e;
75 }
76 }
77
78 return $errors;
79 }
80
81 protected function process()
82 {
83 global $core;
84
85 $request = $this->request;
86
87 $core->session->install['database'] = array
88 (
89 'name' => $request['name'],
90 'username' => $request['username'],
91 'password' => $request['password'],
92 'host' => $request['host'],
93 'prefix' => $request['prefix']
94 );
95
96 $this->response->message = I18n\t('panel.database.success');
97
98 return parent::process();
99 }
100 }