Upgrade Safe User Manager Upgrades

bsoremsugar —  May 11, 2010 — 7 Comments

I have had alot of people ask me how to customize the users module.  Until recently you couldn’t in an upgrade safe way and until, and maybe even after, the users module is given Studio compatibility the only way we can add functionality to the User Editor in an upgrade safe way is by adding tabs to the display.   At least you can in all versions after 5.5.1 with the addTab() function in the YUI toolbox.  Using a few logic hooks you can add tabs to the Users module EditView and DetailView and have the data saved to the user_preferences table all in an upgrade safe manor. In this tutorial I will be showing you how by using my new Dashlet Manager module. It adds the ability to assign a Homepage design (tabs and dashlets) to users so that you can pre-set up all your users Homepages in advance. I wrote this for 5.0.0 but could not make it upgrade safe until 5.5.1 came out.  This module is available here.

So, how do we add a tab to the Users DetailView or EditView? Those views are still drawn using TPL (smarty) files instead of the traditional metadata files (editviewdefs.php and detailviewdefs.php) that are used in other modules so we cant just add to the array and we can’t replace the .tpl files. So somehow we need a way to update the screen after it has been drawn. Luckily in 5.5.1, SugarCRM added tabs to the Users Views and one of those tabs, the Downloads tab, is dynamically added to the view after it has been drawn if the user is editing or viewing their own user settings.  So we can use this same trick to add our tab, however, the Download tabs code is in the views .tpl files and we can’t edit those files or extend them in any way so how to we add this code to the views? While there might be a few ways to do this, I chose to use the “after_ui_frame” logic hook. That hook run right after the page is rendered but before the footer so its perfect. I just add that logic hook and then have the function output the needed JavaScript to add the tab to the screen.

So lets take a look at what I am talking about, this is the new tab I added to the User Editor.

So I added the Dashlet Manager tab to the screen.  It exists within the tags and all the info put into this tabs form will be POSTed with the rest of the form when the save button is clicked.  The first step to this method is logic hooks.  Below is my custom/modules/logic_hooks.php file.  The ‘after_ui_frame’ hook actually draws the form and the tab on the screen and can only be run from the custom/modules/logic_hooks.php.

//custom/modules/logic_hooks.php
$hook_array['after_ui_frame'] = Array();
$hook_array['after_ui_frame'][] = Array(1, 'Add_Tab', 'custom/modules/Users/Users_Enhanced.php','Default_Dashboard', 'addTab');

So, thats the boring part. Below is the actual code that the logic hooks run.  The function is called addTab which simply echos' the correct JavaScript (in this case, the YUI addTab() function) to the page.  The YUI addTab() code is explained here.  We are just adding some JavaScript code to the bottom of the page in this function.  In this example we are populating the 'content' directive of the addTabs() function, but we could have used the 'dataSrc' directive and pulled the for information from an AJAX call.  I might convert to this method before I release the module. Also notice that in logic_hooks that are run in /custom/modules/logic_hooks.php there is no &$bean argument as the logic hook is not being run in a module, that is why I needed to collect information about the record from the $_REQUEST variable and load it manually.

function addTab($event, $arguments) {
global $current_user;
$mod_strings = return_module_language($GLOBALS['current_language'], 'Users');
require_once('custom/modules/Users/Users_Enhanced.php');
$focus=new User();
$focus->retrieve($_REQUEST['record']);
$dd=new defaultHomepage();
$default_homepage = $focus->hm_default_homepage_c;
$only_once = $focus->hm_only_once_c;
$lock = $focus->hm_lockhomepage_c;
if($only_once=='1') {
$only_once="checked";
} else {
$only_once="";
}
if($lock=='1') {
$lock="checked";
} else {
$lock="";
}
$default_homepage_options = $dd->getAllDashboardOptions($focus->id,$default_homepage);
if($_REQUEST['action']=='EditView' && $current_user->is_admin) {
echo "
var EditView_tabs = new YAHOO.widget.TabView("EditView_tabs");
EditView_tabs.on('contentReady', function(e){
EditView_tabs.addTab( new YAHOO.widget.Tab({
label: 'Dashlet Manager',
content: '".$this->createFormHTML($only_once,$lock,$default_homepage_options)."',
cacheData: true
}));
});
n";
}
if($_REQUEST['action']=='DetailView' && $current_user->is_admin) {
echo "
var DetailView_tabs = new YAHOO.widget.TabView("user_detailview_tabs");
DetailView_tabs.on('contentReady', function(e){
DetailView_tabs.addTab( new YAHOO.widget.Tab({
label: 'Dashlet Manager',
content: '".$this->createFormHTML($only_once,$lock,$default_homepage_options)."',
cacheData: true
}));
});
n";
}
}

