Completed
Push — master ( a84749...c8121e )
by Antony
02:41
created

DealerLoop::getAdminDealer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
/*************************************************************************************/
13
14
namespace Dealer\Loop;
15
16
use Dealer\Model\Dealer;
17
use Dealer\Model\DealerQuery;
18
use Dealer\Model\Map\DealerBrandTableMap;
19
use Dealer\Model\Map\DealerContentTableMap;
20
use Dealer\Model\Map\DealerFolderTableMap;
21
use Dealer\Model\Map\DealerProductTableMap;
22
use Dealer\Model\Map\DealerTableMap;
23
use Propel\Runtime\ActiveQuery\Criteria;
24
use Propel\Runtime\ActiveQuery\Join;
25
use Thelia\Core\Template\Element\BaseI18nLoop;
26
use Thelia\Core\Template\Element\LoopResult;
27
use Thelia\Core\Template\Element\LoopResultRow;
28
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
29
use Thelia\Core\Template\Loop\Argument\Argument;
30
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
31
32
/**
33
 * Class DealerLoop
34
 * @package Dealer\Loop
35
 */
36
class DealerLoop extends BaseI18nLoop implements PropelSearchLoopInterface
37
{
38
39
    /**
40
     * @param LoopResult $loopResult
41
     *
42
     * @return LoopResult
43
     */
44
    public function parseResults(LoopResult $loopResult)
45
    {
46
        /** @var Dealer $dealer */
47
        foreach ($loopResult->getResultDataCollection() as $dealer) {
48
            $loopResultRow = new LoopResultRow($dealer);
49
50
            $loopResultRow
51
                ->set('ID', $dealer->getId())
52
                ->set("ADDRESS1", $dealer->getAddress1())
53
                ->set("ADDRESS2", $dealer->getAddress2())
54
                ->set("ADDRESS3", $dealer->getAddress3())
55
                ->set("ZIPCODE", $dealer->getZipcode())
56
                ->set("CITY", $dealer->getCity())
57
                ->set("COUNTRY_ID", $dealer->getCountryId())
58
                ->set("LAT", $dealer->getLatitude())
59
                ->set("LON", $dealer->getLongitude())
60
                ->set("CREATE_DATE", $dealer->getCreatedAt())
61
                ->set("UPDATE_DATE", $dealer->getUpdatedAt())
62
                ->set("VISIBLE", $dealer->getVisible())
63
            ;
64
65
            if ($dealer->hasVirtualColumn('i18n_TITLE')) {
66
                $loopResultRow->set("TITLE", $dealer->getVirtualColumn('i18n_TITLE'));
67
            }
68
69
            if ($dealer->hasVirtualColumn('i18n_ACCESS')) {
70
                $loopResultRow->set("ACCESS", $dealer->getVirtualColumn('i18n_ACCESS'));
71
            }
72
73
            if ($dealer->hasVirtualColumn('i18n_DESCRIPTION')) {
74
                $loopResultRow->set("DESCRIPTION", $dealer->getVirtualColumn('i18n_DESCRIPTION'));
75
            }
76
77
            if ($this->getWithPrevNextInfo()) {
78
79
                $previous = $this->getPrevious($dealer);
80
                $next = $this->getNext($dealer);
81
                $loopResultRow
82
                    ->set("HAS_PREVIOUS", $previous != null ? 1 : 0)
83
                    ->set("HAS_NEXT", $next != null ? 1 : 0)
84
                    ->set("PREVIOUS", $previous != null ? $previous->getId() : -1)
85
                    ->set("NEXT", $next != null ? $next->getId() : -1);
86
            }
87
88
            $loopResult->addRow($loopResultRow);
89
        }
90
91
        return $loopResult;
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    protected function getArgDefinitions()
98
    {
99
        return new ArgumentCollection(
100
            Argument::createIntListTypeArgument('id'),
101
            Argument::createIntListTypeArgument('country_id'),
102
            Argument::createIntListTypeArgument('content_id'),
103
            Argument::createIntListTypeArgument('folder_id'),
104
            Argument::createIntListTypeArgument('brand_id'),
105
            Argument::createIntListTypeArgument('product_id'),
106
            Argument::createBooleanTypeArgument('visible'),
107
            Argument::createAnyListTypeArgument('city'),
108
            Argument::createBooleanTypeArgument('with_prev_next_info', false),
109
            Argument::createEnumListTypeArgument('order', [
110
                'id',
111
                'id-reverse',
112
                'date',
113
                'date-reverse'
114
            ], 'id')
115
        );
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121
    public function buildModelCriteria()
122
    {
123
        $query = DealerQuery::create();
124
125
        // manage translations
126
        $this->configureI18nProcessing(
127
            $query,
128
            [
129
                'TITLE',
130
                'DESCRIPTION',
131
                'ACCESS'
132
            ],
133
            null,
134
            'ID',
135
            $this->getForceReturn()
136
        );
137
138
        if (null != $id = $this->getId()) {
139
            $query->filterById($id);
140
        }
141
142
        if (null != $country_id = $this->getCountryId()) {
143
            $query->filterByCountryId($country_id);
144
        }
145
146
        if (null != $city = $this->getCity()) {
147
            $query->filterByCity($city);
148
        }
149
150
        if (null != $visible = $this->getVisible()) {
151
            $query->filterByVisible($visible);
152
        }
153
154
        if($content = $this->getContentId()){
155
            if(is_array($content)){
156
                $content = implode(",", $content);
157
            }
158
            $contentJoin = new Join(DealerTableMap::ID,DealerContentTableMap::DEALER_ID,Criteria::LEFT_JOIN);
159
            $query
160
                ->addJoinObject($contentJoin)
161
                ->where(DealerContentTableMap::CONTENT_ID." ".Criteria::IN." (".$content.")");
162
            ;
163
164
        }
165
166
        if($folder = $this->getFolderId()){
167
            if(is_array($folder)){
168
                $folder = implode(",", $folder);
169
            }
170
            $contentJoin = new Join(DealerTableMap::ID,DealerFolderTableMap::DEALER_ID,Criteria::LEFT_JOIN);
171
            $query
172
                ->addJoinObject($contentJoin)
173
                ->where(DealerFolderTableMap::FOLDER_ID." ".Criteria::IN." (".$folder.")");
174
            ;
175
176
        }
177
178
        if($brand = $this->getBrandId()){
179
            if(is_array($brand)){
180
                $brand = implode(",", $brand);
181
            }
182
            $contentJoin = new Join(DealerTableMap::ID,DealerBrandTableMap::DEALER_ID,Criteria::LEFT_JOIN);
183
            $query
184
                ->addJoinObject($contentJoin)
185
                ->where(DealerBrandTableMap::BRAND_ID." ".Criteria::IN." (".$brand.")");
186
            ;
187
188
        }
189
190
        if($product = $this->getProductId()){
191
            if(is_array($product)){
192
                $product = implode(",", $product);
193
            }
194
            $contentJoin = new Join(DealerTableMap::ID,DealerProductTableMap::DEALER_ID,Criteria::LEFT_JOIN);
195
            $query
196
                ->addJoinObject($contentJoin)
197
                ->where(DealerProductTableMap::PRODUCT_ID." ".Criteria::IN." (".$product.")");
198
            ;
199
200
        }
201
        
202
        $query =$this->getAdminDealer($query);
203
204
        foreach ($this->getOrder() as $order) {
205
            switch ($order) {
206
                case 'id' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
207
                    $query->orderById();
208
                    break;
209
                case 'id-reverse' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
210
                    $query->orderById(Criteria::DESC);
211
                    break;
212
                case 'date' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
213
                    $query->orderByCreatedAt();
214
                    break;
215
                case 'date-reverse' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
216
                    $query->orderByCreatedAt(Criteria::DESC);
217
                    break;
218
                default:
219
                    break;
220
            }
221
        }
222
223
        return $query;
224
    }
225
226
    /**
227
     * @param Dealer $dealer
228
     * @return Dealer
229
     */
230
    protected function getPrevious($dealer)
231
    {
232
        $query = DealerQuery::create();
233
234
        foreach ($this->getOrder() as $order) {
235
            switch ($order) {
236
                case 'id' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
237
                    $query->orderById(Criteria::DESC);
238
                    $query->filterById($dealer->getId(),Criteria::LESS_THAN);
239
                    break;
240
                case 'id-reverse' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
241
                    $query->orderById();
242
                    $query->filterById($dealer->getId(),Criteria::GREATER_THAN);
243
                    break;
244
                default:
245
                    $query->orderById(Criteria::DESC);
246
                    $query->filterById($dealer->getId(),Criteria::LESS_THAN);
247
                    break;
248
            }
249
        }
250
251
        return $query->findOne();
252
    }
253
254
    /**
255
     * @param Dealer $dealer
256
     * @return Dealer
257
     */
258
    protected function getNext($dealer)
259
    {
260
        $query = DealerQuery::create();
261
262
        foreach ($this->getOrder() as $order) {
263
            switch ($order) {
264
                case 'id' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
265
                    $query->orderById();
266
                    $query->filterById($dealer->getId(),Criteria::GREATER_THAN);
267
                    break;
268
                case 'id-reverse' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
269
                    $query->orderById(Criteria::DESC);
270
                    $query->filterById($dealer->getId(),Criteria::LESS_THAN);
271
                    break;
272
                default:
273
                    $query->orderById();
274
                    $query->filterById($dealer->getId(),Criteria::GREATER_THAN);
275
                    break;
276
            }
277
        }
278
279
        return $query->findOne();
280
    }
281
282
    /**
283
     * @param DealerQuery $query
284
     * @return DealerQuery
285
     */
286
    protected function getAdminDealer($query)
287
    {
288
        $admin = $this->securityContext->getAdminUser();
289
290
        if ($admin === null) {
291
            return $query;
292
        }
293
294
        if ($admin->getProfileId() === null) {
295
            return $query;
296
        }
297
298
        return $query->useDealerAdminQuery()->filterByAdminId($admin->getId())->endUse();
299
    }
300
}