Get value of field and set variable in editview.php based on the value

Okay so this one is a bit two-fold, but I have one piece in place. The goal is - based on a value of a cell (locked_c) we want to have the rest of the fields be set to read only, unless you’re in admin or a specific group (CanEdit). The second piece I’ve tested on it’s own and it works, but adding in the logic to try to change a variable ($readonly) based on the value of the locked_c (checkbox) value seems to not work… Or maybe it’s just not being implemented correctly since it seems to work on the first pass but no subsequent. Essentially we will have a task that sets specific records to locked at a set time. If you are not admin or within the CanEdit group, the fields should be read only.

Within the editviewdefs.php (override in custom/modulename, etc) I have the following code.
Essentially I have a $readonly variable that is declared as an empty string, the $lockedBean variable which I believe is the issue and is either not getting the value of that field or not flushing it and reinstating it when I change records.

This all goes into the logic that works that says if you are admin or in CanEdit, then readonly stays as ‘’, all wrapped in the check on the value. So if the value is locked AND you should be able to edit, the $readonly var stays as ‘’. If the record is locked AND you should NOT be able to edit, the $readonly is set to ‘readonly’. The below fields that would be locked have ‘type’=$readonly.

I have read through some documentation and can’t figure out how to get the specific bean that is in state to pull the locked_c value, without knowing the bean ID at least… I am pretty sure this logic here is my fail point.

$checkLocked=0;
$readonly='';
$Lockedbean = $bean.locked_c.value;

    
     if($Lockedbean == 1)
           {
               $checkLocked=1;
               $readonly='readonly';
            }
         else
         {
             $checkLocked=0;
             $readonly='';
         }
          

require_once 'modules/ACLRoles/ACLRole.php';
    $objACLRole = new ACLRole();
    $roles = $objACLRole->getUserRoles($GLOBALS['current_user']->id);
    $readonly;
 if($checkLocked=1)
 {
        //check if they are in the Admin or Admin Manager's role
        if(in_array('Admin',$roles) || in_array('CanEdit',$roles)){
            $readonly ='';
        }
        else{ //If not pass in a variable with the value readonly
            $readonly='readonly';
        }
}

Ok, so I “think” I understand, but correct me if I’m wrong.

$Lockedbean = $bean.locked_c.value;

This is in editviewdefs.php, This should be the current’s records locked_c. value yes?

If so then I believe the variable you are looking for is:

$fields.locked_c.value

That’s getting me closer!! Why couldn’t I find documentation on the $field info!!??

Okay so here is my code, and yes it’s in the editviewdefs.php file of the custom folder for my specific module.

$locked = $fields.locked_c.value;
require_once 'modules/ACLRoles/ACLRole.php';
    $objACLRole = new ACLRole();
    $roles = $objACLRole->getUserRoles($GLOBALS['current_user']->id);
    $readonly='';
 
    if($locked = 1)
    {
        //check if they are in the Admin or Admin Manager's role
        if(in_array('Admin',$roles) || in_array('CanEdit',$roles)){
            $readonly ='';
        }
        else{ //If not pass in a variable with the value readonly
            $readonly='readonly';
        }
    }

This works… Kind of. So later on for the array of the fields I have 'type' => $readonly in the fields I’d want as readonly in the event that the locked_c.value = 1 AND the user is not in admin or CanEdit group… that all works now it seems, however if the user is NOT in the group and goes to create a new record, even with me initializing the $readonly='' for the variable, those fields are still set to ReadOnly and can’t be edited on a new record. Do I have to pull this logic out to the predisplay or something? Or what is missing. Do checkbox initialize as 0 since it’s unchecked, or is the Checkbox logic weird in here?

Thank you for helping so far!

I have resolved this - I moved the logic to the custom/views/view.edit.php file to check for the specific locked field and to check for group access. I also then created a copy of the editvewdefs.php file named lockedviewdefs.php. The lockedviewdefs file has the readonly fields set to ‘type’->‘readonly’… standardt editviewdefs does not… Like this in the view.edit.php

 require_once 'modules/ACLRoles/ACLRole.php';
    $objACLRole = new ACLRole();
    $roles = $objACLRole->getUserRoles($GLOBALS['current_user']->id);
 
        $bean = $this->bean;
        $isLocked = $bean->locked_c.value;
        
        if(in_array('Admin',$roles) || in_array('CanEdit',$roles)){
            $metadataFile = 'custom/modules/Test_TestCalcs/metadata/editviewdefs.php';
            $this->ev = $this->getEditView();
        }

        elseif ($isLocked==1) {
            $metadataFile = 'custom/modules/Test_TestCalcs/metadata/lockedviewdefs.php';
            $this->ev = $this->getEditView();
            $this->ev->view = 'LockedView';
        } else {
            $metadataFile = 'custom/modules/Test_TestCalcs/metadata/editviewdefs.php';
            $this->ev = $this->getEditView();
        }
        $this->ev->ss =& $this->ss;
        $this->ev->setup($this->module, $this->bean, $metadataFile, get_custom_file_if_exists('include/EditView/EditView.tpl'));
    }

In the metadata file I made the following changes
LockedView:

$viewdefs [$module_name] = 
array (
  'LockedView' =>

Added this right above the panels definitions
 'form' =>
            array(
    'headerTpl' => 'include/EditView/header.tpl',
    'footerTpl' => 'include/EditView/footer.tpl',
        ),
    'panels' => 

Seems to work! Also - ensure you have developer mode off (caches things differently).

Hope this helps someone else!

2 Likes