<commit_before><?php
/**
 * This file is part of the Networking package.
 *
 * (c) net working AG <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Networking\InitCmsBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityNotFoundException;
use Doctrine\ORM\Query;
use Gedmo\Tree\Entity\Repository\MaterializedPathRepository;
use Networking\InitCmsBundle\Model\PageInterface;
use Networking\InitCmsBundle\Model\PageManagerInterface;
use Networking\InitCmsBundle\Model\LayoutBlock;

/**
 * Class PageManager.
 *
 * @author <NAME> <<EMAIL>>
 */
class PageManager extends MaterializedPathRepository implements PageManagerInterface
{
    /**
     * PageManager constructor.
     * @param EntityManagerInterface $om
     * @param $class
     */
    public function __construct(EntityManagerInterface $om, $class)
    {
        if (class_exists($class)) {
            $classMetaData = $om->getClassMetadata($class);

            parent::__construct($om, $classMetaData);
        }
    }

    /**
     * @param string $id
     *
     * @return object
     */
    public function findById($id)
    {
        return $this->find($id);
    }

    /**
     * @param $locale
     * @param null $id
     * @param bool $showHome
     * @param bool $showChildren
     *
     * @return \Doctrine\ORM\QueryBuilder|mixed
     */
    public function getParentPagesQuery($locale, $id = null, $showHome = false, $showChildren = false)
    {
        $qb = $this->createQueryBuilder('p');
        if (!$showHome) {
            $qb->where($qb->expr()->isNull('p.isHome').' OR p.isHome <> 1');
        }
        if ($id) {
            if (!$showChildren) {
                /** @var $page PageInterface */
                $page = $this->find($id);
                $collection = new ArrayCollection($page->getAllChildren());
                $childrenIds = $collection->map(
                    function (PageInterface $p) {
                        return $p->getId();
                    }
                );

                if ($childrenIds->count()) {
                    $qb->andWhere($qb->expr()->notIn('p.id', $childrenIds->toArray()));
                }
            }
            $qb->andWhere($qb->expr()->neq('p.id', $id));
        }

        $qb->andWhere('p.locale = :locale');
        $qb->orderBy('p.path', 'ASC');

        $qb->setParameter(':locale', $locale);

        return $qb;
    }

    /**
     * @param $locale
     * @param null $id
     *
     * @return mixed
     */
    public function getParentPagesChoices($locale, $id = null)
    {
        $qb = $this->getParentPagesQuery($locale, $id);

        return $qb->getQuery()->execute();
    }

    /**
     * @param $sort
     * @param string $order
     * @param int    $hydrationMode
     *
     * @return mixed
     */
    public function getAllSortBy($sort, $order = 'DESC', $hydrationMode = Query::HYDRATE_OBJECT)
    {
        $query = $this->getAllSortByQuery($sort, $order);

        return $query->execute([], $hydrationMode);
    }

    /**
     * @param $sort
     * @param string $order
     *
     * @return Query
     */
    public function getAllSortByQuery($sort, $order = 'DESC')
    {
        $qb = $this->createQueryBuilder('p');
        $qb2 = $this->getEntityManager()->getRepository(PageSnapshot::class)->createQueryBuilder('pp');
        $qb->select('p', 'ps')
            ->leftJoin('p.snapshots', 'ps')
            ->where($qb->expr()->eq(
                'ps.id',
                '('.$qb2->select('MAX(pp.id)')
                    ->where('p.id = pp.page')
                    ->getDQL().')'
            ))
            ->orWhere('ps.id IS NULL')
            ->orderBy('p.'.$sort,  $order);

        return $qb->getQuery();
    }

    /**
     * Return the content identifier for the provided content object for
     * debugging purposes.
     *
     * @param object $content A content instance
     *
     * @return string|null $id id of the content object or null if unable to determine an id
     */
    public function getContentId($content)
    {
        return $content->getId();
    }

