user980643
user980643

Reputation: 113

Accessing SugarCRM Global variable $current_user from a custom Javascript file v6.5

How can I access global SugarCRM variables from a custom javascript file that I have attached to a field on the DetailView in SugarCRM V6.5.

Specifically, I want to access the $current_user global PHP variable from the Javascript file.

I have added the javascript file by editing the custom/modules/Accounts/views/detailviewdef.php and then editing the array to look like this

array (        0 => 
    array (
      'file' => 'modules/Accounts/Account.js',
    ),
  1 => 
    array (
      'file' => 'custom/modules/Accounts/Account1.js',
    ),
  ),

The details that i want to access from the $current_user is actually a customization that I made to the User module to add more configuration option for every user.

I saw somewhere else on Google that i could use var current_user = current_user; in the custom Javascript file and it actually shows the user ID of the logged in user, however what i really want is to get the User detail Object, please does anyone have any ideas.

Regards

Upvotes: 0

Views: 2949

Answers (1)

pauel
pauel

Reputation: 906

I would suggest to write a controller that send back the whole current_user data in json.

custom/modules/Accounts/controller.php

class AccountsController extends SugarController{
    function __construct(){
        parent::__construct();
    }

    function action_getCurrentUser(){
        echo json_encode($GLOBALS['current_user']);
    }
}

And in your js file call it like this:

var request = $.ajax({
        url: 'index.php',
        data: {
            module: 'Accounts',
            action: 'getCurrentUser',
            to_pdf: true, // this reduces the response so that json works
        },

Upvotes: 1

Related Questions