Completed
Push — develop ( 843010...da46c8 )
by Daniel
07:37
created

base_view::set_watch_status()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 0
cts 27
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 5
nop 1
crap 30
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
	 * {@inheritdoc}
72
	 */
73
	public function build_index_query($filter_type, $filter_value, $forum_id = '')
74
	{
75
		$sql_array = $this->get_filter_sql($filter_type, $filter_value, $forum_id);
76
77
		$this->forum->query()
78
			->fetch_forum($forum_id)
79
			->fetch_custom($sql_array)
80
			->set_sorting('t.topic_time')
81
			->build(true, false, false);
82
	}
83
84
	/**
85
	 * {@inheritdoc}
86
	 */
87
	public function render_index(\blitze\content\model\entity\type $entity, $page, $filter_type, $filter_value, array $topic_data_overwrite = array())
88
	{
89
		$content_type = $entity->get_content_name();
90
		$items_per_page = $entity->get_items_per_page();
91
		$start = ($page - 1) * $items_per_page;
92
93
		$this->build_index_query($filter_type, $filter_value, $entity->get_forum_id());
94
95
		if ($entity->get_show_pagination())
96
		{
97
			$total_topics = $this->forum->get_topics_count();
98
			$this->generate_pagination('summary', $total_topics, $start, $items_per_page, array(
99
				'type'			=> $content_type,
100
				'filter_type'	=> $filter_type,
101
				'filter_value'	=> $filter_value,
102
			));
103
		}
104
105
		$this->display_topics($entity, $items_per_page, $start, $topic_data_overwrite);
106
	}
107
108
	/**
109
	 * @param \blitze\content\model\entity\type $entity
110
	 * @param int $items_per_page
111
	 * @param int $start
112
	 * @param array $topic_data_overwrite
113
	 * @return void
114
	 */
115
	protected function display_topics(\blitze\content\model\entity\type $entity, $items_per_page = 1, $start = 0, array $topic_data_overwrite = array())
116
	{
117
		$content_type = $entity->get_content_name();
118
		$topics_data = $this->forum->get_topic_data($items_per_page, $start);
119
		$posts_data = $this->forum->get_post_data('first');
120
		$topic_tracking_info = $this->forum->get_topic_tracking_info($entity->get_forum_id());
121
		$users_cache = $this->forum->get_posters_info();
122
		$attachments = $this->forum->get_attachments($entity->get_forum_id());
123
124
		$this->fields->prepare_to_show($entity, array_keys($topics_data), $entity->get_summary_tags(), $entity->get_summary_tpl(), 'summary');
125
126
		$update_count = array();
127
		foreach ($posts_data as $topic_id => $posts)
128
		{
129
			$post_data	= array_shift($posts);
130
			$topic_data	= $topics_data[$topic_id];
131
132
			$this->template->assign_block_vars('topicrow', array_merge(
133
				$this->fields->show($content_type, $topic_data, $post_data, $users_cache, $attachments, $update_count, $topic_tracking_info),
134
				$topic_data_overwrite
135
			));
136
		}
137
		unset($topics_data, $posts_data, $users_cache, $attachments, $topic_tracking_info);
138
	}
139
140
	/**
141
	 * {@inheritdoc}
142
	 */
143
	public function render_detail(\blitze\content\model\entity\type $entity, $topic_id, array &$update_count, $mode = '', array $topic_data_overwrite = array())
144
	{
145
		$this->language->add_lang('viewtopic');
146
		$this->language->add_lang('content', 'blitze/content');
147
148
		$this->forum->query()
149
			->fetch_topic($topic_id)
150
			->fetch_watch_status()
151
			->fetch_bookmark_status()
152
			->build(true, true, false);
153
154
		return $this->display_topic($mode, $topic_id, $entity, $update_count, $topic_data_overwrite);
155
	}
156
157
	/**
158
	 * @param string $mode
159
	 * @param int $topic_id
160
	 * @param array $update_count
161
	 * @param array $topic_data_overwrite
162
	 * @return array
163
	 * @throws \Exception
164
	 */
165
	protected function display_topic($mode, $topic_id, \blitze\content\model\entity\type $entity, array &$update_count, array $topic_data_overwrite)
