<commit_before><?php

namespace Kunstmaan\PagePartBundle\Repository;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;

use Kunstmaan\UtilitiesBundle\Helper\ClassLookup;
use Kunstmaan\PagePartBundle\Helper\PagePartInterface;
use Kunstmaan\PagePartBundle\Entity\PagePartRef;
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;

/**
 * PagePartRefRepository
 */
class PagePartRefRepository extends EntityRepository
{

    /**
     * @param HasPagePartsInterface $page           The page
     * @param PagePartInterface     $pagepart       The pagepart
     * @param integer               $sequencenumber The sequence numer
     * @param string                $context        The context
     *
     * @return \Kunstmaan\PagePartBundle\Entity\PagePartRef
     */
    public function addPagePart(HasPagePartsInterface $page, PagePartInterface $pagepart, $sequencenumber, $context = "main")
    {
        $pagepartrefs = $this->getPagePartRefs($page);
        foreach ($pagepartrefs as $pagepartref) {
            if ($pagepartref->getSequencenumber() >= $sequencenumber) {
                $pagepartref->setSequencenumber($pagepartref->getSequencenumber() + 1);
                $this->getEntityManager()->persist($pagepartref);
            }
        }
        $pagepartref = new \Kunstmaan\PagePartBundle\Entity\PagePartRef();
        $pagepartref->setContext($context);
        $pageClassname = ClassLookup::getClass($page);
        $pagepartref->setPageEntityname($pageClassname);
        $pagepartref->setPageId($page->getId());
        $pagepartClassname = ClassLookup::getClass($pagepart);
        $pagepartref->setPagePartEntityname($pagepartClassname);
        $pagepartref->setPagePartId($pagepart->getId());
        $pagepartref->setSequencenumber($sequencenumber);
        $this->getEntityManager()->persist($pagepartref);
        $this->getEntityManager()->flush();

        return $pagepartref;
    }

    /**
     * @param HasPagePartsInterface $page    The page
     * @param string                $context The string
     *
     * @return PagePartRef[]
     */
    public function getPagePartRefs(HasPagePartsInterface $page, $context = "main")
    {
        return $this->findBy(array(
            'pageId' => $page->getId(),
            'pageEntityname' => ClassLookup::getClass($page),
            'context' => $context
        ), array('sequencenumber' => 'ASC'));
    }

    /**
     * @param HasPagePartsInterface $page    The page
     * @param string                $context The pagepart context
     *
     * @return PagePartInterface[]
     */
    public function getPageParts(HasPagePartsInterface $page, $context = "main")
    {
        $pagepartrefs = $this->getPagePartRefs($page, $context);
        $result = array();
        foreach ($pagepartrefs as $pagepartref) {
            $result[] = $pagepartref->getPagePart($this->getEntityManager());
        }

        return $result;
    }

    /**
     * @param EntityManager         $em       The entity manager
     * @param HasPagePartsInterface $frompage The page from where you copy the pageparts
     * @param HasPagePartsInterface $topage   The page to where you want to copy the pageparts
     * @param string                $context  The pagepart context
     */
    public function copyPageParts(EntityManager $em, HasPagePartsInterface $frompage, HasPagePartsInterface $topage, $context = "main")
    {
        $frompageparts = $this->getPageParts($frompage, $context);
        $sequencenumber = 1;
        foreach ($frompageparts as $frompagepart) {
            $toppagepart = clone $frompagepart;
            $toppagepart->setId(null);
            $em->persist($toppagepart);
            $em->flush();
            $this->addPagePart($topage, $toppagepart, $sequencenumber, $context);
            $sequencenumber++;
        }
    }

