1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AnimeDb package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace AnimeDb\Bundle\CatalogBundle\Controller; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\Form\Form; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\Form\FormError; |
15
|
|
|
use AnimeDb\Bundle\CatalogBundle\Form\Type\Plugin\Search as SearchFrom; |
16
|
|
|
use AnimeDb\Bundle\CatalogBundle\Form\Type\Plugin\SearchFiller as SearchFillerForm; |
17
|
|
|
use AnimeDb\Bundle\CatalogBundle\Entity\SearchFiller; |
18
|
|
|
use AnimeDb\Bundle\CatalogBundle\Entity\Item; |
19
|
|
|
use AnimeDb\Bundle\CatalogBundle\Plugin\Chain; |
20
|
|
|
use AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Filler\Chain as ChainFiller; |
21
|
|
|
use AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Search\Chain as ChainSearch; |
22
|
|
|
use AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Filler\FillerInterface; |
23
|
|
|
use AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Search\SearchInterface; |
24
|
|
|
use AnimeDb\Bundle\CatalogBundle\Plugin\PluginInterface; |
25
|
|
|
use Symfony\Component\HttpFoundation\Response; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Fill. |
29
|
|
|
* |
30
|
|
|
* @author Peter Gribanov <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class FillController extends BaseController |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Create new item from source fill. |
36
|
|
|
* |
37
|
|
|
* @param string $plugin |
38
|
|
|
* @param Request $request |
39
|
|
|
* |
40
|
|
|
* @return Response |
41
|
|
|
*/ |
42
|
|
|
public function fillerAction($plugin, Request $request) |
43
|
|
|
{ |
44
|
|
|
/* @var $response Response */ |
45
|
|
|
$response = $this->getCacheTimeKeeper()->getResponse(); |
46
|
|
|
// response was not modified for this request |
47
|
|
|
if ($response->isNotModified($request)) { |
48
|
|
|
return $response; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/* @var $chain ChainFiller */ |
52
|
|
|
$chain = $this->get('anime_db.plugin.filler'); |
53
|
|
|
/* @var $filler FillerInterface */ |
54
|
|
|
if (!($filler = $chain->getPlugin($plugin))) { |
55
|
|
|
throw $this->createNotFoundException('Plugin \''.$plugin.'\' is not found'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/* @var $form Form */ |
59
|
|
|
$form = $this->createForm($filler->getForm()); |
60
|
|
|
|
61
|
|
|
$fill_form = null; |
62
|
|
|
$form->handleRequest($request); |
63
|
|
|
if ($form->isValid()) { |
64
|
|
|
$item = $filler->fill($form->getData()); |
65
|
|
|
if (!($item instanceof Item)) { |
66
|
|
|
$form->addError(new FormError('Can`t get content from the specified source')); |
67
|
|
|
} else { |
68
|
|
|
$fill_form = $this->createForm('entity_item', $item)->createView(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->render('AnimeDbCatalogBundle:Fill:filler.html.twig', [ |
73
|
|
|
'plugin' => $plugin, |
74
|
|
|
'plugin_name' => $filler->getTitle(), |
75
|
|
|
'form' => $form->createView(), |
76
|
|
|
'fill_form' => $fill_form, |
77
|
|
|
], $response); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Search source fill for item. |
82
|
|
|
* |
83
|
|
|
* @param string $plugin |
84
|
|
|
* @param Request $request |
85
|
|
|
* |
86
|
|
|
* @return Response |
87
|
|
|
*/ |
88
|
|
|
public function searchAction($plugin, Request $request) |
89
|
|
|
{ |
90
|
|
|
$response = $this->getCacheTimeKeeper()->getResponse(); |
91
|
|
|
// response was not modified for this request |
92
|
|
|
if ($response->isNotModified($request)) { |
93
|
|
|
return $response; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/* @var $search SearchInterface */ |
97
|
|
|
if (!($search = $this->get('anime_db.plugin.search_fill')->getPlugin($plugin))) { |
98
|
|
|
throw $this->createNotFoundException('Plugin \''.$plugin.'\' is not found'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/* @var $form Form */ |
102
|
|
|
$form = $this->createForm($search->getForm()); |
103
|
|
|
|
104
|
|
|
$list = []; |
105
|
|
|
$form->handleRequest($request); |
106
|
|
|
if ($form->isValid()) { |
107
|
|
|
$list = $search->search($form->getData()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// full page or hinclude |
111
|
|
|
if ($request->get('hinclude', 0)) { |
112
|
|
|
$tpl = 'AnimeDbCatalogBundle:Fill:search_hinclude.html.twig'; |
113
|
|
|
} else { |
114
|
|
|
$tpl = 'AnimeDbCatalogBundle:Fill:search.html.twig'; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->render($tpl, [ |
118
|
|
|
'plugin' => $plugin, |
119
|
|
|
'plugin_name' => $search->getTitle(), |
120
|
|
|
'list' => $list, |
121
|
|
|
'form' => $form->createView(), |
122
|
|
|
], $response); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Search source fill for item. |
127
|
|
|
* |
128
|
|
|
* @param Request $request |
129
|
|
|
* |
130
|
|
|
* @return Response |
131
|
|
|
*/ |
132
|
|
|
public function searchInAllAction(Request $request) |
133
|
|
|
{ |
134
|
|
|
/* @var $chain ChainSearch */ |
135
|
|
|
$chain = $this->get('anime_db.plugin.search_fill'); |
136
|
|
|
$response = $this->getResponseFromChain($chain); |
137
|
|
|
|
138
|
|
|
// response was not modified for this request |
139
|
|
|
if ($response->isNotModified($request)) { |
140
|
|
|
return $response; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/* @var $form Form */ |
144
|
|
|
$form = $this->createForm(new SearchFrom()); |
145
|
|
|
$form->handleRequest($request); |
146
|
|
|
|
147
|
|
|
return $this->render('AnimeDbCatalogBundle:Fill:search_in_all.html.twig', [ |
148
|
|
|
'plugins' => $chain->getPlugins(), |
149
|
|
|
'form' => $form->createView(), |
150
|
|
|
], $response); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Search filler by URL for fill item. |
155
|
|
|
* |
156
|
|
|
* @param Request $request |
157
|
|
|
* |
158
|
|
|
* @return Response |
159
|
|
|
*/ |
160
|
|
|
public function searchFillerAction(Request $request) |
161
|
|
|
{ |
162
|
|
|
/* @var $chain ChainFiller */ |
163
|
|
|
$chain = $this->get('anime_db.plugin.filler'); |
164
|
|
|
$response = $this->getResponseFromChain($chain); |
165
|
|
|
|
166
|
|
|
// response was not modified for this request |
167
|
|
|
if ($response->isNotModified($request)) { |
168
|
|
|
return $response; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$entity = new SearchFiller($chain); |
172
|
|
|
/* @var $form Form */ |
173
|
|
|
$form = $this->createForm(new SearchFillerForm(), $entity)->handleRequest($request); |
174
|
|
|
if ($form->isValid()) { |
175
|
|
|
return $this->redirect($entity->getFiller()->getLinkForFill($entity->getUrl())); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $this->render('AnimeDbCatalogBundle:Fill:search_filler.html.twig', [ |
179
|
|
|
'form' => $form->createView(), |
180
|
|
|
], $response); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get response from plugins chain. |
185
|
|
|
* |
186
|
|
|
* @param Chain $chain |
187
|
|
|
* |
188
|
|
|
* @return Response |
189
|
|
|
*/ |
190
|
|
|
protected function getResponseFromChain(Chain $chain) |
191
|
|
|
{ |
192
|
|
|
if (!$chain->getPlugins()) { |
193
|
|
|
throw $this->createNotFoundException('No any plugins'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$names = ''; |
197
|
|
|
/* @var $plugin PluginInterface */ |
198
|
|
|
foreach ($chain->getPlugins() as $plugin) { |
199
|
|
|
$names .= '|'.$plugin->getName(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $this |
203
|
|
|
->getCacheTimeKeeper() |
204
|
|
|
->getResponse() |
205
|
|
|
->setEtag(md5($names)); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|