Completed
Push — master ( a960f0...783e4f )
by Matt
02:41 queued 13s
created

settings::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
1
<?php
2
/**
3
 *
4
 * Topic Image Preview. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2017, Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\TopicImagePreview\event;
12
13
use phpbb\config\config;
14
use phpbb\language\language;
15
use phpbb\request\request;
16
use phpbb\template\template;
17
use phpbb\user;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
20
/**
21
 * Topic Image Preview Event listener.
22
 */
23
class settings implements EventSubscriberInterface
24
{
25
	/** @var config */
26
	protected $config;
27
28
	/** @var language */
29
	protected $language;
30
31
	/** @var request */
32
	protected $request;
33
34
	/** @var template */
35
	protected $template;
36
37
	/** @var user */
38
	protected $user;
39
40
	/**
41
	 * {@inheritdoc}
42
	 */
43
	public static function getSubscribedEvents()
44
	{
45
		return [
46
			// ACP events
47
			'core.acp_board_config_edit_add'		=> 'update_acp_data',
48
			// UCP events
49
			'core.ucp_prefs_view_data'				=> 'handle_ucp_data',
50
			'core.ucp_prefs_view_update_data'		=> 'update_ucp_data',
51
		];
52
	}
53
54
	/**
55
	 * Constructor
56
	 *
57
	 * @param config   $config
58
	 * @param language $language
59
	 * @param request  $request
60
	 * @param template $template
61
	 * @param user     $user
62
	 */
63
	public function __construct(config $config, language $language, request $request, template $template, user $user)
64
	{
65
		$this->config = $config;
66
		$this->language = $language;
67
		$this->request = $request;
68
		$this->template = $template;
69
		$this->user = $user;
70
	}
71
72
	/**
73
	 * Add ACP config options to Post settings.
74
	 *
75
	 * @param \phpbb\event\data $event The event object
76
	 *
77
	 * @return void
78
	 */
79
	public function update_acp_data($event)
80
	{
81
		if ($event['mode'] === 'post' && array_key_exists('legend3', $event['display_vars']['vars']))
82
		{
83
			$this->language->add_lang('tip_acp', 'vse/TopicImagePreview');
84
85
			$my_config_vars = [
86
				'legend_vse_tip'	=> 'ACP_TIP_TITLE',
87
				'vse_tip_new'		=> ['lang' => 'ACP_TIP_DISPLAY_AGE', 'validate' => 'bool', 'type' => 'custom', 'function' => [$this, 'select_vse_tip_new'], 'explain' => true],
88
				'vse_tip_num'		=> ['lang' => 'ACP_TIP_DISPLAY_NUM', 'validate' => 'int:0:99', 'type' => 'number:0:99', 'explain' => true],
89
				'vse_tip_dim'		=> ['lang' => 'ACP_TIP_DISPLAY_DIM', 'validate' => 'int:0:999', 'type' => 'number:0:999', 'explain' => true, 'append' => ' ' . $this->language->lang('PIXEL')],
90
			];
91
92
			$event->update_subarray('display_vars', 'vars', phpbb_insert_config_array($event['display_vars']['vars'], $my_config_vars, ['before' => 'legend3']));
93
		}
94
	}
95
96
	/**
97
	 * Create custom radio buttons.
98
	 *
99
	 * @param mixed  $value
100
	 * @param string $key
101
	 *
102
	 * @return string
103
	 */
104
	public function select_vse_tip_new($value, $key = '')
105
	{
106
		$radio_ary = [1 => 'ACP_TIP_NEWEST_POST', 0 => 'ACP_TIP_OLDEST_POST'];
107
108
		return h_radio('config[vse_tip_new]', $radio_ary, $value, $key);
109
	}
110
111
	/**
112
	 * Get user's Topic Image Preview option and display it in UCP Preferences page
113
	 *
114
	 * @param \phpbb\event\data $event The event object
115
	 *
116
	 * @return void
117
	 */
118
	public function handle_ucp_data($event)
119
	{
120
		// Request the user option vars and add them to the data array
121
		$event->update_subarray(
122
			'data',
123
			'user_vse_tip',
124
			$this->request->variable('user_vse_tip', (int) $this->user->data['user_vse_tip'])
125
		);
126
127
		// Output the data vars to the template (except on form submit)
128
		if (!$event['submit'])
129
		{
130
			$this->language->add_lang('tip_ucp', 'vse/TopicImagePreview');
131
			$this->template->assign_vars([
132
				'S_VSE_TIP_NUM' => $this->config->offsetGet('vse_tip_num'),
133
				'S_VSE_TIP_USER' => $event['data']['user_vse_tip'],
134
			]);
135
		}
136
	}
137
138
	/**
139
	 * Add user's Topic Image Preview option state into UCP sql_array
140
	 *
141
	 * @param \phpbb\event\data $event The event object
142
	 *
143
	 * @return void
144
	 */
145
	public function update_ucp_data($event)
146
	{
147
		$event->update_subarray('sql_ary', 'user_vse_tip', $event['data']['user_vse_tip']);
148
	}
149
}
150