Completed
Push — master ( ef76c8...b7db51 )
by Antony
03:55
created

SchedulesController::getCloneForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
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\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
            $dataAM = $this->formatData($data);
142
            if (null != $dataAM) {
143
                $this->getService()->createFromArray($dataAM, $this->getCurrentEditionLocale());
144
            }
145
            $dataPM = $this->formatData($data, 'PM');
146
            if (null != $dataPM) {
147
                $this->getService()->createFromArray($dataPM, $this->getCurrentEditionLocale());
148
            }
149
150
151
            // Substitute _ID_ in the URL with the ID of the created object
152
            $successUrl = $creationForm->getSuccessUrl();
153
154
            $con->commit();
155
156
            // Redirect to the success URL
157
            return $this->generateRedirect($successUrl);
158
159
        } 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...
160
            $con->rollBack();
161
            // Form cannot be validated
162
            $error_msg = $this->createStandardFormValidationErrorMessage($ex);
163
        } catch (\Exception $ex) {
164
            $con->rollBack();
165
            // Any other error
166
            $error_msg = $ex->getMessage();
167
        }
168
        if (false !== $error_msg) {
169
            $this->setupFormErrorContext(
170
                $this->getTranslator()->trans("%obj creation", ['%obj' => static::CONTROLLER_ENTITY_NAME]),
171
                $error_msg,
172
                $creationForm,
173
                $ex
174
            );
175
176
            // At this point, the form has error, and should be redisplayed.
177
            return $this->getListRenderTemplate();
178
        }
179
180
    }
181
182
    protected function formatData($data, $type = "AM")
183
    {
184
        $retour = $data;
185
        if (isset($data["begin" . $type]) && $data["begin" . $type] != "") {
186
            $retour["begin"] = $data["begin" . $type];
187
        } else {
188
            return null;
189
        }
190
        if (isset($data["end" . $type]) && $data["end" . $type] != "") {
191
            $retour["end"] = $data["end" . $type];
192
        } else {
193
            return null;
194
        }
195
196
        return $retour;
197
    }
198
199
    public function cloneAction()
200
    {
201
        // Check current user authorization
202
        if (null !== $response = $this->checkAuth(AdminResources::MODULE, Dealer::getModuleCode(),
203
                AccessManager::CREATE)
204
        ) {
205
            return $response;
206
        }
207
208
        // Create the Creation Form
209
        $cloneForm = $this->getCloneForm($this->getRequest());
210
211
        $con = Propel::getConnection();
212
        $con->beginTransaction();
213
214
        try {
215
            // Check the form against constraints violations
216
            $form = $this->validateForm($cloneForm, "POST");
217
            // Get the form field values
218
            $data = $form->getData();
219
220
            $this->getService()->cloneFromArray($data);
221
222
223
            // Substitute _ID_ in the URL with the ID of the created object
224
            $successUrl = $cloneForm->getSuccessUrl();
225
226
            $con->commit();
227
228
            // Redirect to the success URL
229
            return $this->generateRedirect($successUrl);
230
231
        } 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...
232
            $con->rollBack();
233
            // Form cannot be validated
234
            $error_msg = $this->createStandardFormValidationErrorMessage($ex);
235
        } catch (\Exception $ex) {
236
            $con->rollBack();
237
            // Any other error
238
            $error_msg = $ex->getMessage();
239
        }
240
        if (false !== $error_msg) {
241
            $this->setupFormErrorContext(
242
                $this->getTranslator()->trans("%obj creation", ['%obj' => static::CONTROLLER_ENTITY_NAME]),
243
                $error_msg,
244
                $cloneForm,
245
                $ex
246
            );
247
248
            // At this point, the form has error, and should be redisplayed.
249
            return $this->getListRenderTemplate();
250
        }
251
    }
252
253
    /**
254
     * Method to get Base Clone Form
255
     * @return \Thelia\Form\BaseForm
256
     */
257
    protected function getCloneForm()
258
    {
259
        return $this->createForm(static::CONTROLLER_ENTITY_NAME . ".clone");
260
    }
261
}