@@ -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}
0 commit comments