<?php
/*
 * PiAnoS (Picture Annotation System), A fingerprint examiner training tool
 * Copyright (C) 2011-2021 University of Lausanne, Switzerland
 * https://ips-labs.unil.ch/pianos
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */
require_once ( "unil/Node.php" );
require_once ( "unil/Sizer.php" );
class MainFrame extends Node
{
    function render( &$rs )
    {
        // Theme-related CSS
        $rs->getPage()->addStyleSheet( Application::get( 'csslib_jquery_ui' ) );
        $rs->setJQueryStyleSheet( Application::get( 'jqueryui_theme' ), 'theme-backend.css' );
        // Theme agnostic CSS
        $rs->getPage()->addStyleSheet( 'css/backend.css' );
        
        $rs->getPage()->addScriptForceReload( Application::get( 'jsforcereload' ) );
        
        $rs->getPage()->addScript( Application::get( 'jslib_jquery_layout' ) );
        $rs->getPage()->addScript( Application::get( 'jslib_jquery_hashchange' ) );
        
        $rs->getPage()->addStyleSheet( Application::get( 'csslib_toastr' ) );
        $rs->getPage()->addScript( Application::get( 'jslib_toastr' ) );
        $rs->getPage()->addScript( Application::get( 'jslib_easing' ) );
        
        $udbPath = Application::get( 'microdb_path' );
        if( $udbPath )
        {
            $udb = array (
                    'udb', 
                    'app', 
                    'canvas', 
                    'crud', 
                    'effects', 
                    'frame', 
                    'iframe', 
                    'jqui-proplist', 
                    'jqui-qtable', 
                    'list', 
                    'model', 
                    'node', 
                    'panel', 
                    'sizeable', 
                    'sizer', 
                    'table', 
                    'tree', 
                    'utils', 
                    'window' 
            );
            foreach( $udb as $script )
                $rs->getPage()->addScript( $udbPath . "/js/" . $script . ".js" );
            $rs->getPage()->addStyleSheet( $udbPath . "/css/udb.css" );
        } else
        {
            $rs->getPage()->addScript( Application::get( 'jslib_microdb' ) );
            $rs->getPage()->addStyleSheet( Application::get( 'csslib_microdb' ) );
        }
        
        $backendJS = Application::get( 'use_minified_pianos4_backend' );
        if( $backendJS )
        {
            $rs->getPage()->addScript( $backendJS );
        } else
        {
            $rs->getPage()->addScript( "js/pianos4_common.js" );
            $rs->getPage()->addScript( "js/backend.js" );
            
            $rs->getPage()->addScript( "js/backend.shared.js" );
            $rs->getPage()->addScript( "js/backend.admin.js" );
            $rs->getPage()->addScript( "js/backend.user.js" );
        }
        
        $rs->getPage()->addScript( Application::get( 'jslib_ajaxupload' ) );
        
        $rs->getPage()->addStyleSheet( Application::get( 'csslib_jquery_layout' ) );
        $rs->getPage()->addScript( Application::get( 'jslib_jquery_cookie' ) );
        
        $rs->getPage()->addScript( Application::get( 'jslib_dropzone' ) );
        
        $rs->getPage()->addScript( Application::get( 'jslib_chosen' ) );
        $rs->getPage()->addStyleSheet( Application::get( 'csslib_chosen' ) );
        
        $rs->getPage()->addScript( Application::get( 'jslib_md5' ) );
        
        $rs->getPage()->addStyleSheet( 'css/app.css' );
        
        $settings = array ();
        
        $configOverrides = Application::get( 'backend_app_settings' );
        if( $configOverrides && count( $configOverrides ) )
            $settings = array_merge( $settings, $configOverrides );
        
        $this->domStart( 'div', 'pianos-backend' );
        $this->domClose();
        
        $this->domStartJS();
        echo "
		$(document).ready(function() {
			// Tool to inspect variables leaking to the global namespace (window namespace, actually)
			//window.__defineSetter__('varname', function(value) { console.trace(); });

			var settings = {};
			";
        if( count( $settings ) )
            echo "settings = " . json_encode( $settings ) . ";\n";
        echo "
			var opt = $.extend({}, settings, {
				user: {
                    id: " . $_SESSION[ 'user' ][ 'id' ] . ",
                    name: " . _J( $_SESSION[ 'user' ][ 'name' ] ) . ",
                    tutor: " . ( $_SESSION[ 'user' ][ 'isadmin' ] ? 'true': 'false' ) . ",
                    can_create_personnal_folder: " . ( Application::get( 'can_create_personnal_folder' ) ? 'true': 'false' ) . ",
                    can_upload_images: " . ( Application::get( 'can_upload_images' ) ? 'true': 'false' ) . ",
                    can_view_personnal_folder_summary: " . ( Application::get( 'can_view_personnal_folder_summary' ) ? 'true': 'false' ) . ",
                    switchedUser: " . ( isset( $_SESSION[ 'user' ][ 'switchedUser' ] ) && $_SESSION[ 'user' ][ 'switchedUser' ] ? 'true': 'false' ) . ",
                    anonymousreview: " . ( Application::get( 'anonymous_review' ) ? 'true': 'false' ) . "
                },
                
                view: {
                    homeData_as_todo_list: " . ( Application::get( 'homeData_as_todo_list' ) ? 'true': 'false' ) . ",
                    homeData_show_all_exercises_for_admin: " . ( Application::get( 'homeData_show_all_exercises_for_admin' ) ? 'true': 'false' ) . ",
                    progress_bar: " . ( Application::get( 'progress_bar' ) ? 'true': 'false' ) . ",
                    show_switchUser: " . ( Application::get( 'show_switchUser' ) ? 'true': 'false' ) . ",
                },
			});
			backend = new Pianos4Backend(opt);
			udb.run(backend);
		});";
        $this->domClose();
    }
}
