<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/
require_once __DIR__ . '/../../src/server/config/boot_global.php';
require_once __DIR__ . '/../../src/server/config/boot_local.php';

function task_buildJavascriptACL() {

    $eol = "\n";
    $refl = new ReflectionClass( "TB_Server_Core_AccessControl" );

    $const = $refl->getConstants();

    $jsConst = 'RIGHTS : { ';
    foreach( $const as $key => $val ) {
       $jsConst .= $eol . substr( $key, 6 ) . ' : ' . '"' . $val . '",';
    }
    $jsConst = rtrim( $jsConst, "," );
    $jsConst .= $eol . "}," . $eol . $eol;

    $props = $refl->getStaticProperties();
    $jsRoles = '';
    foreach( $props as $role => $roleRights) {
        if ( is_array( $roleRights ) ) {
            $jsRoles .= $eol . 'ROLE_' . $role . ' : ' . " [ ";
            foreach( $roleRights as $roleRight ) {
                $jsRoles .= $eol . '"' . $roleRight . '",';
            }
            $jsRoles = rtrim( $jsRoles, "," );
            $jsRoles .= "],";
        }
    }
    $jsRoles = rtrim( $jsRoles, "," );

    var_dump( $jsRoles );


    $js = '
    "use strict";

var TB = TB || {};

TB.ACL = {
' . $jsConst . '
' . $jsRoles . '
};';
    file_put_contents( __DIR__ . '/../../public/client/js/core/TBACL.js', $js );
}

task_buildJavascriptACL();