So thats all the programming, but the manifest.php file for this module is interesting as well


'custom_fields'=>array (
array (
'name'=> 'hm_default_homepage',
'label'=>'LBL_DEFAULT_HOMEPAGE',
'type'=>'varchar',
'max_size'=>255,
'require_option'=>'optional',
'default_value'=>'',
'ext1' => '',
'ext2' => '',
'ext3' => ' ',
'audited'=>1,
'module'=>'Users',
),
array (
'name'=> 'hm_only_once',
'label'=>'LBL_HOMEPAGE_MANAGER_ONLY_ONCE',
'type'=>'bool',
'max_size'=>1,
'require_option'=>'optional',
'default_value'=>'0',
'ext1' => '',
'ext2' => '',
'ext3' => ' ',
'audited'=>1,
'module'=>'Users',
),
array (
'name'=> 'hm_toggle',
'label'=>'LBL_HOMEPAGE_TOGGLE',
'type'=>'bool',
'max_size'=>1,
'require_option'=>'optional',
'default_value'=>'0',
'ext1' => '',
'ext2' => '',
'ext3' => ' ',
'audited'=>0,
'module'=>'Users',
),
array (
'name'=> 'hm_merge',
'label'=>'LBL_HOMEPAGE_MANAGER_MERGE',
'type'=>'bool',
'max_size'=>1,
'require_option'=>'optional',
'default_value'=>'0',
'ext1' => '',
'ext2' => '',
'ext3' => ' ',
'audited'=>0,
'module'=>'Users',
),
array (
'name'=> 'hm_lockhomepage',
'label'=>'LBL_LOCK_HOMEPAGE',
'type'=>'bool',
'max_size'=>1,
'require_option'=>'optional',
'default_value'=>'0',
'ext1' => '',
'ext2' => '',
'ext3' => ' ',
'audited'=>1,
'module'=>'Users',
),
),

'logic_hooks' => array(
array(
'module' => 'Users',
'hook' => 'after_login',
'order' => 67,
'description' => 'Check users Homepage',
'file' => 'custom/modules/Users/Users_Enhanced.php',
'class' => 'defaultHomepage',
'function' => 'afterLogin',
),
array(
'module' => '',
'hook' => 'after_ui_frame',
'order' => 67,
'description' => 'Add Tab to User Editor',
'file' => 'custom/modules/Users/Users_Enhanced.php',
'class' => 'defaultHomepage',
'function' => 'addTab',
),
),

We have custom_fields and logic_hooks defined here.  I add all my custom fields to the Users module to keep my data in, even though Users isn't in Studio yet you can still add custom fields to it and they work just like normal custom fields.  The logic_hooks array is interesting because the 'after_ui_frame' logic hook has no module attached to it.  This will direct SugarCRM to put in the root custom/modules directory.  The custom/modules/logic_hooks.php file runs in all modules and the 'after_ui_frame' logic hook only runs in this file.  If you put it in the custom/modules/Users/logic_hooks.php it will not run.

7 responses to Upgrade Safe User Manager Upgrades

  1. 

    Ken,This module doesn't seem to work with Sugar: 6.0.0beta4When I go to Save a New Home Page Configuration, and type a name in to save…it just says “There is no action by that name.”Any chance you can fix that?

    • 

      Sorry about that, forgot to refresh the zip file after my last rounds of edits. I uploaded a new version to the same project and it should work now.

      I will be updating this module alot over the next few weeks until 6.0 comes out so watch for updates.

  2. 

    Sorry about that, forgot to refresh the zip file after my last rounds of edits. I uploaded a new version to the same project and it should work now.I will be updating this module alot over the next few weeks until 6.0 comes out so watch for updates.

  3. 

    Doesn't seem to be working, it fixed the issue where I couldn't save the template – I can save it now…but when I assign the homepage to other users…it doesn't show up for them. Any idea?

  4. 

    Thanks for the info!!

  5. 

    I updated the module for this one,tested it on CE and PRO with several installations for both 5.5.2 and 6.0. Everything should be working as it should now.

Trackbacks and Pingbacks:

  1. How do you add custom javascript to the email compose page? - April 3, 2013

    [...] [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s