listener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
1
<?php
2
/**
3
 *
4
 * @package phpBB Extension - tas2580 AJAX Notifications
5
 * @copyright (c) 2016 tas2580 (https://tas2580.net)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace tas2580\ajaxnotification\event;
11
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
/**
15
* Event listener
16
*/
17
class listener implements EventSubscriberInterface
18
{
19
	/** @var \phpbb\config\config */
20
	protected $config;
21
22
	/** @var \phpbb\controller\helper */
23
	protected $helper;
24
25
	/** @var \phpbb\template\template */
26
	protected $template;
27
28
	/** @var \phpbb\user */
29
	protected $user;
30
31
	/**
32
	* Constructor
33
	*
34
	* @param \phpbb\config\config 			$config				Config object
35
	* @param \phpbb\controller\helper			$helper				Helper object
36
	* @param \phpbb\template\template		$template				Template object
37
	* @param \phpbb\user					$user				User object
38
	* @access public
39
	*/
40
	public function __construct(\phpbb\config\config $config, \phpbb\controller\helper $helper, \phpbb\template\template $template, \phpbb\user $user)
41
	{
42
		$this->config = $config;
43
		$this->helper = $helper;
44
		$this->template = $template;
45
		$this->user = $user;
46
	}
47
48
	/**
49
	* Assign functions defined in this class to event listeners in the core
50
	*
51
	* @return array
52
	* @static
53
	* @access public
54
	*/
55
	public static function getSubscribedEvents()
56
	{
57
		return array(
58
			'core.page_header'						=> 'page_header',
59
			'core.acp_board_config_edit_add'			=> 'acp_board_config_edit_add',
60
		);
61
	}
62
63
	/**
64
	* Send AJAX URL to template
65
	*
66
	* @return null
67
	* @access public
68
	*/
69
	public function page_header()
70
	{
71
		$this->template->assign_vars(array(
72
			'U_AJAXNOTIFICATION'			=> $this->helper->route('tas2580_ajaxnotification', array()),
73
			'U_AJAXNOTIFICATION_TIMER'		=> $this->config['ajaxnotification_timeout'] * 1000,
74
		));
75
	}
76
77
	/**
78
	* Add field to acp_board load settings page
79
	*
80
	* @param	object	$event	The event object
81
	* @return	null
82
	* @access	public
83
	*/
84
	public function acp_board_config_edit_add($event)
85
	{
86
		if ($event['mode'] == 'load')
87
		{
88
			$this->user->add_lang_ext('tas2580/ajaxnotification', 'common');
89
90
			$display_vars = $event['display_vars'];
91
			$insert = array('ajaxnotification_timeout' => array(
92
				'lang'		=> 'ACP_AJAXNOTIFICATION_TIMEOUT',
93
				'validate'	=> 'int:0:99999',
94
				'type'	=> 'number:0:99999',
95
				'explain'	=> true,
96
				'append'	=> ' ' . $this->user->lang['SECONDS']
97
			));
98
			$display_vars['vars'] = $this->array_insert($display_vars['vars'], 'legend2', $insert);
99
			$event['display_vars'] = $display_vars;
100
		}
101
	}
102
103
	private function array_insert(&$array, $position, $insert)
104
	{
105
		if (is_int($position))
106
		{
107
			array_splice($array, $position, 0, $insert);
108
		}
109
		else
110
		{
111
			$pos   = array_search($position, array_keys($array));
112
			$array = array_merge(
113
				array_slice($array, 0, $pos),
114
				$insert,
115
				array_slice($array, $pos)
116
			);
117
		}
118
		return $array;
119
	}
120
121
}
122