Completed
Push — master ( 9866a1...02e5e5 )
by Daniel
08:18
created

forum_poll::_limit_by_group()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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