PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms 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
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1
  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. namespace Singletons\Controller;
  11. class Admin extends \Cockpit\AuthController {
  12. public function index() {
  13. $_singletons = $this->module('singletons')->getSingletonsInGroup();
  14. $singletons = [];
  15. foreach ($_singletons as $name => $meta) {
  16. $meta['allowed'] = [
  17. 'delete' => $this->module('cockpit')->hasaccess('singletons', 'delete'),
  18. 'create' => $this->module('cockpit')->hasaccess('singletons', 'create'),
  19. 'singleton_edit' => $this->module('singletons')->hasaccess($name, 'edit'),
  20. 'singleton_form' => $this->module('singletons')->hasaccess($name, 'form')
  21. ];
  22. $singletons[] = [
  23. 'name' => $name,
  24. 'label' => isset($meta['label']) && $meta['label'] ? $meta['label'] : $name,
  25. 'meta' => $meta
  26. ];
  27. }
  28. // sort singletons
  29. usort($singletons, function($a, $b) {
  30. return mb_strtolower($a['label']) <=> mb_strtolower($b['label']);
  31. });
  32. return $this->render('singletons:views/index.php', compact('singletons'));
  33. }
  34. public function singleton($name = null) {
  35. if ($name && !$this->module('singletons')->hasaccess($name, 'edit')) {
  36. return $this->helper('admin')->denyRequest();
  37. }
  38. if (!$name && !$this->module('cockpit')->hasaccess('singletons', 'create')) {
  39. return $this->helper('admin')->denyRequest();
  40. }
  41. $singleton = [ 'name'=>'', 'description' => '', 'fields'=>[], 'template' => '', 'data' => null];
  42. if ($name) {
  43. $singleton = $this->module('singletons')->singleton($name);
  44. if (!$singleton) {
  45. return false;
  46. }
  47. if (!$this->app->helper('admin')->isResourceEditableByCurrentUser($singleton['_id'], $meta)) {
  48. return $this->render('cockpit:views/base/locked.php', compact('meta'));
  49. }
  50. $this->app->helper('admin')->lockResourceId($singleton['_id']);
  51. }
  52. // acl groups
  53. $aclgroups = [];
  54. foreach ($this->app->helper('acl')->getGroups() as $group => $superAdmin) {
  55. if (!$superAdmin) $aclgroups[] = $group;
  56. }
  57. return $this->render('singletons:views/singleton.php', compact('singleton', 'aclgroups'));
  58. }
  59. public function form($name = null) {
  60. if (!$name) {
  61. return false;
  62. }
  63. $singleton = $this->module('singletons')->singleton($name);
  64. if (!$singleton) {
  65. return false;
  66. }
  67. if (!$this->module('singletons')->hasaccess($singleton['name'], 'form')) {
  68. return $this->helper('admin')->denyRequest();
  69. }
  70. $singleton = array_merge([
  71. 'sortable' => false,
  72. 'color' => '',
  73. 'icon' => '',
  74. 'description' => ''
  75. ], $singleton);
  76. $this->app->helper('admin')->favicon = [
  77. 'path' => 'singletons:icon.svg',
  78. 'color' => $singleton['color']
  79. ];
  80. $lockId = "singleton_{$singleton['name']}";
  81. if (!$this->app->helper('admin')->isResourceEditableByCurrentUser($lockId, $meta)) {
  82. return $this->render('singletons:views/locked.php', compact('meta', 'singleton'));
  83. }
  84. $data = $this->module('singletons')->getData($name);
  85. $this->app->helper('admin')->lockResourceId($lockId);
  86. return $this->render('singletons:views/form.php', compact('singleton', 'data'));
  87. }
  88. public function remove_singleton($singleton) {
  89. $singleton = $this->module('singletons')->singleton($singleton);
  90. if (!$singleton) {
  91. return false;
  92. }
  93. if (!$this->module('singletons')->hasaccess($singleton['name'], 'delete')) {
  94. return $this->helper('admin')->denyRequest();
  95. }
  96. $this->module('singletons')->removeSingleton($singleton['name']);
  97. return ['success' => true];
  98. }
  99. public function update_data($singleton) {
  100. $singleton = $this->module('singletons')->singleton($singleton);
  101. $data = $this->param('data');
  102. if (!$singleton || !$data) {
  103. return false;
  104. }
  105. if (!$this->module('singletons')->hasaccess($singleton['name'], 'form')) {
  106. return $this->helper('admin')->denyRequest();
  107. }
  108. $lockId = "singleton_{$singleton['name']}";
  109. if (!$this->app->helper('admin')->isResourceEditableByCurrentUser($lockId)) {
  110. $this->stop(['error' => "Saving failed! Singleton is locked!"], 412);
  111. }
  112. $data['_mby'] = $this->module('cockpit')->getUser('_id');
  113. if (isset($data['_by'])) {
  114. $_data = $this->module('singletons')->getData($singleton['name']);
  115. $revision = !(json_encode($_data) == json_encode($data));
  116. } else {
  117. $data['_by'] = $data['_mby'];
  118. $revision = true;
  119. }
  120. $data = $this->module('singletons')->saveData($singleton['name'], $data, ['revision' => $revision]);
  121. $this->app->helper('admin')->lockResourceId($lockId);
  122. return ['data' => $data];
  123. }
  124. public function revisions($singleton, $id) {
  125. if (!$this->module('singletons')->hasaccess($singleton, 'form')) {
  126. return $this->helper('admin')->denyRequest();
  127. }
  128. $singleton = $this->module('singletons')->singleton($singleton);
  129. if (!$singleton) {
  130. return false;
  131. }
  132. $data = $this->app->storage->getKey('singletons', $singleton['name']);
  133. if (!$data) {
  134. return false;
  135. }
  136. $revisions = $this->app->helper('revisions')->getList($id);
  137. return $this->render('singletons:views/revisions.php', compact('singleton', 'data', 'revisions', 'id'));
  138. }
  139. }