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