<?php
class controller_product extends controller
{
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
    
    protected function execute_index()
    {
        $path = func_get_args();
        $current = category::get_by_path($path);
        
        if (!$current)
        {
            $this->notfound();
            return;
        }
        
        category::save_view($current['id']);
        $data['current'] = $current;
        $data['path'] = category::get_path($current['id']);
        $data['child'] = category::get_child($current['id']);
        $data['product'] = product::get_list($current['id']);
        $data['type'] = 'category';
        $data['type_id'] = $current['id'];
        $this->render('index.html', $data);
    }
    
    protected function execute_view()
    {
        $path = func_get_args();
        $product = array_pop($path);
        $current = category::get_by_path($path);
        
        if (!$current)
        {
            $this->notfound();
            return;
        }
        
        $product = product::get_by_path($current['id'], $product);
        
        if (!$product)
        {
            $this->notfound();
            return;
        }
        
        $data['product'] = $product;
        product::save_view($product['id']);
        $data['path'] = category::get_path($product['category_id']);
        $data['type'] = 'product';
        $data['type_id'] = $product['id'];
        $this->render('view.html', $data);
    }
}
