PageRenderTime 64ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/Forms/Controller/Admin.php

https://github.com/agentejo/cockpit
PHP | 117 lines | 73 code | 35 blank | 9 comment | 10 complexity | 10480e8e00c5f246ed2c6abb40e62f06 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 Forms\Controller;
  11. class Admin extends \Cockpit\AuthController {
  12. public function index() {
  13. $_forms = $this->module('forms')->forms(true);
  14. $forms = [];
  15. foreach ($_forms as $name => $meta) {
  16. $forms[] = [
  17. 'name' => $name,
  18. 'label' => isset($meta['label']) && $meta['label'] ? $meta['label'] : $name,
  19. 'meta' => $meta
  20. ];
  21. }
  22. // sort forms
  23. usort($forms, function($a, $b) {
  24. return mb_strtolower($a['label']) <=> mb_strtolower($b['label']);
  25. });
  26. return $this->render('forms:views/index.php', compact('forms'));
  27. }
  28. public function form($name = null) {
  29. $form = [ 'name'=>'', 'in_menu' => false ];
  30. if ($name) {
  31. $form = $this->module('forms')->form($name);
  32. if (!$form) {
  33. return false;
  34. }
  35. }
  36. return $this->render('forms:views/form.php', compact('form'));
  37. }
  38. public function entries($form) {
  39. $form = $this->module('forms')->form($form);
  40. if (!$form) {
  41. return false;
  42. }
  43. $count = $this->module('forms')->count($form['name']);
  44. $form = array_merge([
  45. 'sortable' => false,
  46. 'color' => '',
  47. 'icon' => '',
  48. 'description' => ''
  49. ], $form);
  50. $this->app->helper('admin')->favicon = [
  51. 'path' => 'forms:icon.svg',
  52. 'color' => $form['color']
  53. ];
  54. $view = 'forms:views/entries.php';
  55. if ($override = $this->app->path('#config:forms/'.$form['name'].'/views/entries.php')) {
  56. $view = $override;
  57. }
  58. return $this->render($view, compact('form', 'count'));
  59. }
  60. public function find() {
  61. $form = $this->app->param('form');
  62. $options = $this->app->param('options');
  63. if (!$form) return false;
  64. $entries = $this->app->module('forms')->find($form, $options);
  65. $count = $this->app->module('forms')->count($form, isset($options['filter']) ? $options['filter'] : []);
  66. $pages = isset($options['limit']) ? ceil($count / $options['limit']) : 1;
  67. $page = 1;
  68. if ($pages > 1 && isset($options['skip'])) {
  69. $page = ceil($options['skip'] / $options['limit']) + 1;
  70. }
  71. return compact('entries', 'count', 'pages', 'page');
  72. }
  73. public function export($form) {
  74. if (!$this->app->module('cockpit')->hasaccess('forms', 'manage')) {
  75. return false;
  76. }
  77. $form = $this->module('forms')->form($form);
  78. if (!$form) return false;
  79. $entries = $this->module('forms')->find($form['name']);
  80. return json_encode($entries, JSON_PRETTY_PRINT);
  81. }
  82. }