Passed
Push — develop ( 9d1625...a2d90c )
by Daniel
18:14
created

recent_topics::get_topics_template_data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 19
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 29
rs 9.6333
1
<?php
2
3
/**
4
 *
5
 * @package sitemaker
6
 * @copyright (c) 2021 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\sitemaker\blocks;
12
13
use blitze\sitemaker\services\blocks\driver\block;
14
15
/**
16
 * Recent Topics Block
17
 */
18
class recent_topics extends forum_topics
19
{
20
	/** @var \phpbb\cache\service */
21
	protected $cache;
22
23
	/** @var \phpbb\request\request_interface */
24
	protected $request;
25
26
	/** @var \phpbb\pagination */
27
	protected $pagination;
28
29
	/** @var \phpbb\template\template */
30
	protected $template;
31
32
	/** @var int */
33
	protected $start = 0;
34
35
	/** @var int */
36
	protected $total_topics = 0;
37
38
	/** @var string */
39
	protected $param;
40
41
	/** @var string */
42
	protected $block;
43
44
	/** @var array */
45
	protected $topic_type_class = [
46
		POST_GLOBAL		=> ' global-announce',
47
		POST_ANNOUNCE	=> ' announce',
48
		POST_STICKY		=> ' sticky',
49
		POST_NORMAL		=> '',
50
	];
51
52
	/**
53
	 * Constructor
54
	 *
55
	 * @param \phpbb\auth\auth							$auth				Permission object
56
	 * @param \phpbb\content_visibility					$content_visibility	Content visibility object
57
	 * @param \phpbb\language\language					$translator			Language object
58
	 * @param \phpbb\user								$user				User object
59
	 * @param \Urodoz\Truncate\TruncateService			$truncator			Truncator service
60
	 * @param \blitze\sitemaker\services\date_range		$date_range			Date Range Object
61
	 * @param \blitze\sitemaker\services\forum\data		$forum_data			Forum Data object
62
	 * @param \blitze\sitemaker\services\forum\options	$forum_options		Forum Data object
63
	 * @param string									$phpbb_root_path	Path to the phpbb includes directory.
64
	 * @param string									$php_ext			php file extension
65
	 * @param \phpbb\cache\service						$cache				Cache Service object
66
	 * @param \phpbb\request\request_interface			$request			Request object
67
	 * @param \phpbb\pagination							$pagination			Pagination object
68
	 * @param \phpbb\template\template					$template			Template object
69
	 */
70
	public function __construct(
71
		\phpbb\auth\auth $auth,
72
		\phpbb\content_visibility $content_visibility,
73
		\phpbb\language\language $translator,
74
		\phpbb\user $user,
75
		\Urodoz\Truncate\TruncateService $truncator,
76
		\blitze\sitemaker\services\date_range $date_range,
77
		\blitze\sitemaker\services\forum\data $forum_data,
78
		\blitze\sitemaker\services\forum\options $forum_options,
79
		$phpbb_root_path,
80
		$php_ext,
81
		\phpbb\cache\service $cache,
82
		\phpbb\request\request_interface $request,
83
		\phpbb\pagination $pagination,
84
		\phpbb\template\template $template
85
	)
86
	{
87
		parent::__construct($auth, $content_visibility, $translator, $user, $truncator, $date_range, $forum_data, $forum_options, $phpbb_root_path, $php_ext);
88
89
		$this->cache = $cache;
90
		$this->request = $request;
91
		$this->pagination = $pagination;
92
		$this->template = $template;
93
	}
94
95
	/**
96
	 * {@inheritdoc}
97
	 */
98
	public function get_config(array $settings)
99
	{
100
		$orig_config = parent::get_config($settings);
101
		$config = $orig_config;
102
		$config['max_topics']['default'] = 5;
103
		$config['max_topics']['lang'] = 'TOPICS_PER_PAGE';
104
		unset($config['template'], $config['context']);
105
106
		$config['date_range']	= array('lang' => 'TOPICS_LOOK_BACK', 'validate' => 'int:0:255', 'type' => 'number:1:255', 'maxlength' => 3, 'default' => 60, 'append' => 'DAYS');
107
		$config['last_post']	= array('lang' => 'SHOW_LAST_POST', 'validate' => 'bool', 'type' => 'radio:yes_no', 'default' => true);
108
109
		$keys = array_keys($config);
110
		$keys[array_search('max_topics', $keys)] = 'per_page';
111
		$keys[array_search('date_range', $keys)] = 'look_back';
112
113
		return array_combine($keys, $config);
114
	}
115
116
	/**
117
	 * {@inheritdoc}
118
	 */
119
	public function display(array $bdata, $edit_mode = false)
120
	{
121
		$this->icons = $this->cache->obtain_icons();
0 ignored issues
show
Bug Best Practice introduced by
The property icons does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
122
		$this->param = 'pg' . $bdata['bid'];
123
		$this->block = 'bk-' . $bdata['bid'];
124
125
		$bdata['settings']['context'] = 'last';
126
		$bdata['settings']['template'] = '';
127
		$bdata['settings']['date_range'] = '';
128
129
		if (!$bdata['settings']['last_post'])
130
		{
131
			$bdata['settings']['preview_chars'] = 0;
132
		}
133
134
		return parent::display($bdata, $edit_mode);
135
	}
136
137
	/**
138
	 * {@inheritdoc}
139
	 */
140
	protected function get_block_content(array $topic_data)
141
	{
142
		$base_url = trim(build_url(array($this->param)), '?');
143
144
		return array_merge(
145
			parent::get_block_content($topic_data),
146
			array(
147
				'T_ICONS_PATH'	=> $this->template->retrieve_var('T_ICONS_PATH'),
148
				'S_LAST_POST'	=> $this->settings['last_post'],
149
				'TOTAL_PAGES'	=> ceil($this->total_topics / $this->settings['per_page']),
150
				'CURRENT_PAGE'	=> $this->start + 1,
151
				'BLOCK_ID'		=> $this->block,
152
				'BASE_URL'		=> $base_url . '#' . $this->block,
153
				'PAGE_URL'		=> $base_url . (strpos($base_url, '?') === false ? '?' : '&amp;') . $this->param . '=%s#' . $this->block,
154
			)
155
		);
156
	}
157
158
	/**
159
	 * {@inheritdoc}
160
	 */
161
	protected function build_query()
162
	{
163
		parent::build_query();
164
		$this->forum_data->fetch_custom(array(
165
			'WHERE' => array($this->sort_order[$this->settings['order_by']] . ' > ' . $this->get_time_limit()),
166
		));
167
		$this->forum_data->fetch_db_track();
168
	}
169
170
	/**
171
	 * @return int
172
	 */
173
	protected function get_time_limit()
174
	{
175
		return time() - ($this->settings['look_back'] * 24 * 3600);
176
	}
177
178
	/**
179
	 * {@inheritdoc}
180
	 */
181
	protected function get_topic_data()
182
	{
183
		$this->set_start_page();
184
185
		return $this->forum_data->get_topic_data($this->settings['per_page'], $this->start * $this->settings['per_page']);
186
	}
187
188
	/**
189
	 * @return void
190
	 */
191
	protected function set_start_page()
192
	{
193
		$page = $this->request->variable($this->param, 1);
194
195
		$this->total_topics = $this->forum_data->get_topics_count();
196
		$this->start = $this->pagination->validate_start($page - 1, $this->settings['per_page'], $this->total_topics);
197
	}
198
199
	/**
200
	 * {@inheritdoc}
201
	 */
202
	protected function get_topics_template_data(array &$topic_data, array &$post_data, array $user_data)
203
	{
204
		$template_data = parent::get_topics_template_data($topic_data, $post_data, $user_data);
205
206
		$replies = $template_data['REPLIES'];
207
		$unread_topic = $template_data['S_UNREAD_TOPIC'];
208
209
		// Get folder img, topic status/type related information
210
		$folder_img = $folder_alt = $topic_type = '';
211
		topic_status($topic_data, $replies, $unread_topic, $folder_img, $folder_alt, $topic_type);
212
213
		$template_data['S_HAS_POLL']			= (bool) $topic_data['poll_start'];
214
		$template_data['S_TOPIC_ICONS']			= (bool) $topic_data['enable_icons'];
215
		$template_data['TOPIC_IMG_STYLE']		= $folder_img;
216
		$template_data['TOPIC_FOLDER_IMG']		= $this->user->img($folder_img, $folder_alt);
217
		$template_data['TOPIC_FOLDER_IMG_ALT']	= $this->user->lang[$folder_alt];
218
		$template_data['TOPIC_TYPE_CLASS']		= $this->topic_type_class[$topic_data['topic_type']];
219
		$template_data['TOPIC_TIME_RFC3339']	= gmdate(DATE_RFC3339, $topic_data['topic_time']);
220
		$template_data['LAST_POST_TIME_RFC3339'] = gmdate(DATE_RFC3339, $topic_data['topic_last_post_time']);
221
		$template_data['LAST_POST_TIME']		= $this->user->format_date($topic_data['topic_last_post_time']);
222
223
		if (!empty($this->icons[$topic_data['icon_id']]))
224
		{
225
			$template_data['TOPIC_ICON_IMG']		= $this->icons[$topic_data['icon_id']]['img'];
226
			$template_data['TOPIC_ICON_IMG_WIDTH']	= $this->icons[$topic_data['icon_id']]['width'];
227
			$template_data['TOPIC_ICON_IMG_HEIGHT']	= $this->icons[$topic_data['icon_id']]['height'];
228
		}
229
230
		return $template_data;
231
	}
232
233
	/**
234
	 * {@inheritdoc}
235
	 */
236
	public function get_template()
237
	{
238
		return '@blitze_sitemaker/blocks/recent_topics.html';
239
	}
240
}
241