|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * Acme Demo Extension. An extension for the phpBB Forum Software package. |
| 5 | + * |
| 6 | + * @copyright (c) 2026, Joas Schilling, https://github.com/nickvergessen/ |
| 7 | + * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +namespace acme\demo\controller; |
| 12 | + |
| 13 | +/** |
| 14 | + * Acme Demo Extension ACP controller. |
| 15 | + */ |
| 16 | +class acp_controller |
| 17 | +{ |
| 18 | + /** @var \phpbb\config\config */ |
| 19 | + protected $config; |
| 20 | + |
| 21 | + /** @var \phpbb\language\language */ |
| 22 | + protected $language; |
| 23 | + |
| 24 | + /** @var \phpbb\log\log */ |
| 25 | + protected $log; |
| 26 | + |
| 27 | + /** @var \phpbb\request\request */ |
| 28 | + protected $request; |
| 29 | + |
| 30 | + /** @var \phpbb\template\template */ |
| 31 | + protected $template; |
| 32 | + |
| 33 | + /** @var \phpbb\user */ |
| 34 | + protected $user; |
| 35 | + |
| 36 | + /** @var string Custom form action */ |
| 37 | + protected $u_action; |
| 38 | + |
| 39 | + /** |
| 40 | + * Constructor. |
| 41 | + * |
| 42 | + * @param \phpbb\config\config $config Config object |
| 43 | + * @param \phpbb\language\language $language Language object |
| 44 | + * @param \phpbb\log\log $log Log object |
| 45 | + * @param \phpbb\request\request $request Request object |
| 46 | + * @param \phpbb\template\template $template Template object |
| 47 | + * @param \phpbb\user $user User object |
| 48 | + */ |
| 49 | + public function __construct(\phpbb\config\config $config, \phpbb\language\language $language, \phpbb\log\log $log, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) |
| 50 | + { |
| 51 | + $this->config = $config; |
| 52 | + $this->language = $language; |
| 53 | + $this->log = $log; |
| 54 | + $this->request = $request; |
| 55 | + $this->template = $template; |
| 56 | + $this->user = $user; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Display the options a user can configure for this extension. |
| 61 | + * |
| 62 | + * @return void |
| 63 | + */ |
| 64 | + public function display_options() |
| 65 | + { |
| 66 | + // Add our common language file |
| 67 | + $this->language->add_lang('common', 'acme/demo'); |
| 68 | + |
| 69 | + // Create a form key for preventing CSRF attacks |
| 70 | + add_form_key('acme_demo_acp'); |
| 71 | + |
| 72 | + // Create an array to collect errors that will be output to the user |
| 73 | + $errors = []; |
| 74 | + |
| 75 | + // Is the form being submitted to us? |
| 76 | + if ($this->request->is_set_post('submit')) |
| 77 | + { |
| 78 | + // Test if the submitted form is valid |
| 79 | + if (!check_form_key('acme_demo_acp')) |
| 80 | + { |
| 81 | + $errors[] = $this->language->lang('FORM_INVALID'); |
| 82 | + } |
| 83 | + |
| 84 | + // If no errors, process the form data |
| 85 | + if (empty($errors)) |
| 86 | + { |
| 87 | + // Set the options the user configured |
| 88 | + $this->config->set('acme_demo_goodbye', $this->request->variable('acme_demo_goodbye', 0)); |
| 89 | + |
| 90 | + // Add option settings change action to the admin log |
| 91 | + $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_ACP_DEMO_SETTINGS'); |
| 92 | + |
| 93 | + // Option settings have been updated and logged |
| 94 | + // Confirm this to the user and provide link back to previous page |
| 95 | + trigger_error($this->language->lang('ACP_DEMO_SETTING_SAVED') . adm_back_link($this->u_action)); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + $s_errors = !empty($errors); |
| 100 | + |
| 101 | + // Set output variables for display in the template |
| 102 | + $this->template->assign_vars([ |
| 103 | + 'S_ERROR' => $s_errors, |
| 104 | + 'ERROR_MSG' => $s_errors ? implode('<br>', $errors) : '', |
| 105 | + |
| 106 | + 'U_ACTION' => $this->u_action, |
| 107 | + |
| 108 | + 'ACME_DEMO_GOODBYE' => (bool) $this->config['acme_demo_goodbye'], |
| 109 | + ]); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Set custom form action. |
| 114 | + * |
| 115 | + * @param string $u_action Custom form action |
| 116 | + * @return void |
| 117 | + */ |
| 118 | + public function set_page_url($u_action) |
| 119 | + { |
| 120 | + $this->u_action = $u_action; |
| 121 | + } |
| 122 | +} |
0 commit comments