recent::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 6
dl 0
loc 13
ccs 0
cts 12
cp 0
crap 2
rs 9.9332
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
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\services\blocks\driver\block was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 */
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\services\date_range was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
	protected $date_range;
28
29
	/** @var \blitze\sitemaker\services\forum\data */
0 ignored issues
show
Bug introduced by
The type blitze\sitemaker\services\forum\data was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
		$editor_attributes = [];
84
85
		return array(
86
			'legend1'			=> 'DISPLAY',
87
				'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),
88
				'fields'			=> array('lang' => 'SELECT_FIELDS', 'validate' => 'string', 'type' => 'checkbox', 'options' => $field_options, 'default' => array(), 'explain' => true),
89
				'block_tpl'			=> array('lang' => '', 'validate' => 'string', 'type' => 'code_editor', 'params' => [$editor_attributes, 'TEMPLATE'], 'default' => ''),
90
				'layout'			=> array('lang' => 'DISPLAY_LAYOUT', 'validate' => 'string', 'type' => 'select', 'options' => $this->get_display_layouts(), 'default' => 'layout0'),
91
92
			'legend2'			=> 'SETTINGS',
93
				'topic_type'		=> array('lang' => 'TOPIC_TYPE', 'validate' => 'string', 'type' => 'select', 'options' => $this->get_topic_type_options(), 'default' => POST_NORMAL),
94
				'max_topics'		=> array('lang' => 'MAX_TOPICS', 'validate' => 'int:0:20', 'type' => 'number:0:20', 'maxlength' => 2, 'default' => 5),
95
				'offset_start'		=> array('lang' => 'OFFSET_START', 'validate' => 'int:0:20', 'type' => 'number:0:20', 'maxlength' => 2, 'default' => 0),
96
				'topic_title_limit'	=> array('lang' => 'TOPIC_TITLE_LIMIT', 'validate' => 'int:0:255', 'type' => 'number:0:255', 'maxlength' => 3, 'default' => 25),
97
				'max_chars'			=> array('lang' => 'FIELD_MAX_CHARS', 'validate' => 'int:0:255', 'type' => 'number:0:255', 'maxlength' => 3, 'default' => 125),
98
				'date_range'		=> array('lang' => 'LIMIT_POST_TIME', 'validate' => 'string', 'type' => 'select', 'options' => $this->get_range_options(), 'default' => ''),
99
				'sort_key'			=> array('lang' => 'SORT_BY', 'validate' => 'string', 'type' => 'select', 'options' => $this->sort_options, 'default' => self::SORT_TOPIC_TIME),
100
				'enable_tracking'	=> array('lang' => 'ENABLE_TOPIC_TRACKING', 'validate' => 'bool', 'type' => 'radio:yes_no', 'default' => 1),
101
				'last_modified'		=> array('type' => 'hidden', 'default' => time()),
102
		);
103
	}
104
105
	/**
106
	 * {@inheritdoc}
107
	 */
108
	public function display(array $bdata, $edit_mode = false)
109
	{
110
		$this->settings = $bdata['settings'];
111
		$type = $this->settings['content_type'];
112
113
		if (($entity = $this->content_types->get_type($type, false)) !== false)
114
		{
115
			$forum_id = $entity->get_forum_id();
116
			$this->build_query($forum_id);
117
			$this->forum->build(true, true, false);
118
			$this->ptemplate->assign_vars(array(
119
				'LAYOUT'		=> $this->settings['layout'],
120
				'TITLE_LIMIT'	=> $this->settings['topic_title_limit'],
121
				'FIELD_TYPES'	=> $entity->get_field_types(),
122
			));
123
124
			return $this->show_topics($edit_mode, $bdata['bid'], $forum_id, $type, $entity);
125
		}
126
127
		return array(
128
			'title'		=> '',
129
			'content'	=> ($edit_mode) ? $this->language->lang('NO_CONTENT_TYPE') : '',
130
		);
131
	}
132
133
	/**
134
	 * @param int $forum_id
135
	 * @return void
136
	 */
137
	protected function build_query($forum_id)
138
	{
139
		$sort_keys = array(
140
			self::SORT_TOPIC_TIME	=> 't.topic_time',
141
			self::SORT_TOPIC_VIEWS	=> 't.topic_views',
142
			self::SORT_TOPIC_READ	=> 't.topic_last_view_time'
143
		);
144
145
		$range_info = $this->date_range->get($this->settings['date_range']);
146
147
		$this->forum->query($this->settings['enable_tracking'])
148
			->fetch_forum($forum_id)
149
			->fetch_topic_type(array((int) $this->settings['topic_type']))
150
			->fetch_date_range($range_info['start'], $range_info['stop'])
151
			->set_sorting($sort_keys[$this->settings['sort_key']]);
152
	}
153
154
	/**
155
	 * @param bool $edit_mode
156
	 * @param int $block_id
157
	 * @param int $forum_id
158
	 * @param string $type
159
	 * @param \blitze\content\model\entity\type $entity
160
	 * @return array
161
	 * @internal param int $block_id
162
	 */
163
	protected function show_topics($edit_mode, $block_id, $forum_id, $type, \blitze\content\model\entity\type $entity)
