Completed
Push — develop ( 7a7d04...968c4b )
by Daniel
20:22
created

navbar::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
rs 10
c 1
b 0
f 0
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
		if (!$menu_id = $this->config['sm_navbar_menu'])
50
		{
51
			return [
52
				'menu_id'	=> 0,
53
				'location'	=> '',
54
				'last_modified'	=> 0
55
			];
56
		}
57
58
		$locations = $this->get_locations();
59
60
		return [
61
			'menu_id'	=> $menu_id,
62
			'location'	=> &$locations[$style],
63
			'last_modified'	=> $this->get_last_modified(),
64
		];
65
	}
66
67
	/**
68
	 * @return int
69
	 */
70
	public function get_last_modified()
71
	{
72
		return (int) $this->config['sm_navbar_last_modified'];
73
	}
74
75
	/**
76
	 * @param string $style
77
	 * @return string
78
	 */
79
	public function get_css($style)
80
	{
81
		$board_url = generate_board_url();
82
83
		$css = "@import url('{$board_url}/ext/blitze/sitemaker/styles/all/theme/assets/navbar.min.css');";
84
		$css .= html_entity_decode($this->config_text->get('sm_navbar_' . $style));
85
86
		return $css;
87
	}
88
89
	/**
90
	 * @param string $style
91
	 * @param string $time
92
	 * @return void
93
	 */
94
	public function save($style, $time = 'now')
95
	{
96
		$location = $this->request->variable('location', '');
97
		$css = $this->request->variable('css', '');
98
99
		$this->save_css($style, $css);
100
		$this->save_locations([$style => $location]);
101
102
		$this->config->set('sm_navbar_last_modified', strtotime($time));
103
	}
104
105
	/**
106
	 * @param string $style
107
	 * @param string $css
108
	 * @return void
109
	 */
110
	protected function save_css($style, $css)
111
	{
112
		if ($css)
113
		{
114
			$this->config_text->set('sm_navbar_' . $style, $css);
115
		}
116
		else
117
		{
118
			$this->config_text->delete('sm_navbar_' . $style);
119
		}
120
	}
121
122
	/**
123
	 * @param string $style
124
	 * @return string
125
	 */
126
	protected function save_locations(array $locations)
127
	{
128
		$locations =  array_merge($this->get_locations(), $locations);
129
130
		$this->config->set('sm_navbar_locations', json_encode(array_filter($locations)));
131
	}
132
133
	/**
134
	 * @param bool $all
135
	 * @return void
136
	 */
137
	public function cleanup($all = false)
138
	{
139
		$ids = $this->request->variable('ids', [0]);
140
141
		if ($all || sizeof($ids))
142
		{
143
			$sql = 'SELECT style_name FROM ' . STYLES_TABLE . (sizeof($ids) ? ' WHERE ' . $this->db->sql_in_set('style_id', $ids) : '');
144
			$result = $this->db->sql_query($sql);
145
146
			$locations = [];
147
			while ($row = $this->db->sql_fetchrow($result))
148
			{
149
				$style = strtolower($row['style_name']);
150
				$this->config_text->delete('sm_navbar_' . $style);
151
				$locations[$style] = '';
152
			}
153
			$this->db->sql_freeresult($result);
154
155
			$this->save_locations($locations);
156
		}
157
	}
158
159
	/**
160
	 * @return array
161
	 */
162
	protected function get_locations()
163
	{
164
		return (array) json_decode($this->config['sm_navbar_locations'], true);
165
	}
166
}
167