Completed
Pull Request — master (#2)
by Jakub
04:32
created

admin_controller::list_ads()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 23
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 17
nc 2
nop 0
crap 12
1
<?php
2
/**
3
*
4
* Pages extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 phpBB Limited <https://www.phpbb.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace phpbb\admanagement\controller;
12
13
/**
14
* Admin controller
15
*/
16
class admin_controller
17
{
18
	/** @var \phpbb\db\driver\driver_interface */
19
	protected $db;
20
21
	/** @var \phpbb\template\template */
22
	protected $template;
23
24
	/** @var \phpbb\user */
25
	protected $user;
26
27
	/** @var \phpbb\request\request */
28
	protected $request;
29
30
	/** @var string ads_table */
31
	protected $ads_table;
32
33
	/** @var string Custom form action */
34
	protected $u_action;
35
36
	/**
37
	* Constructor
38
	*
39
	* @param \phpbb\db\driver\driver_interface	$db			DB driver interface
40
	* @param \phpbb\template\template			$template	Template object
41
	* @param \phpbb\user						$user		User object
42
	* @param \phpbb\request\request				$request	Request object
43
	* @param string								$ads_table	Ads table
44
	* @access public
45
	*/
46
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, $ads_table)
47
	{
48
		$this->db = $db;
49
		$this->template = $template;
50
		$this->user = $user;
51
		$this->request = $request;
52
		$this->ads_table = $ads_table;
53
	}
54
55
	/**
56
	* Set page url
57
	*
58
	* @param string $u_action Custom form action
59
	* @return void
60
	* @access public
61
	*/
62
	public function set_page_url($u_action)
63
	{
64
		$this->u_action = $u_action;
65
	}
66
67
	/**
68
	* Get geric ACP page title for Ads module
69
	*
70
	* @return string	Language string for Ads ACP module
71
	* @access public
72
	*/
73
	public function get_page_title()
74
	{
75
		return $this->user->lang('ACP_ADMANAGEMENT_TITLE');
76
	}
77
78
	/**
79
	* Get action
80
	*
81
	* @return string	Language string for Ads ACP module
82
	* @access public
83
	*/
84
	public function get_action()
85
	{
86
		return $this->request->variable('action', '');
87
	}
88
89
	/**
90
	* Display the ads
91
	*
92
	* @return void
93
	* @access public
94
	*/
95
	public function list_ads()
96
	{
97
		$sql = 'SELECT ad_id, ad_name, ad_enabled
98
			FROM ' . $this->ads_table;
99
		$result = $this->db->sql_query($sql);
100
		while ($row = $this->db->sql_fetchrow($result))
101
		{
102
			$ad_enabled = (bool) $row['ad_enabled'];
103
104
			$this->template->assign_block_vars('ads', [
105
				'NAME'		=> $row['ad_name'],
106
				'ENABLED'	=> $ad_enabled,
107
				'U_ENABLE'	=> $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'], // TODO: ACP method
108
				'U_PREVIEW'	=> '', // TODO: frontend logic
109
				'U_EDIT'	=> $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'], // TODO: ACP method
110
				'U_DELETE'	=> $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'], // TODO: ACP method
111
			]);
112
		}
113
		$this->db->sql_freeresult($result);
114
115
		// Set output vars for display in the template
116
		$this->template->assign_vars(array(
117
			'U_ACTION'	=> $this->u_action,
118
			'U_ADD_AD'	=> $this->u_action . '&amp;action=add',
119
		));
120
	}
121
}
122