Download::outputEditDownload()   A
last analyzed

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 Installer
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\installer\controllers;
11
12
use gplcart\core\controllers\backend\Controller;
13
use gplcart\modules\installer\models\Install;
14
15
/**
16
 * Handles incoming requests and outputs data related to Installer module
17
 */
18
class Download extends Controller
19
{
20
21
    /**
22
     * Install model instance
23
     * @var \gplcart\modules\installer\models\Install $install
24
     */
25
    protected $install_model;
26
27
    /**
28
     * @param Install $install
29
     */
30
    public function __construct(Install $install)
31
    {
32
        parent::__construct();
33
34
        $this->install_model = $install;
35
    }
36
37
    /**
38
     * Route page callback to display the module download page
39
     */
40
    public function editDownload()
41
    {
42
        $this->downloadErrorsDownload();
43
        $this->setTitleEditDownload();
44
        $this->setBreadcrumbEditDownload();
45
46
        $this->submitDownload();
47
48
        $sources = $this->getData('download.sources');
49
50
        if (is_array($sources)) {
51
            $this->setData('download.sources', implode(PHP_EOL, $sources));
52
        }
53
54
        $this->outputEditDownload();
55
    }
56
57
    /**
58
     * Set title on the module download page
59
     */
60
    protected function setTitleEditDownload()
61
    {
62
        $title = $this->text('Edit %name settings', array('%name' => $this->text('Installer')));
63
        $this->setTitle($title);
64
    }
65
66
    /**
67
     * Set breadcrumbs on the module download page
68
     */
69
    protected function setBreadcrumbEditDownload()
70
    {
71
        $breadcrumbs = array();
72
73
        $breadcrumbs[] = array(
74
            'text' => $this->text('Dashboard'),
75
            'url' => $this->url('admin')
76
        );
77
78
        $breadcrumbs[] = array(
79
            'text' => $this->text('Modules'),
80
            'url' => $this->url('admin/module/list')
81
        );
82
83
        $this->setBreadcrumbs($breadcrumbs);
84
    }
85
86
    /**
87
     * Downloads an error log file
88
     */
89
    protected function downloadErrorsDownload()
90
    {
91
        $file = $this->install_model->getErrorLogFile();
92
93
        if ($this->isQuery('download_errors') && is_file($file)) {
94
            $this->download($file);
95
        }
96
    }
97
98
    /**
99
     * Handles submitted data
100
     */
101
    protected function submitDownload()
102
    {
103
        if ($this->isPosted('install') && $this->validateDownload()) {
104
            $this->controlAccess('module_installer_download');
105
            $this->install_model->fromUrl($this->getSubmitted('sources'));
106
        }
107
    }
108
109
    /**
110
     * Validate an array of submitted data
111
     */
112
    protected function validateDownload()
113
    {
114
        $this->setSubmitted('download');
115
        $this->setSubmittedArray('sources');
116
        $this->validateElement('sources', 'required');
117
        $this->validateUrlDownload();
118
119
        return !$this->hasErrors();
120
    }
121
122
    /**
123
     * Validates an array of submitted source URLs
124
     */
125
    protected function validateUrlDownload()
126
    {
127
        $invalid = array();
128
129
        foreach ($this->getSubmitted('sources') as $line => $url) {
130
            $line++;
131
            if (filter_var($url, FILTER_VALIDATE_URL) === false) {
132
                $invalid[] = $line;
133
            }
134
        }
135
136
        if (!empty($invalid)) {
137
            $this->setError('sources', $this->text('Error on line @num', array('@num' => implode(',', $invalid))));
138
        }
139
    }
140
141
    /**
142
     * Render and output the download modules page
143
     */
144
    protected function outputEditDownload()
145
    {
146
        $this->output('installer|download');
147
    }
148
149
}
150