Passed
Push — develop ( b3eda6...9f2d35 )
by Daniel
03:59 queued 40s
created

forum_poll::display()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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