Completed
Push — develop ( 7acac6...1f96ef )
by Daniel
10:51
created

util::get_theme_path()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
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->scripts = array(
48 94
			'js'	=> array(),
49 94
			'css'   => array(),
50
		);
51 94
	}
52
53
	/**
54
	 * include css/javascript
55
	 * receives an array of form: array('js' => array('test.js', 'test2.js'), 'css' => array())
56
	 * @param array $scripts
57
	 */
58 2
	public function add_assets(array $scripts)
59
	{
60 2
		foreach ($scripts as $type => $paths)
61
		{
62 1
			$count = (isset($this->scripts[$type])) ? sizeof($this->scripts[$type]) : 0;
63 1
			foreach ($paths as $key => $script)
64
			{
65 1
				$this->scripts[$type][$count++] = $script;
66 1
			}
67 2
		}
68 2
	}
69
70
	/**
71
	 * Pass assets to template
72
	 */
73 2
	public function set_assets()
74
	{
75 2
		$this->_prep_scripts();
76 2
		foreach ($this->scripts as $type => $scripts)
77
		{
78 1
			foreach ($scripts as $file)
79
			{
80 1
				$this->template->assign_block_vars($type, array('UA_FILE' => trim($file)));
81 1
			}
82 2
		}
83
84 2
		$this->scripts = array();
85 2
	}
86
87
	/**
88
	 * Add a secret token to the form (requires the S_FORM_TOKEN template variable)
89
	 * @param string  $form_name The name of the form; has to match the name used in check_form_key, otherwise no restrictions apply
90
	 */
91 1
	public function get_form_key($form_name)
92
	{
93 1
		add_form_key($form_name);
94
95 1
		$rootref = $this->template_context->get_root_ref();
96 1
		$s_form_token = $rootref['S_FORM_TOKEN'];
97
98 1
		return $s_form_token;
99
	}
100
101
	/**
102
	 * @return string
103
	 */
104 14
	public function get_default_avatar()
105
	{
106 14
		return '<img src="' . $this->get_theme_path() . '/images/no_avatar.gif" alt="" />';
107
	}
108
109
	/**
110
	 * Returns a corrected theme path depending on whether or not we are accessing a controller
111
	 * @return string
112
	 */
113 14
	public function get_theme_path()
114
	{
115 14
		$web_path = $this->get_web_path();
116 14
		return "{$web_path}styles/" . rawurlencode($this->user->style['style_path']) . '/theme';
117
	}
118
119
	/**
120
	 * Returns a corrected root path depending on whether or not we are accessing a controller
121
	 * @return string
122
	 */
123 14
	public function get_web_path()
124
	{
125
		// Determine board url - we may need it later
126 14
		$board_url = generate_board_url() . '/';
127
128 14
		$corrected_path = $this->path_helper->get_web_root_path();
129 14
		return (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? $board_url : $corrected_path;
130
	}
131
132
	/**
133
	 *
134
	 */
135 2
	protected function _prep_scripts()
136
	{
137 2
		if (isset($this->scripts['js']))
138 2
		{
139 2
			ksort($this->scripts['js']);
140 2
			$this->scripts['js'] = array_filter(array_unique($this->scripts['js']));
141 2
		}
142
143 2
		if (isset($this->scripts['css']))
144 2
		{
145 2
			ksort($this->scripts['css']);
146 2
			$this->scripts['css'] = array_filter(array_unique($this->scripts['css']));
147 2
		}
148
149 2
		$this->scripts = array_filter($this->scripts);
150 2
	}
151
}
152