Completed
Push — master ( 4d9095...29987d )
by Matt
22:26
created

display::get_theme()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 3
1
<?php
2
/**
3
 *
4
 * Topic Preview
5
 *
6
 * @copyright (c) 2013 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\topicpreview\core;
12
13
use phpbb\config\config;
14
use phpbb\event\dispatcher_interface;
15
use phpbb\language\language;
16
use phpbb\template\template;
17
use phpbb\user;
18
use vse\topicpreview\core\trim\trim;
19
20
class display extends base
21
{
22
	/** @var int default width of topic preview */
23
	const PREVIEW_SIZE = 360;
24
25
	/** @var int default height and width of topic preview avatars */
26
	const AVATAR_SIZE = 60;
27
28
	/** @var dispatcher_interface */
29
	protected $dispatcher;
30
31
	/** @var language */
32
	protected $language;
33
34
	/** @var template */
35
	protected $template;
36
37
	/** @var string phpBB root path */
38
	protected $root_path;
39
40
	/** @var trim */
41
	protected $trim;
42
43
	/**
44
	 * Constructor
45
	 *
46
	 * @param config               $config     Config object
47
	 * @param dispatcher_interface $dispatcher Event dispatcher object
48
	 * @param language             $language   Language object
49
	 * @param template             $template   Template object
50
	 * @param trim                 $trim       Trim text object
51
	 * @param user                 $user       User object
52
	 * @param string               $root_path
53
	 */
54 3
	public function __construct(config $config, dispatcher_interface $dispatcher, language $language, template $template, trim $trim, user $user, $root_path)
55
	{
56 3
		$this->dispatcher = $dispatcher;
57 3
		$this->language = $language;
58 3
		$this->template = $template;
59 3
		$this->trim = $trim;
60 3
		$this->root_path = $root_path;
61 3
		parent::__construct($config, $user);
62
63 3
		$this->setup();
64 3
	}
65
66
	/**
67
	 * Set up some common components
68
	 */
69 3
	public function setup()
70
	{
71
		// Load our language file (only needed if showing last post text)
72 3
		if ($this->last_post_enabled())
73 3
		{
74
			$this->language->add_lang('topic_preview', 'vse/topicpreview');
75
		}
76
77 3
		$this->template->assign_vars(array(
78 3
			'S_TOPICPREVIEW'		=> $this->is_enabled(),
79 3
			'TOPICPREVIEW_THEME'	=> $this->get_theme(),
80 3
			'TOPICPREVIEW_DELAY'	=> $this->config['topic_preview_delay'],
81 3
			'TOPICPREVIEW_DRIFT'	=> $this->config['topic_preview_drift'],
82 3
			'TOPICPREVIEW_WIDTH'	=> !empty($this->config['topic_preview_width']) ? $this->config['topic_preview_width'] : self::PREVIEW_SIZE,
83 3
		));
84 3
	}
85
86
	/**
87
	 * Inject topic preview text into the template
88
	 *
89
	 * @param array $row   Row data
90
	 * @param array $block Template vars array
91
	 *
92
	 * @return array Template vars array
93
	 */
94
	public function display_topic_preview($row, $block)
95
	{
96
		if (!$this->is_enabled())
97
		{
98
			return $block;
99
		}
100
101
		$block = array_merge($block, array(
102
			'TOPIC_PREVIEW_FIRST_POST'		=> $this->get_text_helper($row, 'first_post_text'),
103
			'TOPIC_PREVIEW_LAST_POST'		=> $this->get_text_helper($row, 'last_post_text'),
104
			'TOPIC_PREVIEW_FIRST_AVATAR'	=> $this->get_user_avatar_helper($row, 'fp'),
105
			'TOPIC_PREVIEW_LAST_AVATAR'		=> $this->get_user_avatar_helper($row, 'lp'),
106
		));
107
108
		$tp_avatars = $this->avatars_enabled();
109
110
		/**
111
		 * EVENT to modify the topic preview display output before it gets inserted in the template block
112
		 *
113
		 * @event vse.topicpreview.display_topic_preview
114
		 * @var array row        Topic row data
115
		 * @var array block      Template vars array
116
		 * @var bool  tp_avatars Display avatars setting
117
		 * @since 2.1.0
118
		 */
119
		$vars = array('row', 'block', 'tp_avatars');
120
		extract($this->dispatcher->trigger_event('vse.topicpreview.display_topic_preview', compact($vars)));
0 ignored issues
show
Bug introduced by
$this->dispatcher->trigg...eview', compact($vars)) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
121
122
		return $block;
123
	}
124
125
	/**
126
	 * Get topic preview text helper function
127
	 * This handles the trimming and censoring
128
	 *
129
	 * @param array  $row  User row data
130
	 * @param string $post The first or last post text column key
131
	 *
132
	 * @return string The trimmed and censored topic preview text
133
	 */
134
	protected function get_text_helper($row, $post)
135
	{
136
		// Ignore empty/unset text or when the last post is also the first (and only) post
137
		if (empty($row[$post]) || ($post === 'last_post_text' && $row['topic_first_post_id'] === $row['topic_last_post_id']))
138
		{
139
			return '';
140
		}
141
142
		return censor_text($this->trim->trim_text($row[$post], $this->config['topic_preview_limit']));
143
	}
144
145
	/**
146
	 * Get user avatar helper function
147
	 *
148
	 * @param array  $row    User row data
149
	 * @param string $poster Type of poster, fp or lp
150
	 *
151
	 * @return string Avatar image
152
	 */
153
	protected function get_user_avatar_helper($row, $poster)
154
	{
155
		if (!$this->avatars_enabled())
156
		{
157
			return '';
158
		}
159
160
		$avatar = '';
161
		if (!empty($row[$poster . '_avatar']))
162
		{
163
			$map = array(
164
				'avatar'		=> $row[$poster . '_avatar'],
165
				'avatar_type'	=> $row[$poster . '_avatar_type'],
166
				'avatar_width'	=> $row[$poster . '_avatar_width'],
167
				'avatar_height'	=> $row[$poster . '_avatar_height'],
168
			);
169
			$avatar = phpbb_get_user_avatar($map, 'USER_AVATAR', false, true);
170
		}
171
172
		// If avatar string is empty, fall back to no_avatar.gif
173
		return $avatar ?: '<img class="avatar" src="' . $this->root_path . 'styles/' . rawurlencode($this->user->style['style_path']) . '/theme/images/no_avatar.gif" width="' . self::AVATAR_SIZE . '" height="' . self::AVATAR_SIZE . '" alt="' . $this->language->lang('USER_AVATAR') . '" />';
174
	}
175
176
	/**
177
	 * Get user's style topic preview theme
178
	 * Fall back to no theme if expected theme not found
179
	 *
180
	 * @return mixed Theme name if theme file found, false otherwise
181
	 */
182 3
	protected function get_theme()
183
	{
184 3
		if (!empty($this->user->style['topic_preview_theme']) && file_exists($this->root_path . 'ext/vse/topicpreview/styles/all/theme/' . $this->user->style['topic_preview_theme'] . '.css'))
185 3
		{
186 1
			return $this->user->style['topic_preview_theme'];
187
		}
188
189 2
		return false;
190
	}
191
}
192