Where are saved dashlets for dashboard?

Hey guys !

New week, new question :stuck_out_tongue: i’m trying to make mmy own custom dashboard creation for specific users, but I’m stuck…

I found that some dashlets are stored in user_preference but when I delete the row it doesn’t delete the user dashlet… And yes it’s right user ^^ i found that the dashlet is stored in the session too, but when I delete all my session and user_preferences the dashboard is recontructed. And some dashlets are not stored in user preference like the web dashlet (iframe)

What am I missing ? How the dashboard is stored ?

Thanks !

@Seijitsu
Look at the file - include/MySugar/MySugar.php
May be answers there.

@p.konetskiy, i’ll check thx =)

The dashboard dashlet configuration is setup in two different arrays (‘dashlets’ and ‘pages’) in user_preferences category ‘Home’.

If you run this script in the root folder, it will add the XXXX dashlet to all users:

/**
 * 
 * This script is in charge of adding the XXXXDashlet in the dashboard of all Users in the instance. If the dashboard already contains
 * the dashlet, this function don't do anything.
 */


global $current_user;
$current_user = new User();
$current_user->getSystemUser();

$userEmptyBean = BeanFactory::getBean('Users');
$userBeanArray = $userEmptyBean->get_full_list();
$sticDashlet = array(
    'className' => 'XXXXDashlet',
    'module' => 'Home',
    'options' => array(),
    'fileLocation' => 'custom/modules/Home/Dashlets/XXXXDashlet/XXXXDashlet.php',
);


foreach ($userBeanArray as $userBean) {
    $dashletId = false;
    require_once 'modules/UserPreferences/UserPreference.php';
    $userBean = new UserPreference($userBean);
    $foundInDashlet = false;
    $dashletArray = $userBean->getPreference('dashlets', 'Home');

    foreach($dashletArray as $key => $dashlet) {
        if ($dashlet['className'] === 'XXXXDashlet') {
            $dashletId = $key;
            $GLOBALS['log']->debug('Dashlet found in User dashboard');       
            break;
        }
    }
    if (!$dashletId) {
        $dashletId = create_guid();
        $GLOBALS['log']->debug('Dashlet not found in Dashlet array. We will add it...'); 
        $dashletArray[$dashletId]= $XXXXDashlet;
        $userBean->setPreference('dashlets', $dashletArray, 'Home');
        $GLOBALS['log']->debug('Dashlet added in Dashlet array'); 
    }
    $pages = $userBean->getPreference('pages', 'Home');

    if (in_array($dashletId, $pages[0]['columns'][1]['dashlets'])) {
        $GLOBALS['log']->debug('Dashlet found in Page array');   
    } else {
        $GLOBALS['log']->debug('Dashlet not found in Page. We will add it...'); 
        array_unshift($pages[0]['columns'][1]['dashlets'], $dashletId);
        $userBean->setPreference('pages', $pages, 'Home');
        $GLOBALS['log']->debug('Dashlet added in Page array'); 
    }
    $userBean->savePreferencesToDB();
}


3 Likes

Thx for sharing but I still got some questions :slight_smile:

The var $sticDashlet is nerver used and the var $XXXDashlet neither. That I want to know is how is defined XXXDashlet ?

I’m not sure that I need the $sticDashlet in my case.

Thanks for help

Oh sorry, that was just a typo. I can’t edit the post. But the “$sticDashlet” should be “$XXXXDashlet”.

Like this:

/**
 * 
 * This script is in charge of adding the XXXXDashlet in the dashboard of all Users in the instance. If the dashboard already contains
 * the dashlet, this function don't do anything.
 */


global $current_user;
$current_user = new User();
$current_user->getSystemUser();

$userEmptyBean = BeanFactory::getBean('Users');
$userBeanArray = $userEmptyBean->get_full_list();
$XXXXDashlet = array(
    'className' => 'XXXXDashlet',
    'module' => 'Home',
    'options' => array(),
    'fileLocation' => 'custom/modules/Home/Dashlets/XXXXDashlet/XXXXDashlet.php',
);


foreach ($userBeanArray as $userBean) {
    $dashletId = false;
    require_once 'modules/UserPreferences/UserPreference.php';
    $userBean = new UserPreference($userBean);
    $foundInDashlet = false;
    $dashletArray = $userBean->getPreference('dashlets', 'Home');

    foreach($dashletArray as $key => $dashlet) {
        if ($dashlet['className'] === 'XXXXDashlet') {
            $dashletId = $key;
            $GLOBALS['log']->debug('Dashlet found in User dashboard');       
            break;
        }
    }
    if (!$dashletId) {
        $dashletId = create_guid();
        $GLOBALS['log']->debug('Dashlet not found in Dashlet array. We will add it...'); 
        $dashletArray[$dashletId]= $XXXXDashlet;
        $userBean->setPreference('dashlets', $dashletArray, 'Home');
        $GLOBALS['log']->debug('Dashlet added in Dashlet array'); 
    }
    $pages = $userBean->getPreference('pages', 'Home');

    if (in_array($dashletId, $pages[0]['columns'][1]['dashlets'])) {
        $GLOBALS['log']->debug('Dashlet found in Page array');   
    } else {
        $GLOBALS['log']->debug('Dashlet not found in Page. We will add it...'); 
        array_unshift($pages[0]['columns'][1]['dashlets'], $dashletId);
        $userBean->setPreference('pages', $pages, 'Home');
        $GLOBALS['log']->debug('Dashlet added in Page array'); 
    }
    $userBean->savePreferencesToDB();
}


1 Like