Completed
Pull Request — master (#32)
by Matt
06:58
created

display::get_text_helper()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

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