Completed
Push — master ( 42e677...2a102b )
by Matt
18s queued 11s
created

main_module::display_manage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 *
4
 * phpBB Media Embed PlugIn extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2016 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\mediaembed\acp;
12
13
/**
14
 * phpBB Media Embed Plugin ACP module.
15
 */
16
class main_module
17
{
18
	/** @var \phpbb\config\config $config */
19
	protected $config;
20
21
	/** @var \phpbb\config\db_text $config_text */
22
	protected $config_text;
23
24
	/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
25
	protected $container;
26
27
	/** @var \phpbb\language\language $language */
28
	protected $language;
29
30
	/** @var \phpbb\log\log $log */
31
	protected $log;
32
33
	/** @var \phpbb\mediaembed\cache\cache $media_cache */
34
	protected $media_cache;
35
36
	/** @var \phpbb\request\request $request */
37
	protected $request;
38
39
	/** @var \phpbb\template\template $template */
40
	protected $template;
41
42
	/** @var \phpbb\user */
43
	protected $user;
44
45
	/** @var array $enabled_sites */
46
	protected $enabled_sites;
47
48
	/** @var string $page_title */
49
	public $page_title;
50
51
	/** @var string $tpl_name */
52
	public $tpl_name;
53
54
	/** @var string $u_action */
55
	public $u_action;
56
57
	/** @var array An array of errors */
58
	protected $errors = [];
59
60
	/**
61
	 * Constructor
62
	 *
63
	 * @throws \Exception
64
	 */
65
	public function __construct()
66
	{
67
		global $phpbb_container;
68
69
		$this->container   = $phpbb_container;
70
		$this->config      = $this->container->get('config');
71
		$this->config_text = $this->container->get('config_text');
72
		$this->language    = $this->container->get('language');
73
		$this->log         = $this->container->get('log');
74
		$this->media_cache = $this->container->get('phpbb.mediaembed.cache');
75
		$this->request     = $this->container->get('request');
76
		$this->template    = $this->container->get('template');
77
		$this->user        = $this->container->get('user');
78
79
		$this->language->add_lang('acp', 'phpbb/mediaembed');
80
	}
81
82
	/**
83
	 * Main ACP module
84
	 *
85
	 * @param int    $id   The module ID (not used)
86
	 * @param string $mode The module mode (manage|settings)
87
	 * @throws \Exception
88
	 */
89
	public function main($id, $mode)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
	{
91
		$mode = strtolower($mode);
92
93
		$this->tpl_name   = 'acp_phpbb_mediaembed_' . $mode;
94
		$this->page_title = $this->language->lang('ACP_MEDIA_' . strtoupper($mode));
95
96
		$form_key = 'phpbb/mediaembed';
97
		add_form_key($form_key);
98
99
		if ($this->request->is_set_post('action_purge_cache'))
100
		{
101
			$this->purge_mediaembed_cache();
102
		}
103
104
		if ($this->request->is_set_post('submit'))
105
		{
106
			if (!check_form_key($form_key))
107
			{
108
				trigger_error('FORM_INVALID', E_USER_WARNING);
109
			}
110
111
			$this->{'save_' . $mode}();
112
		}
113
114
		$this->{'display_' . $mode}();
115
	}
116
117
	/**
118
	 * Add settings template vars to the form
119
	 */
120
	protected function display_settings()
121
	{
122
		$this->template->assign_vars([
123
			'S_MEDIA_EMBED_BBCODE'		=> $this->config['media_embed_bbcode'],
124
			'S_MEDIA_EMBED_ALLOW_SIG'	=> $this->config['media_embed_allow_sig'],
125
			'S_MEDIA_EMBED_PARSE_URLS'	=> $this->config['media_embed_parse_urls'],
126
			'S_MEDIA_EMBED_ENABLE_CACHE'=> $this->config['media_embed_enable_cache'],
127
			'U_ACTION'					=> $this->u_action,
128
		]);
129
	}
130
131
	/**
132
	 * Add manage sites template vars to the form
133
	 *
134
	 * @throws \Exception
135
	 */
136
	protected function display_manage()
137
	{
138
		$this->template->assign_vars([
139
			'MEDIA_SITES'	=> $this->get_sites(),
140
			'U_ACTION'		=> $this->u_action,
141
			'ERRORS'		=> $this->errors,
142
		]);
143
	}
144
145
	/**
146
	 * Get a list of available sites
147
	 *
148
	 * @return array An array of available sites
149
	 * @throws \Exception
150
	 */
151
	protected function get_sites()
152
	{
153
		$sites = [];
154
155
		$configurator = $this->container->get('text_formatter.s9e.factory')->get_configurator();
156
		foreach ($configurator->MediaEmbed->defaultSites as $siteId => $siteConfig)
157
		{
158
			$disabled = isset($configurator->BBCodes[$siteId]);
159
			$sites[$siteId] = [
160
				'id'		=> $siteId,
161
				'name'		=> $siteConfig['name'],
162
				'title'		=> $this->language->lang($disabled ? 'ACP_MEDIA_SITE_DISABLED' : 'ACP_MEDIA_SITE_TITLE', $siteId),
163
				'enabled'	=> in_array($siteId, $this->get_enabled_sites()),
164
				'disabled'	=> $disabled,
165
			];
166
		}
167
168
		ksort($sites);
169
170
		$this->errors = array_diff($this->get_enabled_sites(), array_keys($sites));
171
172
		return $sites;
173
	}
174
175
	/**
176
	 * Get enabled media sites stored in the database
177
	 *
178
	 * @return array An array of enabled sites
179
	 */
180
	protected function get_enabled_sites()
181
	{
182
		if ($this->enabled_sites === null)
183
		{
184
			$sites = json_decode($this->config_text->get('media_embed_sites'), true);
185
			$this->enabled_sites = is_array($sites) ? $sites : [];
186
		}
187
188
		return $this->enabled_sites;
189
	}
190
191
	/**
192
	 * Save site managed data to the database
193
	 */
194
	protected function save_manage()
195
	{
196
		$this->config_text->set('media_embed_sites', json_encode($this->request->variable('mark', [''])));
197
198
		$this->media_cache->purge_textformatter_cache();
199
200
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_MANAGE');
201
202
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
203
	}
204
205
	/**
206
	 * Save settings data to the database
207
	 */
208
	protected function save_settings()
209
	{
210
		$this->config->set('media_embed_bbcode', $this->request->variable('media_embed_bbcode', 0));
211
		$this->config->set('media_embed_allow_sig', $this->request->variable('media_embed_allow_sig', 0));
212
		$this->config->set('media_embed_parse_urls', $this->request->variable('media_embed_parse_urls', 0));
213
		$this->config->set('media_embed_enable_cache', $this->request->variable('media_embed_enable_cache', 0));
214
215
		$this->media_cache->purge_textformatter_cache();
216
217
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_SETTINGS');
218
219
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
220
	}
221
222
	/**
223
	 * Purge all MediaEmbed cache files
224
	 */
225
	protected function purge_mediaembed_cache()
226
	{
227
		$this->media_cache->purge_mediaembed_cache();
228
229
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_CACHE_PURGED');
230
231
		trigger_error($this->language->lang('PURGE_CACHE_SUCCESS') . adm_back_link($this->u_action));
232
	}
233
}
234