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

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    public static function get($id)
    {
        return db::query()->select(self::TABLE)
                          ->where('id', $id)
                          ->execute()->fetch();
    }
    
    public static function get_by_path($path)
    {
        return db::query()->select(self::TABLE)
                          ->where('path', $path)
                          ->execute()->fetch();
    }
    
    public static function get_list($section = null)
    {
        $q = db::query()->select(self::TABLE . ' p')
                        ->column('p.*')
                        ->join('meta m', array('AND', array('=', 'p.id', (object) array('type' => 'identifier', 'value' => 'm.type_id')), array('=', 'm.type', 'page')), 'left')
                        ->column('m.title meta');
        
        if ($section !== null)
        {
            $q->join('section s', array('p.section_id', 's.id'), 'inner')
              ->where('s.id', $section);
        }
        
        return $q->execute();
    }
    
    public static function get_list_by_path($section = null)
    {
        $q = db::query()->select(self::TABLE . ' p')
                        ->column('p.*');
        
        if ($section !== null)
        {
            $q->join('section s', array('p.section_id', 's.id'), 'inner')
              ->where('s.path', $section);
        }
        
        return $q->execute();
    }
    
    public static function get_count($section = null)
    {
        $q = db::query()->select(self::TABLE)
                        ->column(sql::raw('COUNT(id) as total'));
        
        if ($section !== null)
        {
            $q->where('section_id', $section);
        }
        
        $row = $q->execute()->fetch();
        return $row['total'];
    }
    
    public static function add(array $data)
    {
        db::query()->insert(self::TABLE)
                   ->column($data)
                   ->execute();
        
        $id = db::get()->get_last_id();
        unset($data['id']);
        search::save('page', $id, strip_tags(implode(' ', $data)), $data['name']);
        return $id;
    }
    
    public static function edit($id, array $data)
    {
        db::query()->update(self::TABLE)
                   ->column($data)
                   ->where('id', $id)
                   ->limit(1)
                   ->execute();
        
        unset($data['id']);
        search::save('page', $id, strip_tags(implode(' ', $data)), $data['name']);
    }
    
    public static function delete($id)
    {
        db::query()->delete(self::TABLE)
                   ->where('id', $id)
                   ->limit(1)
                   ->execute();
        
        search::delete('page', $id);
    }
    
    public static function delete_by_section($id)
    {
        db::query()->delete(self::TABLE)
                   ->where('section_id', $id)
                   ->execute();
    }
    
    public static function get_views($id = null)
    {
        $q = db::query()->select(self::TABLE)
                        ->column(sql::raw('SUM(views) as total'));
        
        if ($id !== null)
        {
            $q->where('id', $id);
        }
        
        $row = $q->execute()->fetch();
        return $row['total'];
    }
    
    public static function save_view($id)
    {
        db::query()->update(self::TABLE)
                   ->column('views', sql::raw('views + 1'))
                   ->where('id', $id)
                   ->execute();
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}
