Passed
Push — develop ( 968c4b...624076 )
by Daniel
19:04
created

services/navbar.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 *
5
 * @package sitemaker
6
 * @copyright (c) 2020 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\sitemaker\services;
12
13
class navbar
14
{
15
	/** @var \phpbb\config\config */
16
	protected $config;
17
18
	/** @var \phpbb\config\db_text */
19
	protected $config_text;
20
21
	/** @var \phpbb\db\driver\driver_interface */
22
	protected $db;
23
24
	/** @var \phpbb\request\request_interface */
25
	protected $request;
26
27
	/**
28
	 * Constructor
29
	 *
30
	 * @param \phpbb\config\config					$config			Config object
31
	 * @param \phpbb\config\db_text					$config_text	Config text object
32
	 * @param \phpbb\db\driver\driver_interface		$db	 			Database connection
33
	 * @param \phpbb\request\request_interface		$request		Request object
34
	 */
35
	public function __construct(\phpbb\config\config $config, \phpbb\config\db_text $config_text, \phpbb\db\driver\driver_interface $db, \phpbb\request\request_interface $request)
36
	{
37
		$this->config = $config;
38
		$this->config_text = $config_text;
39
		$this->db = $db;
40
		$this->request = $request;
41
	}
42
43
	/**
44
	 * @param string $style
45
	 * @return array
46
	 */
47
	public function get_settings($style)
48
	{
49
		return [
50
			'location'	=> &$this->get_locations()[$style],
51
			'modified'	=> $this->get_last_modified(),
52
		];
53
	}
54
55
	/**
56
	 * @return int
57
	 */
58
	public function get_last_modified()
59
	{
60
		return (int) $this->config['sm_navbar_last_modified'];
61
	}
62
63
	/**
64
	 * @param string $style
65
	 * @return string
66
	 */
67
	public function get_css($style)
68
	{
69
		$board_url = generate_board_url();
70
71
		$css = "@import url('{$board_url}/ext/blitze/sitemaker/styles/all/theme/assets/navbar.min.css');";
72
		$css .= html_entity_decode($this->config_text->get('sm_navbar_' . $style));
0 ignored issues
show
It seems like $this->config_text->get('sm_navbar_' . $style) can also be of type null; however, parameter $string of html_entity_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
		$css .= html_entity_decode(/** @scrutinizer ignore-type */ $this->config_text->get('sm_navbar_' . $style));
Loading history...
73
74
		return $css;
75
	}
76
77
	/**
78
	 * @param string $style
79
	 * @param string $time
80
	 * @return void
81
	 */
82
	public function save($style, $time = 'now')
83
	{
84
		$location = $this->request->variable('location', '');
85
		$css = $this->request->variable('css', '');
86
87
		$this->save_css($style, $css);
88
		$this->save_locations([$style => $location]);
89
90
		$this->config->set('sm_navbar_last_modified', strtotime($time));
91
	}
92
93
	/**
94
	 * @param string $style
95
	 * @param string $css
96
	 * @return void
97
	 */
98
	protected function save_css($style, $css)
99
	{
100
		if ($css)
101
		{
102
			$this->config_text->set('sm_navbar_' . $style, $css);
103
		}
104
		else
105
		{
106
			$this->config_text->delete('sm_navbar_' . $style);
107
		}
108
	}
109
110
	/**
111
	 * @param string $style
112
	 * @return string
113
	 */
114
	protected function save_locations(array $locations)
115
	{
116
		$locations =  array_merge($this->get_locations(), $locations);
117
118
		$this->config->set('sm_navbar_locations', json_encode(array_filter($locations)));
119
	}
120
121
	/**
122
	 * @param bool $all
123
	 * @return void
124
	 */
125
	public function cleanup($all = false)
126
	{
127
		$ids = $this->request->variable('ids', [0]);
128
129
		if ($all || sizeof($ids))
130
		{
131
			$sql = 'SELECT style_name FROM ' . STYLES_TABLE . (sizeof($ids) ? ' WHERE ' . $this->db->sql_in_set('style_id', $ids) : '');
132
			$result = $this->db->sql_query($sql);
133
134
			$locations = [];
135
			while ($row = $this->db->sql_fetchrow($result))
136
			{
137
				$style = strtolower($row['style_name']);
138
				$this->config_text->delete('sm_navbar_' . $style);
139
				$locations[$style] = '';
140
			}
141
			$this->db->sql_freeresult($result);
142
143
			$this->save_locations($locations);
144
		}
145
	}
146
147
	/**
148
	 * @return array
149
	 */
150
	protected function get_locations()
151
	{
152
		return (array) json_decode($this->config['sm_navbar_locations'], true);
153
	}
154
}
155