|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** ActionController of Core */ |
|
11
|
|
|
namespace Cv\Controller; |
|
12
|
|
|
|
|
13
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
|
14
|
|
|
use Zend\View\Model\JsonModel; |
|
15
|
|
|
use Core\Form\SummaryFormInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Main Action Controller for the application. |
|
19
|
|
|
* Responsible for displaying the home site. |
|
20
|
|
|
* |
|
21
|
|
|
*/ |
|
22
|
|
|
class ManageController extends AbstractActionController |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* attaches further Listeners for generating / processing the output |
|
27
|
|
|
* @return $this |
|
28
|
|
|
*/ |
|
29
|
|
View Code Duplication |
public function attachDefaultListeners() |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
parent::attachDefaultListeners(); |
|
32
|
|
|
$serviceLocator = $this->serviceLocator; |
|
33
|
|
|
$defaultServices = $serviceLocator->get('DefaultListeners'); |
|
34
|
|
|
$events = $this->getEventManager(); |
|
35
|
|
|
$events->attach($defaultServices); |
|
|
|
|
|
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Home site |
|
41
|
|
|
* |
|
42
|
|
|
*/ |
|
43
|
|
|
public function indexAction() |
|
44
|
|
|
{ |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function formAction() |
|
48
|
|
|
{ |
|
49
|
|
|
$serviceLocator = $this->serviceLocator; |
|
50
|
|
|
$repositories = $serviceLocator->get('repositories'); |
|
51
|
|
|
/* @var $cvRepository \Cv\Repository\Cv */ |
|
52
|
|
|
$cvRepository = $repositories->get('Cv/Cv'); |
|
53
|
|
|
$user = $this->auth()->getUser(); |
|
|
|
|
|
|
54
|
|
|
/* @var $cv \Cv\Entity\Cv */ |
|
55
|
|
|
$cv = $cvRepository->findDraft($user); |
|
56
|
|
|
|
|
57
|
|
|
if (empty($cv)) { |
|
58
|
|
|
// create draft CV |
|
59
|
|
|
$cv = $cvRepository->create(); |
|
60
|
|
|
$cv->setIsDraft(true); |
|
61
|
|
|
$cv->setContact($user->getInfo()); |
|
62
|
|
|
$cv->setUser($user); |
|
63
|
|
|
$repositories->store($cv); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/* @var $container \Core\Form\Container */ |
|
67
|
|
|
$container = $serviceLocator->get('FormElementManager') |
|
68
|
|
|
->get('CvContainer') |
|
69
|
|
|
->setEntity($cv); |
|
70
|
|
|
|
|
71
|
|
|
if ($this->getRequest()->isPost()) { |
|
|
|
|
|
|
72
|
|
|
$params = $this->params(); |
|
73
|
|
|
$form = $container->getForm($params->fromQuery('form')); |
|
74
|
|
|
|
|
75
|
|
|
if ($form) { |
|
76
|
|
|
$form->setData(array_merge( |
|
77
|
|
|
$params->fromPost(), |
|
78
|
|
|
$params->fromFiles() |
|
79
|
|
|
)); |
|
80
|
|
|
|
|
81
|
|
|
if (!$form->isValid()) { |
|
82
|
|
|
return new JsonModel([ |
|
83
|
|
|
'valid' => false, |
|
84
|
|
|
'errors' => $form->getMessages() |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$repositories->store($cv); |
|
89
|
|
|
|
|
90
|
|
|
if ($form instanceof SummaryFormInterface) { |
|
91
|
|
|
$form->setRenderMode(SummaryFormInterface::RENDER_SUMMARY); |
|
92
|
|
|
$viewHelper = 'summaryform'; |
|
93
|
|
|
} else { |
|
94
|
|
|
$viewHelper = 'form'; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// render form |
|
98
|
|
|
$content = $serviceLocator->get('ViewHelperManager') |
|
99
|
|
|
->get($viewHelper) |
|
100
|
|
|
->__invoke($form); |
|
101
|
|
|
|
|
102
|
|
|
return new JsonModel([ |
|
103
|
|
|
'valid' => true, |
|
104
|
|
|
'content' => $content |
|
105
|
|
|
]); |
|
106
|
|
|
} elseif (($action = $params->fromQuery('action')) !== null) { |
|
107
|
|
|
return new JsonModel($container->executeAction($action, $params->fromPost())); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return [ |
|
112
|
|
|
'container' => $container |
|
113
|
|
|
]; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.