Completed
Push — master ( 73b87b...be40cf )
by Daniel
10:47
created

forum_poll::get_sorting()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 0
cts 9
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 6
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\sitemaker\blocks;
11
12
use blitze\sitemaker\services\blocks\driver\block;
13
14
class forum_poll extends block
15
{
16
	/** @var \phpbb\db\driver\driver_interface */
17
	protected $db;
18
19
	/** @var \blitze\sitemaker\services\forum\data */
20
	protected $forum_data;
21
22
	/** @var \blitze\sitemaker\services\forum\options */
23
	protected $forum_options;
24
25
	/** @var \blitze\sitemaker\services\groups */
26
	protected $groups;
27
28
	/** @var \blitze\sitemaker\services\poll */
29
	protected $poll;
30
31
	/** @var array */
32
	protected $settings;
33
34
	const FORUMS_ORDER_FIRST_POST = 0;
35
	const FORUMS_ORDER_LAST_POST = 1;
36
	const FORUMS_ORDER_LAST_READ = 2;
37
38
	/**
39
	 * Constructor
40
	 *
41
	 * @param \phpbb\db\driver\driver_interface			$db	 				Database connection
42
	 * @param \blitze\sitemaker\services\forum\data		$forum_data			Forum Data object
43
	 * @param \blitze\sitemaker\services\forum\options	$forum_options		Forum Options Object
44
	 * @param \blitze\sitemaker\services\groups			$groups				Groups Object
45
	 * @param \blitze\sitemaker\services\poll			$poll				Poll Object
46
	 */
47
	public function __construct(\phpbb\db\driver\driver_interface $db, \blitze\sitemaker\services\forum\data $forum_data, \blitze\sitemaker\services\forum\options $forum_options, \blitze\sitemaker\services\groups $groups, \blitze\sitemaker\services\poll $poll)
48
	{
49
		$this->db = $db;
50
		$this->forum_data = $forum_data;
51
		$this->forum_options = $forum_options;
52
		$this->groups = $groups;
53
		$this->poll = $poll;
54
	}
55
56
	/**
57
	 * {@inheritdoc}
58
	 */
59
	public function get_config(array $settings)
60
	{
61
		$forum_options = $this->forum_options->get_all();
62
		$group_options = $this->groups->get_data();
63
		$topic_type_options = array(POST_NORMAL => 'POST_NORMAL', POST_STICKY => 'POST_STICKY', POST_ANNOUNCE => 'POST_ANNOUNCEMENT', POST_GLOBAL => 'POST_GLOBAL');
64
		$sort_options = array('' => 'RANDOM', self::FORUMS_ORDER_FIRST_POST	=> 'FIRST_POST_TIME', self::FORUMS_ORDER_LAST_POST => 'LAST_POST_TIME', self::FORUMS_ORDER_LAST_READ => 'LAST_READ_TIME');
65
66
		return array(
67
			'legend1'		=> 'SETTINGS',
68
			'user_ids'		=> array('lang' => 'POLL_FROM_USERS', 'validate' => 'string', 'type' => 'textarea:3:40', 'maxlength' => 2, 'explain' => true, 'default' => ''),
69
			'group_ids'		=> array('lang' => 'POLL_FROM_GROUPS', 'validate' => 'string', 'type' => 'multi_select', 'options' => $group_options, 'default' => array(), 'explain' => true),
70
			'topic_ids'		=> array('lang' => 'POLL_FROM_TOPICS', 'validate' => 'string', 'type' => 'textarea:3:40', 'maxlength' => 2, 'explain' => true, 'default' => ''),
71
			'forum_ids'		=> array('lang' => 'POLL_FROM_FORUMS', 'validate' => 'string', 'type' => 'multi_select', 'options' => $forum_options, 'default' => array(), 'explain' => true),
72
			'topic_type'	=> array('lang' => 'TOPIC_TYPE', 'validate' => 'string', 'type' => 'checkbox', 'options' => $topic_type_options, 'default' => array(POST_NORMAL), 'explain' => false),
73
			'order_by'		=> array('lang' => 'ORDER_BY', 'validate' => 'string', 'type' => 'select', 'options' => $sort_options, 'default' => 0, 'explain' => false),
74
		);
75
	}
76
77
	/**
78
	 * {@inheritdoc}
79
	 */
80
	public function display(array $bdata, $edit_mode = false)
81
	{
82
		$this->settings = $bdata['settings'];
83
		$title = 'POLL';
84
85
		if (!($topic_data = $this->get_topic_data()))
86
		{
87
			return array(
88
				'title'		=> $title,
89
				'content'	=> '',
90
			);
91
		}
92
93
		$this->poll->build($topic_data, $this->ptemplate);
94
95
		return array(
96
			'title'		=> $title,
97
			'content'	=> $this->ptemplate->render_view('blitze/sitemaker', 'blocks/forum_poll.html', 'forum_poll_block')
98
		);
99
	}
100
101
	/**
102
	 * @return array|null
103
	 */
104
	private function get_topic_data()
105
	{
106
		$sql_array = array(
107
			'WHERE'		=> array(
108
				't.poll_start <> 0',
109
			),
110
		);
111
112
		$this->limit_by_group($sql_array);
113
114
		$this->forum_data->query()
115
			->fetch_forum($this->settings['forum_ids'])
116
			->fetch_topic_type($this->settings['topic_type'])
117
			->fetch_topic($this->get_array($this->settings['topic_ids']))
118
			->fetch_topic_poster($this->get_array($this->settings['user_ids']))
119
			->set_sorting($this->get_sorting())
120
			->fetch_custom($sql_array)
121
			->build();
122
		$topic_data = $this->forum_data->get_topic_data(1);
123
124
		return array_shift($topic_data);
125
	}
126
127
	/**
128
	 * @param array $sql_array
129
	 */
130
	private function limit_by_group(array &$sql_array)
131
	{
132
		if (!empty($this->settings['group_ids']))
133
		{
134
			$sql_array['FROM'][USER_GROUP_TABLE] = 'ug';
135
			$sql_array['WHERE'][] = 't.topic_poster = ug.user_id';
136
			$sql_array['WHERE'][] = $this->db->sql_in_set('ug.group_id', $this->settings['group_ids']);
137
		}
138
	}
139
140
	/**
141
	 * @param string $string
142
	 * @return array
143
	 */
144
	private function get_array($string)
145
	{
146
		return array_filter(explode(',', str_replace(' ', '', $string)));
147
	}
148
149
	/**
150
	 * @return string
151
	 */
152
	private function get_sorting()
153
	{
154
		$sort_order = array(
155
			self::FORUMS_ORDER_FIRST_POST		=> 't.topic_time',
156
			self::FORUMS_ORDER_LAST_POST		=> 't.topic_last_post_time',
157
			self::FORUMS_ORDER_LAST_READ		=> 't.topic_last_view_time'
158
		);
159
160
		return (isset($sort_order[$this->settings['order_by']])) ? $sort_order[$this->settings['order_by']] : 'RAND()';
161
	}
162
}
163