Skip to content

Commit 26cb29b

Browse files
committed
Add setting
1 parent 2f7ea0f commit 26cb29b

2 files changed

Lines changed: 115 additions & 1 deletion

File tree

event/listener.php

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public function __construct(\phpbb\config\config $config, \phpbb\template\templa
4747
static public function getSubscribedEvents()
4848
{
4949
return array(
50-
'core.page_header' => 'load_google_analytics',
50+
'core.page_header' => 'load_google_analytics',
51+
'core.acp_board_config_edit_add' => 'add_googleanalytics_configs',
52+
'core.validate_config_variable' => 'validate_googleanalytics_id',
5153
);
5254
}
5355

@@ -62,4 +64,73 @@ public function load_google_analytics($event)
6264
{
6365
$this->template->assign_var('GOOGLEANALYTICS_ID', $this->config['googleanalytics_id']);
6466
}
67+
68+
/**
69+
* Add config vars to ACP Board Settings
70+
*
71+
* @param object $event The event object
72+
* @return null
73+
* @access public
74+
*/
75+
public function add_googleanalytics_configs($event)
76+
{
77+
// Add a config to the settings mode, after override_user_style
78+
if ($event['mode'] == 'settings' && isset($event['display_vars']['vars']['override_user_style']))
79+
{
80+
// Store display_vars event in a local variable
81+
$display_vars = $event['display_vars'];
82+
83+
// Define the new config vars
84+
$my_config_vars = array(
85+
'googleanalytics_id' => array(
86+
'lang' => 'GOOGLEANALYTICS_ID',
87+
'validate' => 'googleanayltics_id',
88+
'type' => 'text:40:255',
89+
'explain' => true,
90+
),
91+
);
92+
93+
// Insert the config vars after...
94+
$insert_after = 'override_user_style';
95+
96+
// Rebuild new config var array
97+
$position = array_search($insert_after, array_keys($display_vars['vars'])) + 1;
98+
$display_vars['vars'] = array_merge(
99+
array_slice($display_vars['vars'], 0, $position),
100+
$my_config_vars,
101+
array_slice($display_vars['vars'], $position)
102+
);
103+
104+
// Update the display_vars event with the new array
105+
$event['display_vars'] = $display_vars;
106+
}
107+
}
108+
109+
110+
/**
111+
* Validate the Google Analytics ID
112+
*
113+
* @param object $event The event object
114+
* @return null
115+
* @access public
116+
*/
117+
public function validate_googleanalytics_id($event)
118+
{
119+
// Check if the validate test is for google_analytics
120+
if ($event['config_definition']['validate'] == 'googleanayltics_id')
121+
{
122+
// Store the error and input event data
123+
$error = $event['error'];
124+
$input = $event['cfg_array'][$event['config_name']];
125+
126+
// Add error message if the input is not a valid Google Analytics ID
127+
if (!preg_match('/^ua-\d{4,9}-\d{1,4}$/i', strval($input)))
128+
{
129+
$error[] = $this->user->lang('GOOGLEANALYTICS_ID_INVALID', $input);
130+
}
131+
132+
// Update error event data
133+
$event['error'] = $error;
134+
}
135+
}
65136
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
*
4+
* Google Analytics 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+
/**
12+
* DO NOT CHANGE
13+
*/
14+
if (!defined('IN_PHPBB'))
15+
{
16+
exit;
17+
}
18+
19+
if (empty($lang) || !is_array($lang))
20+
{
21+
$lang = array();
22+
}
23+
24+
// DEVELOPERS PLEASE NOTE
25+
//
26+
// All language files should use UTF-8 as their encoding and the files must not contain a BOM.
27+
//
28+
// Placeholders can now contain order information, e.g. instead of
29+
// 'Page %s of %s' you can (and should) write 'Page %1$s of %2$s', this allows
30+
// translators to re-order the output of data while ensuring it remains correct
31+
//
32+
// You do not need this where single placeholders are used, e.g. 'Message %d' is fine
33+
// equally where a string contains only two placeholders which are used to wrap text
34+
// in a url you again do not need to specify an order e.g., 'Click %sHERE%s' is fine
35+
//
36+
// Some characters you may want to copy&paste:
37+
// ’ » “ ” …
38+
//
39+
40+
$lang = array_merge($lang, array(
41+
'ACP_GOOGLEANALYTICS_ID' => 'Google Analytics ID',
42+
'ACP_GOOGLEANALYTICS_ID_EXPLAIN' => 'Google Analytics tracking ID.',
43+
));

0 commit comments

Comments
 (0)