<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('modules/Accounts/views/view.detail.php');
class CustomAccountsViewDetail extends ViewDetail {
public function display() {
global $current_user;
// Get the value of the current user's 'department' field
$userDepartment = $current_user->department;
// Defines the condition based on the value of the 'department' field
if ($userDepartment != "1") {
// JavaScript code to hide the panel
$jScriptToHidePanel = <<<EndOfScript
<script type="text/javascript">
// Hide the panel
$("a[href=#top-panel-0]").parents('.panel-default').addClass('hidden');
</script>
EndOfScript;
// Call the base class method to show the detail view
parent::display();
// Print the JavaScript
echo $jScriptToHidePanel;
} else {
// If the condition is not met, simply show the detail view without hiding the panel
parent::display();
}
}
}
If the user does not have a 1 in their department box on the user record, the panel will not be displayed.
We must use the value that the panel has in “href”, mine is #top-panel-0. Use the browser inspection.
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('modules/Accounts/metadata/editviewdefs.php');
class CustomAccountsViewEdit extends ViewEdit {
public function display() {
global $current_user;
// Get the value of the current user's 'department' field
$userDepartment = $current_user->department;
// Defines the condition based on the value of the 'department' field
if ($userDepartment != "1") {
// JavaScript code to hide the panel
$jScriptToHidePanel = <<<EndOfScript
<script type="text/javascript">
// Hide the panel
$('div[id="detailpanel_0"]').parents('.panel-default').addClass('hidden');
</script>
EndOfScript;
// Call the base class method to show the detail view
parent::display();
// Print the JavaScript
echo $jScriptToHidePanel;
} else {
// If the condition is not met, simply show the detail view without hiding the panel
parent::display();
}
}
}
If the user does not have a 1 in their department box on the user record, the panel will not be displayed when editing a new or already created record.
We must use the value that the panel has in “id”, use browser inspection, mine is “detailpanel_0”
If we want to hide different panels for different users, this is the example code to use:
/custom/modules/Accounts/views/view.detail.php
<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('modules/Accounts/views/view.detail.php');
class CustomAccountsViewDetail extends ViewDetail {
public function display() {
global $current_user;
// Get the value of the current user's 'department' field
$userDepartment = $current_user->department;
// Defines various conditions based on the value of the 'department' field
$condition1 = ($userDepartment != "1");
$condition2 = ($userDepartment != "2");
$condition3 = ($userDepartment != "3");
// Check each condition and hide the corresponding panels
if ($condition1) {
$jScriptToHidePanel1 = <<<EndOfScript
<script type="text/javascript">
// Hide the first panel
$("a[href=#top-panel-0]").parents('.panel-default').addClass('hidden');
</script>
EndOfScript;
// Print the JavaScript to hide the first panel
echo $jScriptToHidePanel1;
}
if ($condition2) {
$jScriptToHidePanel2 = <<<EndOfScript
<script type="text/javascript">
// Hide the second panel
$("a[href=#top-panel-1]").parents('.panel-default').addClass('hidden');
</script>
EndOfScript;
// Print the JavaScript to hide the second panel
echo $jScriptToHidePanel2;
}
if ($condition3) {
$jScriptToHidePanel3 = <<<EndOfScript
<script type="text/javascript">
// Hide the third panel
$("a[href=#top-panel-2]").parents('.panel-default').addClass('hidden');
</script>
EndOfScript;
// Print the JavaScript to hide the third panel
echo $jScriptToHidePanel3;
}
// Call the base class method to show the detail view
parent::display();
}
}
This may be useful in some cases, but it’s important to note that it does not provide real security - the information is all there in the user’s browser for him to see.
If you actually require security to hide this information, you’d have to do your work in the backend in such a way that the panel does not even get sent to the front end.
Instead of sending that bit of JavaScript to the front end to hide the panel, you should try to change the actual parent display function in order to keep the HTML of the panel from ever getting to the front end.
I tried to do this, but if you use the user id or security group id variables it hides the entire detail view. I don’t know why, but I had to leave that option.
The securitygroups table in the database has the group ids.
The panel to hide has href:#top-panel-0 in DetailView. Look at it with the browser inspector.
Panel value is different in DetailView and EditView
<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('modules/Accounts/views/view.detail.php');
require_once('modules/SecurityGroups/SecurityGroup.php');
class CustomAccountsViewDetail extends ViewDetail {
public function getSecurityGroupId($user_id) {
$current_security_groups = SecurityGroup::getUserSecurityGroups($user_id); // Get the security groups of the current user (returns an array)
foreach ($current_security_groups as $security_group) {
return $security_group['id'];
}
return ''; // If the user does not belong to any group, return an empty string or whatever value you want.
}
public function display() {
global $current_user;
$user_id = $current_user->id;
$group_id_to_show_panel = 'ddcdea94-613e-e025-75b1-6543fc5f8ad3'; // ID of the group that can see the panel
// Get the ID of the group the user belongs to
$user_group_id = $this->getSecurityGroupId($user_id);
// Check if the user's group ID does not match the ID that the dashboard can see
if ($user_group_id !== $group_id_to_show_panel) {
// JavaScript code to hide the panel
$jScriptToHidePanel = <<<EndOfScript
<script type="text/javascript">
// Hide the panel
$("a[href=#top-panel-0]").parents('.panel-default').addClass('hidden');
</script>
EndOfScript;
// Call the base class method to show the detail view
parent::display();
// Print the JavaScript
echo $jScriptToHidePanel;
} else {
// If the condition is not met, simply show the detail view without hiding the panel
parent::display();
}
}
}
Code for detail edit
/custom/modules/Accounts/views/view.edit.php
The securitygroups table in the database has the group ids.
The panel to hide has id:detailpanel_0 in EditView. Look at it with the browser inspector.
Panel value is different in DetailView and EditView
<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('modules/Accounts/metadata/editviewdefs.php');
require_once('modules/SecurityGroups/SecurityGroup.php');
class CustomAccountsViewEdit extends ViewEdit {
public function getSecurityGroupId($user_id) {
$current_security_groups = SecurityGroup::getUserSecurityGroups($user_id); // Get the security groups of the current user (returns an array)
foreach ($current_security_groups as $security_group) {
return $security_group['id'];
}
return ''; // If the user does not belong to any group, return an empty string or whatever value you want.
}
public function display() {
global $current_user;
$user_id = $current_user->id;
$group_id_to_show_panel = 'ddcdea94-613e-e025-75b1-6543fc5f8ad3'; // ID of the group that can see the panel
// Get the ID of the group the user belongs to
$user_group_id = $this->getSecurityGroupId($user_id);
// Check if the user's group ID does not match the ID that the dashboard can see
if ($user_group_id !== $group_id_to_show_panel) {
// JavaScript code to hide the panel
$jScriptToHidePanel = <<<EndOfScript
<script type="text/javascript">
// Hide the panel
$('div[id="detailpanel_0"]').parents('.panel-default').addClass('hidden');
</script>
EndOfScript;
// Call the base class method to show the edit view
parent::display();
// Print the JavaScript
echo $jScriptToHidePanel;
} else {
// If the condition is not met, simply show the edit view without hiding the panel
parent::display();
}
}
}
And the codes if we want to use different security group id conditions for different panels.
Code for detail view.
/custom/modules/Accounts/views/view.detail.php
The securitygroups table in the database has the group ids.
The panel to hide has href:#top-panel-0 in DetailView. Look at it with the browser inspector.
Panel value is different in DetailView and EditView
<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('modules/Accounts/views/view.detail.php');
require_once('modules/SecurityGroups/SecurityGroup.php');
class CustomAccountsViewDetail extends ViewDetail {
public function getSecurityGroupId($user_id) {
$current_security_groups = SecurityGroup::getUserSecurityGroups($user_id);
foreach ($current_security_groups as $security_group) {
return $security_group['id'];
}
return ''; // If the user does not belong to any group, return an empty string or whatever value you want.
}
public function display() {
global $current_user;
$user_id = $current_user->id;
$group_id_to_show_1_panel = 'ddcdea94-613e-e025-75b1-6543fc5f8ad3'; // ID of the group that can see the panel 1
$group_id_to_show_2_panel = '643006fd-c022-e4ab-df66-6543fc09801e'; // ID of the group that can see the panel 2
// Get the ID of the group the user belongs to
$user_group_id = $this->getSecurityGroupId($user_id);
// Check if the user belongs to the group that can see the panel 1
$canSee1Panel = ($user_group_id === $group_id_to_show_1_panel);
// Check if the user belongs to the group that can see panel 2
$canSee2Panel = ($user_group_id === $group_id_to_show_2_panel);
if (!$canSee1Panel) {
// Hide panel 1
$jScriptToHidePanel1 = <<<EndOfScript
<script type="text/javascript">
$("a[href=#top-panel-0]").parents('.panel-default').addClass('hidden');
</script>
EndOfScript;
echo $jScriptToHidePanel1;
}
if (!$canSee2Panel) {
// Hide panel 2
$jScriptToHidePanel2 = <<<EndOfScript
<script type="text/javascript">
$("a[href=#top-panel-1]").parents('.panel-default').addClass('hidden');
</script>
EndOfScript;
echo $jScriptToHidePanel2;
}
// Call the base class method to show the detail view
parent::display();
// Print the JavaScript
echo $jScriptToHidePanel1;
if (isset($jScriptToHidePanel2)) {
echo $jScriptToHidePanel2;
}
}
}
Code for detail edit
/custom/modules/Accounts/views/view.edit.php
The securitygroups table in the database has the group ids.
The panel to hide has id:detailpanel_0 in EditView. Look at it with the browser inspector.
Panel value is different in DetailView and EditView
<?php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('modules/Accounts/metadata/editviewdefs.php');
require_once('modules/SecurityGroups/SecurityGroup.php');
class CustomAccountsViewEdit extends ViewEdit {
public function getSecurityGroupId($user_id) {
$current_security_groups = SecurityGroup::getUserSecurityGroups($user_id); // Obtener los grupos de seguridad del usuario actual (devuelve un array)
foreach ($current_security_groups as $security_group) {
return $security_group['id'];
}
return ''; // If the user does not belong to any group, return an empty string or whatever value you want.
}
public function display() {
global $current_user;
$user_id = $current_user->id;
$group_id_to_show_1_panel = 'ddcdea94-613e-e025-75b1-6543fc5f8ad3'; // ID of the group that can see the panel 1
$group_id_to_show_2_panel = '643006fd-c022-e4ab-df66-6543fc09801e'; // ID of the group that can see the panel 2
// Get the ID of the group the user belongs to
$user_group_id = $this->getSecurityGroupId($user_id);
// Check if the user belongs to the group that can see the panel 1
$canSee1Panel = ($user_group_id === $group_id_to_show_1_panel);
// Check if the user belongs to the group that can see panel 2
$canSee2Panel = ($user_group_id === $group_id_to_show_2_panel);
if (!$canSee1Panel) {
/// Hide panel 1
$jScriptToHidePanel1 = <<<EndOfScript
<script type="text/javascript">
$('div[id="detailpanel_0"]').parents('.panel-default').addClass('hidden');
</script>
EndOfScript;
echo $jScriptToHidePanel1;
}
if (!$canSee2Panel) {
// Hide panel 2
$jScriptToHidePanel2 = <<<EndOfScript
<script type="text/javascript">
$('div[id="detailpanel_1"]').parents('.panel-default').addClass('hidden');
</script>
EndOfScript;
echo $jScriptToHidePanel2;
}
// Call the base class method to show the detail view
parent::display();
// Print the JavaScript
echo $jScriptToHidePanel1;
if (isset($jScriptToHidePanel2)) {
echo $jScriptToHidePanel2;
}
}
}
May I suggest - instead of overriding display, override pre_display. Don’t call the parent method in your method, just copy the entire contents, to start:
public function preDisplay()
{
$metadataFile = $this->getMetaDataFile();
$this->dv = new DetailView2();
$this->dv->ss =& $this->ss;
$this->dv->setup($this->module, $this->bean, $metadataFile, get_custom_file_if_exists('include/DetailView/DetailView.tpl'));
}
You can keep your logic looking at security groups, etc. But instead of injecting JS into the view, to remove bits of the screen, do your magic before the display ever runs. There are many ways to do this but I would try having two separate tpls ready, one with the panel, one without, and select which one to use when calling setup.
Or you could use a single tpl and make it have an optional section.
It’s just that I get itchy with things that look like security but aren’t…