<?php
/**
 * Maho
 *
 * @category    design
 * @package     default_default
 * @copyright   Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
 * @copyright   Copyright (c) 2021-2024 The OpenMage Contributors (https://openmage.org)
 * @copyright   Copyright (c) 2024 Maho (https://mahocommerce.com)
 * @license     https://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/** @var Mage_Adminhtml_Block_Permissions_Tab_Rolesedit $this */
?>
<?= $this->getChildHtml() ?>

<div class="entry-edit">
    <div class="entry-edit-head">
        <h4><?= $this->__('Roles Resources') ?></h4>
    </div>
    <input type="hidden" name="resource" id="role_resources" value="" />
    <fieldset id="role_resources">

        <span class="field-row">
            <label for="all"><?= $this->__('Resource Access') ?></label>
            <select id="all" name="all" onchange="$('resources_container').toggle()" class="select">
                <option value="0" <?= ($this->getEverythingAllowed()?'':'selected') ?>><?= $this->__('Custom') ?></option>
                <option value="1" <?= ($this->getEverythingAllowed()?'selected':'') ?>><?= $this->__('All') ?></option>
            </select>
        </span>

        <span class="field-row" id="resources_container">
            <label><?= $this->__('Resources') ?></label>
            <div class="f-left">
                <div class="tree x-tree" id="resource-tree"></div>
            </div>
        </span>

    </fieldset>
</div>
<!-- Draw Resources Tree -->
<script type="text/javascript">
<?php if($this->getEverythingAllowed()): ?>
    $('resources_container').hide();
<?php endif ?>
Ext.EventManager.onDocumentReady(function() {
    var tree = new Ext.tree.TreePanel('resource-tree', {
        animate:false,
        loader: false,
        enableDD:false,
        containerScroll: true,
        rootUIProvider: Ext.tree.CheckboxNodeUI,
        selModel: new Ext.tree.CheckNodeMultiSelectionModel(),
        rootVisible: false
    });

    tree.on('check', checkHandler, tree);

    // set the root node
    var root = new Ext.tree.TreeNode({
        text: 'root',
        draggable:false,
        checked:'false',
        id:'__root__',
        uiProvider: Ext.tree.CheckboxNodeUI
    });

    tree.setRootNode(root);
    bildResourcesTree(root, <?= $this->getResTreeJson() ?>);
    tree.addListener('click', resourceClick.createDelegate(this));

    // render the tree
    tree.render();
    // root.expand();
    tree.expandAll();

    $('role_resources').value = tree.getChecked().join(',');
});

function resourceClick(node, e){
    node.getUI().check(!node.getUI().checked());
    varienElementMethods.setHasChanges(Event.element(e), e);
};

function bildResourcesTree(parent, config){
    if (!config) return null;

    if (parent && config && config.length){
        for (var i = 0; i < config.length; i++){
            config[i].uiProvider = Ext.tree.CheckboxNodeUI;
            var node = new Ext.tree.TreeNode(config[i]);
            parent.appendChild(node);
            if(config[i].children){
                bildResourcesTree(node, config[i].children);
            }
        }
    }
}

function checkHandler(node)
{
    if ( node.attributes.checked && node.parentNode ) {
        var n = node.parentNode;
        this.removeListener('check', checkHandler);
        do {
            if (!n || n.attributes.id == 'admin' || n.attributes.id == '__root__') {
                break;
            } else {
                n.ui.check(true);
            }
        } while (n = n.parentNode );
        this.on('check', checkHandler);
    }
    if ( !node.isLeaf() && node.hasChildNodes() ) {
        this.removeListener('check', checkHandler);
        processChildren(node, node.attributes.checked);
        this.on('check', checkHandler);
    }
    $('role_resources').value = this.getChecked().join(',');
}

function processChildren(node, state)
{
    if ( !node.hasChildNodes() ) return false;
    for(var i = 0; i < node.childNodes.length; i++ ) {
        node.childNodes[i].ui.check(state);
        if ( node.childNodes[i].hasChildNodes() ) {
            processChildren(node.childNodes[i], state);
        }
    }
    return true;
}
</script>
