<?php

namespace KupShop\ElninoBundle\Controller;

use Image;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class PhotoController extends AbstractController
{
    /**
     * @Route("/photo360/{id}/")
     */
    public function photo360Action($id)
    {
        $ids = [$id => getImagePath($id, 14)];
        foreach (sqlQuery('SELECT id FROM photos WHERE parent = :id_parent ORDER BY id', ['id_parent' => $id]) as $img) {
            $ids[$img['id']] = getImagePath($img['id'], 14);
        }

        $smarty = createSmarty();

        if ($ids) {
            // Create thumbnail of first image
            $image = new \Image();
            $thumbnail = $image->getImage(key($ids), 14);
            $image->createThumbnail($thumbnail);

            foreach ($ids as &$value) {
                $value = '/'.$value;
            }

            $smarty->assign([
                'ids' => $ids,
                'dimensions' => $image->getOutputSize($thumbnail),
            ]);
        }

        return new Response($smarty->fetch('360.tpl'));
    }
}
