PageRenderTime 395ms CodeModel.GetById 161ms app.highlight 63ms RepoModel.GetById 168ms app.codeStats 0ms

/modules/Singletons/Controller/Admin.php

https://github.com/agentejo/cockpit
PHP | 200 lines | 129 code | 61 blank | 10 comment | 25 complexity | 93bd40b0115c9b2433ba49e8bf9063ab MD5 | raw file
  1<?php
  2/**
  3 * This file is part of the Cockpit project.
  4 *
  5 * (c) Artur Heinze - πŸ…°πŸ…ΆπŸ…΄πŸ…½πŸ†ƒπŸ…΄πŸ…ΉπŸ…Ύ, http://agentejo.com
  6 *
  7 * For the full copyright and license information, please view the LICENSE
  8 * file that was distributed with this source code.
  9 */
 10
 11namespace Singletons\Controller;
 12
 13
 14class Admin extends \Cockpit\AuthController {
 15
 16    public function index() {
 17
 18        $_singletons = $this->module('singletons')->getSingletonsInGroup();
 19        $singletons  = [];
 20
 21        foreach ($_singletons as $name => $meta) {
 22
 23            $meta['allowed'] = [
 24                'delete' => $this->module('cockpit')->hasaccess('singletons', 'delete'),
 25                'create' => $this->module('cockpit')->hasaccess('singletons', 'create'),
 26                'singleton_edit' => $this->module('singletons')->hasaccess($name, 'edit'),
 27                'singleton_form' => $this->module('singletons')->hasaccess($name, 'form')
 28            ];
 29
 30            $singletons[] = [
 31              'name'  => $name,
 32              'label' => isset($meta['label']) && $meta['label'] ? $meta['label'] : $name,
 33              'meta'  => $meta
 34            ];
 35        }
 36
 37        // sort singletons
 38        usort($singletons, function($a, $b) {
 39            return mb_strtolower($a['label']) <=> mb_strtolower($b['label']);
 40        });
 41
 42        return $this->render('singletons:views/index.php', compact('singletons'));
 43    }
 44
 45    public function singleton($name = null) {
 46
 47        if ($name && !$this->module('singletons')->hasaccess($name, 'edit')) {
 48            return $this->helper('admin')->denyRequest();
 49        }
 50
 51        if (!$name && !$this->module('cockpit')->hasaccess('singletons', 'create')) {
 52            return $this->helper('admin')->denyRequest();
 53        }
 54
 55        $singleton = [ 'name'=>'', 'description' => '', 'fields'=>[], 'template' => '', 'data' => null];
 56
 57        if ($name) {
 58
 59            $singleton = $this->module('singletons')->singleton($name);
 60
 61            if (!$singleton) {
 62                return false;
 63            }
 64
 65            if (!$this->app->helper('admin')->isResourceEditableByCurrentUser($singleton['_id'], $meta)) {
 66                return $this->render('cockpit:views/base/locked.php', compact('meta'));
 67            }
 68
 69            $this->app->helper('admin')->lockResourceId($singleton['_id']);
 70        }
 71
 72        // acl groups
 73        $aclgroups = [];
 74
 75        foreach ($this->app->helper('acl')->getGroups() as $group => $superAdmin) {
 76
 77            if (!$superAdmin) $aclgroups[] = $group;
 78        }
 79
 80        return $this->render('singletons:views/singleton.php', compact('singleton', 'aclgroups'));
 81    }
 82
 83    public function form($name = null) {
 84
 85        if (!$name) {
 86            return false;
 87        }
 88
 89        $singleton = $this->module('singletons')->singleton($name);
 90
 91        if (!$singleton) {
 92            return false;
 93        }
 94
 95        if (!$this->module('singletons')->hasaccess($singleton['name'], 'form')) {
 96            return $this->helper('admin')->denyRequest();
 97        }
 98
 99        $singleton = array_merge([
100            'sortable' => false,
101            'color' => '',
102            'icon' => '',
103            'description' => ''
104        ], $singleton);
105
106        $this->app->helper('admin')->favicon = [
107            'path' => 'singletons:icon.svg',
108            'color' => $singleton['color']
109        ];
110
111        $lockId = "singleton_{$singleton['name']}";
112
113        if (!$this->app->helper('admin')->isResourceEditableByCurrentUser($lockId, $meta)) {
114            return $this->render('singletons:views/locked.php', compact('meta', 'singleton'));
115        }
116
117        $data = $this->module('singletons')->getData($name);
118
119        $this->app->helper('admin')->lockResourceId($lockId);
120
121        return $this->render('singletons:views/form.php', compact('singleton', 'data'));
122        
123    }
124
125    public function remove_singleton($singleton) {
126
127        $singleton = $this->module('singletons')->singleton($singleton);
128
129        if (!$singleton) {
130            return false;
131        }
132
133        if (!$this->module('singletons')->hasaccess($singleton['name'], 'delete')) {
134            return $this->helper('admin')->denyRequest();
135        }
136
137        $this->module('singletons')->removeSingleton($singleton['name']);
138
139        return ['success' => true];
140    }
141
142    public function update_data($singleton) {
143
144        $singleton = $this->module('singletons')->singleton($singleton);
145        $data = $this->param('data');
146
147        if (!$singleton || !$data) {
148            return false;
149        }
150
151        if (!$this->module('singletons')->hasaccess($singleton['name'], 'form')) {
152            return $this->helper('admin')->denyRequest();
153        }
154
155        $lockId = "singleton_{$singleton['name']}";
156
157        if (!$this->app->helper('admin')->isResourceEditableByCurrentUser($lockId)) {
158            $this->stop(['error' => "Saving failed! Singleton is locked!"], 412);
159        }
160
161        $data['_mby'] = $this->module('cockpit')->getUser('_id');
162
163        if (isset($data['_by'])) {
164            $_data = $this->module('singletons')->getData($singleton['name']);
165            $revision = !(json_encode($_data) == json_encode($data));
166        } else {
167            $data['_by'] = $data['_mby'];
168            $revision = true;
169        }
170
171        $data = $this->module('singletons')->saveData($singleton['name'], $data, ['revision' => $revision]);
172
173        $this->app->helper('admin')->lockResourceId($lockId);
174
175        return ['data' => $data];
176    }
177
178    public function revisions($singleton, $id) {
179
180        if (!$this->module('singletons')->hasaccess($singleton, 'form')) {
181            return $this->helper('admin')->denyRequest();
182        }
183
184        $singleton = $this->module('singletons')->singleton($singleton);
185
186        if (!$singleton) {
187            return false;
188        }
189
190        $data = $this->app->storage->getKey('singletons', $singleton['name']);
191
192        if (!$data) {
193            return false;
194        }
195
196        $revisions = $this->app->helper('revisions')->getList($id);
197
198        return $this->render('singletons:views/revisions.php', compact('singleton', 'data', 'revisions', 'id'));
199    }
200}