Completed
Push — master ( 65690a...66595b )
by Iurii
03:23 queued 57s
created

Module::hookModuleUninstallAfter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package Backup
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2017, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\backup;
11
12
use gplcart\core\Config,
13
    gplcart\core\Container;
14
15
/**
16
 * Main class for Backup module
17
 */
18
class Module
19
{
20
21
    /**
22
     * Database class instance
23
     * @var \gplcart\core\Database $db
24
     */
25
    protected $db;
26
27
    /**
28
     * @param Config $config
29
     */
30
    public function __construct(Config $config)
31
    {
32
        $this->db = $config->getDb();
33
        $this->db->addScheme($this->getDbScheme());
34
    }
35
36
    /**
37
     * Implements hook "module.install.before"
38
     * @param null|string $result
39
     */
40
    public function hookModuleInstallBefore(&$result)
41
    {
42
        if (!class_exists('ZipArchive')) {
43
            $result = $this->getTranslationModel()->text('Class ZipArchive does not exist');
44
        } else {
45
46
            $result_db = $this->db->importScheme('backup', $this->getDbScheme());
47
48
            if ($result_db !== true) {
49
                $result = $result_db;
50
            }
51
        }
52
    }
53
54
    /**
55
     * Implements hook "module.uninstall.after"
56
     */
57
    public function hookModuleUninstallAfter()
58
    {
59
        $this->db->deleteTable('backup');
60
    }
61
62
    /**
63
     * Implements hook "user.role.permissions"
64
     * @param array $permissions
65
     */
66
    public function hookUserRolePermissions(array &$permissions)
67
    {
68
        $permissions['backup'] = /* @text */'Backup: access';
69
        $permissions['backup_delete'] = /* @text */'Backup: delete';
70
        $permissions['backup_download'] = /* @text */'Backup: download';
71
    }
72
73
    /**
74
     * Implements hook "route.list"
75
     * @param array $routes
76
     */
77
    public function hookRouteList(array &$routes)
78
    {
79
        $routes['admin/report/backup'] = array(
80
            'access' => 'backup',
81
            'menu' => array('admin' => /* @text */'Backups'),
82
            'handlers' => array(
83
                'controller' => array('gplcart\\modules\\backup\\controllers\\Backup', 'listBackup')
84
            )
85
        );
86
    }
87
88
    /**
89
     * Performs backup operation
90
     * @param string $handler_id
91
     * @param array $data
92
     * @return boolean|string
93
     */
94
    public function backup($handler_id, array $data)
95
    {
96
        return $this->getModel()->backup($handler_id, $data);
97
    }
98
99
    /**
100
     * Performs restore operation
101
     * @param string $handler_id
102
     * @param array $data
103
     * @return boolean|string
104
     */
105
    public function restore($handler_id, array $data)
106
    {
107
        return $this->getModel()->restore($handler_id, $data);
108
    }
109
110
    /**
111
     * Returns an array of defined handlers
112
     * @return array
113
     */
114
    public function getHandlers()
115
    {
116
        return $this->getModel()->getHandlers();
117
    }
118
119
    /**
120
     * Whether a backup already exists
121
     * @param string $id
122
     * @param null|string $version
123
     * @return bool
124
     */
125
    public function exists($id, $version = null)
126
    {
127
        return $this->getModel()->exists($id, $version);
128
    }
129
130
    /**
131
     * Returns backup model
132
     * @return \gplcart\modules\backup\models\Backup
133
     */
134
    protected function getModel()
135
    {
136
        return Container::get('gplcart\\modules\\backup\\models\\Backup');
137
    }
138
139
    /**
140
     * Translation UI model instance
141
     * @return \gplcart\core\models\Translation
142
     */
143
    protected function getTranslationModel()
144
    {
145
        return Container::get('gplcart\\core\\models\\Translation');
146
    }
147
148
    /**
149
     * Returns an array of database scheme
150
     * @return array
151
     */
152
    protected function getDbScheme()
153
    {
154
        return array(
155
            'backup' => array(
156
                'fields' => array(
157
                    'backup_id' => array('type' => 'int', 'length' => 10, 'auto_increment' => true, 'primary' => true),
158
                    'created' => array('type' => 'int', 'length' => 10, 'not_null' => true),
159
                    'name' => array('type' => 'varchar', 'length' => 255, 'not_null' => true),
160
                    'path' => array('type' => 'varchar', 'length' => 255, 'not_null' => true),
161
                    'user_id' => array('type' => 'int', 'length' => 10, 'not_null' => true, 'default' => 0),
162
                    'type' => array('type' => 'varchar', 'length' => 50, 'not_null' => true, 'default' => ''),
163
                    'version' => array('type' => 'varchar', 'length' => 50, 'not_null' => true, 'default' => ''),
164
                    'id' => array('type' => 'varchar', 'length' => 50, 'not_null' => true, 'default' => '')
165
                )
166
            )
167
        );
168
    }
169
170
}
171