Skip to content

Commit 48d0718

Browse files
author
Codeliner
committed
Add voyage views and storing new voyage
1 parent 70e523b commit 48d0718

File tree

7 files changed

+177
-5
lines changed

7 files changed

+177
-5
lines changed

module/Application/config/module.config.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@
5959
),
6060
),
6161
),
62+
'voyagenumber' => array(
63+
'type' => 'Segment',
64+
'options' => array(
65+
'route' => '/voyagenumber/[:voyagenumber]',
66+
'constraints' => array(
67+
'voyagenumber' => '[a-zA-Z0-9_-]*',
68+
),
69+
'defaults' => array(
70+
),
71+
),
72+
),
6273
),
6374
),
6475
),
@@ -69,10 +80,15 @@
6980
'factories' => array(
7081
'main_navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
7182
'cargo_form' => 'Application\Form\Service\CargoFormFactory',
83+
'voyage_form' => 'Application\Form\Service\VoyageFormFactory',
7284
'cargo_repository' => function($sl) {
7385
$em = $sl->get('doctrine.entitymanager.orm_default');
7486
return $em->getRepository('Application\Domain\Model\Cargo\Cargo');
7587
},
88+
'voyage_repository' => function($sl) {
89+
$em = $sl->get('doctrine.entitymanager.orm_default');
90+
return $em->getRepository('Application\Domain\Model\Voyage\Voyage');
91+
}
7692
),
7793
'abstract_factories' => array(
7894
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
@@ -106,6 +122,14 @@
106122
$cargoController->setCargoRepository($cargoRepository);
107123
$cargoController->setCargoForm($serviceManager->get('cargo_form'));
108124
return $cargoController;
125+
},
126+
'Application\Controller\Voyage' => function($controllerLoader) {
127+
$serviceManager = $controllerLoader->getServiceLocator();
128+
129+
$voyageController = new Application\Controller\VoyageController();
130+
$voyageController->setVoyageForm($serviceManager->get('voyage_form'));
131+
$voyageController->setVoyageRepository($serviceManager->get('voyage_repository'));
132+
return $voyageController;
109133
}
110134
)
111135
),
@@ -141,7 +165,8 @@
141165
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
142166
'cache' => 'array',
143167
'paths' => array(
144-
__DIR__ . '/../src/Application/Domain/Model/Cargo/'
168+
__DIR__ . '/../src/Application/Domain/Model/Cargo/',
169+
__DIR__ . '/../src/Application/Domain/Model/Voyage/'
145170
)
146171
),
147172
'orm_default' => array(
@@ -174,6 +199,24 @@
174199
'label' => 'add Cargo'
175200
)
176201
)
202+
),
203+
'voyage' => array(
204+
'type' => 'uri',
205+
'label' => 'Voyage',
206+
'pages' => array(
207+
'list' => array(
208+
'route' => 'application/default',
209+
'controller' => 'voyage',
210+
'action' => 'index',
211+
'label' => 'list Voyages'
212+
),
213+
'add' => array(
214+
'route' => 'application/default',
215+
'controller' => 'voyage',
216+
'action' => 'add',
217+
'label' => 'add Voyage'
218+
)
219+
)
177220
)
178221
)
179222
),

module/Application/src/Application/Controller/VoyageController.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Application\Controller;
1010

1111
use Application\Domain\Model\Voyage;
12+
use Application\Form\VoyageForm;
1213
use Zend\Mvc\Controller\AbstractActionController;
1314
use Zend\View\Model\ViewModel;
1415
/**
@@ -24,5 +25,61 @@ class VoyageController extends AbstractActionController
2425
*/
2526
protected $voyageRepository;
2627

28+
/**
29+
*
30+
* @var VoyageForm
31+
*/
32+
protected $voyageForm;
33+
34+
35+
public function indexAction()
36+
{
37+
return array('voyages' => $this->voyageRepository->findAll());
38+
}
39+
40+
public function addAction()
41+
{
42+
//we use the post redirect get pattern and redirect to same location
43+
$prg = $this->prg();
44+
45+
if ($prg instanceof \Zend\Http\PhpEnvironment\Response) {
46+
// returned a response to redirect us
47+
return $prg;
48+
} elseif ($prg === false) {
49+
// this wasn't a POST request
50+
// probably this is the first time the form was loaded
51+
return array('form' => $this->voyageForm);
52+
}
53+
54+
$this->voyageForm->setData($prg);
55+
56+
if ($this->voyageForm->isValid()) {
57+
$voyageNumber = new Voyage\VoyageNumber($prg['voyage_number']);
58+
$newVoyage = new Voyage\Voyage($voyageNumber);
59+
60+
$this->voyageForm->bind($newVoyage);
61+
$this->voyageForm->bindValues();
62+
$this->voyageRepository->store($newVoyage);
63+
64+
return $this->redirect()->toRoute(
65+
'application/default',
66+
array(
67+
'controller' => 'voyage',
68+
'action' => 'index'
69+
)
70+
);
71+
} else {
72+
return array('form' => $this->voyageForm);
73+
}
74+
}
2775

