Completed
Push — master ( c9d315...7f8584 )
by Iurii
01:14
created

Backup::backup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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