<?php
/**
 * OpenMage
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available at https://opensource.org/license/afl-3-0-php
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://www.magento.com)
 * @copyright   Copyright (c) 2021-2024 The OpenMage Contributors (https://www.openmage.org)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/** @var Mage_Adminhtml_Block_Template $this */
?>
<script type="text/javascript">
<!--
<?php $myBlock = $this->getLayout()->getBlock('user.roles.grid'); ?>
<?php if( is_object($myBlock) && $myBlock->getJsObjectName()): ?>
    var radioBoxes = $H({});
    var warning = false;
    var userRoles = $H(<?php echo $myBlock->_getSelectedRoles(true) ?>);
    if (userRoles.size() > 0) warning = true;
    $('user_user_roles').value = userRoles.toQueryString();

    function registerUserRole(grid, element, checked){
        if(checked){
            userRoles.each(function(o){userRoles.remove(o[0]);});
            userRoles[element.value] = 0;
        } else {
            userRoles.remove(element.value);
        }
        $('user_user_roles').value = userRoles.toQueryString();
        grid.reloadParams = {'user_roles[]':userRoles.keys()};
    }

    function roleRowClick(grid, event){
        var trElement = Event.findElement(event, 'tr');
        var isInput   = Event.element(event).tagName == 'INPUT';
        if(trElement){
            var checkbox = Element.getElementsBySelector(trElement, 'input');
            if(checkbox[0] && !checkbox[0].checked){
                var checked = isInput ? checkbox[0].checked : !checkbox[0].checked;
                if (checked && warning && radioBoxes.size() > 0) {
                    if ( !confirm('<?php echo Mage::helper('core')->jsQuoteEscape($this->__('Warning!\r\nThis action will remove this user from already assigned role\r\nAre you sure?')) ?>') ) {
                        checkbox[0].checked = false;
                        for(i in radioBoxes) {
                            if( radioBoxes[i].status == 1) {
                                radioBoxes[i].object.checked = true;
                            }
                        }
                        return false;
                    }
                    warning = false;
                }
                <?php echo $myBlock->getJsObjectName() ?>.setCheckboxChecked(checkbox[0], checked);
            }
        }
    }

    function rolesRowInit(grid, row){
        var checkbox = $(row).getElementsByClassName('radio')[0];
        if (checkbox) {
            radioBoxes[checkbox.value] = {'status' : ((checkbox.checked) ? 1 : 0), 'object' : checkbox};
        }
    }

<?php echo $myBlock->getJsObjectName() ?>.rowClickCallback = roleRowClick;
<?php echo $myBlock->getJsObjectName() ?>.initRowCallback = rolesRowInit;
<?php echo $myBlock->getJsObjectName() ?>.checkboxCheckCallback = registerUserRole;
<?php echo $myBlock->getJsObjectName() ?>.rows.each(function(row){rolesRowInit(<?php echo $myBlock->getJsObjectName() ?>, row)});
<?php endif ?>
//-->
</script>
