navbar::get_last_modified()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\path_helper */
25
	protected $path_helper;
26
27
	/** @var \phpbb\request\request_interface */
28
	protected $request;
29
30
	/**
31
	 * Constructor
32
	 *
33
	 * @param \phpbb\config\config					$config			Config object
34
	 * @param \phpbb\config\db_text					$config_text	Config text object
35
	 * @param \phpbb\db\driver\driver_interface		$db	 			Database connection
36
	 * @param \phpbb\path_helper					$path_helper	Path helper object
37
	 * @param \phpbb\request\request_interface		$request		Request object
38
	 */
39
	public function __construct(\phpbb\config\config $config, \phpbb\config\db_text $config_text, \phpbb\db\driver\driver_interface $db, \phpbb\path_helper $path_helper, \phpbb\request\request_interface $request)
40
	{
41
		$this->config = $config;
42
		$this->config_text = $config_text;
43
		$this->db = $db;
44
		$this->path_helper = $path_helper;
45
		$this->request = $request;
46
	}
47
48
	/**
49
	 * @param string $style
50
	 * @return array
51
	 */
52
	public function get_settings($style)
53
	{
54
		return [
55
			'location'	=> &$this->get_locations()[$style],
56
			'modified'	=> $this->get_last_modified(),
57
		];
58
	}
59
60
	/**
61
	 * @return int
62
	 */
63
	public function get_last_modified()
64
	{
65
		return (int) $this->config['sm_navbar_last_modified'];
66
	}
67
68
	/**
69
	 * @param string $style
70
	 * @return string
71
	 */
72
	public function get_css($style)
73
	{
74
		$web_root_path = $this->path_helper->get_web_root_path();
75
76
		$css = "@import url('{$web_root_path}/ext/blitze/sitemaker/styles/all/theme/assets/navbar.min.css');";
77
		$css .= html_entity_decode((string) $this->config_text->get('sm_navbar_' . $style));
78
79
		return $css;
80
	}
81
82
	/**
83
	 * @param string $style
84
	 * @param string $time
85
	 * @return void
86
	 */
87
	public function save($style, $time = 'now')
88
	{
89
		$menu = $this->request->variable('menu', 0);
90
91
		$css = $location = '';
92
		if ($menu)
93
		{
94
			$location = $this->request->variable('location', '');
95
			$css = $this->request->variable('css', '');
96
		}
97
98
		$this->save_css($style, $css);
99
		$this->save_locations([$style => $location]);
100
101
		$this->config->set('sm_navbar_last_modified', strtotime($time));
102
		$this->config->set('sm_navbar_menu', $menu);
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