Skip to content

Commit 249d662

Browse files
committed
Merge pull request #8 from Pico/config
Add config and migration
2 parents 827d2d6 + d9e14c6 commit 249d662

4 files changed

Lines changed: 158 additions & 3 deletions

File tree

config/services.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ services:
44
arguments:
55
- @config
66
- @template
7+
- @user
78
tags:
89
- { name: event.listener }

event/listener.php

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,23 @@ class listener implements EventSubscriberInterface
2323
/** @var \phpbb\template\template */
2424
protected $template;
2525

26+
/** @var \phpbb\user */
27+
protected $user;
28+
2629
/**
2730
* Constructor
2831
*
2932
* @param \phpbb\config\config $config Config object
3033
* @param \phpbb\template\template $template Template object
31-
* @return \phpbb\boardrules\event\listener
34+
* @param \phpbb\user $user User object
35+
* @return \phpbb\googleanalytics\event\listener
3236
* @access public
3337
*/
34-
public function __construct(\phpbb\config\config $config, \phpbb\template\template $template)
38+
public function __construct(\phpbb\config\config $config, \phpbb\template\template $template, \phpbb\user $user)
3539
{
3640
$this->config = $config;
3741
$this->template = $template;
42+
$this->user = $user;
3843
}
3944

4045
/**
@@ -47,7 +52,9 @@ public function __construct(\phpbb\config\config $config, \phpbb\template\templa
4752
static public function getSubscribedEvents()
4853
{
4954
return array(
50-
'core.page_header' => 'load_google_analytics',
55+
'core.acp_board_config_edit_add' => 'add_googleanalytics_configs',
56+
'core.page_header' => 'load_google_analytics',
57+
'core.validate_config_variable' => 'validate_googleanalytics_id',
5158
);
5259
}
5360

@@ -62,4 +69,77 @@ public function load_google_analytics($event)
6269
{
6370
$this->template->assign_var('GOOGLEANALYTICS_ID', $this->config['googleanalytics_id']);
6471
}
72+
73+
/**
74+
* Add config vars to ACP Board Settings
75+
*
76+
* @param object $event The event object
77+
* @return null
78+
* @access public
79+
*/
80+
public function add_googleanalytics_configs($event)
81+
{
82+
// Load language file
83+
$this->user->add_lang_ext('phpbb/googleanalytics', 'googleanalytics_acp');
84+
85+
// Add a config to the settings mode, after override_user_style
86+
if ($event['mode'] == 'settings' && isset($event['display_vars']['vars']['override_user_style']))
87+
{
88+
// Store display_vars event in a local variable
89+
$display_vars = $event['display_vars'];
90+
91+
// Define the new config vars
92+
$ga_config_vars = array(
93+
'googleanalytics_id' => array(
94+
'lang' => 'ACP_GOOGLEANALYTICS_ID',
95+
'validate' => 'googleanalytics_id',
96+
'type' => 'text:40:20',
97+
'explain' => true,
98+
),
99+
);
100+
101+
// Insert the config vars after override_user_style
102+
$insert_after = 'override_user_style';
103+
104+
// Rebuild new config var array
105+
$position = array_search($insert_after, array_keys($display_vars['vars'])) + 1;
106+
$display_vars['vars'] = array_merge(
107+
array_slice($display_vars['vars'], 0, $position),
108+
$ga_config_vars,
109+
array_slice($display_vars['vars'], $position)
110+
);
111+
112+
// Update the display_vars event with the new array
113+
$event['display_vars'] = $display_vars;
114+
}
115+
}
116+
117+
118+
/**
119+
* Validate the Google Analytics ID
120+
*
121+
* @param object $event The event object
122+
* @return null
123+
* @access public
124+
*/
125+
public function validate_googleanalytics_id($event)
126+
{
127+
$input = $event['cfg_array']['googleanalytics_id'];
128+
129+
// Check if the validate test is for google_analytics
130+
if (($event['config_definition']['validate'] == 'googleanalytics_id') && ($input !== ''))
131+
{
132+
// Store the error and input event data
133+
$error = $event['error'];
134+
135+
// Add error message if the input is not a valid Google Analytics ID
136+
if (!preg_match('/^UA-\d{4,9}-\d{1,4}$/', $input))
137+
{
138+
$error[] = $this->user->lang('ACP_GOOGLEANALYTICS_ID_INVALID', $input);
139+
}
140+
141+
// Update error event data
142+
$event['error'] = $error;
143+
}
144+
}
65145
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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' => 'Enter your Google Analytics ID code, e.g.: <samp>UA-0000000-00</samp>.',
43+
'ACP_GOOGLEANALYTICS_ID_INVALID' => '“%s” is not a valid Google Analytics ID code.<br />It should be in the form “UA-0000000-00”.',
44+
));
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
namespace phpbb\googleanalytics\migrations\v10x;
12+
13+
/**
14+
* Migration stage 1: Initial data changes to the database
15+
*/
16+
class m1_initial_data extends \phpbb\db\migration\migration
17+
{
18+
/**
19+
* Add Google Analytics data to the database.
20+
*
21+
* @return array Array of table data
22+
* @access public
23+
*/
24+
public function update_data()
25+
{
26+
return array(
27+
array('config.add', array('googleanalytics_id', '')),
28+
);
29+
}
30+
}

0 commit comments

Comments
 (0)