Completed
Push — master ( 7f8584...71e049 )
by Iurii
01:03
created

Backup::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
1
<?php
2
3
/**
4
 * @package Backup
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2015, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3
8
 */
9
10
namespace gplcart\modules\backup\models;
11
12
use gplcart\core\Model,
13
    gplcart\core\Handler;
14
use gplcart\core\models\User as UserModel,
15
    gplcart\core\models\Language as LanguageModel;
16
17
/**
18
 * Manages basic behaviors and data related to Backup model
19
 */
20
class Backup extends Model
21
{
22
23
    /**
24
     * Language model instance
25
     * @var \gplcart\core\models\Language $language
26
     */
27
    protected $language;
28
29
    /**
30
     * User model instance
31
     * @var \gplcart\core\models\User $user
32
     */
33
    protected $user;
34
35
    /**
36
     * @param UserModel $user
37
     * @param LanguageModel $language
38
     */
39
    public function __construct(UserModel $user, LanguageModel $language)
40
    {
41
        parent::__construct();
42
43
        $this->user = $user;
44
        $this->language = $language;
45
    }
46
47
    /**
48
     * Returns an array of backups or counts them
49
     * @param array $data
50
     * @return array|integer
51
     */
52
    public function getList(array $data = array())
53
    {
54
        $sql = 'SELECT b.*, u.name AS user_name';
55
56
        if (!empty($data['count'])) {
57
            $sql = 'SELECT COUNT(b.backup_id)';
58
        }
59
60
        $sql .= ' FROM backup b'
61
                . ' LEFT JOIN user u ON(b.user_id = u.user_id)'
62
                . ' WHERE b.backup_id > 0';
63
64
        $where = array();
65
66
        if (isset($data['user_id'])) {
67
            $sql .= ' AND b.user_id = ?';
68
            $where[] = $data['user_id'];
69
        }
70
71
        if (isset($data['id'])) {
72
            $sql .= ' AND b.id = ?';
73
            $where[] = $data['id'];
74
        }
75
76
        if (isset($data['version'])) {
77
            $sql .= ' AND b.version = ?';
78
            $where[] = $data['version'];
79
        }
80
81
        if (isset($data['name'])) {
82
            $sql .= ' AND b.name LIKE ?';
83
            $where[] = "%{$data['name']}%";
84
        }
85
86
        $allowed_order = array('asc', 'desc');
87
        $allowed_sort = array('name', 'user_id', 'version',
88
            'id', 'backup_id', 'type', 'created');
89
90
        if (isset($data['sort']) && in_array($data['sort'], $allowed_sort)//
91
                && isset($data['order']) && in_array($data['order'], $allowed_order)
92
        ) {
93
            $sql .= " ORDER BY b.{$data['sort']} {$data['order']}";
94
        } else {
95
            $sql .= ' ORDER BY b.created DESC';
96
        }
97
98
        if (!empty($data['limit'])) {
99
            $sql .= ' LIMIT ' . implode(',', array_map('intval', $data['limit']));
100
        }
101
102
        if (!empty($data['count'])) {
103
            return (int) $this->db->fetchColumn($sql, $where);
104
        }
105
106
        $results = $this->db->fetchAll($sql, $where, array('index' => 'backup_id'));
107
        $this->hook->attach('module.backup.list', $results, $this);
108
        return $results;
109
    }
110
111
    /**
112
     * Adds a backup to the database
113
     * @param array $data
114
     * @return boolean|integer
115
     */
116
    public function add(array $data)
117
    {
118
        $result = null;
119
        $this->hook->attach('module.backup.add.before', $data, $result, $this);
120
121
        if (isset($result)) {
122
            return $result;
123
        }
124
125
        $version = null;
126
        if (isset($data['version'])) {
127
            $version = $data['version'];
128
        }
129
130
        if ($this->exists($data['id'], $version)) {
131
            return false;
132
        }
133
134
        if (empty($data['user_id'])) {
135
            $data['user_id'] = $this->user->getId();
136
        }
137
138
        $data['created'] = GC_TIME;
139
        $result = $this->db->insert('backup', $data);
140
141
        $this->hook->attach('module.backup.add.after', $data, $result, $this);
142
        return $result;
143
    }
144
145
    /**
146
     * Loads a backup from the database
147
     * @param integer $id
148
     * @return array
149
     */
150
    public function get($id)
151
    {
152
        $sql = 'SELECT * FROM backup WHERE backup_id=?';
153
        return $this->db->fetch($sql, array($id));
154
    }
155
156
    /**
157
     * Deletes a backup from disk and database
158
     * @param integer $id
159
     * @return boolean
160
     */
161
    public function delete($id)
162
    {
163
        $result = null;
164
        $this->hook->attach('module.backup.delete.before', $id, $this);
165
166
        if (isset($result)) {
167
            return $result;
168
        }
169
170
        $result = $this->deleteZip($id);
171
172
        if ($result) {
173
            $this->db->delete('backup', array('backup_id' => $id));
174
        }
175
176
        $this->hook->attach('module.backup.delete.after', $id, $result, $this);
177
        return (bool) $result;
178
    }
179
180
    /**
181
     * Deletes a backup ZIP archive
182
     * @param integer $backup_id
183
     * @return boolean
184
     */
185
    protected function deleteZip($backup_id)
186
    {
187
        $backup = $this->get($backup_id);
188
189
        if (empty($backup['path'])) {
190
            return false;
191
        }
192
193
        $file = gplcart_file_absolute_path($backup['path']);
194
        return file_exists($file) && unlink($file);
195
    }
196
197
    /**
198
     * Performs backup operation
199
     * @param string $handler_id
200
     * @param array $data
201
     * @return boolean|string
202
     */
203
    public function backup($handler_id, $data)
204
    {
205
        $handlers = $this->getHandlers();
206
        return Handler::call($handlers, $handler_id, 'backup', array($data, $this));
207
    }
208
209
    /**
210
     * Performs restore operation
211
     * @param string $handler_id
212
     * @param array $data
213
     * @return boolean|string
214
     */
215
    public function restore($handler_id, $data)
216
    {
217
        $handlers = $this->getHandlers();
218
        return Handler::call($handlers, $handler_id, 'restore', array($data, $this));
219
    }
220
221
    /**
222
     * Whether a backup already exists
223
     * @param string $id
224
     * @param null|string $version
225
     * @return bool
226
     */
227
    public function exists($id, $version = null)
228
    {
229
        $list = $this->getList(array('id' => $id, 'version' => $version));
230
        return !empty($list);
231
    }
232
233
    /**
234
     * Returns an array of backup handlers
235
     * @return array
236
     */
237
    public function getHandlers()
238
    {
239
        $handlers = &gplcart_static(__METHOD__);
240
241
        if (isset($handlers)) {
242
            return $handlers;
243
        }
244
245
        $handlers = $this->getDefaultHandlers();
246
        $this->hook->attach('module.backup.handlers', $handlers, $this);
247
        return $handlers;
248
    }
249
250
    /**
251
     * Returns a single handler
252
     * @param string $handler_id
253
     * @return array
254
     */
255
    public function getHandler($handler_id)
256
    {
257
        $handlers = $this->getHandlers();
258
        return empty($handlers[$handler_id]) ? array() : $handlers[$handler_id];
259
    }
260
261
    /**
262
     * Returns an array of default backup handlers
263
     * @return array
264
     */
265
    protected function getDefaultHandlers()
266
    {
267
        $handlers = array();
268
269
        $handlers['module'] = array(
270
            'name' => $this->language->text('Module'),
271
            'handlers' => array(
272
                'backup' => array('gplcart\\modules\\backup\\handlers\\Module', 'backup')
273
        ));
274
275
        return $handlers;
276
    }
277
278
}
279