<?php
class meta
{
    const TABLE = 'meta';
    
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    public static function get($type, $type_id)
    {
        return db::query()->select(self::TABLE)
                          ->where('type', $type)
                          ->where('type_id', $type_id)
                          ->limit(1)
                          ->execute()->fetch();
    }
    
    public static function get_info($type, $type_id)
    {
        return db::query()->select(self::TABLE . ' m')
                          ->column('m.*')
                          ->where('m.type', $type)
                          ->where('m.type_id', $type_id)
                          ->join(self::TABLE . '_language ml', array('m.language', 'ml.id'), 'left')
                          ->column('ml.abbr language')
                          ->join(self::TABLE . '_charset mc', array('m.charset', 'mc.id'), 'left')
                          ->column('mc.name charset')
                          ->join(self::TABLE . '_distribution md', array('m.distribution', 'md.id'), 'left')
                          ->column('md.name distribution')
                          ->join(self::TABLE . '_rating mr', array('m.rating', 'mr.id'), 'left')
                          ->column('mr.name rating')
                          ->join(self::TABLE . '_robots mb', array('m.robots', 'mb.id'), 'left')
                          ->column('mb.name robots')
                          ->join(self::TABLE . '_revisit_after mv', array('m.revisit_after', 'mv.id'), 'left')
                          ->column('mv.name revisit_after')
                          ->limit(1)
                          ->execute()->fetch();
    }
    
    public static function get_charset_list()
    {
        return db::query()->select(self::TABLE . '_charset')
                          ->execute();
    }
    
    public static function get_distribution_list()
    {
        return db::query()->select(self::TABLE . '_distribution')
                          ->execute();
    }
    
    public static function get_language_list()
    {
        return db::query()->select(self::TABLE . '_language')
                          ->execute();
    }
    
    public static function get_rating_list()
    {
        return db::query()->select(self::TABLE . '_rating')
                          ->execute();
    }
    
    public static function get_revisit_after_list()
    {
        return db::query()->select(self::TABLE . '_revisit_after')
                          ->execute();
    }
    
    public static function get_robots_list()
    {
        return db::query()->select(self::TABLE . '_robots')
                          ->execute();
    }
    
    
    public static function edit($type, $type_id, array $data)
    {
        $exists = count(db::query()->select(self::TABLE)
                                   ->where('type', $type)
                                   ->where('type_id', $type_id)
                                   ->limit(1)
                                   ->execute());
        
        $query = db::query();
        
        if ($exists)
        {
            $query->update(self::TABLE)
                  ->where('type', $type)
                  ->where('type_id', $type_id)
                  ->limit(1);
        }
        else
        {
            $query->insert(self::TABLE)
                  ->column('type', $type)
                  ->column('type_id', $type_id);
        }
        
        $query->column($data)
              ->execute();
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}