76+
public function setVoyageRepository(Voyage\VoyageRepositoryInterface $voyageRepository)
77+
{
78+
$this->voyageRepository = $voyageRepository;
79+
}
80+
81+
public function setVoyageForm(VoyageForm $voyageForm)
82+
{
83+
$this->voyageForm = $voyageForm;
84+
}
2885
}

module/Application/src/Application/Form/Service/VoyageFormFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Zend\ServiceManager\FactoryInterface;
1212
use Zend\ServiceManager\ServiceLocatorInterface;
1313
use Application\Form\VoyageForm;
14+
use Application\Form\VoyageNumberHydratorStrategy;
1415
use DoctrineModule\Stdlib\Hydrator\DoctrineObject;
1516
/**
1617
* VoyageFormFactory

module/Application/src/Application/Form/VoyageForm.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct($name = null, $options = array())
2525
parent::__construct('VoyageForm', $options);
2626

2727
$this->add(array(
28-
'name' => 'voyageNumber',
28+
'name' => 'voyage_number',
2929
'options' => array(
3030
'label' => 'Number',
3131
),
@@ -73,13 +73,13 @@ public function __construct($name = null, $options = array())
7373
public function getInputFilter()
7474
{
7575
if (is_null($this->filter)) {
76-
$voyageNumberLengthValidator = new StringLength(30);
76+
$voyageNumberLengthValidator = new StringLength(3, 30);
7777
$voyageNumberInput = new Input('voyage_number');
7878
$voyageNumberInput->setRequired(true)
7979
->getValidatorChain()
8080
->addValidator($voyageNumberLengthValidator);
8181

82-
$nameLengthValidator = new StringLength(100);
82+
$nameLengthValidator = new StringLength(3, 100);
8383
$nameInput = new Input('name');
8484
$nameInput->setRequired(true);
8585
$nameInput->getValidatorChain()

module/Application/src/Application/Infrastructure/Persistence/Doctrine/Type/VoyageNumber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
3232
*/
3333
public function convertToDatabaseValue($value, AbstractPlatform $platform)
3434
{
35-
if (!$value instanceof DomainVoyageNumber) {
35+
if (!$value instanceof DomainVoyageNumber) {
3636
throw ConversionException::conversionFailed($value, $this->getName());
3737
}
3838

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/*
3+
* This file is part of the codeliner/php-ddd-cargo-sample package.
4+
* (c) Alexander Miertsch <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
?>
10+
<div class="jumbotron">
11+
<h1><?php echo $this->translate('Add new Voyage') ?></h1>
12+
<p><?php echo $this->translate('The capacity of a Voyage controls how many Cargos can be booked. The size amount of all assigned Cargos must be less than or equal the capacity. Only the Overbooking-Policy can temporary increase the capacity.') ?></p>
13+
</div>
14+
<div class="row">
15+
<div class="col-md-12">
16+
<?php
17+
$form = $this->form;
18+
$form->prepare();
19+
20+
$form->setAttribute('method', 'post');
21+
22+
echo $this->form()->openTag($form);
23+
24+
$number = $form->get('voyage_number');
25+
echo $this->ztbFormElement($number);
26+
27+
$name = $form->get('name');
28+
echo $this->ztbFormElement($name);
29+
30+
$capacity = $form->get('capacity');
31+
echo $this->ztbFormElement($capacity);
32+
33+
echo '<p></p>';
34+
35+
echo $this->ztbFormElement($form->get('send'));
36+
37+
echo $this->form()->closeTag();
38+
?>
39+
</div>
40+
</div>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/*
3+
* This file is part of the codeliner/php-ddd-cargo-sample package.
4+
* (c) Alexander Miertsch <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
?>
10+
<div class="jumbotron">
11+
<h1><?php echo $this->translate('Voyage Overview') ?></h1>
12+
<p><?php echo $this->translate('Choose a Voyage from the list to view all assigned Cargos.') ?></p>
13+
</div>
14+
<div class="row">
15+
<div class="col-md-12">
16+
<?php if (empty($this->voyages)): ?>
17+
<p class="alert-danger"><?php echo $this->translate('The list is empty. You have to add a Voyage first!'); ?></p>
18+
<?php else: ?>
19+
<ul>
20+
<?php foreach ($this->voyages as $voyage): ?>
21+
<li><a href="<?php echo $this->url('application/default/voyagenumber', array('controller' => 'voyage', 'action' => 'show', 'voyagenumber' => $voyage->getVoyageNumber()->toString())) ?>"><?php echo $voyage->getVoyageNumber() ?></a></li>
22+
<?php endforeach; ?>
23+
</ul>
24+
<?php endif; ?>
25+
</div>
26+
</div>
27+
<div class="row">
28+
<div class="col-md-12">
29+
<p><a class="btn btn-success btn-lg" href="<?php echo $this->url('application/default', array('controller' => 'voyage', 'action' => 'add')) ?>"><?php echo $this->translate('Add new Voyage') ?> &raquo;</a></p>
30+
</div>
31+
</div>

0 commit comments

Comments
 (0)