Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SugarCRM 7: How to make certain fields readonly
<?php
/**
* Project: SugarCRM Logic Expression for checking User role
* Original Dev: Antonio Musarra, January 2014
* @2009-2014 Antonio Musarra <antonio.musarra[at]gmail.com>
*
* Desc: SugarCRM Logic Expression ext class
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
require_once('include/Expressions/Expression/Generic/GenericExpression.php');
require_once('modules/ACLRoles/ACLRole.php');
/**
* <b>hasUserRoleName(String roleName)</b><br>
* Returns true if current user has the assigned rolename.<br/>
* ex: <i>hasUserRoleName("CRMAdministrators")</i>
*/
class HasUserRoleNameExpression extends GenericExpression
{
function evaluate()
{
$params = $this->getParameters();
$roleName = $params[0]->evaluate();
// exception handling
if (empty($roleName))
{
throw new Exception("HasUserRoleName: The parameter is not empty");
}
global $current_user;
$GLOBALS['log']->info("Check if $current_user->id has rolename called $roleName...");
$isEnabledRole = in_array($roleName, ACLRole::getUserRoleNames($current_user->id));
if($isEnabledRole)
{
$GLOBALS['log']->info("Check if $current_user->id has rolename called $roleName...[TRUE]");
return AbstractExpression::$TRUE;
} else {
$GLOBALS['log']->info("Check if $current_user->id has rolename called $roleName...[FALSE]");
return AbstractExpression::$FALSE;
}
}
/**
* Returns the JS Equivalent of the evaluate function.
*/
static function getJSEvaluate() {
return <<<EOQ
var roleName = this.getParameters();
var user = SUGAR.App.data.createBean('Users', {id: SUGAR.App.user.id});
user.fetch();
var user_roles = user.getRelatedCollection('aclroles');
user_roles.fetch({relate:true});
function checkRole(element, index, array) {
console.log(element);
if(element.get('name') == roleName)
{
return SUGAR.expressions.Expression.TRUE;
} else {
return SUGAR.expressions.Expression.FALSE;
}
}
user_roles.models.forEach(checkRole);
return SUGAR.expressions.Expression.FALSE;
EOQ;
}
/**
* Returns the maximum number of parameters needed.
*/
static function getParameterCount()
{
return 1;
}
/**
* Returns the opreation name that this Expression should be
* called by.
*/
static function getOperationName()
{
return "hasUserRoleName";
}
/**
* The first parameter is a string.
*/
static function getParameterTypes() {
return AbstractExpression::$STRING_TYPE;
}
/**
* Returns the String representation of this Expression.
*/
function toString() {
}
}
?>
<?php
/**
* Project: SugarCRM Logic Expression for checking if user is admin
* Original Dev: Antonio Musarra, Oct 2014
* @2009-2014 Antonio Musarra <antonio.musarra[at]gmail.com>
*
* Desc: SugarCRM Logic Expression ext class
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
require_once('include/Expressions/Expression/Generic/GenericExpression.php');
/**
* <b>IsUserAdmin()</b><br>
* Returns true if current user is admin<br/>
* ex: <i>isUserAdmin()</i>
*/
class IsUserAdminExpression extends GenericExpression
{
function evaluate()
{
$params = $this->getParameters();
$userId = $params[0]->evaluate();
global $current_user;
$GLOBALS['log']->info("Check if $current_user->id is admin...");
if($current_user->is_admin)
{
$GLOBALS['log']->info("Check if $current_user->id is admin...[TRUE]");
return AbstractExpression::$TRUE;
} else {
$GLOBALS['log']->info("Check if $current_user->id is admin...[FALSE]");
return AbstractExpression::$FALSE;
}
}
/**
* Returns the JS Equivalent of the evaluate function.
*/
static function getJSEvaluate() {
return <<<EOQ
var userId = this.getParameters();
var type = SUGAR.App.user.get('type');
if(type === "admin")
{
return SUGAR.expressions.Expression.TRUE;
} else {
return SUGAR.expressions.Expression.FALSE;
}
EOQ;
}
/**
* Returns the maximum number of parameters needed.
*/
static function getParameterCount()
{
return 1;
}
/**
* Returns the opreation name that this Expression should be
* called by.
*/
static function getOperationName()
{
return "isUserAdmin";
}
/**
* The first parameter is a string.
*/
static function getParameterTypes() {
return AbstractExpression::$STRING_TYPE;
}
/**
* Returns the String representation of this Expression.
*/
function toString() {
}
}
?>
$dependencies['Accounts']['name_readonly'] = array(
'hooks' => array("edit"),
'trigger' => 'not(isUserAdmin(""))',
'triggerFields' => array('name'),
'onload' => true,
//Actions is a list of actions to fire when the trigger is true
'actions' => array(
array(
'name' => 'ReadOnly',
//The parameters passed in will depend on the action type set in 'name'
'params' => array(
'target' => 'name',
'value' => 'true',
),
),
),
);
@shajiuddin

This comment has been minimized.

Copy link

commented Jan 28, 2016

Hi amusarra,
Please confirm where these files should be place
Regards

@indrekots

This comment has been minimized.

Copy link

commented Mar 14, 2016

Thanks! This is exactly what I was looking for.

@orlybg

This comment has been minimized.

Copy link

commented Aug 3, 2016

Hi amusarra, thanks for the example. One question, what part of the logic gets evaluated first? php, js? at the end both are evaluated? thanks!

@nikoup

This comment has been minimized.

Copy link

commented Oct 20, 2016

hi, i want to to make one "Record" as read only after setting the status to "held" example, i want to make mettings record after setting them to "held" as read only. Can you help me with the code?

So far i made this:
`<?php

$dependencies['Meetings']['readonly_fields'] = array(
    'hooks' => array("edit"),
    'trigger' => 'equal($status,held)',
    //Optional, the trigger for the dependency. Defaults to 'true'.
    'triggerFields' => array('status'),
    'onload' => true,
    //Actions is a list of actions to fire when the trigger is true
    // You could list multiple fields here each in their own array under 'actions'
    'actions' => array(
        array(
            'status' => 'ReadOnly',
            //The parameters passed in will depend on the action type set in 'name'
            'params' => array(
                'target' => 'status',
                'value' => 'true',
            ),
        ),
    ),
);`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.