Completed
Push — master ( eba45d...dc6b5b )
by Iurii
01:05
created

Backup::setBreadcrumbListBackup()   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 0
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\controllers;
11
12
use gplcart\modules\backup\models\Backup as ModuleBackupModel;
13
use gplcart\core\controllers\backend\Controller as BackendController;
14
15
/**
16
 * Handles incoming requests and outputs data related to Backup module
17
 */
18
class Backup extends BackendController
19
{
20
21
    /**
22
     * @var \gplcart\modules\backup\models\Backup $backup
23
     */
24
    protected $backup;
25
26
    /**
27
     * Pager limit
28
     * @var array
29
     */
30
    protected $data_limit;
31
32
    /**
33
     * @param ModuleBackupModel $backup
34
     */
35
    public function __construct(ModuleBackupModel $backup)
36
    {
37
        parent::__construct();
38
39
        $this->backup = $backup;
40
    }
41
42
    /**
43
     * Displays the backup overview page
44
     */
45
    public function listBackup()
46
    {
47
        $this->downloadListBackup();
48
        $this->actionListBackup();
49
50
        $this->setTitleListBackup();
51
        $this->setBreadcrumbListBackup();
52
53
        $this->setFilterListBackup();
54
        $this->setPagerListBackup();
55
56
        $this->setData('backups', $this->getListBackup());
57
        $this->setData('handlers', $this->backup->getHandlers());
58
59
        $this->outputListBackup();
60
    }
61
62
    /**
63
     * Sets filter parameters
64
     */
65
    protected function setFilterListBackup()
66
    {
67
        $allowed = array('created', 'name', 'user_id', 'type',
68
            'version', 'id', 'backup_id');
69
70
        $this->setFilter($allowed);
71
    }
72
73
    /**
74
     * Downloads a backup
75
     */
76
    protected function downloadListBackup()
77
    {
78
        $backup_id = $this->getQuery('download');
79
80
        if (empty($backup_id)) {
81
            return null;
82
        }
83
84
        $this->controlAccess('backup_download');
85
86
        $backup = $this->backup->get($backup_id);
87
88
        if (!empty($backup['path'])) {
89
            $this->download(gplcart_file_absolute($backup['path']));
90
        }
91
    }
92
93
    /**
94
     * Applies an action to the selected backups
95
     */
96
    protected function actionListBackup()
97
    {
98
        list($selected, $action) = $this->getPostedAction();
99
100
        $deleted = 0;
101
        foreach ($selected as $id) {
102
            if ($action === 'delete' && $this->access('backup_delete')) {
103
                $deleted += (int) $this->backup->delete($id);
104
            }
105
        }
106
107
        if ($deleted > 0) {
108
            $message = $this->text('Deleted %num item(s)', array('%num' => $deleted));
109
            $this->setMessage($message, 'success');
110
        }
111
    }
112
113
    /**
114
     * Sets title on the backup overview page
115
     */
116
    protected function setTitleListBackup()
117
    {
118
        $this->setTitle($this->text('Backups'));
119
    }
120
121
    /**
122
     * Sets breadcrumbs on the backup overview page
123
     */
124
    protected function setBreadcrumbListBackup()
125
    {
126
        $this->setBreadcrumbHome();
127
    }
128
129
    /**
130
     * Render and output the backup overview page
131
     */
132
    protected function outputListBackup()
133
    {
134
        $this->output('backup|list');
135
    }
136
137
    /**
138
     * Returns an array of backups
139
     * @return array
140
     */
141
    protected function getListBackup()
142
    {
143
        $options = $this->query_filter;
144
        $options['limit'] = $this->data_limit;
145
        return $this->backup->getList($options);
146
    }
147
148
    /**
149
     * Sets pager
150
     * @return array
151
     */
152
    protected function setPagerListBackup()
153
    {
154
        $options = $this->query_filter;
155
        $options['count'] = true;
156
157
        $pager = array(
158
            'query' => $this->query_filter,
159
            'total' => (int) $this->backup->getList($options)
160
        );
161
162
        return $this->data_limit = $this->setPager($pager);
163
    }
164
165
}
166