Completed
Push — develop ( de7659...415144 )
by Daniel
09:44
created

forum_poll::get_config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 17
ccs 13
cts 13
cp 1
rs 9.4285
cc 1
eloc 13
nc 1
nop 1
crap 1
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 4
	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 4
		$this->db = $db;
50 4
		$this->forum_data = $forum_data;
51 4
		$this->forum_options = $forum_options;
52 4
		$this->groups = $groups;
53 4
		$this->poll = $poll;
54 4
	}
55
56
	/**
57
	 * {@inheritdoc}
58
	 */
59 1
	public function get_config(array $settings)
60
	{
61 1
		$forum_options = $this->forum_options->get_all();
62 1
		$group_options = $this->groups->get_data();
63 1
		$topic_type_options = array(POST_NORMAL => 'POST_NORMAL', POST_STICKY => 'POST_STICKY', POST_ANNOUNCE => 'POST_ANNOUNCEMENT', POST_GLOBAL => 'POST_GLOBAL');
64 1
		$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 1
			'legend1'		=> 'SETTINGS',
68 1
			'user_ids'		=> array('lang' => 'POLL_FROM_USERS', 'validate' => 'string', 'type' => 'textarea:3:40', 'maxlength' => 2, 'explain' => true, 'default' => ''),
69 1
			'group_ids'		=> array('lang' => 'POLL_FROM_GROUPS', 'validate' => 'string', 'type' => 'multi_select', 'options' => $group_options, 'default' => array(), 'explain' => true),
70 1
			'topic_ids'		=> array('lang' => 'POLL_FROM_TOPICS', 'validate' => 'string', 'type' => 'textarea:3:40', 'maxlength' => 2, 'explain' => true, 'default' => ''),
71 1
			'forum_ids'		=> array('lang' => 'POLL_FROM_FORUMS', 'validate' => 'string', 'type' => 'multi_select', 'options' => $forum_options, 'default' => array(), 'explain' => true),
72 1
			'topic_type'	=> array('lang' => 'TOPIC_TYPE', 'validate' => 'string', 'type' => 'checkbox', 'options' => $topic_type_options, 'default' => array(POST_NORMAL), 'explain' => false),
73 1
			'order_by'		=> array('lang' => 'ORDER_BY', 'validate' => 'string', 'type' => 'select', 'options' => $sort_options, 'default' => 0, 'explain' => false),
74 1
		);
75
	}
76
77
	/**
78
	 * {@inheritdoc}
79
	 */
80 3
	public function display(array $bdata, $edit_mode = false)
81
	{
82 3
		$this->settings = $bdata['settings'];
83 3
		$title = 'POLL';
84
85 3
		if (!($topic_data = $this->get_topic_data()))
86 3
		{
87
			return array(
88 1
				'title'		=> $title,
89 1
				'content'	=> '',
90 1
			);
91
		}
92
93 2
		$this->poll->build($topic_data, $this->ptemplate);
94
95
		return array(
96 2
			'title'		=> $title,
97 2
			'content'	=> $this->ptemplate->render_view('blitze/sitemaker', 'blocks/forum_poll.html', 'forum_poll_block')
98 2
		);
99
	}
100
101
	/**
102
	 * @return array|null
103
	 */
104 3
	private function get_topic_data()
105
	{
106
		$sql_array = array(
107
			'WHERE'		=> array(
108 3
				't.poll_start <> 0',
109 3
			),
110 3
		);
111
112 3
		$this->limit_by_group($sql_array);
113
114 3
		$this->forum_data->query()
115 3
			->fetch_forum($this->settings['forum_ids'])
116 3
			->fetch_topic_type($this->settings['topic_type'])
117 3
			->fetch_topic($this->get_array($this->settings['topic_ids']))
118 3
			->fetch_topic_poster($this->get_array($this->settings['user_ids']))
119 3
			->set_sorting($this->get_sorting())
120 3
			->fetch_custom($sql_array)
121 3
			->build();
122 3
		$topic_data = $this->forum_data->get_topic_data(1);
123
124 3
		return array_shift($topic_data);
125
	}
126
127
	/**
128
	 * @param array $sql_array
129
	 */
130 3
	private function limit_by_group(array &$sql_array)
131
	{
132 3
		if (!empty($this->settings['group_ids']))
133 3
		{
134 1
			$sql_array['FROM'][USER_GROUP_TABLE] = 'ug';
135 1
			$sql_array['WHERE'][] = 't.topic_poster = ug.user_id';
136 1
			$sql_array['WHERE'][] = $this->db->sql_in_set('ug.group_id', $this->settings['group_ids']);
137 1
		}
138 3
	}
139
140
	/**
141
	 * @param string $string
142
	 * @return array
143
	 */
144 3
	private function get_array($string)
145
	{
146 3
		return array_filter(explode(',', str_replace(' ', '', $string)));
147
	}
148
149
	/**
150
	 * @return string
151
	 */
152 3
	private function get_sorting()
153
	{
154
		$sort_order = array(
155 3
			self::FORUMS_ORDER_FIRST_POST		=> 't.topic_time',
156 3
			self::FORUMS_ORDER_LAST_POST		=> 't.topic_last_post_time',
157 3
			self::FORUMS_ORDER_LAST_READ		=> 't.topic_last_view_time'
158 3
		);
159
160 3
		return (isset($sort_order[$this->settings['order_by']])) ? $sort_order[$this->settings['order_by']] : 'RAND()';
161
	}
162
}
163