0 ignored issues
show
Unused Code introduced by
The parameter $mode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
166
	{
167
		$forum_id = $entity->get_forum_id();
168
		$content_type = $entity->get_content_name();
169
		$content_langname = $entity->get_content_name();
170
171
		$topics_data = $this->forum->get_topic_data();
172
		$post_data = $this->forum->get_post_data('first');
173
		$topic_tracking_info = $this->forum->get_topic_tracking_info($forum_id);
174
		$users_cache = $this->forum->get_posters_info();
175
		$attachments = $this->forum->get_attachments($forum_id);
176
177
		if (!sizeof($post_data))
178
		{
179
			throw new \Exception($this->language->lang('CONTENT_NO_EXIST'));
180
		}
181
182
		$this->fields->prepare_to_show($entity, array_keys($topics_data), $entity->get_detail_tags(), $entity->get_detail_tpl(), 'detail');
183
184
		$topic_data = array_shift($topics_data);
185
		$post_data = array_shift($post_data[$topic_id]);
186
		$tpl_data = array_merge($topic_data,
187
			$this->fields->show($content_type, $topic_data, $post_data, $users_cache, $attachments, $update_count, $topic_tracking_info, $topic_data_overwrite),
188
			$this->fields->get_topic_tools_data($topic_data)
189
		);
190
191
		$this->template->assign_vars(array_change_key_case($tpl_data, CASE_UPPER));
192
		$this->fields->show_attachments($attachments, $post_data['post_id']);
193
		$this->show_author_info($forum_id, $post_data['poster_id'], $content_langname, $users_cache[$post_data['poster_id']], $entity->get_show_poster_info());
194
		$this->show_author_contents($topic_data, $content_type, $content_langname, $entity->get_show_poster_contents());
195
		$this->quickmod->show_tools($topic_data);
196
197
		return array_merge($topic_data, array(
198
			'topic_title'		=> $tpl_data['TOPIC_TITLE'],
199
			'total_comments'	=> $tpl_data['TOPIC_COMMENTS'],
200
			'topic_url'			=> $tpl_data['TOPIC_URL'],
201
		));
202
	}
203
204
	/**
205
	 * @param array $attachments
206
	 * @param int $post_id
207
	 * @return void
208
	 */
209
	 protected function show_attachments(array $attachments, $post_id)
210
	 {
211
		if (!empty($attachments[$post_id]))
212
		{
213
			foreach ($attachments[$post_id] as $attachment)
214
			{
215
				$this->template->assign_block_vars('attachment', array(
216
					'DISPLAY_ATTACHMENT'	=> $attachment)
217
				);
218
			}
219
		}
220
	 }
221
222
	/**
223
	 * @param string $view_mode
224
	 * @param int $total_topics
225
	 * @param int $start
226
	 * @param int $items_per_page
227
	 * @param array $params
228
	 */
229
	protected function generate_pagination($view_mode, $total_topics, &$start, $items_per_page, array $params)
230
	{
231
		$route = ($view_mode === 'summary') ? 'index' : 'show';
232
		$start = $this->pagination->validate_start($start, $items_per_page, $total_topics);
233
		$this->pagination->generate_template_pagination(
234
			array(
235
				'routes' => array(
236
					'blitze_content_' . $route,
237
					'blitze_content_' . $route . '_page',
238
				),
239
				'params' => $params,
240
			),
241
			'pagination', 'page', $total_topics, $items_per_page, $start
242
		);
243
	}
244
245
	/**
246
	 * {@inheritdoc}
247
	 */
248
	protected function get_filter_sql($filter_type, $filter_value, $forum_id)
249
	{
250
		$sql_array = array();
251
252
		/**
253
		 * Event to filter topics by field value e.g category/food
254
		 *
255
		 * @event blitze.content.view.filter
256
		 * @var mixed								forum_id		Forum id, if available
257
		 * @var string								filter_type		Filter type e.g category|tag
258
		 * @var string								filter_value	The filter value e.g food
259
		 * @var array								sql_array		Array to modify sql query to get topics
260
		 */
261
		$vars = array('forum_id', 'filter_type', 'filter_value', 'sql_array');
262
		extract($this->phpbb_dispatcher->trigger_event('blitze.content.view.filter', compact($vars)));
263
264
		return $sql_array;
265
	}
266
267
	/**
268
	 * @param int $forum_id
269
	 * @param int $poster_id
270
	 * @param string $content_langname
271
	 * @param array $user_cache
272
	 * @param bool $show_author_info
273
	 * @return void
274
	 */
