Ocultar campo en editviewsdef en función de un valor del CurrentUser

Buenas.

Tengo un listener en un js

document.addEventListener( 'DOMContentLoaded', function() {
   showHidePanelContacts();
});

Como podría pasarle un valor del CurrentUser para ocultar un campo de edirviewdet?

O como se haría sin el listener. El listener lo tengo para ocultar ciertos paneles en el detalviewdefs en función de ciertos valores de campos.

Un saludo.

Parents Reply Children
  • Hi all

    Sorry about this. Our idea is to have a field inside the Contacts module called "Personal Information" that only users working in a specific department will be able to check. I mean users with a given role, which in the users' department field (field you can find in the Admin Users module) have a value set to "Value1" they will be able to see the field, and users which do not have this value in the "$user->department" field can not.

    My first idea was to use the global variable GLOBAL $current_user and accesing to the value of the department of the current user that is logged

    $value= $current_user->department

    Then check if "$value=='value1' the application will show the field, and if not will hide it.

    The thin is that to show/hide fields or pannels in the DetailView I am using a JS code that is called "on page load" but most of the times I use current values of fields of the Contact... but how can I use the current user's department value to show hide a field?. I mean $current_user is a global variable that can be used in PHP, but my code to show/hide pannels is a JS file.... how can I pass this argument to the JS file??

    Other option would be using the role of the current user, but would be the same, wouldnt it?

    My JS code to hide fields or panels inside the Contacts DetailView is this one

    document.addEventListener( 'DOMContentLoaded', function() {
       showHidePanelContacts();
    });

    function showHidePanelContacts() {
          /*document.getElementById('LBL_EDITVIEW_PANEL3').style.display = 'none';
            document.getElementById('detailpanel_4').style.display = 'none';
            $('detailpanel_4').closest('a').hide(); */

        switch (document.getElementById('lead_source').value) {
        case 'Miembro':
            //Mostramos Panel Miembro    
            document.getElementById('detailpanel_2').style.display = 'block';
            //document.getElementById('otras_observaciones_c').style.visibility = 'hidden';
            //Ocultamos Panel Amigo
            document.getElementById('detailpanel_3').style.visibility = 'none';
            //Ocultamos Panel Capellan
            document.getElementById('detailpanel_4').style.display = 'none';
            //Ocultamos Panel Voluntario
            document.getElementById('detailpanel_5').style.display = 'none';
            //Ocultamos Panel Beneficiario
            document.getElementById('detailpanel_6').style.display = 'none';
            break;
        case 'Amigo':
            //Ocultamos Panel Miembro    
            document.getElementById('detailpanel_2').style.display = 'none';
            //document.getElementById('otras_observaciones_c').style.visibility = 'visible';
            //Mostramos Panel Amigo
            document.getElementById('detailpanel_3').style.display = 'block';
            //Ocultamos Panel Capellan
            document.getElementById('detailpanel_4').style.display = 'none';
            //Ocultamos Panel Voluntario
            document.getElementById('detailpanel_5').style.display = 'none';
            //Ocultamos Panel Beneficiario
            document.getElementById('detailpanel_6').style.display = 'none';
            break;
        case 'Capellan':
            //Ocultamos Panel Miembro
            document.getElementById('detailpanel_2').style.display = 'none';
            //Ocultamos Panel Amigo
            document.getElementById('detailpanel_3').style.display = 'none';
            //Mostramos Panel Capellan
            document.getElementById('detailpanel_4').style.display = 'block';
            //Ocultamos Panel Voluntario
            document.getElementById('detailpanel_5').style.display = 'none';
            //Ocultamos Panel Beneficiario
            document.getElementById('detailpanel_6').style.display = 'none';
            break;
        case 'Voluntario':
            //Ocultamos Panel Miembro
            document.getElementById('detailpanel_2').style.display = 'none';
            //Ocultamos Panel Amigo
            document.getElementById('detailpanel_3').style.display = 'none';
            //Ocultamos Panel Capellan
            document.getElementById('detailpanel_4').style.display = 'none';
            //Mostramos Panel Voluntario
            document.getElementById('detailpanel_5').style.display = 'block';
            //Ocultamos Panel Beneficiario
            document.getElementById('detailpanel_6').style.display = 'none';
            break;
        case 'Beneficiario':
            //Ocultamos Panel Miembro
            document.getElementById('detailpanel_2').style.display = 'none'
            //Ocultamos Panel Amigo
            document.getElementById('detailpanel_3').style.display = 'none';
            //Ocultamos Panel Capellan
            document.getElementById('detailpanel_4').style.display = 'none';
            //Ocultamos Panel Voluntario
            document.getElementById('detailpanel_5').style.display = 'none';
            //Mostramos Panel Beneficiario
            document.getElementById('detailpanel_6').style.display = 'block';
            break;

        default:
            //Ocultamos Panel Miembro
            document.getElementById('detailpanel_2').style.display = 'none';
            //Ocultamos Panel Amigo
            document.getElementById('detailpanel_3').style.display = 'none';
            //Ocultamos Panel Capellan
            document.getElementById('detailpanel_4').style.display = 'none';
            //Ocultamos Panel Voluntario
            document.getElementById('detailpanel_5').style.display = 'none';
            //Ocultamos Panel Beneficiario
            document.getElementById('detailpanel_6').style.display = 'none';
            break;
        }

        if (document.getElementById('lead_source').value == 'Miembro') {
                if (document.getElementById('es_amigo_c').checked == true )
                    //Mostramos Panel Amigo
                    console.log(document.getElementById('detailpanel_3').style.display = 'block');
                else
                    //Ocultamos Panel Amigo
                    document.getElementById('detailpanel_3').style.display = 'none';
            }
    }

  • Hola Óscar del Río,

    Te contesto en inglés, así el resto de la comunidad puede entender mejor la respuesta.

    I would recommend you doing this through an after_ui_frame LogicHook triggered on the module you want to make the customization.

    The code of the LogicHook would be like the following:

    class LoadUserInEditView {

       function loadUserRolesInEditView( $bean, $event, $arguments ) {

           if( !$bean ) {
             return;
           }

           global $current_user;

           $department = $current_user->department;

           $departmentHtml = '<input type="hidden" style="display: none" id="user_department">' . $department . '</input>';

           echo $departmentHtml;

       }

    }

    This would set in the DOM a hidden input element which will store the department of the current user.

    Then in your javascript:

    document.addEventListener( 'DOMContentLoaded', function() {
       showHidePanelContacts();

       var department = $('#user_department').html();

       if( department !== 'my_desired_department' ) {
          $('#field_to_hide').hide();
       }
      
    });

    Espero que te sirva. Cualquier duda me dices!

    Saludos/Regards,

    David.