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\Dealer; |
17
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
18
|
|
|
use Thelia\Controller\Admin\BaseAdminController; |
19
|
|
|
use Thelia\Core\Security\AccessManager; |
20
|
|
|
use Thelia\Core\Security\Resource\AdminResources; |
21
|
|
|
use Thelia\Form\Exception\FormValidationException; |
22
|
|
|
use Thelia\Tools\URL; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class GeoDealerController |
26
|
|
|
* @package Dealer\Controller |
27
|
|
|
*/ |
28
|
|
|
class GeoDealerController extends BaseAdminController |
29
|
|
|
{ |
30
|
|
|
protected $service; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Save changes on a modified object, and either go back to the object list, or stay on the edition page. |
34
|
|
|
* |
35
|
|
|
* @return \Thelia\Core\HttpFoundation\Response the response |
36
|
|
|
*/ |
37
|
|
|
public function processUpdateAction() |
38
|
|
|
{ |
39
|
|
|
// Check current user authorization |
40
|
|
|
if (null !== $response = $this->checkAuth(AdminResources::MODULE, Dealer::getModuleCode(), |
41
|
|
|
AccessManager::VIEW) |
42
|
|
|
) { |
43
|
|
|
return $response; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Error (Default: false) |
47
|
|
|
$error_msg = false; |
48
|
|
|
|
49
|
|
|
// Create the Form from the request |
50
|
|
|
$changeForm = $this->getForm($this->getRequest()); |
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
// Check the form against constraints violations |
54
|
|
|
$form = $this->validateForm($changeForm, "POST"); |
55
|
|
|
|
56
|
|
|
// Get the form field values |
57
|
|
|
$data = $form->getData(); |
58
|
|
|
|
59
|
|
|
$updatedObject = $this->getService()->updateFromArray($data); |
60
|
|
|
|
61
|
|
|
// Check if object exist |
62
|
|
|
if (!$updatedObject) { |
63
|
|
|
throw new \LogicException( |
64
|
|
|
$this->getTranslator()->trans("No %obj was updated.", ['%obj' => 'Dealer']) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
// If we have to stay on the same page, do not redirect to the successUrl, |
68
|
|
|
// just redirect to the edit page again. |
69
|
|
|
if ($this->getRequest()->get('save_mode') == 'stay') { |
70
|
|
|
$id = $this->getRequest()->query->get("dealer_id"); |
71
|
|
|
|
72
|
|
|
return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/Dealer/dealer/edit", |
|
|
|
|
73
|
|
|
["dealer_id" => $id, ])); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Redirect to the success URL |
77
|
|
|
return $this->generateSuccessRedirect($changeForm); |
78
|
|
|
} catch (FormValidationException $ex) { |
79
|
|
|
// Form cannot be validated |
80
|
|
|
$error_msg = $this->createStandardFormValidationErrorMessage($ex); |
81
|
|
|
/*} catch (\Exception $ex) { |
82
|
|
|
// Any other error |
83
|
|
|
$error_msg = $ex->getMessage();*/ |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (false !== $error_msg) { |
87
|
|
|
// At this point, the form has errors, and should be redisplayed. |
88
|
|
|
$this->setupFormErrorContext( |
89
|
|
|
$this->getTranslator()->trans("%obj modification", ['%obj' => 'Dealer']), |
90
|
|
|
$error_msg, |
91
|
|
|
$changeForm, |
92
|
|
|
$ex |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$id = $this->getRequest()->query->get("dealer_id"); |
96
|
|
|
|
97
|
|
|
return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/Dealer/dealer/edit", |
|
|
|
|
98
|
|
|
["dealer_id" => $id, ])); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function getForm($data = null) |
103
|
|
|
{ |
104
|
|
|
if (!is_array($data)) { |
105
|
|
|
$data = []; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->createForm("dealer-geo", "form", $data); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function getService() |
112
|
|
|
{ |
113
|
|
|
if (!$this->service) { |
114
|
|
|
$this->service = $this->getContainer()->get("dealer_geo_service"); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->service; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.