Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
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
    /**
21
     * @param HasPagePartsInterface $page               The page
22
     * @param PagePartInterface     $pagepart           The pagepart
23
     * @param integer               $sequencenumber     The sequence numer
24
     * @param string                $context            The context
25
     * @param bool                  $pushOtherPageParts Push other pageparts (sequence + 1)
26
     *
27
     * @return PagePartRef
28
     */
29
    public function addPagePart(HasPagePartsInterface $page, PagePartInterface $pagepart, $sequencenumber, $context = "main", $pushOtherPageParts = true)
30
    {
31
        if ($pushOtherPageParts) {
32
            $pagepartrefs = $this->getPagePartRefs($page, $context);
33
            foreach ($pagepartrefs as $pagepartref) {
34
                if ($pagepartref->getSequencenumber() >= $sequencenumber) {
35
                    $pagepartref->setSequencenumber($pagepartref->getSequencenumber() + 1);
36
                    $this->getEntityManager()->persist($pagepartref);
37
                }
38
            }
39
        }
40
41
        $pagepartref = new PagePartRef();
42
        $pagepartref->setContext($context);
43
        $pageClassname = ClassLookup::getClass($page);
44
        $pagepartref->setPageEntityname($pageClassname);
45
        $pagepartref->setPageId($page->getId());
46
        $pagepartClassname = ClassLookup::getClass($pagepart);
47
        $pagepartref->setPagePartEntityname($pagepartClassname);
48
        $pagepartref->setPagePartId($pagepart->getId());
49
        $pagepartref->setSequencenumber($sequencenumber);
50
        $this->getEntityManager()->persist($pagepartref);
51
        $this->getEntityManager()->flush();
52
53
        return $pagepartref;
54
    }
55
56
    /**
57
     * @param HasPagePartsInterface $page    The page
58
     * @param string                $context The string
59
     *
60
     * @return PagePartRef[]
61
     */
62
    public function getPagePartRefs(HasPagePartsInterface $page, $context = "main")
63
    {
64
        return $this->findBy(array(
65
            'pageId' => $page->getId(),
66
            'pageEntityname' => ClassLookup::getClass($page),
67
            'context' => $context
68
        ), array('sequencenumber' => 'ASC'));
69
    }
70
71
    /**
72
     * @param HasPagePartsInterface $page    The page
73
     * @param string                $context The pagepart context
74
     *
75
     * @return PagePartInterface[]
76
     */
77
    public function getPageParts(HasPagePartsInterface $page, $context = "main")
78
    {
79
        $pagepartrefs = $this->getPagePartRefs($page, $context);
80
81
        // Group pagepartrefs per type and remember the sorting order
82
        $types = $order = array();
83
        $counter = 1;
84
        foreach ($pagepartrefs as $pagepartref) {
85
            $types[$pagepartref->getPagePartEntityname()][] = $pagepartref->getPagePartId();
86
            $order[$pagepartref->getPagePartEntityname() . $pagepartref->getPagePartId()] = $counter;
87
            $counter++;
88
        }
89
90
        // Fetch all the pageparts (only one query per pagepart type)
91
        $pageparts = array();
92 View Code Duplication
        foreach ($types as $classname => $ids) {
93
            $result = $this->getEntityManager()->getRepository($classname)->findBy(array('id' => $ids));
94
            $pageparts = array_merge($pageparts, $result);
95
        }
96
97
        // Order the pageparts
98
        usort($pageparts, function(EntityInterface $a, EntityInterface $b) use ($order) {
99
            $aPosition = $order[get_class($a) . $a->getId()];
100
            $bPosition = $order[get_class($b) . $b->getId()];
101
102
            if ($aPosition < $bPosition) {
103
                return -1;
104
            } elseif ($aPosition > $bPosition) {
105
                return 1;
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
}
204