    /**
     * @param PageInterface              $draftPage
     * @param \JMS\Serializer\Serializer $serializer
     *
     * @return PageInterface
     */
    public function revertToPublished(PageInterface $draftPage, \JMS\Serializer\SerializerInterface $serializer)
    {
        $pageSnapshot = $draftPage->getSnapshot();
        $contentRoute = $draftPage->getContentRoute();

        /** @var $publishedPage PageInterface */
        $publishedPage = $serializer->deserialize(
            $pageSnapshot->getVersionedData(),
            $this->getClassName(),
            'json'
        );

        // Save the layout blocks in a temp variable so that we can<commit_msg>fix revertToPublished method<commit_after><?php
/**
 * This file is part of the Networking package.
 *
 * (c) net working AG <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Networking\InitCmsBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityNotFoundException;
use Doctrine\ORM\Query;
use Gedmo\Tree\Entity\Repository\MaterializedPathRepository;
use Networking\InitCmsBundle\Model\PageInterface;
use Networking\InitCmsBundle\Model\PageManagerInterface;
use Networking\InitCmsBundle\Model\LayoutBlock;
use Networking\InitCmsBundle\Serializer\PageSnapshotDeserializationContext;

/**
 * Class PageManager.
 *
 * @author <NAME> <<EMAIL>>
 */
class PageManager extends MaterializedPathRepository implements PageManagerInterface
{
    /**
     * PageManager constructor.
     * @param EntityManagerInterface $om
     * @param $class
     */
    public function __construct(EntityManagerInterface $om, $class)
    {
        if (class_exists($class)) {
            $classMetaData = $om->getClassMetadata($class);

            parent::__construct($om, $classMetaData);
        }
    }

    /**
     * @param string $id
     *
     * @return object
     */
    public function findById($id)
    {
        return $this->find($id);
    }

    /**
     * @param $locale
     * @param null $id
     * @param bool $showHome
     * @param bool $showChildren
     *
     * @return \Doctrine\ORM\QueryBuilder|mixed
     */
    public function getParentPagesQuery($locale, $id = null, $showHome = false, $showChildren = false)
    {
        $qb = $this->createQueryBuilder('p');
        if (!$showHome) {
            $qb->where($qb->expr()->isNull('p.isHome').' OR p.isHome <> 1');
        }
        if ($id) {
            if (!$showChildren) {
                /** @var $page PageInterface */
                $page = $this->find($id);
                $collection = new ArrayCollection($page->getAllChildren());
                $childrenIds = $collection->map(
                    function (PageInterface $p) {
                        return $p->getId();
                    }
                );

                if ($childrenIds->count()) {
                    $qb->andWhere($qb->expr()->notIn('p.id', $childrenIds->toArray()));
                }
            }
            $qb->andWhere($qb->expr()->neq('p.id', $id));
        }

        $qb->andWhere('p.locale = :locale');
        $qb->orderBy('p.path', 'ASC');

        $qb->setParameter(':locale', $locale);

        return $qb;
    }

    /**
     * @param $locale
     * @param null $id
     *
     * @return mixed
     */
    public function getParentPagesChoices($locale, $id = null)
    {
        $qb = $this->getParentPagesQuery($locale, $id);

        return $qb->getQuery()->execute();
    }

    /**
     * @param $sort
     * @param string $order
     * @param int    $hydrationMode
     *
     * @return mixed
     */
    public function getAllSortBy($sort, $order = 'DESC', $hydrationMode = Query::HYDRATE_OBJECT)
    {
        $query = $this->getAllSortByQuery($sort, $order);

        return $query->execute([], $hydrationMode);
    }

    /**
     * @param $sort
     * @param string $order
     *
     * @return Query
     */
    public function getAllSortByQuery($sort, $order = 'DESC')
    {
        $qb = $this->createQueryBuilder('p');
        $qb2 = $this->getEntityManager()->getRepository(PageSnapshot::class)->createQueryBuilder('pp');
        $qb->select('p', 'ps')
            ->leftJoin('p.snapshots', 'ps')
            ->where($qb->expr()->eq(
                'ps.id',
                '('.$qb2->select('MAX(pp.id)')
                    ->where('p.id = pp.page')
                    ->getDQL().')'
            ))
            ->orWhere('ps.id IS NULL')
            ->orderBy('p.'.$sort,  $order);

        return $qb->getQuery();
    }

    /**
     * Return the content identifier for the provided content object for
     * debugging purposes.
     *
     * @param object $content A content instance
     *
     * @return string|null $id id of the content object or null if unable to determine an id
     */
    public function getContentId($content)
    {
        return $content->getId();
    }

    /**
     * @param PageInterface              $draftPage
     * @param \JMS\Serializer\Serializer $serializer
     *
     * @return PageInterface
     */
    public function revertToPublished(PageInterface $draftPage, \JMS\Serializer\SerializerInterface $serializer)
    {
        $pageSnapshot = $draftPage->getSnapshot();
        $contentRoute = $draftPage->getContentRoute();

        $context = new PageSnapshotDeserializationContext();
        $context->setDeserializeTranslations(true);

        /** @var $publishedPage PageInterface */
        $publishedPage = $serializer->deserialize(
            $pageSnapshot->getVersionedData(),
            $this->getClassName(),
            'json',
            $context
        );

        // Save the layout blocks in a temp variable so that we can
{'fraction_non_alphanumeric': 0.14260160985789527, 'fraction_numerical': 0.0005962436649110603, 'mean_word_length': 2.1196528208307504, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4908266', 'n_tokens_mistral': 2928, 'n_tokens_neox': 2852, 'n_words': 614}