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

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    public static function get($id)
    {
        $menu_item = db::query()->select(self::TABLE)
                     ->where('id', $id)
                     ->execute()->fetch();
        
        if ($menu_item) {
            $menu_item['data'] = json_decode($menu_item['data'], false);
        }
        
        return $menu_item;
    }
    
    public static function get_by_alias($alias)
    {
        $menu_item = db::query()->select(self::TABLE)
                     ->where('alias', $alias)
                     ->execute()->fetch();
        
        if ($menu_item) {
            $menu_item['data'] = json_decode($menu_item['data'], false);
        }
        
        return $menu_item;
    }
    
    public static function get_list($menu = null)
    {
        $menu_item_type = array();
        $q = db::query()->select(self::TABLE . ' mi')
                        ->column('mi.*')
                        ->join('meta m', array('AND', array('=', 'mi.id', (object) array('type' => 'identifier', 'value' => 'm.type_id')), array('=', 'm.type', 'menu_item')), 'left')
                        ->column('m.title meta')
                        ->join('menu_item_type mit', array('=', 'mi.menu_item_type_id', 'mit.id'), 'inner')
                        ->column('mit.alias type_alias')
                        ->order('mi.order', 'ASC');
        
        if ($menu !== null)
        {
            $q->where('menu_id', $menu);
        }
        
        foreach ($q->execute() as $mit) {
            $mit['data'] = json_decode($mit['data'], false);
            $menu_item_type[] = $mit;
        }
        
        return $menu_item_type;
    }
    
    public static function get_count($menu = null)
    {
        $q = db::query()->select(self::TABLE)
                        ->column(sql::raw('COUNT(id) as total'));
        
        if ($menu !== null)
        {
            $q->where('menu_id', $menu);
        }
        
        $row = $q->execute()->fetch();
        return $row['total'];
    }
    
    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_menu($id)
    {
        db::query()->delete(self::TABLE)
                   ->where('menu_id', $id)
                   ->execute();
    }
    
    public static function delete_by_menu_item_type($id)
    {
        db::query()->delete(self::TABLE)
                   ->where('menu_item_type_id', $id)
                   ->execute();
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}
