Completed
Pull Request — master (#17)
by zzuutt
02:42
created

DealerLoop::getArgDefinitions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 8
Metric Value
dl 0
loc 22
rs 9.2
c 8
b 0
f 8
cc 1
eloc 19
nc 1
nop 0
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\Security\AccessManager;
26
use Thelia\Core\Security\Resource\AdminResources;
27
use Thelia\Core\Template\Element\BaseI18nLoop;
28
use Thelia\Core\Template\Element\LoopResult;
29
use Thelia\Core\Template\Element\LoopResultRow;
30
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
31
use Thelia\Core\Template\Loop\Argument\Argument;
32
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
33
34
/**
35
 * Class DealerLoop
36
 * @package Dealer\Loop
37
 */
38
class DealerLoop extends BaseI18nLoop implements PropelSearchLoopInterface
39
{
40
41
    /**
42
     * @param LoopResult $loopResult
43
     *
44
     * @return LoopResult
45
     */
46
    public function parseResults(LoopResult $loopResult)
47
    {
48
        /** @var Dealer $dealer */
49
        foreach ($loopResult->getResultDataCollection() as $dealer) {
50
            $loopResultRow = new LoopResultRow($dealer);
51
52
            $loopResultRow
53
                ->set('ID', $dealer->getId())
54
                ->set("ADDRESS1", $dealer->getAddress1())
55
                ->set("ADDRESS2", $dealer->getAddress2())
56
                ->set("ADDRESS3", $dealer->getAddress3())
57
                ->set("ZIPCODE", $dealer->getZipcode())
58
                ->set("CITY", $dealer->getCity())
59
                ->set("COUNTRY_ID", $dealer->getCountryId())
60
                ->set("LAT", $dealer->getLatitude())
61
                ->set("LON", $dealer->getLongitude())
62
                ->set("CREATE_DATE", $dealer->getCreatedAt())
63
                ->set("UPDATE_DATE", $dealer->getUpdatedAt())
64
                ->set("VISIBLE", $dealer->getVisible())
65
            ;
66
67
            if ($dealer->hasVirtualColumn('i18n_TITLE')) {
68
                $loopResultRow->set("TITLE", $dealer->getVirtualColumn('i18n_TITLE'));
69
            }
70
71
            if ($dealer->hasVirtualColumn('i18n_ACCESS')) {
72
                $loopResultRow->set("ACCESS", $dealer->getVirtualColumn('i18n_ACCESS'));
73
            }
74
75
            if ($dealer->hasVirtualColumn('i18n_DESCRIPTION')) {
76
                $loopResultRow->set("DESCRIPTION", $dealer->getVirtualColumn('i18n_DESCRIPTION'));
77
            }
78
79
            if ($this->getWithPrevNextInfo()) {
80
81
                $previous = $this->getPrevious($dealer);
82
                $next = $this->getNext($dealer);
83
                $loopResultRow
84
                    ->set("HAS_PREVIOUS", $previous != null ? 1 : 0)
85
                    ->set("HAS_NEXT", $next != null ? 1 : 0)
86
                    ->set("PREVIOUS", $previous != null ? $previous->getId() : -1)
87
                    ->set("NEXT", $next != null ? $next->getId() : -1);
88
            }
89
90
            $loopResult->addRow($loopResultRow);
91
        }
92
93
        return $loopResult;
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    protected function getArgDefinitions()
100
    {
101
        return new ArgumentCollection(
102
            Argument::createIntListTypeArgument('id'),
103
            Argument::createIntListTypeArgument('country_id'),
104
            Argument::createIntListTypeArgument('content_id'),
105
            Argument::createIntListTypeArgument('folder_id'),
106
            Argument::createIntListTypeArgument('brand_id'),
107
            Argument::createIntListTypeArgument('product_id'),
108
            Argument::createBooleanTypeArgument('visible'),
109
            Argument::createAnyListTypeArgument('city'),
110
            Argument::createBooleanTypeArgument('with_prev_next_info', false),
111
            Argument::createEnumListTypeArgument('order', [
112
                'id',
113
                'id-reverse',
114
                'date',
115
                'date-reverse',
116
-                'title',
117
-                'title-reverse'
118
            ], 'id')
119
        );
120
    }
121
122
    /**
123
     * @inheritdoc
124
     */
125
    public function buildModelCriteria()
126
    {
127
        $query = DealerQuery::create();
128
129
        // manage translations
130
        $this->configureI18nProcessing(
131
            $query,
132
            [
133
                'TITLE',
134
                'DESCRIPTION',
135
                'ACCESS'
136
            ],
137
            null,
138
            'ID',
139
            $this->getForceReturn()
140
        );
141
142
        if (null != $id = $this->getId()) {
143
            $query->filterById($id);
144
        }
145
146
        if (null != $country_id = $this->getCountryId()) {
147
            $query->filterByCountryId($country_id);
148
        }
149
150
        if (null != $city = $this->getCity()) {
151
            $query->filterByCity($city);
152
        }
153
154
        if (null != $visible = $this->getVisible()) {
155
            $query->filterByVisible($visible);
156
        }
157
158
        if($content = $this->getContentId()){
159
            if(is_array($content)){
160
                $content = implode(",", $content);
161
            }
162
            $contentJoin = new Join(DealerTableMap::ID,DealerContentTableMap::DEALER_ID,Criteria::LEFT_JOIN);
163
            $query
164
                ->addJoinObject($contentJoin)
165
                ->where(DealerContentTableMap::CONTENT_ID." ".Criteria::IN." (".$content.")");
166
            ;
167
168
        }
169
170
        if($folder = $this->getFolderId()){
171
            if(is_array($folder)){
172
                $folder = implode(",", $folder);
173
            }
174
            $contentJoin = new Join(DealerTableMap::ID,DealerFolderTableMap::DEALER_ID,Criteria::LEFT_JOIN);
175
            $query
176
                ->addJoinObject($contentJoin)
177
                ->where(DealerFolderTableMap::FOLDER_ID." ".Criteria::IN." (".$folder.")");
178
            ;
179
180
        }
181
182
        if($brand = $this->getBrandId()){
183
            if(is_array($brand)){
184
                $brand = implode(",", $brand);
185
            }
186
            $contentJoin = new Join(DealerTableMap::ID,DealerBrandTableMap::DEALER_ID,Criteria::LEFT_JOIN);
187
            $query
188
                ->addJoinObject($contentJoin)
189
                ->where(DealerBrandTableMap::BRAND_ID." ".Criteria::IN." (".$brand.")");
190
            ;
191
192
        }
193
194
        if($product = $this->getProductId()){
195
            if(is_array($product)){
196
                $product = implode(",", $product);
197
            }
198
            $contentJoin = new Join(DealerTableMap::ID,DealerProductTableMap::DEALER_ID,Criteria::LEFT_JOIN);
199
            $query
200
                ->addJoinObject($contentJoin)
201
                ->where(DealerProductTableMap::PRODUCT_ID." ".Criteria::IN." (".$product.")");
202
            ;
203
204
        }
205
        
206
        $query =$this->getAdminDealer($query);
207
208
        foreach ($this->getOrder() as $order) {
209
            switch ($order) {
210
                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...
211
                    $query->orderById();
212
                    break;
213
                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...
214
                    $query->orderById(Criteria::DESC);
215
                    break;
216
                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...
217
                    $query->orderByCreatedAt();
218
                    break;
219
                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...
220
                    $query->orderByCreatedAt(Criteria::DESC);
221
                    break;
222
                case 'title' :
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...
223
-                   $query->useDealerI18nQuery()->orderByTitle()->endUse();
224
-                   break;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_BREAK
Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

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

Loading history...
225
-               case 'title-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...
226
-                   $query->useDealerI18nQuery()->orderByTitle(Criteria::DESC)->endUse();
227
-                   break;
0 ignored issues
show
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

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

Loading history...
228
                default:
229
                    break;
230
            }
231
        }
232
233
        return $query;
234
    }
235
236
    /**
237
     * @param Dealer $dealer
238
     * @return Dealer
239
     */
240
    protected function getPrevious($dealer)
241
    {
242
        $query = DealerQuery::create();
243
244
        foreach ($this->getOrder() as $order) {
245
            switch ($order) {
246
                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...
247
                    $query->orderById(Criteria::DESC);
248
                    $query->filterById($dealer->getId(),Criteria::LESS_THAN);
249
                    break;
250
                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...
251
                    $query->orderById();
252
                    $query->filterById($dealer->getId(),Criteria::GREATER_THAN);
253
                    break;
254
                default:
255
                    $query->orderById(Criteria::DESC);
256
                    $query->filterById($dealer->getId(),Criteria::LESS_THAN);
257
                    break;
258
            }
259
        }
260
261
        return $query->findOne();
262
    }
263
264
    /**
265
     * @param Dealer $dealer
266
     * @return Dealer
267
     */
268
    protected function getNext($dealer)
269
    {
270
        $query = DealerQuery::create();
271
272
        foreach ($this->getOrder() as $order) {
273
            switch ($order) {
274
                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...
275
                    $query->orderById();
276
                    $query->filterById($dealer->getId(),Criteria::GREATER_THAN);
277
                    break;
278
                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...
279
                    $query->orderById(Criteria::DESC);
280
                    $query->filterById($dealer->getId(),Criteria::LESS_THAN);
281
                    break;
282
                default:
283
                    $query->orderById();
284
                    $query->filterById($dealer->getId(),Criteria::GREATER_THAN);
285
                    break;
286
            }
287
        }
288
289
        return $query->findOne();
290
    }
291
292
    /**
293
     * @param DealerQuery $query
294
     * @return DealerQuery
295
     */
296
    protected function getAdminDealer($query)
297
    {
298
        $admin = $this->securityContext->getAdminUser();
299
        $auth = $this->securityContext->isGranted(array("ADMIN"), array(AdminResources::MODULE), array('Dealer'), array(AccessManager::VIEW));
300
301
        if ($admin === null) {
302
            return $query;
303
        }
304
305
        if ($admin->getProfileId() === null || $auth === true) {
306
            return $query;
307
        }
308
309
        return $query->useDealerAdminQuery()->filterByAdminId($admin->getId())->endUse();
310
    }
311
}
312