· 7 years ago · Sep 21, 2018, 11:36 AM
1<?php
2
3namespace Invertus\Training\Install;
4
5use training;
6
7class Installer
8{
9 public $module;
10 public $configuration;
11
12 public function __construct($module, $configuration)
13 {
14 $this->module = $module;
15 $this->configuration = $configuration;
16 }
17
18 public function install()
19 {
20 if (!$this->installHooks()) {
21 return false;
22 }
23
24 if (!$this->installDatabase()) {
25 return false;
26 }
27
28 return true;
29 }
30
31 public function installHooks()
32 {
33 foreach ($this->configuration['hooks'] as $hook) {
34 if (!$this->module->registerHook($hook)) {
35 return false;
36 }
37 }
38
39 return true;
40 }
41
42 public function installDatabase()
43 {
44 $db = Db::getInstance();
45
46 if (!$db->execute('
47 CREATE TABLE IF NOT EXISTS '._DB_PREFIX_.'article (
48 `id_article` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
49 `type` VARCHAR(255) NOT NULL,
50 PRIMARY KEY (id_article)
51 ) ENGINE '._MYSQL_ENGINE_.' DEFAULT CHARSET = UTF8
52 ')) {
53 return false;
54 }
55
56 if (!$db->execute('
57 CREATE TABLE IF NOT EXISTS '._DB_PREFIX_.'article_lang (
58 `id_article` INT(10) NOT NULL,
59 `id_lang` INT(10) NOT NULL,
60 `id_shop` INT(10) NOT NULL,
61 `title` VARCHAR(255) NOT NULL,
62 PRIMARY KEY (id_article, id_lang, id_shop)
63 ) ENGINE '._MYSQL_ENGINE_.' DEFAULT CHARSET = UTF8
64 ')) {
65 return false;
66 }
67
68 if (!$db->execute('
69 CREATE TABLE IF NOT EXISTS '._DB_PREFIX_.'article_shop (
70 `id_article` INT(10) NOT NULL,
71 `id_shop` INT(10) NOT NULL,
72 `type` VARCHAR(255) NOT NULL,
73 PRIMARY KEY (id_article, id_shop)
74 ) ENGINE '._MYSQL_ENGINE_.' DEFAULT CHARSET = UTF8
75 ')) {
76 return false;
77 }
78
79 return true;
80 }
81
82 public function uninstall()
83 {
84 if (!$this->deleteTables()) {
85 return false;
86 }
87
88 return true;
89 }
90
91 public function deleteTables()
92 {
93 $db = Db::getInstance();
94
95 if (!$db->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'article')) {
96 return false;
97 }
98
99 if (!$db->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'article_lang')) {
100 return false;
101 }
102
103 if (!$db->execute('DROP TABLE IF EXISTS '._DB_PREFIX_.'article_shop')) {
104 return false;
105 }
106
107 return true;
108 }
109}