RewriteUrlLoop::buildModelCriteria()   A
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.2728
cc 5
nc 12
nop 0
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the RewriteUrl module for Thelia.                       */
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
namespace RewriteUrl\Loop;
14
15
use Propel\Runtime\ActiveQuery\Criteria;
16
use RewriteUrl\Model\RewritingRedirectType;
17
use RewriteUrl\Model\RewritingRedirectTypeQuery;
18
use Thelia\Core\Template\Element\BaseLoop;
19
use Thelia\Core\Template\Element\LoopResult;
20
use Thelia\Core\Template\Element\LoopResultRow;
21
use Thelia\Core\Template\Element\PropelSearchLoopInterface;
22
use Thelia\Core\Template\Loop\Argument\Argument;
23
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
24
use Thelia\Model\Base\RewritingUrlQuery;
25
26
/**
27
 * Class RewriteUrlLoop
28
 * @package RewriteUrl\Loop
29
 * @author Vincent Lopes <[email protected]>
30
 * @author Gilles Bourgeat <[email protected]>
31
 */
32
class RewriteUrlLoop extends BaseLoop implements PropelSearchLoopInterface
33
{
34
    /**
35
     * @return ArgumentCollection
36
     */
37
    protected function getArgDefinitions()
38
    {
39
        return new ArgumentCollection(
40
            Argument::createIntTypeArgument('id'),
41
            Argument::createAnyTypeArgument('view'),
42
            Argument::createIntTypeArgument('view_id'),
43
            Argument::createIntTypeArgument('redirect')
44
        );
45
    }
46
47
    /**
48
     * @return \Thelia\Model\RewritingUrlQuery
49
     */
50
    public function buildModelCriteria()
51
    {
52
        $search = RewritingUrlQuery::create();
53
54
        if (null !== $id = $this->getId()) {
55
            $search->filterById($id);
56
        }
57
58
        if (null !== $view_id = $this->getViewId()) {
59
            $search->filterByViewId($view_id)->filterByView($this->getView())->find();
60
        }
61
62
        $redirect = $this->getRedirect();
63
        if ($redirect == 1) {
64
            $search->filterByRedirected(null, Criteria::NOT_EQUAL)->find();
65
        } elseif ($redirect == 0) {
66
            $search->filterByRedirected(null, Criteria::EQUAL)->find();
67
        }
68
69
        return $search;
70
    }
71
72
    /**
73
     * @param LoopResult $loopResult
74
     * @return LoopResult
75
     */
76
    public function parseResults(LoopResult $loopResult)
77
    {
78
        $redirectTypeSearch = RewritingRedirectTypeQuery::create();
79
        foreach ($loopResult->getResultDataCollection() as $rewriteURl) {
80
            $loopResultRow = (new LoopResultRow($rewriteURl))
81
                ->set('ID_URL', $rewriteURl->getID())
82
                ->set('URL', $rewriteURl->getUrl())
83
                ->set('VIEW', $rewriteURl->getView())
84
                ->set('VIEW_LOCALE', $rewriteURl->getViewLocale())
85
                ->set('REDIRECTED', $rewriteURl->getRedirected())
86
                ->set('VIEW_ID', $rewriteURl->getViewId());
87
88
89
            if ($rewriteURl->getRedirected()) {
90
                $redirectType = $redirectTypeSearch->findPk($rewriteURl->getID());
91
                if ($redirectType == null) {
92
                    $httpcode = RewritingRedirectType::DEFAULT_REDIRECT_TYPE;
93
                } else {
94
                    $httpcode = $redirectType->getHttpcode();
95
                }
96
                $loopResultRow->set('HTTPCODE', $httpcode);
97
            }
98
99
            $loopResult->addRow($loopResultRow);
100
        }
101
102
        return $loopResult;
103
    }
104
}
105