Failed Conditions
Pull Request — master (#1543)
by Tsuyoshi
489:47 queued 481:38
created

PageLayoutRepository::newPageLayout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 9
ccs 0
cts 0
cp 0
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
crap 2
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 5
    public function setApplication($app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
45
    {
46 5
        $this->app = $app;
47 5
    }
48
49
    public function findUnusedBlocks(DeviceType $DeviceType, $pageId)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
50
    {
51
        $em = $this
52
            ->getEntityManager();
53
        $blockRepo = $em->getRepository('Eccube\Entity\Block');
54
        $ownBlockPositions = $this->get($DeviceType, $pageId)->getBlockPositions();
55
        $ids = array();
56
        foreach ($ownBlockPositions as $ownBlockPosition) {
57
            $ids[] = $ownBlockPosition->getBlock()->getId();
58
        }
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
        if (empty($ids)) {
62
            $ids[] = \Eccube\Entity\Block::UNUSED_BLOCK_ID;
63
        }
64
65
        return $blockRepo->createQueryBuilder('b')
66
            ->where('b.id not in (:ids)')
67
            ->setParameter(':ids', $ids)
68
            ->getQuery()
69
            ->getResult();
70
    }
71
72 2 View Code Duplication
    public function get(DeviceType $DeviceType, $pageId)
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...
introduced by
Missing function doc comment
Loading history...
73
    {
74 2
        $qb = $this->createQueryBuilder('p')
75 2
            ->select('p, bp, b')
76 2
            ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id')
77 2
            ->leftJoin('bp.Block', 'b')
78 2
            ->andWhere('p.DeviceType = :DeviceType AND p.id = :pageId')
79 2
            ->addOrderBy('bp.target_id', 'ASC')
80
            ->addOrderBy('bp.block_row', 'ASC');
81
82
        $ownResult = $qb
83 2
            ->getQuery()
84
            ->setParameters(array(
85
                'DeviceType'  => $DeviceType,
86
                'pageId'        => $pageId,
87 2
            ))
88
            ->getSingleResult();
89
90 2
        $qb = $this->createQueryBuilder('p')
91 2
            ->select('p, bp, b')
92 2
            ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id')
93 2
            ->leftJoin('bp.Block', 'b')
94 2
            ->andWhere('p.DeviceType = :DeviceType AND bp.anywhere = 1')
95 2
            ->addOrderBy('bp.target_id', 'ASC')
96
            ->addOrderBy('bp.block_row', 'ASC');
97
98
        $anyResults = $qb
99 2
            ->getQuery()
100
            ->setParameters(array(
101
                'DeviceType' => $DeviceType,
102 2
            ))
103
            ->getResult();
104
105
        $OwnBlockPosition = $ownResult->getBlockPositions();
106
        foreach ($anyResults as $anyResult) {
107
            $BlockPositions = $anyResult->getBlockPositions();
108
            foreach ($BlockPositions as $BlockPosition) {
109
                if (!$OwnBlockPosition->contains($BlockPosition)) {
110
                    $ownResult->addBlockPosition($BlockPosition);
111
                }
112
            }
113
        }
114
115 2
        return $ownResult;
116
117 2
    }
118
119 View Code Duplication
    public function getByUrl(DeviceType $DeviceType, $url)
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...
introduced by
Missing function doc comment
Loading history...
120
    {
121
        $qb = $this->createQueryBuilder('p')
122
            ->select('p, bp, b')
123
            ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id')
124
            ->leftJoin('bp.Block', 'b')
125
            ->andWhere('p.DeviceType = :DeviceType AND p.url = :url')
126
            ->addOrderBy('bp.target_id', 'ASC')
127
            ->addOrderBy('bp.block_row', 'ASC');
128
129
        $ownResult = $qb
130
            ->getQuery()
131
            ->setParameters(array(
132
                'DeviceType' => $DeviceType,
133
                'url'  => $url,
134
            ))
135
            ->getSingleResult();
136
137
        $qb = $this->createQueryBuilder('p')
138
            ->select('p, bp, b')
139
            ->leftJoin('p.BlockPositions', 'bp', 'WITH', 'p.id = bp.page_id')
140
            ->leftJoin('bp.Block', 'b')
141
            ->andWhere('p.DeviceType = :DeviceType AND bp.anywhere = 1')
142
            ->addOrderBy('bp.target_id', 'ASC')
143
            ->addOrderBy('bp.block_row', 'ASC');
144
145
        $anyResults = $qb
146
            ->getQuery()
147
            ->setParameters(array(
148
                'DeviceType' => $DeviceType,
149
            ))
150
            ->getResult();
151
152
        $OwnBlockPosition = $ownResult->getBlockPositions();
153
        foreach ($anyResults as $anyResult) {
154
            $BlockPositions = $anyResult->getBlockPositions();
155
            foreach ($BlockPositions as $BlockPosition) {
156
                if (!$OwnBlockPosition->contains($BlockPosition)) {
157
                    $ownResult->addBlockPosition($BlockPosition);
158
                }
159
            }
160
        }
161
162
        return $ownResult;
163
    }
164
165
    public function newPageLayout(DeviceType $DeviceType)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
166
    {
167
        $PageLayout = new \Eccube\Entity\PageLayout();
168
        $PageLayout
169
            ->setDeviceType($DeviceType)
170
            ->setEditFlg(PageLayout::EDIT_FLG_USER);
171
172
        return $PageLayout;
173
    }
174
175 1
    public function findOrCreate($page_id, DeviceType $DeviceType)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
176
    {
177
        if (is_null($page_id)) {
178
            $PageLayout = $this
179
                ->newPageLayout($DeviceType);
180
            return $PageLayout;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
181
        } else {
182
            return $this->get($DeviceType, $page_id);
183
        }
184 1
    }
185
186
    /**
187
     * ページの属性を取得する.
188
     *
189
     * この関数は, dtb_pagelayout の情報を検索する.
190
     * $deviceTypeId は必須. デフォルト値は DEVICE_TYPE_PC.
191
     *
192
     * @access public
193
     * @param  \Eccube\Entity\Master\DeviceType  $DeviceType 端末種別ID
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
194
     * @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...
195
     * @param  string[]                          $parameters 追加の検索パラメーター
0 ignored issues
show
introduced by
Expected 25 spaces after parameter type; 26 found
Loading history...
196
     * @return array                             ページ属性の配列
197
     */
198 1 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...
199
    {
200 1
        $qb = $this->createQueryBuilder('l')
201 1
            ->orderBy('l.id', 'DESC')
202 1
            ->where('l.DeviceType = :DeviceType')
203 1
            ->setParameter('DeviceType', $DeviceType)
204 1
            ->andWhere('l.id <> 0')
205
            ->orderBy('l.id', 'ASC');
206
        if (!is_null($where)) {
207
            $qb->andWhere($where);
208
            foreach ($parameters as $key => $val) {
209
                $qb->setParameter($key, $val);
210
            }
211
        }
212
213
        $PageLayouts = $qb
214 1
            ->getQuery()
215
            ->getResult();
216
217 1
        return $PageLayouts;
218
    }
219
220
    /**
221
     * 書き込みパスの取得
222
     * User定義の場合: /html/user_data
223
     * そうでない場合: /app/template/{template_code}
224
     *
225
     * @param  boolean $isUser
226
     * @return string
227
     */
228
    public function getWriteTemplatePath($isUser = false)
229
    {
230
        return ($isUser) ? $this->app['config']['user_data_realdir'] : $this->app['config']['template_realdir'];
231
    }
232
233
    /**
234
     * 読み込みファイルの取得
235
     *
236
     * 1. template_realdir
237
     *      app/template/{template_code}
238
     * 2. template_default_readldir
239
     *      src/Eccube/Resource/template/default
240
     *
241
     * @param string $fileName
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
242
     * @param  boolean $isUser
243
     *
244
     * @return array
245
     */
246 1
    public function getReadTemplateFile($fileName, $isUser = false)
247
    {
248 1
        if ($isUser) {
249
            $readPaths = array(
250
                $this->app['config']['user_data_realdir'],
251
            );
252
        } else {
253
            $readPaths = array(
254 1
                $this->app['config']['template_realdir'],
255 1
                $this->app['config']['template_default_realdir'],
256 1
            );
257
        }
258
259 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...
260
            $filePath = $readPath . '/' . $fileName . '.twig';
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
261
            $fs = new Filesystem();
262
            if ($fs->exists($filePath)) {
263
                return array(
264
                    'file_name' => $fileName,
265 1
                    'tpl_data' => file_get_contents($filePath),
266
                );
267
            }
268
        }
269 1
    }
270
}
271