    /**
     * @param HasPagePartsInterface $page              The page<commit_msg>fix for deep cloning in pageparts (needed for sub entities)<commit_after><?php

namespace Kunstmaan\PagePartBundle\Repository;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;

use Kunstmaan\AdminBundle\Entity\DeepCloneInterface;
use Kunstmaan\UtilitiesBundle\Helper\ClassLookup;
use Kunstmaan\PagePartBundle\Helper\PagePartInterface;
use Kunstmaan\PagePartBundle\Entity\PagePartRef;
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;

/**
 * PagePartRefRepository
 */
class PagePartRefRepository extends EntityRepository
{

    /**
     * @param HasPagePartsInterface $page           The page
     * @param PagePartInterface     $pagepart       The pagepart
     * @param integer               $sequencenumber The sequence numer
     * @param string                $context        The context
     *
     * @return \Kunstmaan\PagePartBundle\Entity\PagePartRef
     */
    public function addPagePart(HasPagePartsInterface $page, PagePartInterface $pagepart, $sequencenumber, $context = "main")
    {
        $pagepartrefs = $this->getPagePartRefs($page);
        foreach ($pagepartrefs as $pagepartref) {
            if ($pagepartref->getSequencenumber() >= $sequencenumber) {
                $pagepartref->setSequencenumber($pagepartref->getSequencenumber() + 1);
                $this->getEntityManager()->persist($pagepartref);
            }
        }
        $pagepartref = new \Kunstmaan\PagePartBundle\Entity\PagePartRef();
        $pagepartref->setContext($context);
        $pageClassname = ClassLookup::getClass($page);
        $pagepartref->setPageEntityname($pageClassname);
        $pagepartref->setPageId($page->getId());
        $pagepartClassname = ClassLookup::getClass($pagepart);
        $pagepartref->setPagePartEntityname($pagepartClassname);
        $pagepartref->setPagePartId($pagepart->getId());
        $pagepartref->setSequencenumber($sequencenumber);
        $this->getEntityManager()->persist($pagepartref);
        $this->getEntityManager()->flush();

        return $pagepartref;
    }

    /**
     * @param HasPagePartsInterface $page    The page
     * @param string                $context The string
     *
     * @return PagePartRef[]
     */
    public function getPagePartRefs(HasPagePartsInterface $page, $context = "main")
    {
        return $this->findBy(array(
            'pageId' => $page->getId(),
            'pageEntityname' => ClassLookup::getClass($page),
            'context' => $context
        ), array('sequencenumber' => 'ASC'));
    }

    /**
     * @param HasPagePartsInterface $page    The page
     * @param string                $context The pagepart context
     *
     * @return PagePartInterface[]
     */
    public function getPageParts(HasPagePartsInterface $page, $context = "main")
    {
        $pagepartrefs = $this->getPagePartRefs($page, $context);
        $result = array();
        foreach ($pagepartrefs as $pagepartref) {
            $result[] = $pagepartref->getPagePart($this->getEntityManager());
        }

        return $result;
    }

    /**
     * @param EntityManager         $em       The entity manager
     * @param HasPagePartsInterface $fromPage The page from where you copy the pageparts
     * @param HasPagePartsInterface $toPage   The page to where you want to copy the pageparts
     * @param string                $context  The pagepart context
     */
    public function copyPageParts(EntityManager $em, HasPagePartsInterface $fromPage, HasPagePartsInterface $toPage, $context = "main")
    {
        $fromPageParts = $this->getPageParts($fromPage, $context);
        $sequenceNumber = 1;
        foreach ($fromPageParts as $fromPagePart) {
            $toPagePart = clone $fromPagePart;
            $toPagePart->setId(null);
            if ($toPagePart instanceof DeepCloneInterface) {
                $toPagePart->deepClone();
            }
            $em->persist($toPagePart);
            $em->flush();
            $this->addPagePart($toPage, $toPagePart, $sequenceNumber, $context);
            $sequenceNumber++;
        }
    }

    /**
     * @param HasPagePartsInterface $page              The page
{'fraction_non_alphanumeric': 0.11445932539682539, 'fraction_numerical': 0.000496031746031746, 'mean_word_length': 2.687700045724737, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 70, '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': '506731', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2244, 'n_words': 439}