Passed
Push — release-3.0.0 ( 4d9055...2010c1 )
by Daniel
07:48
created

recent::get_template()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * @package sitemaker
6
 * @copyright (c) 2013 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\content\blocks;
12
13
class recent extends \blitze\sitemaker\services\blocks\driver\block
14
{
15
	/** @var \phpbb\config\db */
16
	protected $config;
17
18
	/** @var\phpbb\language\language */
19
	protected $language;
20
21
	/** @var \blitze\content\services\types */
22
	protected $content_types;
23
24
	/* @var \blitze\content\services\fields */
25
	protected $fields;
26
27
	/** @var \blitze\sitemaker\services\date_range */
28
	protected $date_range;
29
30
	/** @var \blitze\sitemaker\services\forum\data */
31
	protected $forum;
32
33
	/** @var string */
34
	protected $tpl_name = 'recent_content';
35
36
	/** @var  array */
37
	protected $settings;
38
39
	/** @var array */
40
	protected $sort_options = array();
41
42
	/** @var */
43
	const SORT_TOPIC_TIME = 0;
44
45
	/** @var */
46
	const SORT_TOPIC_VIEWS = 1;
47
48
	/** @var */
49
	const SORT_TOPIC_READ = 2;
50
51
	/**
52
	 * Constructor
53
	 *
54
	 * @param \phpbb\config\db							$config				Config object
55
	 * @param \phpbb\language\language					$language			Language Object
56
	 * @param \blitze\content\services\types			$content_types		Content types object
57
	 * @param \blitze\content\services\fields			$fields				Content fields object
58
	 * @param \blitze\sitemaker\services\date_range		$date_range			Date Range Object
59
	 * @param \blitze\sitemaker\services\forum\data		$forum				Forum Data object
60
	 */
61
	public function __construct(\phpbb\config\db $config, \phpbb\language\language $language, \blitze\content\services\types $content_types, \blitze\content\services\fields $fields, \blitze\sitemaker\services\date_range $date_range, \blitze\sitemaker\services\forum\data $forum)
62
	{
63
		$this->config = $config;
64
		$this->language = $language;
65
		$this->content_types = $content_types;
66
		$this->fields = $fields;
67
		$this->date_range = $date_range;
68
		$this->forum = $forum;
69
70
		$this->sort_options = array(
71
			self::SORT_TOPIC_TIME	=> 'TOPIC_TIME',
72
			self::SORT_TOPIC_VIEWS	=> 'TOPIC_VIEWS',
73
			self::SORT_TOPIC_READ	=> 'LAST_READ_TIME',
74
		);
75
	}
76
77
	/**
78
	 * {@inheritdoc}
79
	 */
80
	public function get_config(array $settings)
81
	{
82
		$editor_attributes = [];
83
		$content_type_options = $field_options = array();
84
		$settings += array(
85
			'content_type'	=> $this->get_content_type_options($content_type_options, $field_options),
86
			'fields'		=> [],
87
		);
88
89
		$type = $settings['content_type'];
90
91
		if ($type)
92
		{
93
			$field_options[$type] = array_merge(array_flip((array) $settings['fields']), (array) $field_options[$type]);
94
		}
95
96
		return array(
97
			'legend1'			=> 'DISPLAY',
98
			'content_type'		=> array('lang' => 'CONTENT_TYPE', 'validate' => 'string', 'type' => 'select:1:toggable', 'object' => $this, 'method' => 'select_content_type', 'options' => $content_type_options, 'default' => ''),
99
			'fields'			=> array('lang' => 'SELECT_FIELDS', 'validate' => 'string', 'type' => 'checkbox:sortable', 'options' => $field_options, 'default' => array(), 'explain' => true),
100
			'block_tpl'			=> array('lang' => '', 'validate' => 'string', 'type' => 'code_editor', 'params' => [$editor_attributes, 'TEMPLATE'], 'default' => ''),
101
			'layout'			=> array('lang' => 'DISPLAY_LAYOUT', 'validate' => 'string', 'type' => 'select', 'options' => $this->get_display_layouts(), 'default' => 'layout0'),
102
103
			'legend2'			=> 'SETTINGS',
104
			'topic_type'		=> array('lang' => 'TOPIC_TYPE', 'validate' => 'string', 'type' => 'select', 'options' => $this->get_topic_type_options(), 'default' => POST_NORMAL),
105
			'max_topics'		=> array('lang' => 'MAX_TOPICS', 'validate' => 'int:0:20', 'type' => 'number:0:20', 'maxlength' => 2, 'default' => 5),
106
			'offset_start'		=> array('lang' => 'OFFSET_START', 'validate' => 'int:0:20', 'type' => 'number:0:20', 'maxlength' => 2, 'default' => 0),
107
			'topic_title_limit'	=> array('lang' => 'TOPIC_TITLE_LIMIT', 'validate' => 'int:0:255', 'type' => 'number:0:255', 'maxlength' => 3, 'default' => 25),
108
			'max_chars'			=> array('lang' => 'FIELD_MAX_CHARS', 'validate' => 'int:0:255', 'type' => 'number:0:255', 'maxlength' => 3, 'default' => 125),
109
			'date_range'		=> array('lang' => 'LIMIT_POST_TIME', 'validate' => 'string', 'type' => 'select', 'options' => $this->get_range_options(), 'default' => ''),
110
			'sort_key'			=> array('lang' => 'SORT_BY', 'validate' => 'string', 'type' => 'select', 'options' => $this->sort_options, 'default' => self::SORT_TOPIC_TIME),
111
			'enable_tracking'	=> array('lang' => 'ENABLE_TOPIC_TRACKING', 'validate' => 'bool', 'type' => 'radio:yes_no', 'default' => 1),
112
			'last_modified'		=> array('type' => 'hidden', 'default' => time()),
113
		);
114
	}
115
116
	/**
117
	 * {@inheritdoc}
118
	 */
119
	public function get_template()
120
	{
121
		return '@blitze_content/blocks/' . $this->tpl_name . '.html';
122
	}
123
124
	/**
125
	 * {@inheritdoc}
126
	 */
127
	public function display(array $bdata, $edit_mode = false)
128
	{
129
		$this->settings = $bdata['settings'];
130
		$type = $this->settings['content_type'];
131
132
		$title = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $title is dead and can be removed.
Loading history...
133
134
		if (($entity = $this->content_types->get_type($type, false)) !== false)
135
		{
136
			$forum_id = $entity->get_forum_id();
137
			$title = $this->get_block_title($entity->get_content_langname());
138
139
			$this->build_query($forum_id);
140
			$this->forum->build(true, true, false);
141
142
			return array(
143
				'title'	=> $title,
144
				'data'	=> array(
145
					'LAYOUT'		=> $this->settings['layout'],
146
					'TITLE_LIMIT'	=> $this->settings['topic_title_limit'],
147
					'FIELD_TYPES'	=> $entity->get_field_types(),
148
					'TOPICS'		=> $this->show_topics($edit_mode, $bdata['bid'], $forum_id, $type, $entity),
149
				)
150
			);
151
		}
152
153
		return array(
154
			'title'		=> 'BLITZE_CONTENT_BLOCK_RECENT',
155
			'content'	=> ($edit_mode) ? $this->language->lang('NO_CONTENT_TYPE') : '',
156
		);
157
	}
158
159
	/**
160
	 * @param int $forum_id
161
	 * @return void
162
	 */
163
	protected function build_query($forum_id)
164
	{
165
		$sort_keys = array(
166
			self::SORT_TOPIC_TIME	=> 't.topic_time',
167
			self::SORT_TOPIC_VIEWS	=> 't.topic_views',
168
			self::SORT_TOPIC_READ	=> 't.topic_last_view_time'
169
		);
170
171
		$range_info = $this->date_range->get($this->settings['date_range']);
172
173
		$this->forum->query($this->settings['enable_tracking'])
174
			->fetch_forum($forum_id)
175
			->fetch_topic_type(array((int) $this->settings['topic_type']))
176
			->fetch_date_range($range_info['start'], $range_info['stop'])
177
			->set_sorting($sort_keys[$this->settings['sort_key']]);
178
	}
179
180
	/**
181
	 * @param bool $edit_mode
182
	 * @param int $block_id
183
	 * @param int $forum_id
184
	 * @param string $type
185
	 * @param \blitze\content\model\entity\type $entity
186
	 * @return array
187
	 * @internal param int $block_id
188
	 */
189
	protected function show_topics($edit_mode, $block_id, $forum_id, $type, \blitze\content\model\entity\type $entity)
190
	{
191
		global $template;
192
		$topics_data = $this->forum->get_topic_data($this->settings['max_topics'], $this->settings['offset_start']);
193
		$posts_data = $this->forum->get_post_data('first');
194
195
		$topics = [];
196
		if (sizeof($posts_data) || $edit_mode !== false)
197
		{
198
			$users_cache = $this->forum->get_posters_info();
199
			$attachments = $this->forum->get_attachments($forum_id);
200
			$topic_tracking_info = $this->forum->get_topic_tracking_info($forum_id);
201
			$block_fields = $this->get_block_fields($entity->get_field_types());
202
203
			$this->fields->prepare_to_show($entity, array_keys($topics_data), $block_fields, $this->settings['block_tpl'], 'summary', $block_id . '_block')
204
				->set_display_mode('block');
205
			$this->set_max_chars($block_fields);
206
207
			$update_count = array();
208
			foreach ($topics_data as $topic_id => $topic_data)
209
			{
210
				$post_data	= array_shift($posts_data[$topic_id]);
211
				$topics[] = $this->fields->show($type, $topic_data, $post_data, $users_cache, $attachments, $update_count, $topic_tracking_info);
212
			}
213
			unset($topics_data, $posts_data, $users_cache, $attachments, $topic_tracking_info);
214
		}
215
216
		return $topics;
217
	}
218
219
	/**
220
	 * @param string $content_langname
221
	 * @return string
222
	 */
223
	protected function get_block_title($content_langname)
224
	{
225
		$topic_types = array(
226
			POST_GLOBAL		=> 'CONTENT_GLOBAL_ANNOUNCEMENTS',
227
			POST_ANNOUNCE	=> 'CONTENT_ANNOUNCEMENTS',
228
			POST_STICKY		=> 'CONTENT_STICKY_POSTS',
229
		);
230
231
		return $this->language->lang((isset($topic_types[$this->settings['topic_type']])) ? $topic_types[$this->settings['topic_type']] : 'CONTENT_' . $this->sort_options[$this->settings['sort_key']], $content_langname);
232
	}
233
234
	/**
235
	 * @param array $field_types
236
	 * @return array
237
	 */
238
	protected function get_block_fields(array $field_types)
239
	{
240
		$block_fields = (!empty($this->settings['fields'])) ? $this->settings['fields'] : array();
241
		$block_fields = array_flip($block_fields);
242
		$field_types = array_merge($block_fields, $field_types);
243
244
		return array_intersect_key($field_types, $block_fields);
245
	}
246
247
	/**
248
	 * @param array $fields
249
	 * @return void
250
	 */
251
	protected function set_max_chars(array $fields)
252
	{
253
		$textarea_fields = array_keys($fields, 'textarea');
254
255
		foreach ($textarea_fields as $field)
256
		{
257
			$this->fields->overwrite_field_data($field, array(
258
				'field_props' => array(
259
					'max_chars' => $this->settings['max_chars'],
260
				),
261
			));
262
		}
263
	}
264
265
	/**
266
	 * @param array $type_options
267
	 * @param array $field_options
268
	 * @return string|null
269
	 */
270
	protected function get_content_type_options(array &$type_options, array &$field_options)
271
	{
272
		$content_types = $this->content_types->get_all_types();
273
274
		$type_options = $field_options = array();
275
		foreach ($content_types as $type => $entity)
276
		{
277
			/** @var \blitze\content\model\entity\type $entity */
278
			$type_options[$type] = $entity->get_content_langname();
279
280
			$content_fields = $entity->get_content_fields();
281
			foreach ($content_fields as $field => $fdata)
282
			{
283
				$field_options[$type][$field] = $fdata['field_label'];
284
			}
285
		}
286
		reset($content_types);
287
288
		return key($content_types);
289
	}
290
291
	/**
292
	 * @param array $content_types
293
	 * @param string $type
294
	 * @return string
295
	 */
296
	public function select_content_type(array $content_types, $type)
297
	{
298
		$html = '';
299
		foreach ($content_types as $value => $title)
300
		{
301
			$selected = ($type == $value) ? ' selected="selected"' : '';
302
			$html .= '<option value="' . $value . '"' . $selected . ' data-toggle-setting="#fields-col-' . $value . '">' . $title . '</option>';
303
		}
304
305
		return $html;
306
	}
307
308
	/**
309
	 * @return array
310
	 */
311
	protected function get_topic_type_options()
312
	{
313
		return array(
314
			POST_NORMAL		=> 'POST_NORMAL',
315
			POST_STICKY		=> $this->language->lang('TOPIC_TYPE_FEATURED'),
316
			POST_ANNOUNCE	=> $this->language->lang('TOPIC_TYPE_RECOMMENDED'),
317
			POST_GLOBAL		=> $this->language->lang('TOPIC_TYPE_MUST_READ'),
318
		);
319
	}
320
321
	/**
322
	 * @return array
323
	 */
324
	protected function get_range_options()
325
	{
326
		return array(
327
			''		=> 'ALL_TIME',
328
			'today'	=> 'TODAY',
329
			'week'	=> 'THIS_WEEK',
330
			'month'	=> 'THIS_MONTH',
331
			'year'	=> 'THIS_YEAR',
332
		);
333
	}
334
335
	/**
336
	 * @return array
337
	 */
338
	protected function get_display_layouts()
339
	{
340
		$layouts = array('layout0', 'layout1', 'layout2');
341
		return (array) array_combine($layouts, $layouts);
342
	}
343
}
344