Completed
Pull Request — 4.0 (#3658)
by Kentaro
06:26
created

PageRepository::getByUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 19
ccs 16
cts 16
cp 1
crap 1
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Repository;
15
16
use Doctrine\ORM\NoResultException;
17
use Eccube\Common\EccubeConfig;
18
use Eccube\Entity\Master\DeviceType;
19
use Eccube\Entity\Page;
20
use Symfony\Bridge\Doctrine\RegistryInterface;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
23
/**
24
 * PageRepository
25
 *
26
 * This class was generated by the Doctrine ORM. Add your own custom
27
 * repository methods below.
28
 */
29
class PageRepository extends AbstractRepository
30
{
31
    /**
32
     * @var EccubeConfig
33
     */
34
    protected $eccubeConfig;
35
36
    /**
37
     * @var string
38
     * @path %eccube_theme_user_data_dir% (app/template/user_data)
39
     */
40
    protected $userDataRealDir;
41
42
    /**
43
     * @var string
44
     * @path %eccube_theme_app_dir% (app/template)
45
     */
46
    protected $templateRealDir;
47
48
    /**
49
     * @var string
50
     * @path %eccube_theme_src_dir% (src/Eccube/Resource/template)
51
     */
52
    protected $templateDefaultRealDir;
53
54
    /**
55
     * PageRepository constructor.
56
     *
57
     * @param RegistryInterface $registry
58
     * @param EccubeConfig $eccubeConfig
59
     * @param ContainerInterface $container
60
     */
61 775
    public function __construct(RegistryInterface $registry, EccubeConfig $eccubeConfig, ContainerInterface $container)
62
    {
63 775
        parent::__construct($registry, Page::class);
64 775
        $this->eccubeConfig = $eccubeConfig;
65 775
        $this->userDataRealDir = $container->getParameter('eccube_theme_user_data_dir');
66 775
        $this->templateRealDir = $container->getParameter('eccube_theme_app_dir');
67 775
        $this->templateDefaultRealDir = $container->getParameter('eccube_theme_src_dir');
68
    }
69
70
    /**
71
     * @param DeviceType $DeviceType
72
     * @param int $pageId
73
     *
74
     * @return array
75
     */
76 1
    public function findUnusedBlocks(DeviceType $DeviceType, $pageId)
77
    {
78 1
        $em = $this->getEntityManager();
79 1
        $blockRepo = $em->getRepository('Eccube\Entity\Block');
80 1
        $ownBlockPositions = $this->getByDeviceTypeAndId($DeviceType, $pageId)->getBlockPositions();
81 1
        $ids = [];
82 1
        foreach ($ownBlockPositions as $ownBlockPosition) {
83 1
            $ids[] = $ownBlockPosition->getBlock()->getId();
84
        }
85
86
        // $idsが空配列だと、$ids以外のblockを取得するSQLが生成されないため、存在しないidを入れる
87 1
        if (empty($ids)) {
88
            $ids[] = \Eccube\Entity\Block::UNUSED_BLOCK_ID;
89
        }
90
91 1
        return $blockRepo->createQueryBuilder('b')
92 1
            ->where('b.id not in (:ids)')
93 1
            ->setParameter(':ids', $ids)
94 1
            ->getQuery()
95 1
            ->getResult();
96
    }
97
98
    /**
99
     * @param DeviceType $DeviceType
100
     * @param int $pageId
101
     *
102
     * @return mixed
103
     */
104 5
    public function getByDeviceTypeAndId(DeviceType $DeviceType, $pageId)
105
    {
106 5
        $qb = $this->createQueryBuilder('p')
107 5
            ->select('p, bp, b')
108 5
            ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id')
109 5
            ->leftJoin('bp.Block', 'b')
110 5
            ->andWhere('p.DeviceType = :DeviceType AND p.id = :pageId')
111 5
            ->addOrderBy('bp.section', 'ASC')
112 5
            ->addOrderBy('bp.block_row', 'ASC');
113
114
        $ownResult = $qb
115 5
            ->getQuery()
116 5
            ->setParameters([
117 5
                'DeviceType' => $DeviceType,
118 5
                'pageId' => $pageId,
119
            ])
120 5
            ->getSingleResult();
121
122 5
        $qb = $this->createQueryBuilder('p')
123 5
            ->select('p, bp, b')
124 5
            ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id')
125 5
            ->leftJoin('bp.Block', 'b')
126 5
            ->andWhere('p.DeviceType = :DeviceType AND bp.anywhere = 1')
127 5
            ->addOrderBy('bp.section', 'ASC')
128 5
            ->addOrderBy('bp.block_row', 'ASC');
129
130
        $anyResults = $qb
131 5
            ->getQuery()
132 5
            ->setParameters([
133 5
                'DeviceType' => $DeviceType,
134
            ])
135 5
            ->getResult();
136
137 5
        $OwnBlockPosition = $ownResult->getBlockPositions();
138 5
        foreach ($anyResults as $anyResult) {
139 5
            $BlockPositions = $anyResult->getBlockPositions();
140 5
            foreach ($BlockPositions as $BlockPosition) {
141 5
                if (!$OwnBlockPosition->contains($BlockPosition)) {
142 5
                    $ownResult->addBlockPosition($BlockPosition);
143
                }
144
            }
145
        }
146
147 5
        return $ownResult;
148
    }
149
150
    /**
151
     * @param DeviceType $DeviceType
152
     * @param string $url
153
     * @throw NoResultException
154
     *
155
     * @return Page
156
     */
157 159
    public function getByUrl(DeviceType $DeviceType, $url)
158
    {
159 159
        $qb = $this->createQueryBuilder('p');
160 159
        $Page = $qb->select('p, pll,l, bp, b')
161 159
            ->leftJoin('p.PageLayouts', 'pll')
162 159
            ->leftJoin('pll.Layout', 'l')
163 159
            ->leftJoin('l.BlockPositions', 'bp')
164 159
            ->leftJoin('bp.Block', 'b')
165 159
            ->where('p.url = :route')
166 159
            ->andWhere('l.DeviceType = :DeviceType')
167 159
            ->orderBy('bp.block_row', 'ASC')
168 159
            ->setParameter('route', $url)
169 159
            ->setParameter('DeviceType', $DeviceType)
170 159
            ->getQuery()
171 159
            ->useResultCache(true, $this->getCacheLifetime())
172 159
            ->getSingleResult();
173
174 144
        return $Page;
175
    }
176
177
    /**
178
     * @param DeviceType $DeviceType
179
     *
180
     * @return Page
181
     */
182 125
    public function newPage(DeviceType $DeviceType)
183
    {
184 125
        $Page = new \Eccube\Entity\Page();
185
        $Page
186 125
            ->setDeviceType($DeviceType)
187 125
            ->setEditType(Page::EDIT_TYPE_USER);
188
189 125
        return $Page;
190
    }
191
192
    /**
193
     * ページの属性を取得する.
194
     *
195
     * この関数は, dtb_Page の情報を検索する.
196
     * $deviceTypeId は必須. デフォルト値は DEVICE_TYPE_PC.
197
     *
198 5
     * @param  \Eccube\Entity\Master\DeviceType  $DeviceType 端末種別ID
199
     * @param  string                            $where 追加の検索条件
200 5
     * @param  string[]                          $parameters 追加の検索パラメーター
201
     *
202 2
     * @return array                             ページ属性の配列
203
     */
204 2
    public function getPageList(DeviceType $DeviceType, $where = null, $parameters = [])
205
    {
206 3
        $qb = $this->createQueryBuilder('l')
207
            ->orderBy('l.id', 'DESC')
208
            ->where('l.DeviceType = :DeviceType')
209
            ->setParameter('DeviceType', $DeviceType)
210
            ->andWhere('l.id <> 0')
211
            ->andWhere('l.MasterPage is null')
212
            ->orderBy('l.id', 'ASC');
213
        if (!is_null($where)) {
214
            $qb->andWhere($where);
215
            foreach ($parameters as $key => $val) {
216
                $qb->setParameter($key, $val);
217
            }
218
        }
219
220
        $Pages = $qb
221
            ->getQuery()
222 2
            ->getResult();
223
224 2
        return $Pages;
225 2
    }
226
}
227