Completed
Push — master ( 853dbc...35e61c )
by Iurii
01:26
created

Language::getListLanguage()   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 0
1
<?php
2
3
/**
4
 * @package CLI
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, 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\cli\controllers;
11
12
/**
13
 * Handles commands related to languages
14
 */
15
class Language extends Base
16
{
17
18
    /**
19
     * Constructor
20
     */
21
    public function __construct()
22
    {
23
        parent::__construct();
24
    }
25
26
    /**
27
     * Callback for "language-update"
28
     */
29
    public function cmdUpdateLanguage()
30
    {
31
        $params = $this->getParam();
32
33
        if (empty($params[0]) || count($params) < 2) {
34
            $this->errorExit($this->text('Invalid command'));
35
        }
36
37
        $this->setSubmitted(null, $params);
38
        $this->setSubmitted('update', $params[0]);
39
        $this->validateComponent('language');
40
        $this->updateLanguage($params[0]);
41
        $this->output();
42
    }
43
44
    /**
45
     * Callback for "language-add"
46
     */
47
    public function cmdAddLanguage()
48
    {
49
        if ($this->getParam()) {
50
            $this->submitAddLanguage();
51
        } else {
52
            $this->wizardAddLanguage();
53
        }
54
55
        $this->output();
56
    }
57
58
    /**
59
     * Callback for "language-get" command
60
     */
61
    public function cmdGetLanguage()
62
    {
63
        $list = $this->getListLanguage();
64
        $this->outputFormat($list);
65
        $this->outputFormatTableLanguage($list);
66
        $this->output();
67
    }
68
69
    /**
70
     * Callback for "language-delete" command
71
     */
72
    public function cmdDeleteLanguage()
73
    {
74
        $id = $this->getParam(0);
75
        $all = $this->getParam('all');
76
77
        if (!isset($id) && empty($all)) {
78
            $this->errorExit($this->text('Invalid command'));
79
        }
80
81
        $result = false;
82
83
        if (isset($id)) {
84
            $result = $this->language->delete($id);
85
        } else if (!empty($all)) {
86
            $deleted = $count = 0;
87
            foreach ($this->language->getList(array('in_database' => true)) as $language) {
88
                $count++;
89
                $deleted += (int) $this->language->delete($language['code']);
90
            }
91
            $result = ($count == $deleted);
92
        }
93
94
        if (!$result) {
95
            $this->errorExit($this->text('An error occurred'));
96
        }
97
98
        $this->output();
99
    }
100
101
    /**
102
     * Returns an array of languages
103
     * @return array
104
     */
105
    protected function getListLanguage()
106
    {
107
        $id = $this->getParam(0);
108
109
        if (!isset($id)) {
110
            $list = $this->language->getList();
111
            $this->limitArray($list);
112
            return $list;
113
        }
114
115
        $result = $this->language->get($id);
116
117
        if (empty($result)) {
118
            $this->errorExit($this->text('Invalid ID'));
119
        }
120
121
        return array($result);
122
    }
123
124
    /**
125
     * Output formatted table
126
     * @param array $items
127
     */
128
    protected function outputFormatTableLanguage(array $items)
129
    {
130
        $header = array(
131
            $this->text('Code'),
132
            $this->text('Name'),
133
            $this->text('Native name'),
134
            $this->text('Right-to-left'),
135
            $this->text('Default'),
136
            $this->text('In database'),
137
            $this->text('Enabled')
138
        );
139
140
        $rows = array();
141
142
        foreach ($items as $item) {
143
            $rows[] = array(
144
                $item['code'],
145
                $this->text($item['name']),
146
                $item['native_name'],
147
                empty($item['rtl']) ? $this->text('No') : $this->text('Yes'),
148
                empty($item['default']) ? $this->text('No') : $this->text('Yes'),
149
                empty($item['in_database']) ? $this->text('No') : $this->text('Yes'),
150
                empty($item['status']) ? $this->text('No') : $this->text('Yes'),
151
            );
152
        }
153
154
        $this->outputFormatTable($rows, $header);
155
    }
156
157
    /**
158
     * Adds a language
159
     */
160
    protected function addLanguage()
161
    {
162
        if (!$this->isError() && !$this->language->add($this->getSubmitted())) {
163
            $this->errorExit($this->text('Language has not been added'));
164
        }
165
    }
166
167
    /**
168
     * Updates a language
169
     * @param string $code
170
     */
171
    protected function updateLanguage($code)
172
    {
173
        if (!$this->isError() && !$this->language->update($code, $this->getSubmitted())) {
174
            $this->errorExit($this->text('Language has not been updated'));
175
        }
176
    }
177
178
    /**
179
     * Add a language at once
180
     */
181
    protected function submitAddLanguage()
182
    {
183
        $this->setSubmitted(null, $this->getParam());
184
        $this->validateComponent('language');
185
        $this->addLanguage();
186
    }
187
188
    /**
189
     * Adds a language step by step
190
     */
191
    protected function wizardAddLanguage()
192
    {
193
        $this->validatePrompt('code', $this->text('Code'), 'language');
194
        $this->validatePrompt('name', $this->text('Name'), 'language', '');
195
        $this->validatePrompt('native_name', $this->text('Native name'), 'language', '');
196
        $this->validatePrompt('status', $this->text('Status'), 'language', 0);
197
        $this->validatePrompt('default', $this->text('Default') . '?', 'language', 0);
198
        $this->validatePrompt('weight', $this->text('Weight'), 'language', 0);
199
        $this->validatePrompt('rtl', $this->text('Right-to-left') . '?', 'language', 0);
200
        $this->validateComponent('language');
201
        $this->addLanguage();
202
    }
203
204
}
205