<?php
class theme
{
    const DIRECTORY = 'web/skin/theme/';
    const TABLE = 'theme';
    
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    public static function get_list($type = null)
    {
        $theme = array();
        
        foreach (glob(self::DIRECTORY . '*') as $f)
        {
            $theme[] = basename($f);
        }
        
        return $theme;
    }
    
    public static function load($theme)
    {
        // Delete headers.
        foreach (glob(header::DIRECTORY . '*.jpg') as $f)
        {
            folder::delete($f);
        }
        
        // Load theme headers.
        foreach (glob(self::DIRECTORY . $theme . '/header/*.jpg') as $f)
        {
            folder::copy($f, header::DIRECTORY . basename($f));
        }
        
        // Delete favicons.
        foreach (glob(favicon::DIRECTORY . '*.ico') as $f)
        {
            folder::delete($f);
        }
        
        // Load theme favicons.
        foreach (glob(self::DIRECTORY . $theme . '/favicon/*.ico') as $f)
        {
            folder::copy($f, favicon::DIRECTORY . basename($f));
        }
        
        // Get theme.
        $setting = db::query()->select(self::TABLE)
                              ->where('theme', $theme)
                              ->execute();
        
        // Load theme.
        foreach ($setting as $s)
        {
            db::query()->update(setting::TABLE)
                       ->where('type', 'theme')
                       ->where('name', $s['name'])
                       ->column('value', $s['value'])
                       ->limit(1)
                       ->execute();
        }
    }
    
    public static function save($theme)
    {
        // Delete current theme with same name, if exists.
        self::delete($theme);
        
        $folder = self::DIRECTORY . $theme . '/';
        
        // Create theme folder.
        folder::create($folder, 0775);
        
        // Save theme headers.
        folder::copy(header::DIRECTORY, $folder . '/header/');
        
        // Save theme favicons.
        folder::copy(favicon::DIRECTORY, $folder . '/favicon/');
        
        folder::mode($folder, 0775);
        
        // Get settings.
        $setting = db::query()->select(setting::TABLE)
                              ->where('type', 'theme')
                              ->execute();
        
        // Save theme.
        foreach ($setting as $s)
        {
            db::query()->insert(self::TABLE)
                       ->column('theme', $theme)
                       ->column('name', $s['name'])
                       ->column('value', $s['value'])
                       ->execute();
        }
    }
    
    public static function delete($theme)
    {
        $folder = self::DIRECTORY . $theme . '/';
        
        // Delete folder.
        if (file_exists($folder))
        {
            folder::delete($folder);
        }
        
        // Delete theme.
        db::query()->delete(self::TABLE)
                   ->where('theme', $theme)
                   ->execute();
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}
