<?php
/**
 * ManageMetaTagController
 * 
 * @author Visual Media
 */

namespace VisualM\TagManagerBundle\Controller;

use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use VisualM\TagManagerBundle\Entity\MetaTag;
use VisualM\TagManagerBundle\Form\Type\MetaTagType;


/**
 * ManageMetaTagController
 * 
 * @author Visual Media
 */
class ManageMetaTagController extends Controller
{
    /**
     * Return available Fields
     * 
     * @return Response Response
     */
    public function availableFieldsAction()
    {
        return $this->render('VisualMTagManagerBundle:ManageMetaTag:availableFields.html.twig', [
            'data_fields' => $this->container->get('visualm.tag_manager.util.extended_data_dictionary')->getAvailableFields()
        ]);
    }

    /**
     * Return available Sets
     *
     * @return Response Response
     */
    public function availableSetsAction()
    {
        return $this->render('VisualMTagManagerBundle:ManageMetaTag:availableSets.html.twig', [
            'meta_tag_sets' => $this->container->get('visualm.tag_manager.manager.meta_tag_set')->getIndex([ 'kiesbaar' => true ], [ [ 'naam', 'ASC' ]])
        ]);
    }

    /**
     * Create Form
     * 
     * @param int $id MetaTagSet Id
     *
     * @return Response Response
     */
    public function addSetAction($id)
    {
        if (null === ($meta_tag_set = $this->container->get('visualm.tag_manager.manager.meta_tag_set')->getFirst([ 'id' => $id ]))) {
            throw $this->createNotFoundException();
        }

        // Create a new collection of meta tags based on the MetaTagPresets contained in the set
        $meta_tags = new ArrayCollection();
        foreach($meta_tag_set->getMetaTagPresets() as $meta_tag_preset) {
            $meta_tag = new MetaTag();
            $meta_tag->setInhoud($meta_tag_preset->getInhoud());
            $meta_tag->setInhoudIsExpression($meta_tag_preset->getInhoudIsExpression());
            $meta_tag->setInhoudGewijzigd(false);
            $meta_tag->setZichtbaar($meta_tag_preset->getZichtbaar());
            $meta_tag->setZichtbaarIsExpression($meta_tag_preset->getZichtbaarIsExpression());
            $meta_tag->setZichtbaarGewijzigd(false);
            $meta_tag->setMetaTagPreset($meta_tag_preset);
            $meta_tags->add($meta_tag);
        }

        // Create a new form on the fly.
        // We need to generate a random name for the form as the form name is used
        // as part of the field id's
        $form = $this->container->get('form.factory')->createNamedBuilder('add_set_' . mt_rand(10000, 99999), 'form')
            ->add('metaTags', 'collection', array(
                'type' => MetaTagType::class,
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => true,
                'label' => false,
                'prototype' => false,
                'mapped' => false,
                'data' => $meta_tags
            ))->getForm();



        return $this->render('VisualMTagManagerBundle:ManageMetaTag:addSet.html.twig', array(
            'form' => $form->createView(),
            'meta_tag_set' => $meta_tag_set
        ));
        
    }
 
}
