Completed
Push — dev-master ( e1a6ef...11f3cc )
by Vijay
03:14
created

Apps::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 19
rs 9.4285
1
<?php
2
3
namespace FFCMS\Controllers\Admin;
4
5
use FFMVC\Helpers;
6
use FFCMS\{Traits, Controllers, Models, Mappers};
7
8
/**
9
 * Admin Apps CMS Controller Class.
10
 *
11
 * @author Vijay Mahrra <[email protected]>
12
 * @copyright 2016 Vijay Mahrra
13
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
14
 */
15
class Apps extends Admin
16
{
17
    /**
18
     * For admin listing and search results
19
     */
20
    use Traits\SearchController;
21
22
    protected $template_path = 'cms/admin/apps/';
23
24
25
    /**
26
     *
27
     *
28
     * @param \Base $f3
29
     * @return void
30
     */
31
    public function listing(\Base $f3)
32
    {
33
        $view = strtolower(trim(strip_tags($f3->get('REQUEST.view'))));
34
        $view = empty($view) ? 'list.phtml' : $view . '.phtml';
35
        $f3->set('REQUEST.view', $view);
36
37
        $f3->set('results', $this->getListingResults($f3, new Mappers\OAuth2Apps));
38
39
        $f3->set('breadcrumbs', [
40
            _('Admin') => 'admin',
41
            _('Apps') => 'admin_apps_list',
42
        ]);
43
44
        $f3->set('form', $f3->get('REQUEST'));
45
        echo \View::instance()->render($this->template_path . $view);
46
    }
47
48
49
    /**
50
     *
51
     *
52
     * @param \Base $f3
53
     * @return void
54
     */
55
    public function search(\Base $f3)
56
    {
57
        $view = strtolower(trim(strip_tags($f3->get('REQUEST.view'))));
58
        $view = empty($view) ? 'list.phtml' : $view . '.phtml';
59
        $f3->set('REQUEST.view', $view);
60
61
        $f3->set('results', $this->getSearchResults($f3, new Mappers\OAuth2Apps));
62
63
        $f3->set('breadcrumbs', [
64
            _('Admin') => 'admin',
65
            _('Apps') => 'admin_apps_list',
66
            _('Search') => '',
67
        ]);
68
69
        $f3->set('form', $f3->get('REQUEST'));
70
        echo \View::instance()->render($this->template_path . $view);
71
    }
72
73
    /**
74
     *
75
     *
76
     * @param \Base $f3
77
     * @return void
78
     */
79 View Code Duplication
    public function edit(\Base $f3)
80
    {
81
        $this->redirectLoggedOutUser();
82
        $this->csrf();
83
84
        $client_id = $f3->get('REQUEST.client_id');
85
        $oAuth2Model = Models\OAuth2::instance();
86
        $appsMapper = $oAuth2Model->getAppsMapper();
87
        $appsMapper->load(['client_id = ?', $client_id]);
88
        $data = $appsMapper->cast();
89
90
        $f3->set('breadcrumbs', [
91
            _('Admin') => 'admin',
92
            _('Apps') => 'admin_apps_list',
93
            _('Edit') => '',
94
        ]);
95
96
        $f3->set('form', $data);
97
        echo \View::instance()->render($this->template_path . 'edit.phtml');
98
    }
99
100
101
    /**
102
     *
103
     *
104
     * @param \Base $f3
105
     * @return void
106
     */
107
    public function editPost(\Base $f3)
108
    {
109
        $this->csrf('@admin_apps_list');
110
        $this->redirectLoggedOutUser();
111
112
        if (false == $f3->get('isRoot')) {
113
            $this->notify(_('You do not have (root) permission!'), 'error');
114
            return $f3->reroute('@admin');
115
        }
116
117
        $oAuth2Model = Models\OAuth2::instance();
118
        $appsMapper = $oAuth2Model->getAppsMapper();
119
120
        // filter input vars of request, set back into REQUEST
121
        $appsMapper->copyfrom($f3->get('REQUEST'));
122
        $data = $appsMapper->filter();
123
        $request = $f3->get('REQUEST');
124
        foreach ($data as $k => $v) {
125
            if (array_key_exists($k, $request)) {
126
                $f3->set('REQUEST.' . $k, $v);
127
            }
128
        }
129
130
        // check app name exists
131 View Code Duplication
        if (!$appsMapper->load(['LOWER(client_id) = LOWER(?) AND users_uuid = ?',
132
                $request['client_id'], $f3->get('uuid')])) {
133
            $this->notify(_('The app does not exist!'), 'warning');
134
            $f3->reroute('@api_apps');
135
            return;
136
        }
137
138
        // check required fields
139
        $appsMapper->copyfrom($f3->get('REQUEST'));
140
        $appsMapper->validationRequired([
141
            'name',
142
            'description',
143
            'callback_uri',
144
            'status',
145
        ]);
146
147
        // at this point the app can be validated
148 View Code Duplication
        if (true !== $appsMapper->validate()) {
149
            $this->notify(['info' => $appsMapper->validationErrors($appsMapper->validate(false))]);
150
            $f3->reroute('@api_apps');
151
        }
152
153 View Code Duplication
        if ($appsMapper->save()) {
154
            $this->notify(_('The app has been updated!'), 'success');
155
        } else {
156
            $this->notify(_('App update failed!'), 'error');
157
        }
158
159
        $f3->reroute('@admin_apps_list');
160
    }
161
162
163
    /**
164
     *
165
     *
166
     * @param \Base $f3
167
     * @return void
168
     */
169
    public function delete(\Base $f3)
170
    {
171
        $this->redirectLoggedOutUser();
172
        $this->csrf();
173
174
        if (false == $f3->get('isRoot')) {
175
            $this->notify(_('You do not have (root) permission!'), 'error');
176
            return $f3->reroute('@admin_apps_list');
177
        }
178
179
        $client_id = $f3->get('REQUEST.client_id');
180
181
        $mapper = new Mappers\OAuth2Apps;
182
        $mapper->load(['client_id = ?', $client_id]);
183
        $mapper->erase();
184
        $this->notify('App deleted!', 'success');
185
186
        return $f3->reroute('@admin_apps_list');
187
    }
188
189
}
190