Completed
Push — master ( 5bcfa0...b7378d )
by Antony
02:35
created

SchedulesController::formatData()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 8.8571
cc 5
eloc 11
nc 3
nop 2
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\Controller;
15
16
use Dealer\Controller\Base\BaseController;
17
use Dealer\Dealer;
18
use Dealer\Model\DealerShedules;
19
use Propel\Runtime\Propel;
20
use Symfony\Component\HttpFoundation\RedirectResponse;
21
use Thelia\Core\Security\AccessManager;
22
use Thelia\Core\Security\Resource\AdminResources;
23
use Thelia\Form\Exception\FormValidationException;
24
use Thelia\Tools\URL;
25
26
27
/**
28
 * Class SchedulesController
29
 * @package Dealer\Controller
30
 */
31
class SchedulesController extends BaseController
32
{
33
    const CONTROLLER_ENTITY_NAME = "dealer-schedules";
34
35
    /**
36
     * Use to get render of list
37
     * @return mixed
38
     */
39
    protected function getListRenderTemplate()
40
    {
41
        $id = $this->getRequest()->request->get("dealer_id");
42
43
        return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/Dealer/dealer/edit",
44
            ["dealer_id" => $id,]));
45
    }
46
47
    /**
48
     * Must return a RedirectResponse instance
49
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
50
     */
51
    protected function redirectToListTemplate()
52
    {
53
        $id = $this->getRequest()->request->get("dealer_id");
54
55
        return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/Dealer/dealer/edit",
56
            ["dealer_id" => $id,]));
57
    }
58
59
    /**
60
     * Use to get Edit render
61
     * @return mixed
62
     */
63
    protected function getEditRenderTemplate()
64
    {
65
        return $this->render("dealer-edit");
66
    }
67
68
    /**
69
     * Use to get Create render
70
     * @return mixed
71
     */
72
    protected function getCreateRenderTemplate()
73
    {
74
        return $this->render("dealer-edit");
75
    }
76
77
    /**
78
     * @return mixed
79
     */
80
    protected function getObjectId($object)
81
    {
82
        /** @var DealerShedules $object */
83
        $object->getId();
84
    }
85
86
    /**
87
     * Load an existing object from the database
88
     */
89
    protected function getExistingObject()
90
    {
91
        // TODO: Implement getExistingObject() method.
92
    }
93
94
    /**
95
     * Hydrate the update form for this object, before passing it to the update template
96
     *
97
     * @param mixed $object
98
     */
99
    protected function hydrateObjectForm($object)
100
    {
101
        // TODO: Implement hydrateObjectForm() method.
102
    }
103
104
    /**
105
     * Method to get current controller associated service
106
     * @return object
107
     */
108
    protected function getService()
109
    {
110
        if (!$this->service) {
111
            $this->service = $this->getContainer()->get("dealer_schedules_service");
112
        }
113
114
        return $this->service;
115
    }
116
117
    /**
118
     * Create an object
119
     * @return mixed|\Symfony\Component\HttpFoundation\Response
120
     */
121
    public function createAction()
