Completed
Push — master ( f7b119...9e4c7b )
by Yangsin
353:17 queued 347:09
created

PageLayoutRepository::getByUrl()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 50
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 40
cts 40
cp 1
rs 8.8571
c 0
b 0
f 0
cc 4
eloc 37
nc 4
nop 2
crap 4
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Repository;
26
27
use Doctrine\ORM\EntityRepository;
28
use Doctrine\ORM\Query\Expr;
29
use Eccube\Entity\Master\DeviceType;
30
use Eccube\Entity\PageLayout;
31
use Symfony\Component\Filesystem\Filesystem;
32
use Symfony\Component\Finder\Finder;
33
34
/**
35
 * PageLayoutRepository
36
 *
37
 * This class was generated by the Doctrine ORM. Add your own custom
38
 * repository methods below.
39
 */
40
class PageLayoutRepository extends EntityRepository
41
{
42
    protected $app;
43
44 192
    public function setApplication($app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
45
    {
46 192
        $this->app = $app;
47 192
    }
48
49 7
    public function findUnusedBlocks(DeviceType $DeviceType, $pageId)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
50
    {
51 7
        $em = $this
52 7
            ->getEntityManager();
53 7
        $blockRepo = $em->getRepository('Eccube\Entity\Block');
54 7
        $ownBlockPositions = $this->get($DeviceType, $pageId)->getBlockPositions();
55 7
        $ids = array();
56 7
        foreach ($ownBlockPositions as $ownBlockPosition) {
57 7
            $ids[] = $ownBlockPosition->getBlock()->getId();
58 7
        }
59
60
        # $idsが空配列だと、$ids以外のblockを取得するSQLが生成されないため、存在しないidを入れる
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
61 7
        if (empty($ids)) {
62
            $ids[] = \Eccube\Entity\Block::UNUSED_BLOCK_ID;
63
        }
64
65 7
        return $blockRepo->createQueryBuilder('b')
66 7
            ->where('b.id not in (:ids)')
67 7
            ->setParameter(':ids', $ids)
68 7
            ->getQuery()
69 7
            ->getResult();
70
    }
71
72 11
    public function get(DeviceType $DeviceType, $pageId)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
73
    {
74 11
        $qb = $this->createQueryBuilder('p')
75 11
            ->select('p, bp, b')
76 11
            ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id')
77 11
            ->leftJoin('bp.Block', 'b')
78 11
            ->andWhere('p.DeviceType = :DeviceType AND p.id = :pageId')
79 11
            ->addOrderBy('bp.target_id', 'ASC')
80 11
            ->addOrderBy('bp.block_row', 'ASC');
81
82
        $ownResult = $qb
83 11
            ->getQuery()
84 11
            ->setParameters(array(
85 11
                'DeviceType'  => $DeviceType,
86 11
                'pageId'        => $pageId,
87 11
            ))
88 11
            ->getSingleResult();
89
90 11
        $qb = $this->createQueryBuilder('p')
91 11
            ->select('p, bp, b')
92 11
            ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id')
93 11
            ->leftJoin('bp.Block', 'b')
94 11
            ->andWhere('p.DeviceType = :DeviceType AND bp.anywhere = 1')
95 11
            ->addOrderBy('bp.target_id', 'ASC')
96 11
            ->addOrderBy('bp.block_row', 'ASC');
97
98
        $anyResults = $qb
99 11
            ->getQuery()
100 11
            ->setParameters(array(
101 11
                'DeviceType' => $DeviceType,
102 11
            ))
103 11
            ->getResult();
104
105 11
        $OwnBlockPosition = $ownResult->getBlockPositions();
106 11
        foreach ($anyResults as $anyResult) {
107 11
            $BlockPositions = $anyResult->getBlockPositions();
108 11
            foreach ($BlockPositions as $BlockPosition) {
109 11
                if (!$OwnBlockPosition->contains($BlockPosition)) {
110 2
                    $ownResult->addBlockPosition($BlockPosition);
111 2
                }
112 11
            }
113 11
        }
114
115 11
        return $ownResult;
116
117 5
    }
118
119 168
    public function getByUrl(DeviceType $DeviceType, $url)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
120
    {
121 168
        $options = $this->app['config']['doctrine_cache'];
122 168
        $lifetime = $options['result_cache']['lifetime'];
123
124 168
        $qb = $this->createQueryBuilder('p')
125 168
            ->select('p, bp, b')
126 168
            ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id')
127 168
            ->leftJoin('bp.Block', 'b')
128 168
            ->andWhere('p.DeviceType = :DeviceType AND p.url = :url')
129 168
            ->addOrderBy('bp.target_id', 'ASC')
130 168
            ->addOrderBy('bp.block_row', 'ASC');
131
132
        $ownResult = $qb
133 168
            ->getQuery()
134 168
            ->useResultCache(true, $lifetime)
135 168
            ->setParameters(array(
136 168
                'DeviceType' => $DeviceType,
137 168
                'url'  => $url,
138 168
            ))
139 168
            ->getSingleResult();
140
141 103
        $qb = $this->createQueryBuilder('p')
142 103
            ->select('p, bp, b')
143 103
            ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id')
144 103
            ->leftJoin('bp.Block', 'b')
145 103
            ->andWhere('p.DeviceType = :DeviceType AND bp.anywhere = 1')
146 103
            ->addOrderBy('bp.target_id', 'ASC')
147 103
            ->addOrderBy('bp.block_row', 'ASC');
148
149
        $anyResults = $qb
150 103
            ->getQuery()
151 103
            ->useResultCache(true, $lifetime)
152 103
            ->setParameters(array(
153 103
                'DeviceType' => $DeviceType,
154 103
            ))
155 103
            ->getResult();
156
157 103
        $OwnBlockPosition = $ownResult->getBlockPositions();
158 103
        foreach ($anyResults as $anyResult) {
159 103
            $BlockPositions = $anyResult->getBlockPositions();
160 103
            foreach ($BlockPositions as $BlockPosition) {
161 103
                if (!$OwnBlockPosition->contains($BlockPosition)) {
162 91
                    $ownResult->addBlockPosition($BlockPosition);
163 91
                }
164 103
            }
165 103
        }
166
167 103
        return $ownResult;
168
    }
169
170 67
    public function newPageLayout(DeviceType $DeviceType)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
171
    {
172 67
        $PageLayout = new \Eccube\Entity\PageLayout();
173
        $PageLayout
174 67
            ->setDeviceType($DeviceType)
175 67
            ->setEditFlg(PageLayout::EDIT_FLG_USER);
176
177 67
        return $PageLayout;
178
    }
179
180 5
    public function findOrCreate($page_id, DeviceType $DeviceType)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
181
    {
182 5
        if (is_null($page_id)) {
183 2
            $PageLayout = $this
184 2
                ->newPageLayout($DeviceType);
185 2
            return $PageLayout;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
186
        } else {
187 3
            return $this->get($DeviceType, $page_id);
188
        }
189
    }
190
191
    /**
192
     * ページの属性を取得する.
193
     *
194
     * この関数は, dtb_pagelayout の情報を検索する.
195
     * $deviceTypeId は必須. デフォルト値は DEVICE_TYPE_PC.
196
     *
197
     * @access public
198
     * @param  \Eccube\Entity\Master\DeviceType  $DeviceType 端末種別ID
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
199
     * @param  string                            $where 追加の検索条件
0 ignored issues
show
introduced by
Expected 27 spaces after parameter type; 28 found
Loading history...
introduced by
Expected 6 spaces after parameter name; 1 found
Loading history...
200
     * @param  string[]                          $parameters 追加の検索パラメーター
0 ignored issues
show
introduced by
Expected 25 spaces after parameter type; 26 found
Loading history...
201
     * @return array                             ページ属性の配列
202
     */
203 3 View Code Duplication
    public function getPageList(DeviceType $DeviceType, $where = null, $parameters = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
    {
205 3
        $qb = $this->createQueryBuilder('l')
206 3
            ->orderBy('l.id', 'DESC')
207 3
            ->where('l.DeviceType = :DeviceType')
208 3
            ->setParameter('DeviceType', $DeviceType)
209 3
            ->andWhere('l.id <> 0')
210 3
            ->orderBy('l.id', 'ASC');
211 3
        if (!is_null($where)) {
212
            $qb->andWhere($where);
213
            foreach ($parameters as $key => $val) {
214
                $qb->setParameter($key, $val);
215
            }
216
        }
217
218
        $PageLayouts = $qb
219 3
            ->getQuery()
220 3
            ->getResult();
221
222 3
        return $PageLayouts;
223
    }
224
225
    /**
226
     * 書き込みパスの取得
227
     * User定義の場合: /html/user_data
228
     * そうでない場合: /app/template/{template_code}
229
     *
230
     * @param  boolean $isUser
231
     * @return string
232
     */
233 7
    public function getWriteTemplatePath($isUser = false)
234
    {
235 7
        return ($isUser) ? $this->app['config']['user_data_realdir'] : $this->app['config']['template_realdir'];
236
    }
237
238
    /**
239
     * 読み込みファイルの取得
240
     *
241
     * 1. template_realdir
242
     *      app/template/{template_code}
243
     * 2. template_default_readldir
244
     *      src/Eccube/Resource/template/default
245
     *
246
     * @param string $fileName
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
247
     * @param  boolean $isUser
248
     *
249
     * @return array
250
     */
251 5
    public function getReadTemplateFile($fileName, $isUser = false)
252
    {
253 5
        if ($isUser) {
254
            $readPaths = array(
255 1
                $this->app['config']['user_data_realdir'],
256 1
            );
257 1
        } else {
258
            $readPaths = array(
259 4
                $this->app['config']['template_realdir'],
260 4
                $this->app['config']['template_default_realdir'],
261 4
            );
262
        }
263
264 5 View Code Duplication
        foreach ($readPaths as $readPath) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
265 5
            $filePath = $readPath . '/' . $fileName . '.twig';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
266 5
            $fs = new Filesystem();
267 5
            if ($fs->exists($filePath)) {
268
                return array(
269 5
                    'file_name' => $fileName,
270 5
                    'tpl_data' => file_get_contents($filePath),
271 5
                );
272
            }
273 3
        }
274
    }
275
}
276