Completed
Push — master ( e6c0c9...d841f8 )
by Jeroen
35:52 queued 19:21
created

Repository/PagePartRefRepository.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\PagePartBundle\Repository;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\EntityRepository;
7
use Kunstmaan\AdminBundle\Entity\DeepCloneInterface;
8
use Kunstmaan\AdminBundle\Entity\EntityInterface;
9
use Kunstmaan\PagePartBundle\Entity\PagePartRef;
10
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
11
use Kunstmaan\PagePartBundle\Helper\PagePartInterface;
12
use Kunstmaan\UtilitiesBundle\Helper\ClassLookup;
13
14
/**
15
 * PagePartRefRepository
16
 */
17
class PagePartRefRepository extends EntityRepository
18
{
19
    /**
20
     * @param HasPagePartsInterface $page               The page
21
     * @param PagePartInterface     $pagepart           The pagepart
22
     * @param int                   $sequencenumber     The sequence numer
23
     * @param string                $context            The context
24
     * @param bool                  $pushOtherPageParts Push other pageparts (sequence + 1)
25
     *
26
     * @return PagePartRef
27
     */
28
    public function addPagePart(HasPagePartsInterface $page, PagePartInterface $pagepart, $sequencenumber, $context = 'main', $pushOtherPageParts = true)
29
    {
30
        if ($pushOtherPageParts) {
31
            $pagepartrefs = $this->getPagePartRefs($page, $context);
32
            foreach ($pagepartrefs as $pagepartref) {
33
                if ($pagepartref->getSequencenumber() >= $sequencenumber) {
34
                    $pagepartref->setSequencenumber($pagepartref->getSequencenumber() + 1);
35
                    $this->getEntityManager()->persist($pagepartref);
36
                }
37
            }
38
        }
39
40
        $pagepartref = new PagePartRef();
41
        $pagepartref->setContext($context);
42
        $pageClassname = ClassLookup::getClass($page);
43
        $pagepartref->setPageEntityname($pageClassname);
44
        $pagepartref->setPageId($page->getId());
45
        $pagepartClassname = ClassLookup::getClass($pagepart);
46
        $pagepartref->setPagePartEntityname($pagepartClassname);
47
        $pagepartref->setPagePartId($pagepart->getId());
48
        $pagepartref->setSequencenumber($sequencenumber);
49
        $this->getEntityManager()->persist($pagepartref);
50
        $this->getEntityManager()->flush();
51
52
        return $pagepartref;
53
    }
54
55
    /**
56
     * @param HasPagePartsInterface $page    The page
57
     * @param string                $context The string
58
     *
59
     * @return PagePartRef[]
60
     */
61
    public function getPagePartRefs(HasPagePartsInterface $page, $context = 'main')
62
    {
63
        return $this->findBy(array(
64
            'pageId' => $page->getId(),
65
            'pageEntityname' => ClassLookup::getClass($page),
66
            'context' => $context,
67
        ), array('sequencenumber' => 'ASC'));
68
    }
69
70
    /**
71
     * @param HasPagePartsInterface $page    The page
72
     * @param string                $context The pagepart context
73
     *
74
     * @return PagePartInterface[]
75
     */
76
    public function getPageParts(HasPagePartsInterface $page, $context = 'main')
77
    {
78
        $pagepartrefs = $this->getPagePartRefs($page, $context);
79
80
        // Group pagepartrefs per type and remember the sorting order
81
        $types = $order = array();
82
        $counter = 1;
83
        foreach ($pagepartrefs as $pagepartref) {
84
            $types[$pagepartref->getPagePartEntityname()][] = $pagepartref->getPagePartId();
85
            $order[$pagepartref->getPagePartEntityname() . $pagepartref->getPagePartId()] = $counter;
86
            ++$counter;
87
        }
88
89
        // Fetch all the pageparts (only one query per pagepart type)
90
        $pageparts = array();
91 View Code Duplication
        foreach ($types as $classname => $ids) {
92
            $result = $this->getEntityManager()->getRepository($classname)->findBy(array('id' => $ids));
93
            $pageparts = array_merge($pageparts, $result);
94
        }
95
96
        // Order the pageparts
97
        usort($pageparts, function (EntityInterface $a, EntityInterface $b) use ($order) {
98
            $aPosition = $order[get_class($a) . $a->getId()];
99
            $bPosition = $order[get_class($b) . $b->getId()];
100
101
            if ($aPosition < $bPosition) {
102
                return -1;
103
            } elseif ($aPosition > $bPosition) {
104
                return 1;
105
            }
106
107
            return 0;
108
        });
109
110
        return $pageparts;
111
    }
112
113
    /**
114
     * @param EntityManager         $em       The entity manager
115
     * @param HasPagePartsInterface $fromPage The page from where you copy the pageparts
116
     * @param HasPagePartsInterface $toPage   The page to where you want to copy the pageparts
117
     * @param string                $context  The pagepart context
118
     */
119
    public function copyPageParts(EntityManager $em, HasPagePartsInterface $fromPage, HasPagePartsInterface $toPage, $context = 'main')
120
    {
121
        $fromPageParts = $this->getPageParts($fromPage, $context);
122
        $sequenceNumber = 1;
123
        foreach ($fromPageParts as $fromPagePart) {
124
            $toPagePart = clone $fromPagePart;
125
            $toPagePart->setId(null);
126
            if ($toPagePart instanceof DeepCloneInterface) {
127
                $toPagePart->deepClone();
128
            }
129
            $em->persist($toPagePart);
130
            $em->flush($toPagePart);
131
            $this->addPagePart($toPage, $toPagePart, $sequenceNumber, $context, false);
132
            ++$sequenceNumber;
133
        }
134
    }
135
136
    /**
137
     * @param HasPagePartsInterface $page              The page
138
     * @param string                $pagepartClassname The classname of the pagepart
139
     * @param string                $context           The context
140
     *
141
     * @return mixed
142
     */
143 View Code Duplication
    public function countPagePartsOfType(HasPagePartsInterface $page, $pagepartClassname, $context = 'main')
144
    {
145
        $em = $this->getEntityManager();
146
        $pageClassname = ClassLookup::getClass($page);
147
148
        $sql = 'SELECT COUNT(pp.id) FROM KunstmaanPagePartBundle:PagePartRef pp
149
                 WHERE pp.pageEntityname = :pageEntityname
150
                   AND pp.pageId = :pageId
151
                   AND pp.pagePartEntityname = :pagePartEntityname
152
                   AND pp.context = :context';
153
154
        return $em->createQuery($sql)
155
                ->setParameter('pageEntityname', $pageClassname)
156
                ->setParameter('pageId', $page->getId())
157
                ->setParameter('pagePartEntityname', $pagepartClassname)
158
                ->setParameter('context', $context)->getSingleScalarResult();
159
    }
160
161
    /**
162
     * Test if entity has pageparts for the specified context
163
     *
164
     * @param HasPagePartsInterface $page    The page
165
     * @param string                $context The context
166
     *
167
     * @return bool
168
     */
169 View Code Duplication
    public function hasPageParts(HasPagePartsInterface $page, $context = 'main')
170
    {
171
        $em = $this->getEntityManager();
172
        $pageClassname = ClassLookup::getClass($page);
173
174
        $sql = 'SELECT COUNT(pp.id) FROM KunstmaanPagePartBundle:PagePartRef pp
175
                 WHERE pp.pageEntityname = :pageEntityname
176
                   AND pp.pageId = :pageId
177
                   AND pp.context = :context';
178
179
        return $em->createQuery($sql)
180
                ->setParameter('pageEntityname', $pageClassname)
181
                ->setParameter('pageId', $page->getId())
182
                ->setParameter('context', $context)->getSingleScalarResult() != 0;
183
    }
184
185
    /**
186
     * @param int    $id             The id
187
     * @param string $context        The context
188
     * @param int    $sequenceNumber The sequence number
189
     *
190
     * @return PagePartInterface
0 ignored issues
show
Should the return type not be PagePartInterface|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
191
     */
192
    public function getPagePart($id, $context = 'main', $sequenceNumber)
193
    {
194
        $ppRef = $this->find($id);
195
        $ppRef->setContext($context);
196
        $ppRef->setSequenceNumber($sequenceNumber);
197
        $this->getEntityManager()->persist($ppRef);
198
        $this->getEntityManager()->flush($ppRef);
199
200
        return $this->getEntityManager()->getRepository($ppRef->getPagePartEntityName())->find($ppRef->getPagePartId());
201
    }
202
}
203