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

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    public static function get($id)
    {
        $module = db::query()->select(self::TABLE)
                          ->where('id', $id)
                          ->execute()->fetch();
        
        if ($module) {
            $module['data'] = json_decode($module['data'], false);
        }
        
        return $module;
    }
    
    public static function get_list()
    {
        $module = array();
        
        foreach (db::query()->select(self::TABLE . ' m')->execute() as $m) {
            $m['data'] = json_decode($m['data'], false);
            $module[] = $m;
        }
        
        return $module;
    }
    
    public static function add(array $data)
    {
        if (array_key_exists('data', $data)) {
            if (!is_object($data['data']) && !is_array($data['data'])) {
                throw new Exception('Data is not valid.');
            }
        } else {
            $data['data'] = new stdClass();
        }
        
        $data['data'] = json_encode($data['data'], false);
        
        db::query()->insert(self::TABLE)
                   ->column($data)
                   ->execute();
        
        $id = db::get()->get_last_id();
        return $id;
    }
    
    public static function edit($id, array $data)
    {
        if (array_key_exists('data', $data)) {
            if (!is_object($data['data']) && !is_array($data['data'])) {
                throw new Exception('Data is not valid.');
            }
        } else {
            $data['data'] = new stdClass();
        }
        
        $data['data'] = json_encode($data['data'], false);
        
        db::query()->update(self::TABLE)
                   ->column($data)
                   ->where('id', $id)
                   ->limit(1)
                   ->execute();
    }
    
    public static function delete($id)
    {
        db::query()->delete(self::TABLE)
                   ->where('id', $id)
                   ->limit(1)
                   ->execute();
    }
    
    public static function delete_by_type($id)
    {
        db::query()->delete(self::TABLE)
                   ->where('module_type_id', $id)
                   ->execute();
    }
    
    public static function render($id, $request)
    {
        $content = '';
        
        $render = function($module) use ($request) {
            ob_start();
            include fractal::find('module/' . $module['alias'] . '.php');
            return ob_get_clean();
        };
        
        $module = db::query()->select(module::TABLE . ' m')
                             ->column('m.*')
                             ->column('mt.alias')
                             ->join(module_type::TABLE . ' mt', array('mt.id', 'm.module_type_id'))
                             ->join(position::TABLE . ' p', array('p.id', 'm.position_id'))
                             ->where('m.id', $id)
                             ->order('m.order', 'ASC')
                             ->execute() ->fetch();;
        
        if ($module) {
            $module['data'] = json_decode($module['data'], false);
            $content .= '<div class="module ' . htmlspecialchars($module['alias']) . '">' . $render($module) . '</div>';
        }
        
        return $content;
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}