122
    {
123
        // Check current user authorization
124
        if (null !== $response = $this->checkAuth(AdminResources::MODULE, Dealer::getModuleCode(),
125
                AccessManager::CREATE)
126
        ) {
127
            return $response;
128
        }
129
130
        // Create the Creation Form
131
        $creationForm = $this->getCreationForm($this->getRequest());
132
133
        $con = Propel::getConnection();
134
        $con->beginTransaction();
135
136
        try {
137
            // Check the form against constraints violations
138
            $form = $this->validateForm($creationForm, "POST");
139
            // Get the form field values
140
            $data = $form->getData();
141
            foreach ($data["day"] as $day) {
142
                $currentData = $data;
143
                $currentData["day"] = $day;
144
                $dataAM = $this->formatData($currentData);
145
                if (null != $dataAM) {
146
                    $this->getService()->createFromArray($dataAM, $this->getCurrentEditionLocale());
147
                }
148
                $dataPM = $this->formatData($currentData, 'PM');
149
                if (null != $dataPM) {
150
                    $this->getService()->createFromArray($dataPM, $this->getCurrentEditionLocale());
151
                }
152
            }
153
154
155
            // Substitute _ID_ in the URL with the ID of the created object
156
            $successUrl = $creationForm->getSuccessUrl();
157
158
            $con->commit();
159
160
            // Redirect to the success URL
161
            return $this->generateRedirect($successUrl);
162
163
        } catch (FormValidationException $ex) {
0 ignored issues
show
Bug introduced by
The class Thelia\Form\Exception\FormValidationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
164
            $con->rollBack();
165
            // Form cannot be validated
166
            $error_msg = $this->createStandardFormValidationErrorMessage($ex);
167
        } catch (\Exception $ex) {
168
            $con->rollBack();
169
            // Any other error
170
            $error_msg = $ex->getMessage();
171
        }
172
        if (false !== $error_msg) {
173
            $this->setupFormErrorContext(
174
                $this->getTranslator()->trans("%obj creation", ['%obj' => static::CONTROLLER_ENTITY_NAME]),
175
                $error_msg,
176
                $creationForm,
177
                $ex
178
            );
179
180
            // At this point, the form has error, and should be redisplayed.
181
            return $this->getListRenderTemplate();
182
        }
183
184
    }
185
186
    protected function formatData($data, $type = "AM")
187
    {
188
        $retour = $data;
189
        if (isset($data["begin" . $type]) && $data["begin" . $type] != "") {
190
            $retour["begin"] = $data["begin" . $type];
191
        } else {
192
            return null;
193
        }
194
        if (isset($data["end" . $type]) && $data["end" . $type] != "") {
195
            $retour["end"] = $data["end" . $type];
196
        } else {
197
            return null;
198
        }
199
200
        return $retour;
201
    }
202
203
    public function cloneAction()
204
    {
205
        // Check current user authorization
206
        if (null !== $response = $this->checkAuth(AdminResources::MODULE, Dealer::getModuleCode(),
207
                AccessManager::CREATE)
208
        ) {
209
            return $response;
210
        }
211
212
        // Create the Creation Form
213
        $cloneForm = $this->getCloneForm($this->getRequest());
214
215
        $con = Propel::getConnection();
216
        $con->beginTransaction();
217
218
        try {
219
            // Check the form against constraints violations
220
            $form = $this->validateForm($cloneForm, "POST");
221
            // Get the form field values
222
            $data = $form->getData();
223
224
            $this->getService()->cloneFromArray($data);
225
226
227
            // Substitute _ID_ in the URL with the ID of the created object
228
            $successUrl = $cloneForm->getSuccessUrl();
229
230
            $con->commit();
231
232
            // Redirect to the success URL
233
            return $this->generateRedirect($successUrl);
234
235
        } catch (FormValidationException $ex) {
0 ignored issues
show
Bug introduced by
The class Thelia\Form\Exception\FormValidationException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
236
            $con->rollBack();
237
            // Form cannot be validated
238
            $error_msg = $this->createStandardFormValidationErrorMessage($ex);
239
        } catch (\Exception $ex) {
240
            $con->rollBack();
241
            // Any other error
242
            $error_msg = $ex->getMessage();
243
        }
244
        if (false !== $error_msg) {
245
            $this->setupFormErrorContext(
246
                $this->getTranslator()->trans("%obj creation", ['%obj' => static::CONTROLLER_ENTITY_NAME]),
247
                $error_msg,
248
                $cloneForm,
249
                $ex
250
            );
251
252
            // At this point, the form has error, and should be redisplayed.
253
            return $this->getListRenderTemplate();
254
        }
255
    }
256
257
    /**
258
     * Method to get Base Clone Form
259
     * @return \Thelia\Form\BaseForm
260
     */
261
    protected function getCloneForm()
262
    {
263
        return $this->createForm(static::CONTROLLER_ENTITY_NAME . ".clone");
264
    }
265
}