Completed
Push — master ( c96b51...1deced )
by Daniel
08:48
created

forum_poll::_limit_by_user()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 3
nc 2
nop 1
crap 2
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
	/**
30
	 * Constructor
31
	 *
32
	 * @param \phpbb\db\driver\driver_interface			$db	 				Database connection
33
	 * @param \blitze\sitemaker\services\forum\data		$forum_data			Forum Data object
34
	 * @param \blitze\sitemaker\services\forum\options	$forum_options		Forum Options Object
35
	 * @param \blitze\sitemaker\services\groups			$groups				Groups Object
36
	 * @param \blitze\sitemaker\services\poll			$poll				Poll Object
37
	 */
38 3
	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)
39
	{
40 3
		$this->db = $db;
41 3
		$this->forum_data = $forum_data;
42 3
		$this->forum_options = $forum_options;
43 3
		$this->groups = $groups;
44 3
		$this->poll = $poll;
45 3
	}
46
47
	/**
48
	 * {@inheritdoc}
49
	 */
50 1
	public function get_config(array $settings)
51
	{
52 1
		$forum_options = $this->forum_options->get_all();
53 1
		$group_options = $this->groups->get_data();
54 1
		$topic_type_options = array(POST_NORMAL => 'POST_NORMAL', POST_STICKY => 'POST_STICKY', POST_ANNOUNCE => 'POST_ANNOUNCEMENT', POST_GLOBAL => 'POST_GLOBAL');
55 1
		$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');
56
57
		return array(
58 1
			'legend1'		=> 'SETTINGS',
59 1
			'user_ids'		=> array('lang' => 'POLL_FROM_USERS', 'validate' => 'string', 'type' => 'textarea:3:40', 'maxlength' => 2, 'explain' => true, 'default' => ''),
60 1
			'group_ids'		=> array('lang' => 'POLL_FROM_GROUPS', 'validate' => 'string', 'type' => 'multi_select', 'options' => $group_options, 'default' => array(), 'explain' => true),
61 1
			'topic_ids'		=> array('lang' => 'POLL_FROM_TOPICS', 'validate' => 'string', 'type' => 'textarea:3:40', 'maxlength' => 2, 'explain' => true, 'default' => ''),
62 1
			'forum_ids'		=> array('lang' => 'POLL_FROM_FORUMS', 'validate' => 'string', 'type' => 'multi_select', 'options' => $forum_options, 'default' => array(), 'explain' => true),
63 1
			'topic_type'	=> array('lang' => 'TOPIC_TYPE', 'validate' => 'string', 'type' => 'checkbox', 'options' => $topic_type_options, 'default' => array(POST_NORMAL), 'explain' => false),
64 1
			'order_by'		=> array('lang' => 'ORDER_BY', 'validate' => 'string', 'type' => 'select', 'options' => $sort_options, 'default' => 0, 'explain' => false),
65 1
		);
66
	}
67
68
	/**
69
	 * {@inheritdoc}
70
	 */
71 2
	public function display(array $bdata, $edit_mode = false)
72
	{
73 2
		$this->settings = $bdata['settings'];
0 ignored issues
show
Bug introduced by
The property settings does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
74 2
		$title = 'POLL';
75
76 2
		if (!($topic_data = $this->_get_topic_data()))
77 2
		{
78
			return array(
79
				'title'		=> $title,
80
				'content'	=> '',
81
			);
82
		}
83
84 2
		$this->poll->build($topic_data, $this->ptemplate);
85
86
		return array(
87 2
			'title'		=> $title,
88 2
			'content'	=> $this->ptemplate->render_view('blitze/sitemaker', 'blocks/forum_poll.html', 'forum_poll_block')
89 2
		);
90
	}
91
92
	/**
93
	 * @return array|null
94
	 */
95 2
	private function _get_topic_data()
96
	{
97
		$sql_array = array(
98
			'WHERE'		=> array(
99 2
				't.poll_start <> 0',
100 2
			),
101 2
		);
102
103 2
		$this->_limit_by_user($sql_array);
104 2
		$this->_limit_by_topic($sql_array);
105 2
		$this->_limit_by_group($sql_array);
106
107 2
		$this->forum_data->query()
108 2
			->fetch_forum($this->settings['forum_ids'])
109 2
			->fetch_topic_type($this->settings['topic_type'])
110 2
			->set_sorting($this->_get_sorting())
111 2
			->fetch_custom($sql_array)
112 2
			->build();
113 2
		$topic_data = $this->forum_data->get_topic_data(1);
114
115 2
		return array_shift($topic_data);
116
	}
117
118
	/**
119
	 * @param array $sql_array
120
	 */
121 2
	private function _limit_by_user(array &$sql_array)
122
	{
123 2
		$from_users_ary = array_filter(explode(',', str_replace(' ', '', $this->settings['user_ids'])));
124 2
		$sql_array['WHERE'][] = (sizeof($from_users_ary)) ? $this->db->sql_in_set('t.topic_poster', $from_users_ary) : '';
125 2
	}
126
127
	/**
128
	 * @param array $sql_array
129
	 */
130 2
	private function _limit_by_topic(array &$sql_array)
131
	{
132 2
		$from_topics_ary = array_filter(explode(',', str_replace(' ', '', $this->settings['topic_ids'])));
133 2
		$sql_array['WHERE'][] = (sizeof($from_topics_ary)) ? $this->db->sql_in_set('t.topic_id', $from_topics_ary) : '';
134 2
	}
135
136
	/**
137
	 * @param array $sql_array
138
	 */
139 2
	private function _limit_by_group(array &$sql_array)
140
	{
141 2
		if (!empty($this->settings['group_ids']))
142 2
		{
143 1
			$sql_array['FROM'][USER_GROUP_TABLE] = 'ug';
144 1
			$sql_array['WHERE'][] = 't.topic_poster = ug.user_id';
145 1
			$sql_array['WHERE'][] = $this->db->sql_in_set('ug.group_id', $this->settings['group_ids']);
146 1
		}
147 2
	}
148
149
	/**
150
	 * @return string
151
	 */
152 2
	private function _get_sorting()
153
	{
154
		$sort_order = array(
155 2
			FORUMS_ORDER_FIRST_POST		=> 't.topic_time',
156 2
			FORUMS_ORDER_LAST_POST		=> 't.topic_last_post_time',
157 2
			FORUMS_ORDER_LAST_READ		=> 't.topic_last_view_time'
158 2
		);
159
160 2
		return (isset($sort_order[$this->settings['order_by']])) ? $sort_order[$this->settings['order_by']] : 'RAND()';
161
	}
162
}
163