275
	protected function show_author_info($forum_id, $poster_id, $content_langname, array $user_cache, $show_author_info)
276
	{
277
		if ($show_author_info)
278
		{
279
			$this->forum->query()
280
				->fetch_forum($forum_id)
281
				->fetch_topic_poster($poster_id)
282
				->build(true, true, false);
283
			$user_content_topics = $this->forum->get_topics_count();
284
285
			$this->template->assign_vars(array_merge($user_cache, array(
286
				'S_USER_INFO'			=> true,
287
				'L_USER_ABOUT'			=> $this->language->lang('AUTHOR_INFO_EXPLAIN', $user_cache['username_full'], $user_cache['joined'], $user_content_topics, $content_langname, $user_cache['posts']),
288
				'L_USER_VIEW_ALL'		=> $this->language->lang('VIEW_AUTHOR_CONTENTS', $content_langname, $user_cache['username']),
289
				'L_SEARCH_USER_POSTS'	=> $this->language->lang('SEARCH_USER_POSTS', $user_cache['username']),
290
				'U_SEARCH_CONTENTS'		=> $this->helper->get_search_users_posts_url($forum_id, $user_cache['username']),
291
			)));
292
		}
293
	}
294
295
	/**
296
	 * @param array $topic_data
297
	 * @param string $content_type
298
	 * @param string $content_langname
299
	 * @param bool $show_author_contents
300
	 * @return void
301
	 */
302
	protected function show_author_contents($topic_data, $content_type, $content_langname, $show_author_contents)
303
	{
304
		if ($show_author_contents)
305
		{
306
			$this->forum->query()
307
				->fetch_forum($topic_data['forum_id'])
308
				->fetch_topic_poster($topic_data['topic_poster'])
309
				->fetch_custom(array(
310
					'WHERE' => array('t.topic_id <> ' . (int) $topic_data['topic_id'])
311
				))->build(true, true, false);
312
313
			$topics_data = $this->forum->get_topic_data(5);
314
			$topic_tracking_info = $this->forum->get_topic_tracking_info($topic_data['forum_id']);
315
316
			$this->template->assign_var('AUTHOR_CONTENT', $this->language->lang('AUTHOR_CONTENTS', $content_langname, $topic_data['topic_first_poster_name']));
317
318
			foreach ($topics_data as $row)
319
			{
320
				$this->template->assign_block_vars('content', $this->fields->get_min_topic_info($content_type, $row, $topic_tracking_info));
321
			}
322
		}
323
	}
324
325
	protected function set_watch_status(array $topic_data)
326
	{
327
		$s_watching_topic = array(
328
			'link'			=> '',
329
			'link_toggle'	=> '',
330
			'title'			=> '',
331
			'title_toggle'	=> '',
332
			'is_watching'	=> false,
333
		);
334
335
		if ($config['allow_topic_notify'])
336
		{
337
			$notify_status = (isset($topic_data['notify_status'])) ? $topic_data['notify_status'] : null;
338
			watch_topic_forum('topic', $s_watching_topic, $user->data['user_id'], $forum_id, $topic_id, $notify_status, $start, $topic_data['topic_title']);
0 ignored issues
show
Bug introduced by
The variable $user does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $forum_id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $topic_id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $start does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
339
340
			// Reset forum notification if forum notify is set
341
			if ($config['allow_forum_notify'] && $auth->acl_get('f_subscribe', $forum_id))
0 ignored issues
show
Bug introduced by
The variable $config does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $auth does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
342
			{
343
				$s_watching_forum = $s_watching_topic;
344
				watch_topic_forum('forum', $s_watching_forum, $user->data['user_id'], $forum_id, 0);
345
			}
346
		}
347
348
		$this->template->assign_vars(array(
349
			'U_WATCH_TOPIC'			=> $s_watching_topic['link'],
350
			'U_WATCH_TOPIC_TOGGLE'	=> $s_watching_topic['link_toggle'],
351
			'S_WATCH_TOPIC_TITLE'	=> $s_watching_topic['title'],
352
			'S_WATCH_TOPIC_TOGGLE'	=> $s_watching_topic['title_toggle'],
353
			'S_WATCHING_TOPIC'		=> $s_watching_topic['is_watching'],
354
		));
355
	}
356
}
357