Passed
Push — develop ( 772ac1...77e6e2 )
by Daniel
04:01
created

recent::get_block_title()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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