Completed
Pull Request — master (#16)
by zzuutt
02:39
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
                $previous = $this->getPrevious($dealer);
81
                $next = $this->getNext($dealer);
82
                $loopResultRow
83
                    ->set("HAS_PREVIOUS", $previous != null ? 1 : 0)
84
                    ->set("HAS_NEXT", $next != null ? 1 : 0)
85
                    ->set("PREVIOUS", $previous != null ? $previous->getId() : -1)
86
                    ->set("NEXT", $next != null ? $next->getId() : -1);
87
            }
88
89
            $loopResult->addRow($loopResultRow);
90
        }
91
92
        return $loopResult;
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98
    protected function getArgDefinitions()
99
    {
100
        return new ArgumentCollection(
101
            Argument::createIntListTypeArgument('id'),
102
            Argument::createIntListTypeArgument('country_id'),
103
            Argument::createIntListTypeArgument('content_id'),
104
            Argument::createIntListTypeArgument('folder_id'),
105
            Argument::createIntListTypeArgument('brand_id'),
106
            Argument::createIntListTypeArgument('product_id'),
107
            Argument::createBooleanTypeArgument('visible'),
108
            Argument::createAnyListTypeArgument('city'),
109
            Argument::createBooleanTypeArgument('with_prev_next_info', false),
110
            Argument::createEnumListTypeArgument('order', [
111
                'id',
112
                'id-reverse',
113
                'date',
114
                'date-reverse',
115
-                'title',
116
-                'title-reverse'
117
            ], 'id')
118
        );
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124
    public function buildModelCriteria()
125
    {
126
        $query = DealerQuery::create();
127
128
        // manage translations
129
        $this->configureI18nProcessing(
130
            $query,
131
            [
132
                'TITLE',
133
                'DESCRIPTION',
134
                'ACCESS'
135
            ],
136
            null,
137
            'ID',
138
            $this->getForceReturn()
139
        );
140
141
        if (null != $id = $this->getId()) {
142
            $query->filterById($id);
143
        }
144
145
        if (null != $country_id = $this->getCountryId()) {
146
            $query->filterByCountryId($country_id);
147
        }
148
149
        if (null != $city = $this->getCity()) {
150
            $query->filterByCity($city);
151
        }
152
153
        if (null != $visible = $this->getVisible()) {
154
            $query->filterByVisible($visible);
155
        }
156
157
        if ($content = $this->getContentId()) {
158
            if (is_array($content)) {
159
                $content = implode(",", $content);
160
            }
161
            $contentJoin = new Join(DealerTableMap::ID, DealerContentTableMap::DEALER_ID, Criteria::LEFT_JOIN);
162
            $query
163
                ->addJoinObject($contentJoin)
164
                ->where(DealerContentTableMap::CONTENT_ID." ".Criteria::IN." (".$content.")");
165
            ;
166
        }
167
168
        if ($folder = $this->getFolderId()) {
169
            if (is_array($folder)) {
170
                $folder = implode(",", $folder);
171
            }
172
            $contentJoin = new Join(DealerTableMap::ID, DealerFolderTableMap::DEALER_ID, Criteria::LEFT_JOIN);
173
            $query
174
                ->addJoinObject($contentJoin)
175
                ->where(DealerFolderTableMap::FOLDER_ID." ".Criteria::IN." (".$folder.")");
176
            ;
177
        }
178
179
        if ($brand = $this->getBrandId()) {
180
            if (is_array($brand)) {
181
                $brand = implode(",", $brand);
182
            }
183
            $contentJoin = new Join(DealerTableMap::ID, DealerBrandTableMap::DEALER_ID, Criteria::LEFT_JOIN);
184
            $query
185
                ->addJoinObject($contentJoin)
186
                ->where(DealerBrandTableMap::BRAND_ID." ".Criteria::IN." (".$brand.")");
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
        $query =$this->getAdminDealer($query);
202
203
        foreach ($this->getOrder() as $order) {
204
            switch ($order) {
205
                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...
206
                    $query->orderById();
207
                    break;
208
                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...
209
                    $query->orderById(Criteria::DESC);
210
                    break;
211
                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...
212
                    $query->orderByCreatedAt();
213
                    break;
214
                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...
215
                    $query->orderByCreatedAt(Criteria::DESC);
216
                    break;
217
                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...
218
-                   $query->useDealerI18nQuery()->orderByTitle()->endUse();
219
-                   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...
220
-               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...
221
-                   $query->useDealerI18nQuery()->orderByTitle(Criteria::DESC)->endUse();
222
-                   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...
223
                default:
224
                    break;
225
            }
226
        }
227
228
        return $query;
229
    }
230
231
    /**
232
     * @param Dealer $dealer
233
     * @return Dealer
234
     */
235
    protected function getPrevious($dealer)
236
    {
237
        $query = DealerQuery::create();
238
239
        foreach ($this->getOrder() as $order) {
240
            switch ($order) {
241
                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...
242
                    $query->orderById(Criteria::DESC);
243
                    $query->filterById($dealer->getId(), Criteria::LESS_THAN);
244
                    break;
245
                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...
246
                    $query->orderById();
247
                    $query->filterById($dealer->getId(), Criteria::GREATER_THAN);
248
                    break;
249
                default:
250
                    $query->orderById(Criteria::DESC);
251
                    $query->filterById($dealer->getId(), Criteria::LESS_THAN);
252
                    break;
253
            }
254
        }
255
256
        return $query->findOne();
257
    }
258
259
    /**
260
     * @param Dealer $dealer
261
     * @return Dealer
262
     */
263
    protected function getNext($dealer)
264
    {
265
        $query = DealerQuery::create();
266
267
        foreach ($this->getOrder() as $order) {
268
            switch ($order) {
269
                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...
270
                    $query->orderById();
271
                    $query->filterById($dealer->getId(), Criteria::GREATER_THAN);
272
                    break;
273
                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...
274
                    $query->orderById(Criteria::DESC);
275
                    $query->filterById($dealer->getId(), Criteria::LESS_THAN);
276
                    break;
277
                default:
278
                    $query->orderById();
279
                    $query->filterById($dealer->getId(), Criteria::GREATER_THAN);
280
                    break;
281
            }
282
        }
283
284
        return $query->findOne();
285
    }
286
287
    /**
288
     * @param DealerQuery $query
289
     * @return DealerQuery
290
     */
291
    protected function getAdminDealer($query)
292
    {
293
        $admin = $this->securityContext->getAdminUser();
294
        $auth = $this->securityContext->isGranted(array("ADMIN"), array(AdminResources::MODULE), array('Dealer'), array(AccessManager::VIEW));
295
296
        if ($admin === null) {
297
            return $query;
298
        }
299
300
        if ($admin->getProfileId() === null || $auth === true) {
301
            return $query;
302
        }
303
304
        return $query->useDealerAdminQuery()->filterByAdminId($admin->getId())->endUse();
305
    }
306
}
307