forked from pablodip/PablodipAdminModuleBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminModule.php
More file actions
128 lines (113 loc) · 3.71 KB
/
Copy pathAdminModule.php
File metadata and controls
128 lines (113 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/*
* This file is part of the PablodipAdminModuleBundle package.
*
* (c) Pablo Díez <pablodip@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Pablodip\AdminModuleBundle\Module;
use Pablodip\AdminModuleBundle\Field\Guesser\DefaultOptionGuesser;
use Pablodip\ModuleBundle\Extension\Molino\BaseMolinoExtension;
use Pablodip\ModuleBundle\Module\Module;
use Pablodip\ModuleBundle\Extension\Model\ModelExtension;
use Pablodip\ModuleBundle\Field\FieldBag;
use Pablodip\AdminModuleBundle\Field\Guesser\IdFieldGuesser;
use Pablodip\AdminModuleBundle\Field\Guesser\ValidatorFieldGuesser;
use Pablodip\AdminModuleBundle\AdminSession;
use Pablodip\AdminModuleBundle\Action;
/**
* AdminModule.
*
* @author Pablo Díez <pablodip@gmail.com>
*/
abstract class AdminModule extends Module
{
private $adminSession;
/**
* {@inheritdoc}
*/
protected function registerExtensions()
{
return array_merge(parent::registerExtensions(), array(
new ModelExtension(),
$this->registerMolinoExtension(),
));
}
/**
* Returns the molino extension.
*
* @return BaseMolinoExtension A molino extension.
*/
abstract protected function registerMolinoExtension();
/**
* {@inheritdoc}
*/
protected function defineConfiguration()
{
$this->getOption('model_field_guessers')->add(array(
new DefaultOptionGuesser(),
new IdFieldGuesser(),
new ValidatorFieldGuesser($this->getContainer()->get('validator')->getMetadataFactory()),
));
$this->addOption('admin_session_parameter', 'as');
$this->addOption('model_form_options', array());
$this->addActions(array(
new Action\ListAction(),
new Action\BatchAction(),
new Action\NewAction(),
new Action\CreateAction(),
new Action\EditAction(),
new Action\UpdateAction(),
new Action\DeleteAction(),
));
}
/**
* {@inheritdoc}
*/
protected function parseConfiguration()
{
if ($this->getContainer()->isScopeActive('request')) {
$this->adminSession = new AdminSession(
$this->getContainer()->get('request'),
$this->getContainer()->get('session'),
$this->getOption('admin_session_parameter')
);
$this->addParameterToPropagate($this->getOption('admin_session_parameter'));
}
}
/**
* Returns the admin session.
*
* @return AdminSession The admin session.
*/
public function getAdminSession()
{
return $this->adminSession;
}
/**
* Default method is POST, override if required
*
* @param $model
* @param FieldBag $fields
* @param array $options
* @return mixed
*/
public function createModelForm($model, FieldBag $fields, array $options = array('method' => 'POST'))
{
// pull in any options defined in defineConfiguration
$options = array_merge($options, $this->getOption("model_form_options"));
$formBuilder = $this->getContainer()
->get('form.factory')
->createBuilder('form', $model, $options)
;
foreach ($fields as $field) {
$type = $field->hasOption('form_type') ? $field->getOption('form_type') : null;
$options = $field->hasOption('form_options') ? $field->getOption('form_options') : array();
$options['label'] = $field->getLabel();
$formBuilder->add($field->getName(), $type, $options);
}
return $formBuilder->getForm();
}
}