164
	{
165
		$topics_data = $this->forum->get_topic_data($this->settings['max_topics'], $this->settings['offset_start']);
166
		$posts_data = $this->forum->get_post_data('first');
167
168
		$content = '';
169
		if (sizeof($posts_data) || $edit_mode !== false)
170
		{
171
			$users_cache = $this->forum->get_posters_info();
172
			$attachments = $this->forum->get_attachments($forum_id);
173
			$topic_tracking_info = $this->forum->get_topic_tracking_info($forum_id);
174
			$block_fields = $this->get_block_fields($entity->get_field_types());
175
176
			$this->fields->prepare_to_show($entity, array_keys($topics_data), $block_fields, $this->settings['block_tpl'], 'block', $block_id . '_block');
177
			$this->set_max_chars($block_fields);
178
179
			$update_count = array();
180
			foreach ($topics_data as $topic_id => $topic_data)
181
			{
182
				$post_data	= array_shift($posts_data[$topic_id]);
183
				$this->ptemplate->assign_block_vars('topicrow', $this->fields->show($type, $topic_data, $post_data, $users_cache, $attachments, $update_count, $topic_tracking_info));
184
			}
185
			unset($topics_data, $posts_data, $users_cache, $attachments, $topic_tracking_info);
186
187
			$content = $this->ptemplate->render_view('blitze/content', "blocks/{$this->tpl_name}.html", $this->tpl_name . '_block');
188
		}
189
190
		return array(
191
			'title'		=> $this->get_block_title($entity->get_content_langname()),
192
			'content'	=> $content,
193
		);
194
	}
195
196
	/**
197
	 * @param string $content_langname
198
	 * @return string
199
	 */
200
	protected function get_block_title($content_langname)
201
	{
202
		$topic_types = array(
203
			POST_GLOBAL		=> 'CONTENT_GLOBAL_ANNOUNCEMENTS',
204
			POST_ANNOUNCE	=> 'CONTENT_ANNOUNCEMENTS',
205
			POST_STICKY		=> 'CONTENT_STICKY_POSTS',
206
		);
207
208
		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);
209
	}
210
211
	/**
212
	 * @param array $field_types
213
	 * @return array
214
	 */
215
	protected function get_block_fields(array $field_types)
216
	{
217
		$block_fields = (!empty($this->settings['fields'])) ? $this->settings['fields'] : array();
218
		return array_intersect_key($field_types, array_flip($block_fields));
219
	}
220
221
	/**
222
	 * @param array $fields
223
	 * @return void
224
	 */
225
	protected function set_max_chars(array $fields)
226
	{
227
		$textarea_fields = array_keys($fields, 'textarea');
228
229
		foreach ($textarea_fields as $field)
230
		{
231
			$this->fields->overwrite_field_data($field, array(
232
				'field_props' => array(
233
					'max_chars' => $this->settings['max_chars'],
234
				),
235
			));
236
		}
237
	}
238
239
	/**
240
	 * @param array $type_options
241
	 * @param array $field_options
242
	 * @return string|null
243
	 */
244
	protected function get_content_type_options(array &$type_options, array &$field_options)
245
	{
246
		$content_types = $this->content_types->get_all_types();
247
248
		$type_options = $field_options = array();
249
		foreach ($content_types as $type => $entity)
250
		{
251
			/** @var \blitze\content\model\entity\type $entity */
252
			$type_options[$type] = $entity->get_content_langname();
253
254
			$content_fields = $entity->get_content_fields();
255
			foreach ($content_fields as $field => $fdata)
256
			{
257
				$field_options[$type][$field] = $fdata['field_label'];
258
			}
259
		}
260
		reset($content_types);
261
262
		return key($content_types);
263
	}
264
265
	/**
266
	 * @param array $content_types
267
	 * @param string $type
268
	 * @return string
269
	 */
270
	public function select_content_type(array $content_types, $type)
271
	{
272
		$html = '';
273
		foreach ($content_types as $value => $title)
274
		{
275
			$selected = ($type == $value) ? ' selected="selected"' : '';
276
			$html .= '<option value="' . $value . '"' . $selected . ' data-toggle-setting="#fields-col-' . $value . '">' . $title . '</option>';
277
		}
278
279
		return $html;
280
	}
281
282
	/**
283
	 * @return array
284
	 */
285
	protected function get_topic_type_options()
286
	{
287
		return array(
288
			POST_NORMAL		=> 'POST_NORMAL',
289
			POST_STICKY		=> 'POST_STICKY',
290
			POST_ANNOUNCE	=> 'POST_ANNOUNCEMENT',
291
			POST_GLOBAL		=> 'POST_GLOBAL',
292
		);
293
	}
294
295
	/**
296
	 * @return array
297
	 */
298
	protected function get_range_options()
299
	{
300
		return array(
301
			''		=> 'ALL_TIME',
302
			'today'	=> 'TODAY',
303
			'week'	=> 'THIS_WEEK',
304
			'month'	=> 'THIS_MONTH',
305
			'year'	=> 'THIS_YEAR',
306
		);
307
	}
308
309
	/**
310
	 * @return array
311
	 */
312
	protected function get_display_layouts()
313
	{
314
		$layouts = array('layout0', 'layout1', 'layout2');
315
		return array_combine($layouts, $layouts);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_combine($layouts, $layouts) could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
316
	}
317
}
318