Completed
Push — master ( 08ac9c...63e3ab )
by Michael
13s
created

main_module::get_sites()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 3
eloc 11
nc 3
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\cache\driver\driver_interface $cache */
19
	protected $cache;
20
21
	/** @var \phpbb\config\config $config */
22
	protected $config;
23
24
	/** @var \phpbb\config\db_text $config_text */
25
	protected $config_text;
26
27
	/** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
28
	protected $container;
29
30
	/** @var \phpbb\request\request $request */
31
	protected $request;
32
33
	/** @var \phpbb\template\template $template */
34
	protected $template;
35
36
	/** @var \phpbb\language\language $language */
37
	protected $language;
38
39
	/** @var \phpbb\log\log $log */
40
	protected $log;
41
42
	/** @var \phpbb\user */
43
	protected $user;
44
45
	/** @var array $enabled_sites */
46
	protected $enabled_sites;
47
48
	/** @var string $form_key */
49
	protected $form_key;
50
51
	/** @var string $page_title */
52
	public $page_title;
53
54
	/** @var string $tpl_name */
55
	public $tpl_name;
56
57
	/** @var string $u_action */
58
	public $u_action;
59
60
	/**
61
	 * Constructor
62
	 */
63
	public function __construct()
64
	{
65
		global $phpbb_container;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
66
67
		$this->container   = $phpbb_container;
68
		$this->cache       = $this->container->get('cache');
69
		$this->config      = $this->container->get('config');
70
		$this->config_text = $this->container->get('config_text');
71
		$this->language    = $this->container->get('language');
72
		$this->log         = $this->container->get('log');
73
		$this->request     = $this->container->get('request');
74
		$this->template    = $this->container->get('template');
75
		$this->user        = $this->container->get('user');
76
		$this->form_key    = 'phpbb/mediaembed';
77
78
		$this->language->add_lang('acp', 'phpbb/mediaembed');
79
	}
80
81
	/**
82
	 * Main ACP module
83
	 *
84
	 * @param int    $id   The module ID
85
	 * @param string $mode The module mode
86
	 */
87
	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...
88
	{
89
		add_form_key($this->form_key);
90
91
		if ($this->request->is_set_post('submit'))
92
		{
93
			$this->{'save_' . strtolower($mode)}();
94
		}
95
96
		switch ($mode)
97
		{
98
			case 'manage':
99
				$this->display($mode, ['MEDIA_SITES' => $this->get_sites()]);
100
			break;
101
102
			case 'settings':
103
				$this->display($mode, [
104
					'S_MEDIA_EMBED_BBCODE'		=> $this->config['media_embed_bbcode'],
105
					'S_MEDIA_EMBED_ALLOW_SIG'	=> $this->config['media_embed_allow_sig'],
106
				]);
107
			break;
108
		}
109
	}
110
111
	/**
112
	 * Display data in the ACP module
113
	 *
114
	 * @param string $mode The ACP module mode (manage|settings)
115
	 * @param array  $data Array of data to assign to the template
116
	 */
117
	protected function display($mode, $data)
118
	{
119
		$this->tpl_name   = 'acp_phpbb_mediaembed_' . strtolower($mode);
120
		$this->page_title = $this->language->lang('ACP_MEDIA_' . strtoupper($mode));
121
122
		$this->template->assign_vars(array_merge($data, [
123
			'U_ACTION'	=> $this->u_action,
124
		]));
125
	}
126
127
	/**
128
	 * Get a list of available sites
129
	 *
130
	 * @return array An array of available sites
131
	 */
132
	protected function get_sites()
133
	{
134
		$sites = [];
135
136
		$configurator = $this->container->get('text_formatter.s9e.factory')->get_configurator();
137
		foreach ($configurator->MediaEmbed->defaultSites as $siteId => $siteConfig)
138
		{
139
			if (isset($configurator->BBCodes[$siteId]))
140
			{
141
				continue;
142
			}
143
144
			$sites[] = [
145
				'id'		=> $siteId,
146
				'name'		=> $siteConfig['name'],
147
				'enabled'	=> in_array($siteId, $this->get_enabled_sites()),
148
			];
149
		}
150
151
		return $sites;
152
	}
153
154
	/**
155
	 * Get enabled media sites stored in the database
156
	 *
157
	 * @return array An array of enabled sites
158
	 */
159
	protected function get_enabled_sites()
160
	{
161
		if ($this->enabled_sites === null)
162
		{
163
			$sites = json_decode($this->config_text->get('media_embed_sites'), true);
164
			$this->enabled_sites = is_array($sites) ? $sites : [];
165
		}
166
167
		return $this->enabled_sites;
168
	}
169
170
	/**
171
	 * Save site managed data to the database
172
	 */
173
	protected function save_manage()
174
	{
175
		$this->check_form_key();
176
177
		$this->config_text->set('media_embed_sites', json_encode($this->request->variable('mark', [''])));
178
179
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.parser.key'));
180
		$this->cache->destroy($this->container->getParameter('text_formatter.cache.renderer.key'));
181
182
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_MANAGE');
183
184
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
185
	}
186
187
	/**
188
	 * Save settings data to the database
189
	 */
190
	protected function save_settings()
191
	{
192
		$this->check_form_key();
193
194
		$this->config->set('media_embed_bbcode', $this->request->variable('media_embed_bbcode', 0));
195
		$this->config->set('media_embed_allow_sig', $this->request->variable('media_embed_allow_sig', 0));
196
197
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PHPBB_MEDIA_EMBED_SETTINGS');
198
199
		trigger_error($this->language->lang('CONFIG_UPDATED') . adm_back_link($this->u_action));
200
	}
201
202
	/**
203
	 * Check the form key, trigger error if invalid
204
	 */
205
	protected function check_form_key()
206
	{
207
		if (!check_form_key($this->form_key))
208
		{
209
			trigger_error('FORM_INVALID');
210
		}
211
	}
212
}
213