<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/


class TB_Client_Core_View {

    const TEMPLATE_SUFFIX = '.tpl.php';

    /**
     * @return string
     */
    public static function getAllPageTemplates() {
        $pages = '';
        $pagesPath = __DIR__ . '/../view/page/';

        // Create recursive dir iterator which skips dot folders
        $dir = new RecursiveDirectoryIterator( $pagesPath, FilesystemIterator::SKIP_DOTS );

        // Flatten the recursive iterator, folders come before their files
        $it  = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST );

        // Maximum depth is 1 level deeper than the base folder
        $it->setMaxDepth( 1 );

        $pages = '';
        // Write javascript includes
        foreach ( $it as $fileinfo ) {
            if ( $fileinfo->isFile() ) {
                $id = str_replace( self::TEMPLATE_SUFFIX, '', $fileinfo->getFilename() );
                $pages .= '<script id="tpl_' . $id . '" type="text/ractive">' . "\n";
                ob_start();
                include_once $pagesPath . $fileinfo->getFilename();
                $tpl = ob_get_contents();
                ob_end_clean();
                $pages .= $tpl;
                $pages .= "\n</script>\n";
            }
        }

        return $pages;
    }

    /**
     * Get page template filenames
     *
     * @return array
     */
    public static function getPageTemplateFilenames() {
        $tf = array();

        $pagesPath = __DIR__ . '/../view/page/';

        // Create recursive dir iterator which skips dot folders
        $dir = new RecursiveDirectoryIterator( $pagesPath, FilesystemIterator::SKIP_DOTS );

        // Flatten the recursive iterator, folders come before their files
        $it  = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST );

        // Maximum depth is 1 level deeper than the base folder
        $it->setMaxDepth( 1 );

        $pages = '';
        // Write javascript includes
        foreach ( $it as $fileinfo ) {
            if ( $fileinfo->isFile() ) {
                $id = str_replace( self::TEMPLATE_SUFFIX, '', $fileinfo->getFilename() );
                array_push( $tf, $id );
            }
        }

        return $tf;
    }

    /**
     * @param string $version Version used for cache breaking. Default is NULL
     * @param string $absPath Add absolute path of includes. Default is NULL
     * @return array
     */
    static public function getAllCss( $version = NULL, $absPath = NULL ) {
        $cacheBreak = is_null( $version ) ? '' : '?' . $version;
        $absolutePath = is_null( $absPath ) ? '' : rtrim( $absPath, '/' ) . '/';
        $csses = array(

            $absolutePath . 'css/bootstrap.min.css' . $cacheBreak,
            $absolutePath . 'css/bootflat.min.css' . $cacheBreak,
            $absolutePath . 'css/jasny-bootstrap.min.css' . $cacheBreak,
            $absolutePath . 'css/toastr.min.css' . $cacheBreak,
            $absolutePath . 'css/sweetalert.css' . $cacheBreak,
            $absolutePath . 'css/font-awesome.min.css' . $cacheBreak,
            $absolutePath . 'css/Pe-icon-7-stroke.css' . $cacheBreak,
            $absolutePath . 'css/ladda-themeless.min.css' . $cacheBreak,
            $absolutePath . 'css/jquery.datetimepicker.css' . $cacheBreak,
            $absolutePath . 'css/easy-autocomplete.min.css' . $cacheBreak,
            $absolutePath . 'css/slim.min.css' . $cacheBreak,
            $absolutePath . 'css/custom.css' . $cacheBreak

        );

        return $csses;
    }

    /**
     *
     * @param string $version Version used for cache breaking. Default is NULL
     * @param string $absPath Add absolute path of includes. Default is NULL
     * @return array
     */
    static public function getAllJs( $version = NULL, $absPath = NULL ) {
        $cacheBreak = is_null( $version ) ? '' : '?' . $version;
        $absolutePath = is_null( $absPath ) ? '' : rtrim( $absPath, '/' ) . '/';

        // Libs
        $jsses = array(
            // Jquery is the master dependency of the projcet
            //$absolutePath . 'js/libs/jquery-2.1.1.min.js' . $cacheBreak,
            $absolutePath . 'js/libs/bootstrap.min.js' . $cacheBreak,
            $absolutePath . 'js/libs/icheck.min.js' . $cacheBreak,
            $absolutePath . 'js/libs/jquery.fs.selecter.min.js' . $cacheBreak,
            $absolutePath . 'js/libs/jquery.fs.stepper.min.js' . $cacheBreak,
            $absolutePath . 'js/libs/store.min.js' . $cacheBreak,
            $absolutePath . 'js/libs/tmpl.js' . $cacheBreak,
            $absolutePath . 'js/libs/sweetalert.min.js' . $cacheBreak,
            $absolutePath . 'js/libs/toastr.min.js' . $cacheBreak,
            $absolutePath . 'js/libs/jasny-bootstrap.min.js' . $cacheBreak,
            $absolutePath . 'js/libs/spin.min.js' . $cacheBreak,
            $absolutePath . 'js/libs/ladda.min.js' . $cacheBreak,
            $absolutePath . 'js/libs/jquery.datetimepicker.js' . $cacheBreak,
            $absolutePath . 'js/libs/moment.js' . $cacheBreak,
            $absolutePath . 'js/libs/moment-timezone.min.js' . $cacheBreak,
            $absolutePath . 'js/libs/pdfmake.min.js' . $cacheBreak,
            $absolutePath . 'js/libs/vfs_fonts.js' . $cacheBreak,
            $absolutePath . 'js/libs/modernizr.js' . $cacheBreak,
            $absolutePath . 'js/libs/slim.kickstart.min.js' . $cacheBreak,
            $absolutePath . 'js/libs/jquery.easy-autocomplete.min.js' . $cacheBreak,
            $absolutePath . 'js/libs/zipcelx.js' . $cacheBreak
        );

        // Custom code
        $incs = array( 'js/config', 'js/core', 'js/control', 'js/ext' );
        $clientRoot = Francis_Utils_Config::get( 'path.root' ) . '/public/client';
        foreach( $incs as $jspath ) {
            // Create recursive dir iterator which skips dot folders
            $dir = new RecursiveDirectoryIterator( $clientRoot . '/' . $jspath,
                FilesystemIterator::SKIP_DOTS);

            // Flatten the recursive iterator, folders come before their files
            $it  = new RecursiveIteratorIterator($dir,
                RecursiveIteratorIterator::SELF_FIRST);

            // Maximum depth is 1 level deeper than the base folder
            $it->setMaxDepth(1);

            // Get javascript includes
            foreach ($it as $fileinfo) {
                if ($fileinfo->isFile()) {
                    array_push( $jsses, $absolutePath . $jspath . '/' . $fileinfo->getFilename() . $cacheBreak );
                }
            }
        }


        return $jsses;
    }

    /**
     * Grabs HTML from Startpage
     * @param $url
     * @return string
     */
    public static function grabHTMLFromStartpage( $url ) {

        $doc = new DOMDocument();

        // We don't want to bother with white spaces
        $doc->preserveWhiteSpace = false;
        $doc->strictErrorChecking = false;
        $doc->recover = true;

        // Avoid crashing on HTML5 doc definition
        libxml_use_internal_errors(true);
        $doc->loadHTMLFile( $url );
        libxml_use_internal_errors(false);

        $xpath = new DOMXPath($doc);
        $query = "//div[@id='content']";

        $entries = $xpath->query($query);

        //return $entries->item(0)->nodeValue;
        return $doc->saveHTML( $entries->item(0) );
    }
} 