1 | <?php |
||
21 | class ListBuilder implements ListBuilderInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var TypeGuesserInterface |
||
25 | */ |
||
26 | protected $guesser; |
||
27 | |||
28 | /** |
||
29 | * @var string[] |
||
30 | */ |
||
31 | protected $templates = array(); |
||
32 | |||
33 | /** |
||
34 | * @param TypeGuesserInterface $guesser |
||
35 | * @param string[] $templates |
||
36 | */ |
||
37 | public function __construct(TypeGuesserInterface $guesser, array $templates = array()) |
||
38 | { |
||
39 | $this->guesser = $guesser; |
||
40 | $this->templates = $templates; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | public function getBaseList(array $options = array()) |
||
47 | { |
||
48 | return new FieldDescriptionCollection(); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public function buildField($type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin) |
||
55 | { |
||
56 | if ($type == null) { |
||
57 | $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager()); |
||
58 | $fieldDescription->setType($guessType->getType()); |
||
59 | } else { |
||
60 | $fieldDescription->setType($type); |
||
61 | } |
||
62 | |||
63 | $this->fixFieldDescription($admin, $fieldDescription); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | public function addField(FieldDescriptionCollection $list, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin) |
||
70 | { |
||
71 | $this->buildField($type, $fieldDescription, $admin); |
||
72 | $admin->addListFieldDescription($fieldDescription->getName(), $fieldDescription); |
||
73 | |||
74 | $list->add($fieldDescription); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription) |
||
142 | |||
143 | /** |
||
144 | * @param FieldDescriptionInterface $fieldDescription |
||
145 | * |
||
146 | * @return FieldDescriptionInterface |
||
147 | */ |
||
148 | public function buildActionFieldDescription(FieldDescriptionInterface $fieldDescription) |
||
149 | { |
||
150 | if (null === $fieldDescription->getTemplate()) { |
||
151 | $fieldDescription->setTemplate('SonataAdminBundle:CRUD:list__action.html.twig'); |
||
152 | } |
||
153 | |||
154 | if (null === $fieldDescription->getType()) { |
||
155 | $fieldDescription->setType('action'); |
||
156 | } |
||
157 | |||
158 | if (null === $fieldDescription->getOption('name')) { |
||
159 | $fieldDescription->setOption('name', 'Action'); |
||
160 | } |
||
161 | |||
162 | if (null === $fieldDescription->getOption('code')) { |
||
163 | $fieldDescription->setOption('code', 'Action'); |
||
164 | } |
||
165 | |||
166 | if (null !== $fieldDescription->getOption('actions')) { |
||
167 | $actions = $fieldDescription->getOption('actions'); |
||
168 | foreach ($actions as $k => $action) { |
||
169 | if (!isset($action['template'])) { |
||
170 | $actions[$k]['template'] = sprintf('SonataAdminBundle:CRUD:list__action_%s.html.twig', $k); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | $fieldDescription->setOption('actions', $actions); |
||
175 | } |
||
176 | |||
177 | return $fieldDescription; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param string $type |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | private function getTemplate($type) |
||
193 | } |
||
194 |