Completed
Pull Request — master (#1780)
by Gilles
10:52
created

CurrencyController::setVisibleAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3143
cc 3
eloc 10
nc 3
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
namespace Thelia\Controller\Admin;
14
15
use Thelia\Core\Event\Currency\CurrencyCreateEvent;
16
use Thelia\Core\Event\Currency\CurrencyDeleteEvent;
17
use Thelia\Core\Event\Currency\CurrencyUpdateEvent;
18
use Thelia\Core\Event\Currency\CurrencyUpdateRateEvent;
19
use Thelia\Core\Event\TheliaEvents;
20
use Thelia\Core\Event\UpdatePositionEvent;
21
use Thelia\Core\Security\AccessManager;
22
use Thelia\Core\Security\Resource\AdminResources;
23
use Thelia\Form\Definition\AdminForm;
24
use Thelia\Model\CurrencyQuery;
25
26
/**
27
 * Manages currencies
28
 *
29
 * @author Franck Allimant <[email protected]>
30
 */
31
class CurrencyController extends AbstractCrudController
32
{
33
    public function __construct()
34
    {
35
        parent::__construct(
36
            'currency',
37
            'manual',
38
            'order',
39
            AdminResources::CURRENCY,
40
            TheliaEvents::CURRENCY_CREATE,
41
            TheliaEvents::CURRENCY_UPDATE,
42
            TheliaEvents::CURRENCY_DELETE,
43
            null, // No visibility toggle
44
            TheliaEvents::CURRENCY_UPDATE_POSITION
45
        );
46
    }
47
48
    protected function getCreationForm()
49
    {
50
        return $this->createForm(AdminForm::CURRENCY_CREATION);
51
    }
52
53
    protected function getUpdateForm()
54
    {
55
        return $this->createForm(AdminForm::CURRENCY_MODIFICATION);
56
    }
57
58
    protected function getCreationEvent($formData)
59
    {
60
        $createEvent = new CurrencyCreateEvent();
61
62
        $createEvent
63
        ->setCurrencyName($formData['name'])
64
        ->setLocale($formData["locale"])
65
        ->setSymbol($formData['symbol'])
66
        ->setFormat($formData['format'])
67
        ->setCode($formData['code'])
68
        ->setRate($formData['rate'])
69
        ;
70
71
        return $createEvent;
72
    }
73
74
    protected function getUpdateEvent($formData)
75
    {
76
        $changeEvent = new CurrencyUpdateEvent($formData['id']);
77
78
        // Create and dispatch the change event
79
        $changeEvent
80
        ->setCurrencyName($formData['name'])
81
        ->setLocale($formData["locale"])
82
        ->setSymbol($formData['symbol'])
83
        ->setFormat($formData['format'])
84
        ->setCode($formData['code'])
85
        ->setRate($formData['rate'])
86
        ;
87
88
        return $changeEvent;
89
    }
90
91
    protected function createUpdatePositionEvent($positionChangeMode, $positionValue)
92
    {
93
        return new UpdatePositionEvent(
94
            $this->getRequest()->get('currency_id', null),
95
            $positionChangeMode,
96
            $positionValue
97
        );
98
    }
99
100
    protected function getDeleteEvent()
101
    {
102
        return new CurrencyDeleteEvent($this->getRequest()->get('currency_id'));
103
    }
104
105
    protected function eventContainsObject($event)
106
    {
107
        return $event->hasCurrency();
108
    }
109
110
    protected function hydrateObjectForm($object)
111
    {
112
        // Prepare the data that will hydrate the form
113
        $data = array(
114
                'id'     => $object->getId(),
115
                'name'   => $object->getName(),
116
                'locale' => $object->getLocale(),
117
                'code'   => $object->getCode(),
118
                'symbol' => $object->getSymbol(),
119
                'format' => $object->getFormat(),
120
                'rate'   => $object->getRate()
121
        );
122
123
        // Setup the object form
124
        return $this->createForm(AdminForm::CURRENCY_MODIFICATION, "form", $data);
125
    }
126
127
    protected function getObjectFromEvent($event)
128
    {
129
        return $event->hasCurrency() ? $event->getCurrency() : null;
130
    }
131
132
    protected function getExistingObject()
133
    {
134
        $currency =  CurrencyQuery::create()
135
        ->findOneById($this->getRequest()->get('currency_id'));
136
137
        if (null !== $currency) {
138
            $currency->setLocale($this->getCurrentEditionLocale());
139
        }
140
141
        return $currency;
142
    }
143
144
    protected function getObjectLabel($object)
145
    {
146
        return $object->getName();
147
    }
148
149
    protected function getObjectId($object)
150
    {
151
        return $object->getId();
152
    }
153
154
    protected function renderListTemplate($currentOrder)
155
    {
156
        return $this->render('currencies', array('order' => $currentOrder));
157
    }
158
159
    protected function renderEditionTemplate()
160
    {
161
        return $this->render('currency-edit', array('currency_id' => $this->getRequest()->get('currency_id')));
162
    }
163
164
    protected function redirectToEditionTemplate()
165
    {
166
        return $this->generateRedirectFromRoute(
167
            "admin.configuration.currencies.update",
168
            [
169
                'currency_id' => $this->getRequest()->get('currency_id'),
170
            ]
171
        );
172
    }
173
174
    protected function redirectToListTemplate()
175
    {
176
        return $this->generateRedirectFromRoute('admin.configuration.currencies.default');
177
    }
178
179
    /**
180
     * Update currencies rates
181
     */
182
    public function updateRatesAction()
183
    {
184
        // Check current user authorization
185
        if (null !== $response = $this->checkAuth($this->resourceCode, array(), AccessManager::UPDATE)) {
186
            return $response;
187
        }
188
189
        try {
190
            $event = new CurrencyUpdateRateEvent();
191
192
            $this->dispatch(TheliaEvents::CURRENCY_UPDATE_RATES, $event);
193
194
            if ($event->hasUndefinedRates()) {
195
                return $this->render('currencies', [
196
                    'undefined_rates' => $event->getUndefinedRates()
197
                ]);
198
            }
199
200
        } catch (\Exception $ex) {
201
            // Any error
202
            return $this->errorPage($ex);
203
        }
204
205
        return $this->redirectToListTemplate();
206
    }
207
208
    /**
209
     * Sets the default currency
210
     */
211
    public function setDefaultAction()
212
    {
213
        // Check current user authorization
214
        if (null !== $response = $this->checkAuth($this->resourceCode, array(), AccessManager::UPDATE)) {
215
            return $response;
216
        }
217
218
        $changeEvent = new CurrencyUpdateEvent((int) $this->getRequest()->get('currency_id', 0));
219
220
        // Create and dispatch the change event
221
        $changeEvent->setIsDefault(true)->setVisible(1);
222
223
        try {
224
            $this->dispatch(TheliaEvents::CURRENCY_SET_DEFAULT, $changeEvent);
225
        } catch (\Exception $ex) {
226
            // Any error
227
            return $this->errorPage($ex);
228
        }
229
230
        return $this->redirectToListTemplate();
231
    }
232
233
    /**
234
     * Sets if the currency is visible for Front
235
     */
236
    public function setVisibleAction()
237
    {
238
        // Check current user authorization
239
        if (null !== $response = $this->checkAuth($this->resourceCode, array(), AccessManager::UPDATE)) {
240
            return $response;
241
        }
242
243
        $changeEvent = new CurrencyUpdateEvent((int) $this->getRequest()->get('currency_id', 0));
244
245
        // Create and dispatch the change event
246
        $changeEvent->setVisible((int) $this->getRequest()->get('visible', 0));
247
248
        try {
249
            $this->dispatch(TheliaEvents::CURRENCY_SET_VISIBLE, $changeEvent);
250
        } catch (\Exception $ex) {
251
            // Any error
252
            return $this->errorPage($ex);
253
        }
254
255
        return $this->redirectToListTemplate();
256
    }
257
}
258