Completed
Push — develop ( c5a37f...226ad1 )
by Daniel
06:54
created

base_view::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 8
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2016 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\services\views\driver;
11
12
abstract class base_view implements views_interface
13
{
14
	/** @var \phpbb\event\dispatcher_interface */
15
	protected $phpbb_dispatcher;
16
17
	/** @var\phpbb\language\language */
18
	protected $language;
19
20
	/** @var \phpbb\pagination */
21
	protected $pagination;
22
23
	/** @var \phpbb\template\template */
24
	protected $template;
25
26
	/* @var \blitze\content\services\fields */
27
	protected $fields;
28
29
	/** @var \blitze\sitemaker\services\forum\data */
30
	protected $forum;
31
32
	/* @var \blitze\content\services\helper */
33
	protected $helper;
34
35
	/* @var \blitze\content\services\quickmod */
36
	protected $quickmod;
37
38
	/**
39
	 * Constructor
40
	 *
41
	 * @param \phpbb\event\dispatcher_interface			$phpbb_dispatcher	Event dispatcher object
42
	 * @param \phpbb\language\language					$language			Language Object
43
	 * @param \phpbb\pagination							$pagination			Pagination object
44
	 * @param \phpbb\template\template					$template			Template object
45
	 * @param \blitze\content\services\fields			$fields				Content fields object
46
	 * @param \blitze\sitemaker\services\forum\data		$forum				Forum Data object
47
	 * @param \blitze\content\services\helper			$helper				Content helper object
48
	 * @param \blitze\content\services\quickmod			$quickmod			Quick moderator tools
49
	*/
50
	public function __construct(\phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbb\language\language $language, \phpbb\pagination $pagination, \phpbb\template\template $template, \blitze\content\services\fields $fields, \blitze\sitemaker\services\forum\data $forum, \blitze\content\services\helper $helper, \blitze\content\services\quickmod $quickmod)
51
	{
52
		$this->phpbb_dispatcher = $phpbb_dispatcher;
53
		$this->language = $language;
54
		$this->pagination = $pagination;
55
		$this->template = $template;
56
		$this->fields = $fields;
57
		$this->forum = $forum;
58
		$this->helper = $helper;
59
		$this->quickmod = $quickmod;
60
	}
61
62
	/**
63
	 * {@inheritdoc}
64
	 */
65
	public function get_detail_template()
66
	{
67
		return 'views/content_detail.html';
68
	}
69
70
	/**
71
	 * @param array $filters
72
	 * @param int $forum_id
73
	 * @return void
74
	 */
75
	public function build_index_query(array $filters, $forum_id = 0)
76
	{
77
		$sql_array = $this->get_filter_sql($filters, $forum_id);
78
79
		$this->forum->query()
80
			->fetch_forum($forum_id)
81
			->fetch_custom($sql_array)
82
			->set_sorting('t.topic_time')
83
			->build(true, false, false);
84
	}
85
86
	/**
87
	 * {@inheritdoc}
88
	 * @param array $topic_data_overwrite
89
	 */
90
	public function render_index(\blitze\content\model\entity\type $entity, $page, array $filters, array $topic_data_overwrite = array())
91
	{
92
		$content_type = $entity->get_content_name();
93
		$items_per_page = $entity->get_items_per_page();
94
		$forum_id = $entity->get_forum_id();
95
		$start = ($page - 1) * $items_per_page;
96
97
		$this->build_index_query($filters, $forum_id);
98
		$this->set_mcp_url($forum_id);
99
100
		if ($entity->get_show_pagination())
101
		{
102
			list($filter_type, $filter_value) = each($filters);
103
			$filter_value = (array) $filter_value;
104
105
			$total_topics = $this->forum->get_topics_count();
106
			$this->generate_pagination('summary', $total_topics, $start, $items_per_page, array(
107
				'type'			=> $content_type,
108
				'filter_type'	=> $filter_type,
109
				'filter_value'	=> current($filter_value),
110
			));
111
		}
112
113
		$this->display_topics($entity, $items_per_page, $start, $topic_data_overwrite);
114
	}
115
116
	/**
117
	 * @param \blitze\content\model\entity\type $entity
118
	 * @param int $items_per_page
119
	 * @param int $start
120
	 * @param array $topic_data_overwrite
121
	 * @return void
122
	 */
123
	protected function display_topics(\blitze\content\model\entity\type $entity, $items_per_page = 1, $start = 0, array $topic_data_overwrite = array())
124
	{
125
		$content_type = $entity->get_content_name();
126
		$topics_data = $this->forum->get_topic_data($items_per_page, $start);
127
		$posts_data = $this->forum->get_post_data('first');
128
		$topic_tracking_info = $this->forum->get_topic_tracking_info($entity->get_forum_id());
129
		$users_cache = $this->forum->get_posters_info();
130
		$attachments = $this->forum->get_attachments($entity->get_forum_id());
131
132
		$this->fields->prepare_to_show($entity, array_keys($topics_data), $entity->get_summary_fields(), $entity->get_summary_tpl(), 'summary');
133
134
		$update_count = array();
135
		foreach ($posts_data as $topic_id => $posts)
136
		{
137
			$post_data	= array_shift($posts);
138
			$topic_data	= $topics_data[$topic_id];
139
140
			$this->template->assign_block_vars('topicrow', array_merge(
141
				$this->fields->show($content_type, $topic_data, $post_data, $users_cache, $attachments, $update_count, $topic_tracking_info),
142
				$topic_data_overwrite
143
			));
144
		}
145
		unset($topics_data, $posts_data, $users_cache, $attachments, $topic_tracking_info);
146
	}
147
148
	/**
149
	 * {@inheritdoc}
150
	 * @param array $topic_data_overwrite
151
	 */
152
	public function render_detail(\blitze\content\model\entity\type $entity, $topic_id, array &$update_count, array $topic_data_overwrite = array())
153
	{
154
		$this->language->add_lang('viewtopic');
155
		$this->language->add_lang('content', 'blitze/content');
156
		$this->set_mcp_url($entity->get_forum_id(), $topic_id);
157
158
		$this->forum->query()
159
			->fetch_topic($topic_id)
160
			->fetch_watch_status()
161
			->fetch_bookmark_status()
162
			->build(true, true, false);
163
164
		return $this->display_topic($topic_id, $entity, $update_count, $topic_data_overwrite);
165
	}
166
167
	/**
168
	 * @param int $topic_id
169
	 * @param \blitze\content\model\entity\type $entity
170
	 * @param array $update_count
171
	 * @param array $topic_data_overwrite
172
	 * @return array
173
	 * @throws \Exception
174
	 */
175
	protected function display_topic($topic_id, \blitze\content\model\entity\type $entity, array &$update_count, array $topic_data_overwrite)
176
	{
177
		$forum_id = $entity->get_forum_id();
178
		$content_type = $entity->get_content_name();
179
		$content_langname = $entity->get_content_name();
180
181
		$topics_data = $this->forum->get_topic_data();
182
		$post_data = $this->forum->get_post_data('first');
183
		$topic_tracking_info = $this->forum->get_topic_tracking_info($forum_id);
184
		$users_cache = $this->forum->get_posters_info();
185
		$attachments = $this->forum->get_attachments($forum_id);
186
187
		if (!sizeof($post_data))
188
		{
189
			throw new \Exception($this->language->lang('CONTENT_NO_EXIST'));
190
		}
191
192
		$this->fields->prepare_to_show($entity, array_keys($topics_data), $entity->get_detail_fields(), $entity->get_detail_tpl(), 'detail');
193
194
		$topic_data = array_shift($topics_data);
195
		$post_data = array_shift($post_data[$topic_id]);
196
		$tpl_data = array_merge($topic_data,
197
			$this->fields->show($content_type, $topic_data, $post_data, $users_cache, $attachments, $update_count, $topic_tracking_info, $topic_data_overwrite),
198
			$this->fields->get_topic_tools_data($topic_data)
199
		);
200
201
		$this->template->assign_vars(array_change_key_case($tpl_data, CASE_UPPER));
202
		$this->fields->show_attachments($attachments, $post_data['post_id']);
203
		$this->show_author_info($forum_id, $post_data['poster_id'], $content_langname, $users_cache[$post_data['poster_id']], $entity->get_show_poster_info());
204
		$this->show_author_contents($topic_data, $content_type, $content_langname, $entity->get_show_poster_contents());
205
		$this->quickmod->show_tools($topic_data);
206
207
		return array_merge($topic_data, array(
208
			'topic_title'		=> $tpl_data['TOPIC_TITLE'],
209
			'total_comments'	=> $tpl_data['TOPIC_COMMENTS'],
210
			'topic_url'			=> $tpl_data['TOPIC_URL'],
211
		));
212
	}
213
214
	/**
215
	 * @param array $attachments
216
	 * @param int $post_id
217
	 * @return void
218
	 */
219
	 protected function show_attachments(array $attachments, $post_id)
220
	 {
221
		if (!empty($attachments[$post_id]))
222
		{
223
			foreach ($attachments[$post_id] as $attachment)
224
			{
225
				$this->template->assign_block_vars('attachment', array(
226
					'DISPLAY_ATTACHMENT'	=> $attachment)
227
				);
228
			}
229
		}
230
	 }
231
232
	/**
233
	 * @param string $view_mode
234
	 * @param int $total_topics
235
	 * @param int $start
236
	 * @param int $items_per_page
237
	 * @param array $params
238
	 */
239
	protected function generate_pagination($view_mode, $total_topics, &$start, $items_per_page, array $params)
240
	{
241
		$params = array_filter($params);
242
		$route_type = $this->get_route_type($view_mode, $params);
243
		$start = $this->pagination->validate_start($start, $items_per_page, $total_topics);
244
		$this->pagination->generate_template_pagination(
245
			array(
246
				'routes' => array(
247
					'blitze_content_' . $route_type,
248
					'blitze_content_' . $route_type . '_page',
249
				),
250
				'params' => $params,
251
			),
252
			'pagination', 'page', $total_topics, $items_per_page, $start
253
		);
254
	}
255
256
	/**
257
	 * @param string $view_mode
258
	 * @param array $params
259
	 * @return string
260
	 */
261
	protected function get_route_type($view_mode, array $params)
262
	{
263
		$types = array(
264
			'show'		=> 'show',
265
			'summary'	=> join('_', array_filter(array(
266
				(!empty($params['type'])) ? 'type' : '',
267
				(!empty($params['filters'])) ? 'multi' : '',
268
				(!empty($params['filter_type'])) ? 'filter' : '',
269
			))),
270
		);
271
272
		return $types[$view_mode];
273
	}
274
275
	/**
276
	 * {@inheritdoc}
277
	 */
278
	protected function get_filter_sql(array $filters, $forum_id)
279
	{
280
		$sql_array = array();
281
282
		/**
283
		 * Event to filter topics by field value e.g category/food
284
		 *
285
		 * @event blitze.content.view.filter
286
		 * @var mixed								forum_id		Forum id, if available
287
		 * @var array								filters			Filters
288
		 * @var array								sql_array		Array to modify sql query to get topics
289
		 */
290
		$vars = array('forum_id', 'filters', 'sql_array');
291
		extract($this->phpbb_dispatcher->trigger_event('blitze.content.view.filter', compact($vars)));
292
293
		return $sql_array;
294
	}
295
296
	/**
297
	 * @param int $forum_id
298
	 * @param int $poster_id
299
	 * @param string $content_langname
300
	 * @param array $user_cache
301
	 * @param bool $show_author_info
302
	 * @return void
303
	 */
304
	protected function show_author_info($forum_id, $poster_id, $content_langname, array $user_cache, $show_author_info)
305
	{
306
		if ($show_author_info)
307
		{
308
			$this->forum->query()
309
				->fetch_forum($forum_id)
310
				->fetch_topic_poster($poster_id)
311
				->build(true, true, false);
312
			$user_content_topics = $this->forum->get_topics_count();
313
314
			$this->template->assign_vars(array_merge($user_cache, array(
315
				'S_USER_INFO'			=> true,
316
				'L_USER_ABOUT'			=> $this->language->lang('AUTHOR_INFO_EXPLAIN', $user_cache['username_full'], $user_cache['joined'], $user_content_topics, $content_langname, $user_cache['posts']),
317
				'L_USER_VIEW_ALL'		=> $this->language->lang('VIEW_AUTHOR_CONTENTS', $content_langname, $user_cache['username']),
318
				'L_SEARCH_USER_POSTS'	=> $this->language->lang('SEARCH_USER_POSTS', $user_cache['username']),
319
				'U_SEARCH_CONTENTS'		=> $this->helper->get_search_users_posts_url($forum_id, $user_cache['username']),
320
			)));
321
		}
322
	}
323
324
	/**
325
	 * @param array $topic_data
326
	 * @param string $content_type
327
	 * @param string $content_langname
328
	 * @param bool $show_author_contents
329
	 * @return void
330
	 */
331
	protected function show_author_contents($topic_data, $content_type, $content_langname, $show_author_contents)
332
	{
333
		if ($show_author_contents)
334
		{
335
			$this->forum->query()
336
				->fetch_forum($topic_data['forum_id'])
337
				->fetch_topic_poster($topic_data['topic_poster'])
338
				->fetch_custom(array(
339
					'WHERE' => array('t.topic_id <> ' . (int) $topic_data['topic_id'])
340
				))->build(true, true, false);
341
342
			$topics_data = $this->forum->get_topic_data(5);
343
			$topic_tracking_info = $this->forum->get_topic_tracking_info($topic_data['forum_id']);
344
345
			$this->template->assign_var('AUTHOR_CONTENT', $this->language->lang('AUTHOR_CONTENTS', $content_langname, $topic_data['topic_first_poster_name']));
346
347
			foreach ($topics_data as $row)
348
			{
349
				$this->template->assign_block_vars('content', $this->fields->get_min_topic_info($content_type, $row, $topic_tracking_info));
350
			}
351
		}
352
	}
353
354
	/**
355
	 * @param int $forum_id
356
	 * @param int $topic_id
357
	 * @return void
358
	 */
359
	protected function set_mcp_url($forum_id, $topic_id = 0)
360
	{
361
		$this->template->assign_var('U_MCP', $this->helper->get_mcp_url($forum_id, $topic_id));
362
	}
363
}
364