<?php

include_once './config.php';
include_once './models/services/GeometryService.php';
include_once './models/services/BrandService.php';
include_once './models/services/ModelService.php';
include_once './libs/Smarty.class.php';

if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
    if (isset($_SESSION['brand']))
    {
        $brandName = $_SESSION['brand'];

        $brandService = new BrandService();
        $brand = $brandService->getBrandByName($brandName);

        $geometryService = new GeometryService();
        $geometries = $geometryService->getAllGeometriesByBrand($brand->id, is_updater());

        $models = array();
        foreach ($geometries as $geo)
        {
            $modelService = new ModelService();
            $models[$geo->id] = $modelService->getAllModelsByGeometry($geo->id, is_updater());
        }

        if ($geometries)
        {
            $smarty = new Smarty;
            $smarty->assign('brand', $brand);
            $smarty->assign('geometries', $geometries);
            if ($models)
            {
                $smarty->assign('models', $models);
            }
            $smarty->assign('geometries', $geometries);
            $smarty->assign('user_can_update', is_updater());  
            $smarty->assign('user_logged_in', is_logged_in($user));      
            $smarty->display('views/geometries.tpl');
        }
        else
        {
            header('location:add_geometry.php?brand=' . $brand->name);
        }
    }
    else
    {
        $brandService = new BrandService();
        $brands = $brandService->getAllBrands();

        $smarty = new Smarty;
        $smarty->assign('brands', $brands);
        $smarty->assign('action', "geometries.php");
        $smarty->display('views/selectbrand.tpl');
    }
}
?>
