websiteWebsite
codingteam CodingTeam
A free forge, lightweight and extensible.

 

Browse the code

Revision log Information on the revision
Revision: 107 (differences)
Author: xbright
Log message: * Switch to the GNU Affero General Public License v3
Change revision:
<?php
# ***** BEGIN LICENSE BLOCK *****
#
#    This file is a part of CodingTeam. See <http://www.codingteam.net>.
#    Copyright (C) 2007 CodingTeam (See AUTHORS and THANKS for details)
#
#    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, version 3 only.
#
#    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/>.
#
# ***** END LICENSE BLOCK *****
 
class Template {
    private $tpl, $page, $ct_session, $ct_db, $basedir, $baseurl, $langlist;
 
    function __construct ($tpl, $page, $ct_session, $ct_db, $basedir, $baseurl, $langlist)
    {
        $this->tpl = $tpl;
        $this->page = $page;
        $this->ct_session = $ct_session;
        $this->ct_db = $ct_db;
        $this->basedir = $basedir;
        $this->baseurl = $baseurl;
        $this->langlist = $langlist;
    
        /* if ($this->ct_session->isLogged($this->ct_db)) // WARNING: add a '!' before test for the true return
        {
            $cachename = ereg_replace('/', '-', $page);
            $cachenamefile = $this->basedir.'/public/cache/'.md5($cachename).'.ctcache';
            if (@filemtime($cachenamefile) < time() - ( 3600 * 24 * 360 ))
            {
                ob_start();
                
                $this->parse();
    
                $cachecontent = ob_get_contents();
                ob_end_flush();
                $filec = fopen($cachenamefile, 'w');
                fwrite($filec, $cachecontent);
                fclose($filec);
            }
            else
                include ($cachenamefile);
        }
        else
        {
            $this->parse();
        }*/
        $this->parse();
    }
    
    function parse ()
    {
        // Load all head modules
        $headmodules = array();
        $head = array();
        $dir = $this->basedir.'/inc/modules/';
        $headmodulesdir = opendir($dir);
        while ($mod = readdir($headmodulesdir))
            if (substr($mod, 0, 5) == 'head_')
                array_push($headmodules, $mod);
 
        for($i=0; $i<count($headmodules); $i++)
        {
            require($this->basedir.'/inc/modules/'.$headmodules[$i].'/'.$headmodules[$i].'.php');
            $head[$i] = new $headmodules[$i]($this->ct_session, $this->ct_db, $this->page, $this->baseurl);
            $head[$i]->treatForms();
        }
 
        $pagetag = explode('/', $this->page);
        $i = 0;
        foreach ($pagetag as $tag_ => $value)
            $req[$i++] = $value;
            
        $temp['tpl:lang'] = $req[1];
        $temp['tpl:requesturi'] = mb_substr($this->page, 4);
 
        $mod = $this->basedir.'/inc/modules/'.$req[2].'/'.$req[2].'.php';
        if (file_exists($mod))
        {
            require($mod);
            $view_loaded = new Module($this->ct_session, $this->ct_db, $this->page);
            $view_loaded->treatForms();       
        }
        else
        {
            exit(_('No module found.'));
        }
        
        // Import the .tpl file
        $this->tpl_file = file_get_contents($this->tpl);
        $temp['tpl:baseurl'] = $this->baseurl;
        $temp['tpl:cssdir'] = 'inc/templates/';
        $temp['tpl:images'] = 'public/images/';
        $temp['tpl:logosimages'] = 'public/images/logos/';
    
        // Set global values
        $temp['slogan'] = _('Create. Participate. Evolve. Free Software.');
        $temp['link_freesoftwareprojects'] = _('Free software projects');
        $temp['link_forum'] = _('Forum');
        $temp['link_jabberserver'] = _('Jabber server');
        $temp['link_notepad'] = _('Notepad');
        $poweredby = _('Powered by CodingTeam <strong>%s</strong>');
        $temp['poweredby'] = str_replace('%s', file_get_contents($this->basedir.'/VERSION'), $poweredby);
        $temp['link_about'] = _('About');
        $temp['link_admin'] = _('Administration');
        
        // Set flag for translation
        ob_start(); 
        foreach ($this->langlist as $tag => $value)
            echo '<a href="'.$value.'/'.mb_substr($this->page, 4).'"><img src="public/images/i18n/flag_'.$value.'.png" alt="'.$value.'" /></a> ';
        $temp['tpl:langlinks'] = ob_get_clean();
 
        // Highlight current module in the head menu
        if ($req[2] == 'index')
            $temp['tpl:index'] = 'id="current"';
        else
            $temp['tpl:index'] = '';
 
        if ($req[2] == 'projects')
            $temp['tpl:projects'] = 'id="current"';
        else
            $temp['tpl:projects'] = '';
            
        if ($req[2] == 'forum')
            $temp['tpl:forum'] = 'id="current"';
        else
            $temp['tpl:forum'] = '';
            
        if ($req[2] == 'jabber')
            $temp['tpl:jabber'] = 'id="current"';
        else
            $temp['tpl:jabber'] = '';
        
        if ($req[2] == 'paste')
            $temp['tpl:paste'] = 'id="current"';
        else
            $temp['tpl:paste'] = '';        
        
        // Set global views
        if (array_key_exists(0, $head))
        {
            ob_start();
            $head[0]->getPageContent();
            $temp['view:head_member'] = ob_get_clean();
        }
        
        if (array_key_exists(1, $head))
        {
            ob_start();
            $head[1]->getPageContent();
            $temp['view:head_search'] = ob_get_clean();
        }
        
        // Set page data
        $view_loaded->getPageData();
        $temp['title'] = $view_loaded->title;
        $temp['htmlmeta'] = $view_loaded->meta;
 
        // Set page menu
        ob_start();
        $view_loaded->getPageMenu();
        $temp['view:pagemenu'] = ob_get_clean(); 
 
        // Set page content
        ob_start();
        $view_loaded->getPageContent();
        $temp['view:pagecontent'] = ob_get_clean();
 
        // Edit the template
        foreach ($temp as $key => $value)
        {
            $search[] = '{'.$key.'}';
            $replace[] = $value;
        }
        $this->tpl_show = str_replace($search, $replace, $this->tpl_file);
        
        // Show the result
        echo $this->tpl_show;
    }
 
}
?>