Completed
Push — develop ( 56e698...1a7010 )
by Daniel
08:15
created

util::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
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\user */
21
	protected $user;
22
23
	/** string */
24
	protected $web_path;
25
26
	/** array */
27
	protected $assets;
28
29
	/**
30
	 * Constructor
31
	 *
32
	 * @param \phpbb\path_helper					$path_helper		Path helper object
33
	 * @param \phpbb\template\template				$template			Template object
34
	 * @param \phpbb\user							$user				User object
35
	 */
36 101
	public function __construct(\phpbb\path_helper $path_helper, \phpbb\template\template $template, \phpbb\user $user)
37
	{
38 101
		$this->path_helper = $path_helper;
39 101
		$this->template = $template;
40 101
		$this->user = $user;
41
42 101
		$this->reset();
43 101
	}
44
45
	/**
46
	 * include css/javascript
47
	 * @param array $assets array of form array('js' => array('test.js', 'test2.js'), 'css' => array())
48
	 * @return void
49
	 */
50 2
	public function add_assets(array $assets)
51
	{
52 2
		foreach ($assets as $type => $scripts)
53
		{
54 1
			foreach ($scripts as $order => $script)
55
			{
56 1
				if (!isset($this->assets[$type][$script]) || $this->assets[$type][$script] < $order)
57 1
				{
58 1
					$this->assets[$type][$script] = $order;
59 1
				}
60 1
			}
61 2
		}
62 2
	}
63
64
	/**
65
	 * Pass assets to template
66
	 */
67 2
	public function set_assets()
68
	{
69 2
		uasort($this->assets['js'], array($this, 'sort_assets'));
70 2
		uasort($this->assets['css'], array($this, 'sort_assets'));
71
72 2
		$this->template->assign_var('assets', array(
73 2
			'css'	=> array_keys($this->assets['css']),
74 2
			'js'	=> array_keys($this->assets['js']),
75 2
		));
76
77 2
		$this->reset();
78 2
	}
79
80
	/**
81
	 * Add a secret token to the form (requires the S_FORM_TOKEN template variable)
82
	 * @param string  $form_name The name of the form; has to match the name used in check_form_key, otherwise no restrictions apply
83
	 */
84 1
	public function get_form_key($form_name)
85
	{
86 1
		add_form_key($form_name);
87
88 1
		return $this->template->retrieve_var('S_FORM_TOKEN');
89
	}
90
91
	/**
92
	 * @return string
93
	 */
94 15
	public function get_default_avatar()
95
	{
96 15
		return '<img src="' . $this->get_theme_path() . '/images/no_avatar.gif" alt="" />';
97
	}
98
99
	/**
100
	 * Returns a corrected theme path depending on whether or not we are accessing a controller
101
	 * @return string
102
	 */
103 15
	public function get_theme_path()
104
	{
105 15
		$web_path = $this->get_web_path();
106 15
		return "{$web_path}styles/" . rawurlencode($this->user->style['style_path']) . '/theme';
107
	}
108
109
	/**
110
	 * Returns a corrected root path depending on whether or not we are accessing a controller
111
	 * @return string
112
	 */
113 15
	public function get_web_path()
114
	{
115
		// Determine board url - we may need it later
116 15
		$board_url = generate_board_url() . '/';
117
118 15
		$corrected_path = $this->path_helper->get_web_root_path();
119 15
		return (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? $board_url : $corrected_path;
120
	}
121
122
	/**
123
	 * @param int $a
124
	 * @param int $b
125
	 * @return int
126
	 */
127 1
	protected function sort_assets($a, $b)
128
	{
129 1
		if ($a == $b)
130 1
		{
131
			return 0;
132
		}
133
134 1
		return ($a < $b) ? -1 : 1;
135
	}
136
137
	/**
138
	 * @return void
139
	 */
140 101
	protected function reset()
141
	{
142 101
		$this->assets = array(
143 101
			'js'	=> array(),
144 101
			'css'   => array(),
145
		);
146 101
	}
147
}
148