1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package sitemaker |
5
|
|
|
* @copyright (c) 2013 Daniel A. (blitze) |
6
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace blitze\sitemaker\services; |
11
|
|
|
|
12
|
|
|
class util |
13
|
|
|
{ |
14
|
|
|
/** @var \phpbb\path_helper */ |
15
|
|
|
protected $path_helper; |
16
|
|
|
|
17
|
|
|
/** @var \phpbb\template\template */ |
18
|
|
|
protected $template; |
19
|
|
|
|
20
|
|
|
/** @var \phpbb\template\context */ |
21
|
|
|
protected $template_context; |
22
|
|
|
|
23
|
|
|
/** @var \phpbb\user */ |
24
|
|
|
protected $user; |
25
|
|
|
|
26
|
|
|
/** string */ |
27
|
|
|
protected $web_path; |
28
|
|
|
|
29
|
|
|
/** array */ |
30
|
|
|
protected $scripts; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor |
34
|
|
|
* |
35
|
|
|
* @param \phpbb\path_helper $path_helper Path helper object |
36
|
|
|
* @param \phpbb\template\template $template Template object |
37
|
|
|
* @param \phpbb\template\context $template_context Template context object |
38
|
|
|
* @param \phpbb\user $user User object |
39
|
|
|
*/ |
40
|
94 |
|
public function __construct(\phpbb\path_helper $path_helper, \phpbb\template\template $template, \phpbb\template\context $template_context, \phpbb\user $user) |
41
|
|
|
{ |
42
|
94 |
|
$this->path_helper = $path_helper; |
43
|
94 |
|
$this->template = $template; |
44
|
94 |
|
$this->template_context = $template_context; |
45
|
94 |
|
$this->user = $user; |
46
|
|
|
|
47
|
94 |
|
$this->set_corrected_paths(); |
48
|
94 |
|
$this->scripts = array( |
49
|
94 |
|
'js' => array(), |
50
|
94 |
|
'css' => array(), |
51
|
|
|
); |
52
|
94 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* include css/javascript |
56
|
|
|
* receives an array of form: array('js' => array('test.js', 'test2.js'), 'css' => array()) |
57
|
|
|
* @param array $scripts |
58
|
|
|
*/ |
59
|
2 |
|
public function add_assets(array $scripts) |
60
|
|
|
{ |
61
|
2 |
|
foreach ($scripts as $type => $paths) |
62
|
|
|
{ |
63
|
1 |
|
$count = (isset($this->scripts[$type])) ? sizeof($this->scripts[$type]) : 0; |
64
|
1 |
|
foreach ($paths as $key => $script) |
65
|
|
|
{ |
66
|
1 |
|
$this->scripts[$type][$count++] = $script; |
67
|
1 |
|
} |
68
|
2 |
|
} |
69
|
2 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Pass assets to template |
73
|
|
|
*/ |
74
|
2 |
|
public function set_assets() |
75
|
|
|
{ |
76
|
2 |
|
$this->_prep_scripts(); |
77
|
2 |
|
foreach ($this->scripts as $type => $scripts) |
78
|
|
|
{ |
79
|
1 |
|
foreach ($scripts as $file) |
80
|
|
|
{ |
81
|
1 |
|
$this->template->assign_block_vars($type, array('UA_FILE' => trim($file))); |
82
|
1 |
|
} |
83
|
2 |
|
} |
84
|
|
|
|
85
|
2 |
|
$this->scripts = array(); |
86
|
2 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Add a secret token to the form (requires the S_FORM_TOKEN template variable) |
90
|
|
|
* @param string $form_name The name of the form; has to match the name used in check_form_key, otherwise no restrictions apply |
91
|
|
|
*/ |
92
|
1 |
|
public function get_form_key($form_name) |
93
|
|
|
{ |
94
|
1 |
|
add_form_key($form_name); |
95
|
|
|
|
96
|
1 |
|
$rootref = $this->template_context->get_root_ref(); |
97
|
1 |
|
$s_form_token = $rootref['S_FORM_TOKEN']; |
98
|
|
|
|
99
|
1 |
|
return $s_form_token; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function get_default_avatar() |
106
|
13 |
|
{ |
107
|
|
|
return '<img src="' . $this->get_theme_path() . '/images/no_avatar.gif" alt="" />'; |
108
|
13 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns a corrected theme path depending on whether or not we are accessing a controller |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function get_theme_path() |
115
|
|
|
{ |
116
|
|
|
return "{$this->web_path}styles/" . rawurlencode($this->user->style['style_path']) . '/theme'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
2 |
|
* Returns a corrected root path depending on whether or not we are accessing a controller |
121
|
|
|
* @return string |
122
|
2 |
|
*/ |
123
|
2 |
|
public function get_web_path() |
124
|
2 |
|
{ |
125
|
2 |
|
return $this->web_path; |
126
|
2 |
|
} |
127
|
|
|
|
128
|
2 |
|
protected function _prep_scripts() |
129
|
2 |
|
{ |
130
|
2 |
|
if (isset($this->scripts['js'])) |
131
|
2 |
|
{ |
132
|
2 |
|
ksort($this->scripts['js']); |
133
|
|
|
$this->scripts['js'] = array_filter(array_unique($this->scripts['js'])); |
134
|
2 |
|
} |
135
|
2 |
|
|
136
|
|
|
if (isset($this->scripts['css'])) |
137
|
|
|
{ |
138
|
|
|
ksort($this->scripts['css']); |
139
|
|
|
$this->scripts['css'] = array_filter(array_unique($this->scripts['css'])); |
140
|
94 |
|
} |
141
|
|
|
|
142
|
|
|
$this->scripts = array_filter($this->scripts); |
143
|
94 |
|
} |
144
|
|
|
|
145
|
94 |
|
/** |
146
|
94 |
|
* Sets the correct paths to use when we are on a controller or not |
147
|
94 |
|
*/ |
148
|
|
|
protected function set_corrected_paths() |
149
|
|
|
{ |
150
|
|
|
// Determine board url - we may need it later |
151
|
|
|
$board_url = generate_board_url() . '/'; |
152
|
|
|
|
153
|
|
|
$corrected_path = $this->path_helper->get_web_root_path(); |
154
|
|
|
$this->web_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? $board_url : $corrected_path; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|