PluginsController::delete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
c 0
b 0
f 0
nc 2
nop 1
dl 18
loc 18
rs 9.4285
1
<?php
2
/**
3
 * Licensed under The GPL-3.0 License
4
 * For full copyright and license information, please see the LICENSE.txt
5
 * Redistributions of files must retain the above copyright notice.
6
 *
7
 * @since    2.0.0
8
 * @author   Christopher Castro <[email protected]>
9
 * @link     http://www.quickappscms.org
10
 * @license  http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License
11
 */
12
namespace System\Controller\Admin;
13
14
use Cake\Network\Exception\NotFoundException;
15
use CMS\Console\WebShellDispatcher;
16
use CMS\Core\Plugin;
17
use Installer\Utility\PackageUploader;
18
use System\Controller\AppController;
19
20
/**
21
 * Controller for handling plugin tasks.
22
 *
23
 * Here is where can install new plugin or remove existing ones.
24
 */
25
class PluginsController extends AppController
26
{
27
28
    /**
29
     * Main action.
30
     *
31
     * @return void
32
     */
33
    public function index()
34
    {
35
        $collection = plugin()->filter(function ($plugin) {
36
            return !$plugin->isTheme;
37
        });
38
        $plugins = $collection;
39
        $enabled = count($collection->filter(function ($plugin) {
40
            return $plugin->status;
41
        })->toArray());
42
        $disabled = count($collection->filter(function ($plugin) {
43
            return !$plugin->status;
44
        })->toArray());
45
46
        $this->title(__d('system', 'Plugins'));
47
        $this->set(compact('plugins', 'all', 'enabled', 'disabled'));
48
        $this->_awaitingPlugins();
49
        $this->Breadcrumb->push('/admin/system/plugins');
50
    }
51
52
    /**
53
     * Installs a new plugin.
54
     *
55
     * @return void
56
     */
57
    public function install()
58
    {
59
        if ($this->request->data()) {
60
            $task = false;
61
            $uploadError = false;
62
            $activate = !empty($this->request->data['activate']) ? ' -a' : '';
63
64
            if (isset($this->request->data['download'])) {
65
                $task = (bool)WebShellDispatcher::run("Installer.plugins install -s \"{$this->request->data['url']}\"{$activate}");
66
            } elseif (isset($this->request->data['file_system'])) {
67
                $task = (bool)WebShellDispatcher::run("Installer.plugins install -s \"{$this->request->data['path']}\"{$activate}");
68 View Code Duplication
            } else {
69
                $uploader = new PackageUploader($this->request->data['file']);
70
                if ($uploader->upload()) {
71
                    $task = (bool)WebShellDispatcher::run('Installer.plugins install -s "' . $uploader->dst() . '"' . $activate);
72
                } else {
73
                    $uploadError = true;
74
                    $this->Flash->set(__d('system', 'Plugins installed but some errors occur'), [
75
                        'element' => 'System.installer_errors',
76
                        'params' => ['errors' => $uploader->errors(), 'type' => 'warning'],
77
                    ]);
78
                }
79
            }
80
81 View Code Duplication
            if ($task) {
82
                $this->Flash->success(__d('system', 'Plugins successfully installed!'));
83
                $this->redirect($this->referer());
84
            } elseif (!$task && !$uploadError) {
85
                $this->Flash->set(__d('system', 'Plugins could not be installed'), [
86
                    'element' => 'System.installer_errors',
87
                    'params' => ['errors' => WebShellDispatcher::output()],
88
                ]);
89
            }
90
        }
91
92
        $this->title(__d('system', 'Install Plugin'));
93
        $this->Breadcrumb
94
            ->push('/admin/system/plugins')
95
            ->push(__d('system', 'Install new plugin'), '#');
96
    }
97
98
    /**
99
     * Uninstalls the given plugin.
100
     *
101
     * @param string $pluginName Plugin's name
102
     * @return void Redirects to previous page
103
     */
104 View Code Duplication
    public function delete($pluginName)
105
    {
106
        $plugin = plugin($pluginName); // throws if not exists
107
        $task = (bool)WebShellDispatcher::run("Installer.plugins uninstall -p {$plugin->name}");
108
109
        if ($task) {
110
            $this->Flash->success(__d('system', 'Plugin was successfully removed!'));
111
        } else {
112
            $this->Flash->set(__d('system', 'Plugins could not be removed'), [
113
                'element' => 'System.installer_errors',
114
                'params' => ['errors' => WebShellDispatcher::output()],
115
            ]);
116
        }
117
118
        $this->title(__d('system', 'Uninstall Plugin'));
119
        header('Location:' . $this->referer());
120
        exit();
121
    }
122
123
    /**
124
     * Enables the given plugin.
125
     *
126
     * @param string $pluginName Plugin's name
127
     * @return void Redirects to previous page
128
     */
129 View Code Duplication
    public function enable($pluginName)
130
    {
131
        $plugin = plugin($pluginName);
132
        $task = (bool)WebShellDispatcher::run("Installer.plugins toggle -p {$plugin->name} -s enable");
133
134
        if ($task) {
135
            $this->Flash->success(__d('system', 'Plugin was successfully enabled!'));
136
        } else {
137
            $this->Flash->set(__d('system', 'Plugin could not be enabled'), [
138
                'element' => 'System.installer_errors',
139
                'params' => ['errors' => WebShellDispatcher::output()],
140
            ]);
141
        }
142
143
        $this->title(__d('system', 'Enable Plugin'));
144
        header('Location:' . $this->referer());
145
        exit();
146
    }
147
148
    /**
149
     * Disables the given plugin.
150
     *
151
     * @param string $pluginName Plugin's name
152
     * @return void Redirects to previous page
153
     */
154 View Code Duplication
    public function disable($pluginName)
155
    {
156
        $plugin = plugin($pluginName);
157
        $task = (bool)WebShellDispatcher::run("Installer.plugins toggle -p {$plugin->name} -s disable");
158
159
        if ($task) {
160
            $this->Flash->success(__d('system', 'Plugin was successfully disabled!'));
161
        } else {
162
            $this->Flash->set(__d('system', 'Plugin could not be disabled'), [
163
                'element' => 'System.installer_errors',
164
                'params' => ['errors' => WebShellDispatcher::output()],
165
            ]);
166
        }
167
168
        $this->title(__d('system', 'Disable Plugin'));
169
        header('Location:' . $this->referer());
170
        exit();
171
    }
172
173
    /**
174
     * Handles plugin's specifics settings.
175
     *
176
     * When saving plugin's information `PluginsTable` will trigger the
177
     * following events:
178
     *
179
     * - `Plugin.<PluginName>.settingsValidate`
180
     * - `Plugin.<PluginName>.beforeSave`
181
     * - `Plugin.<PluginName>.afterSave`
182
     *
183
     * Check `PluginsTable` documentation for more details.
184
     *
185
     * Additionally plugins may define default values for each input, to do this
186
     * they must catch the event:
187
     *
188
     * - `Plugin.<PluginName>.settingsDefaults`
189
     *
190
     * They must return an associative array of default values for each input in the
191
     * form.
192
     *
193
     * Validation rules can be applied to settings, plugins must simply catch the
194
     * event:
195
     *
196
     * - `Plugin.<PluginName>.validate`
197
     *
198
     * @param string $pluginName Plugin's name
199
     * @return void
200
     * @throws \Cake\Network\Exception\NotFoundException When plugin do not exists
201
     */
202 View Code Duplication
    public function settings($pluginName)
203
    {
204
        $info = plugin($pluginName);
205
        $this->loadModel('System.Plugins');
206
        $plugin = $this->Plugins->get($pluginName, ['flatten' => true]);
207
208
        if (!$info->hasSettings || $info->isTheme) {
209
            throw new NotFoundException(__d('system', 'The requested page was not found.'));
210
        }
211
212
        if ($this->request->data()) {
213
            $plugin = $this->Plugins->patchEntity($plugin, $this->request->data(), ['entity' => $plugin]);
214
            if (!$plugin->errors()) {
215
                if ($this->Plugins->save($plugin)) {
216
                    $this->Flash->success(__d('system', 'Plugin settings saved!'));
217
                    $this->redirect($this->referer());
218
                }
219
            } else {
220
                $this->Flash->danger(__d('system', 'Plugin settings could not be saved.'));
221
            }
222
        }
223
224
        $this->title(__d('system', 'Plugin’s Settings'));
225
        $this->set(compact('plugin', 'info'));
226
        $this->Breadcrumb
227
            ->push('/admin/system/plugins')
228
            ->push(__d('system', 'Settings for "{0}" plugin', $info->name), '#');
229
    }
230
}
231