Updated the Google Analytics and Permissions helper functions.

This commit is contained in:
Alex Tselegidis 2021-10-28 13:59:27 +02:00
parent 8ca881110b
commit adc6d00bb5
2 changed files with 28 additions and 28 deletions

View file

@ -20,41 +20,40 @@
*/ */
function google_analytics_script() function google_analytics_script()
{ {
$CI =& get_instance(); $google_analytics_code = setting('google_analytics_code');
$CI->load->model('settings_model'); if ($google_analytics_code !== '')
{
$google_analytics_code = $CI->settings_model->get_setting('google_analytics_code'); // If the Google Analytics code starts with UA then it is a Universal Analytics Property and the script stays
// the legacy one.
if ($google_analytics_code !== '') { if (substr($google_analytics_code, 0, 2) === 'UA')
{
// If the google analytics code starts with UA then it is a Universal Analytics Property and the script stays
// the legacy one
if (substr($google_analytics_code, 0, 2) === "UA") {
echo ' echo '
<script> <script>
(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){ (function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,"script","//www.google-analytics.com/analytics.js","ga"); })(window,document,"script","//www.google-analytics.com/analytics.js","ga");
ga("create", "' . $google_analytics_code . '", "auto"); ga("create", "' . $google_analytics_code . '", "auto");
ga("send", "pageview"); ga("send", "pageview");
</script> </script>
'; ';
} }
// If the google analytics code starts with a G then it is a Google Analytics 4-Property and the script // If the Google Analytics code starts with a G then it is a Google Analytics 4-Property and the script
// to inject it looks different. // to inject it looks different.
if (substr($google_analytics_code, 0, 2) === "G-") { if (substr($google_analytics_code, 0, 2) === 'G-')
{
echo ' echo '
<script async src="https://www.googletagmanager.com/gtag/js?id=' . $google_analytics_code . '"></script> <script async src="https://www.googletagmanager.com/gtag/js?id=' . $google_analytics_code . '"></script>
<script> <script>
window.dataLayer = window.dataLayer || []; window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);} function gtag(){dataLayer.push(arguments);}
gtag("js", new Date()); gtag("js", new Date());
gtag("config", "' . $google_analytics_code . '"); gtag("config", "' . $google_analytics_code . '");
</script> </script>
'; ';
} }
} }
} }

View file

@ -32,7 +32,7 @@ if ( ! function_exists('can'))
$CI = &get_instance(); $CI = &get_instance();
$CI->load->model('roles_model'); $CI->load->model('roles_model');
$CI->load->model('user_model'); $CI->load->model('users_model');
if (empty($user_id)) if (empty($user_id))
{ {
@ -40,8 +40,9 @@ if ( ! function_exists('can'))
} }
else else
{ {
$user = $CI->user_model->get_user($user_id); $user = $CI->users_model->find($user_id);
$role_slug = $CI->roles_model->value('slug', $user['id_roles']);
$role_slug = $CI->roles_model->value($user['id_roles'], 'slug');
} }
if (empty($role_slug)) if (empty($role_slug))
@ -49,7 +50,7 @@ if ( ! function_exists('can'))
return FALSE; return FALSE;
} }
$permissions = $CI->roles_model->get_privileges($role_slug); $permissions = $CI->roles_model->get_permissions_by_slug($role_slug);
return $permissions[$resource][$action]; return $permissions[$resource][$action];
} }