<?php
/**
 * Add MetaTag for Content that has the set included when adding a new MetaTagPreset
 *
 * @author Elze Kool <info@visualmedia.nl>
 */

namespace VisualM\TagManagerBundle\Doctrine\EventListener;

use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use VisualM\TagManagerBundle\Entity\MetaTag;
use VisualM\TagManagerBundle\Entity\MetaTagPreset;



/**
 * Add MetaTag for Content that has the set included when adding a new MetaTagPreset
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class AddMetaTagFromPresetSubscriber implements EventSubscriber, ContainerAwareInterface
{
    use ContainerAwareTrait;

    /**
     * Needs Flush
     * @var bool
     */
    private $needsFlush = false;
    
    /**
     * Specifies the list of events to listen
     *
     * @return array
     */
    public function getSubscribedEvents()
    {
        return array(
            'postPersist',
            'postFlush'
        );
    }

    /**
     * postPersist event
     *
     * @param LifecycleEventArgs $args Event Data
     */
    public function postPersist(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();
        if (!($entity instanceof MetaTagPreset)) {
            return;
        }

        // First get list of preset ids to check for in content
        $meta_tag_preset_ids = [];
        foreach($entity->getMetaTagSet()->getMetaTagPresets() as $meta_tag_preset) {
            if ($meta_tag_preset->getId() !== null) {
                $meta_tag_preset_ids[] = $meta_tag_preset->getId();
            }
        }

        // Then find the content ids that contain one of them
        $meta_tag_repository = $this->container->get('visualm.tag_manager.manager.meta_tag');
        $content_ids = $meta_tag_repository->getDistinctContentIds(['meta_tag_preset_id' => $meta_tag_preset_ids]);

        // Create a new MetaTag for all contentIds
        foreach($content_ids as $content_id) {
            // Create new tag from preset
            $meta_tag = new MetaTag();
            $meta_tag->setAutoAdded(true);
            $meta_tag->setContentId($content_id);
            $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);

            $args->getEntityManager()->persist($meta_tag);
            $this->needsFlush = true;
        }
    }

    /**
     * postFlush event
     *
     * @param PostFlushEventArgs $eventArgs Event Data
     */
    public function postFlush(PostFlushEventArgs $eventArgs)
    {
        if ($this->needsFlush) {
            $this->needsFlush = false;
            $eventArgs->getEntityManager()->flush();
        }
    }
}



