CRUDController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Controller;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  17. use Sonata\AdminBundle\Exception\ModelManagerException;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  20. use Sonata\AdminBundle\Admin\BaseFieldDescription;
  21. class CRUDController extends Controller
  22. {
  23. /**
  24. * The related Admin class
  25. *
  26. * @var \Sonata\AdminBundle\Admin\AdminInterface
  27. */
  28. protected $admin;
  29. /**
  30. * @param mixed $data
  31. * @param integer $status
  32. * @param array $headers
  33. *
  34. * @return Response with json encoded data
  35. */
  36. public function renderJson($data, $status = 200, $headers = array())
  37. {
  38. // fake content-type so browser does not show the download popup when this
  39. // response is rendered through an iframe (used by the jquery.form.js plugin)
  40. // => don't know yet if it is the best solution
  41. if ($this->get('request')->get('_xml_http_request')
  42. && strpos($this->get('request')->headers->get('Content-Type'), 'multipart/form-data') === 0) {
  43. $headers['Content-Type'] = 'text/plain';
  44. } else {
  45. $headers['Content-Type'] = 'application/json';
  46. }
  47. return new Response(json_encode($data), $status, $headers);
  48. }
  49. /**
  50. *
  51. * @return boolean true if the request is done by an ajax like query
  52. */
  53. public function isXmlHttpRequest()
  54. {
  55. return $this->get('request')->isXmlHttpRequest() || $this->get('request')->get('_xml_http_request');
  56. }
  57. /**
  58. * Returns the correct RESTful verb, given either by the request itself or
  59. * via the "_method" parameter.
  60. *
  61. * @return string HTTP method, either
  62. */
  63. protected function getRestMethod()
  64. {
  65. $request = $this->getRequest();
  66. if (Request::getHttpMethodParameterOverride() || !$request->request->has('_method')) {
  67. return $request->getMethod();
  68. }
  69. return $request->request->get('_method');
  70. }
  71. /**
  72. * Sets the Container associated with this Controller.
  73. *
  74. * @param ContainerInterface $container A ContainerInterface instance
  75. */
  76. public function setContainer(ContainerInterface $container = null)
  77. {
  78. $this->container = $container;
  79. $this->configure();
  80. }
  81. /**
  82. * Contextualize the admin class depends on the current request
  83. *
  84. * @throws \RuntimeException
  85. * @return void
  86. */
  87. public function configure()
  88. {
  89. $adminCode = $this->container->get('request')->get('_sonata_admin');
  90. if (!$adminCode) {
  91. throw new \RuntimeException(sprintf('There is no `_sonata_admin` defined for the controller `%s` and the current route `%s`', get_class($this), $this->container->get('request')->get('_route')));
  92. }
  93. $this->admin = $this->container->get('sonata.admin.pool')->getAdminByAdminCode($adminCode);
  94. if (!$this->admin) {
  95. throw new \RuntimeException(sprintf('Unable to find the admin class related to the current controller (%s)', get_class($this)));
  96. }
  97. $rootAdmin = $this->admin;
  98. if ($this->admin->isChild()) {
  99. $this->admin->setCurrentChild(true);
  100. $rootAdmin = $rootAdmin->getParent();
  101. }
  102. $request = $this->container->get('request');
  103. $rootAdmin->setRequest($request);
  104. if ($request->get('uniqid')) {
  105. $this->admin->setUniqid($request->get('uniqid'));
  106. }
  107. }
  108. /**
  109. * return the base template name
  110. *
  111. * @return string the template name
  112. */
  113. public function getBaseTemplate()
  114. {
  115. if ($this->isXmlHttpRequest()) {
  116. return $this->admin->getTemplate('ajax');
  117. }
  118. return $this->admin->getTemplate('layout');
  119. }
  120. /**
  121. * @param string $view
  122. * @param array $parameters
  123. * @param Response $response
  124. *
  125. * @return Response
  126. */
  127. public function render($view, array $parameters = array(), Response $response = null)
  128. {
  129. $parameters['admin'] = isset($parameters['admin']) ? $parameters['admin'] : $this->admin;
  130. $parameters['base_template'] = isset($parameters['base_template']) ? $parameters['base_template'] : $this->getBaseTemplate();
  131. $parameters['admin_pool'] = $this->get('sonata.admin.pool');
  132. return parent::render($view, $parameters);
  133. }
  134. /**
  135. * return the Response object associated to the list action
  136. *
  137. * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
  138. *
  139. * @return Response
  140. */
  141. public function listAction()
  142. {
  143. if (false === $this->admin->isGranted('LIST')) {
  144. throw new AccessDeniedException();
  145. }
  146. $datagrid = $this->admin->getDatagrid();
  147. $formView = $datagrid->getForm()->createView();
  148. // set the theme for the current Admin Form
  149. $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
  150. return $this->render($this->admin->getTemplate('list'), array(
  151. 'action' => 'list',
  152. 'form' => $formView,
  153. 'datagrid' => $datagrid
  154. ));
  155. }
  156. /**
  157. * execute a batch delete
  158. *
  159. * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
  160. *
  161. * @param \Sonata\AdminBundle\Datagrid\ProxyQueryInterface $query
  162. *
  163. * @return \Symfony\Component\HttpFoundation\RedirectResponse
  164. */
  165. public function batchActionDelete(ProxyQueryInterface $query)
  166. {
  167. if (false === $this->admin->isGranted('DELETE')) {
  168. throw new AccessDeniedException();
  169. }
  170. $modelManager = $this->admin->getModelManager();
  171. try {
  172. $modelManager->batchDelete($this->admin->getClass(), $query);
  173. $this->addFlash('sonata_flash_success', 'flash_batch_delete_success');
  174. } catch ( ModelManagerException $e ) {
  175. $this->addFlash('sonata_flash_error', 'flash_batch_delete_error');
  176. }
  177. return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters())));
  178. }
  179. /**
  180. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException|\Symfony\Component\Security\Core\Exception\AccessDeniedException
  181. *
  182. * @param mixed $id
  183. *
  184. * @return Response|RedirectResponse
  185. */
  186. public function deleteAction($id)
  187. {
  188. $id = $this->get('request')->get($this->admin->getIdParameter());
  189. $object = $this->admin->getObject($id);
  190. if (!$object) {
  191. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  192. }
  193. if (false === $this->admin->isGranted('DELETE', $object)) {
  194. throw new AccessDeniedException();
  195. }
  196. if ($this->getRestMethod() == 'DELETE') {
  197. try {
  198. $this->admin->delete($object);
  199. if ($this->isXmlHttpRequest()) {
  200. return $this->renderJson(array('result' => 'ok'));
  201. }
  202. $this->addFlash('sonata_flash_success', 'flash_delete_success');
  203. } catch (ModelManagerException $e) {
  204. if ($this->isXmlHttpRequest()) {
  205. return $this->renderJson(array('result' => 'error'));
  206. }
  207. $this->addFlash('sonata_flash_error', 'flash_delete_error');
  208. }
  209. return new RedirectResponse($this->admin->generateUrl('list'));
  210. }
  211. return $this->render($this->admin->getTemplate('delete'), array(
  212. 'object' => $object,
  213. 'action' => 'delete'
  214. ));
  215. }
  216. /**
  217. * return the Response object associated to the edit action
  218. *
  219. *
  220. * @param mixed $id
  221. *
  222. * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
  223. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  224. *
  225. * @return Response
  226. */
  227. public function editAction($id = null)
  228. {
  229. // the key used to lookup the template
  230. $templateKey = 'edit';
  231. $id = $this->get('request')->get($this->admin->getIdParameter());
  232. $object = $this->admin->getObject($id);
  233. if (!$object) {
  234. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  235. }
  236. if (false === $this->admin->isGranted('EDIT', $object)) {
  237. throw new AccessDeniedException();
  238. }
  239. $this->admin->setSubject($object);
  240. /** @var $form \Symfony\Component\Form\Form */
  241. $form = $this->admin->getForm();
  242. $form->setData($object);
  243. if ($this->getRestMethod() == 'POST') {
  244. $form->bind($this->get('request'));
  245. $isFormValid = $form->isValid();
  246. // persist if the form was valid and if in preview mode the preview was approved
  247. if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
  248. $this->admin->update($object);
  249. $this->addFlash('sonata_flash_success', 'flash_edit_success');
  250. if ($this->isXmlHttpRequest()) {
  251. return $this->renderJson(array(
  252. 'result' => 'ok',
  253. 'objectId' => $this->admin->getNormalizedIdentifier($object)
  254. ));
  255. }
  256. // redirect to edit mode
  257. return $this->redirectTo($object);
  258. }
  259. // show an error message if the form failed validation
  260. if (!$isFormValid) {
  261. if (!$this->isXmlHttpRequest()) {
  262. $this->addFlash('sonata_flash_error', 'flash_edit_error');
  263. }
  264. } elseif ($this->isPreviewRequested()) {
  265. // enable the preview template if the form was valid and preview was requested
  266. $templateKey = 'preview';
  267. $this->admin->getShow();
  268. }
  269. }
  270. $view = $form->createView();
  271. // set the theme for the current Admin Form
  272. $this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme());
  273. return $this->render($this->admin->getTemplate($templateKey), array(
  274. 'action' => 'edit',
  275. 'form' => $view,
  276. 'object' => $object,
  277. ));
  278. }
  279. /**
  280. * redirect the user depend on this choice
  281. *
  282. * @param object $object
  283. *
  284. * @return Response
  285. */
  286. public function redirectTo($object)
  287. {
  288. $url = false;
  289. if ($this->get('request')->get('btn_update_and_list')) {
  290. $url = $this->admin->generateUrl('list');
  291. }
  292. if ($this->get('request')->get('btn_create_and_list')) {
  293. $url = $this->admin->generateUrl('list');
  294. }
  295. if ($this->get('request')->get('btn_create_and_create')) {
  296. $params = array();
  297. if ($this->admin->hasActiveSubClass()) {
  298. $params['subclass'] = $this->get('request')->get('subclass');
  299. }
  300. $url = $this->admin->generateUrl('create', $params);
  301. }
  302. if (!$url) {
  303. $url = $this->admin->generateObjectUrl('edit', $object);
  304. }
  305. return new RedirectResponse($url);
  306. }
  307. /**
  308. * return the Response object associated to the batch action
  309. *
  310. * @throws \RuntimeException
  311. * @return Response
  312. */
  313. public function batchAction()
  314. {
  315. if ($this->getRestMethod() != 'POST') {
  316. throw new \RuntimeException('invalid request type, POST expected');
  317. }
  318. $confirmation = $this->get('request')->get('confirmation', false);
  319. if ($data = json_decode($this->get('request')->get('data'), true)) {
  320. $action = $data['action'];
  321. $idx = $data['idx'];
  322. $all_elements = $data['all_elements'];
  323. $this->get('request')->request->replace($data);
  324. } else {
  325. $this->get('request')->request->set('idx', $this->get('request')->get('idx', array()));
  326. $this->get('request')->request->set('all_elements', $this->get('request')->get('all_elements', false));
  327. $action = $this->get('request')->get('action');
  328. $idx = $this->get('request')->get('idx');
  329. $all_elements = $this->get('request')->get('all_elements');
  330. $data = $this->get('request')->request->all();
  331. }
  332. $batchActions = $this->admin->getBatchActions();
  333. if (!array_key_exists($action, $batchActions)) {
  334. throw new \RuntimeException(sprintf('The `%s` batch action is not defined', $action));
  335. }
  336. $camelizedAction = BaseFieldDescription::camelize($action);
  337. $isRelevantAction = sprintf('batchAction%sIsRelevant', ucfirst($camelizedAction));
  338. if (method_exists($this, $isRelevantAction)) {
  339. $nonRelevantMessage = call_user_func(array($this, $isRelevantAction), $idx, $all_elements);
  340. } else {
  341. $nonRelevantMessage = count($idx) != 0 || $all_elements; // at least one item is selected
  342. }
  343. if (!$nonRelevantMessage) { // default non relevant message (if false of null)
  344. $nonRelevantMessage = 'flash_batch_empty';
  345. }
  346. $datagrid = $this->admin->getDatagrid();
  347. $datagrid->buildPager();
  348. if (true !== $nonRelevantMessage) {
  349. $this->addFlash('sonata_flash_info', $nonRelevantMessage);
  350. return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters())));
  351. }
  352. $askConfirmation = isset($batchActions[$action]['ask_confirmation']) ? $batchActions[$action]['ask_confirmation'] : true;
  353. if ($askConfirmation && $confirmation != 'ok') {
  354. $formView = $datagrid->getForm()->createView();
  355. return $this->render($this->admin->getTemplate('batch_confirmation'), array(
  356. 'action' => 'list',
  357. 'datagrid' => $datagrid,
  358. 'form' => $formView,
  359. 'data' => $data,
  360. ));
  361. }
  362. // execute the action, batchActionXxxxx
  363. $final_action = sprintf('batchAction%s', ucfirst($camelizedAction));
  364. if (!method_exists($this, $final_action)) {
  365. throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $final_action));
  366. }
  367. $query = $datagrid->getQuery();
  368. $query->setFirstResult(null);
  369. $query->setMaxResults(null);
  370. if (count($idx) > 0) {
  371. $this->admin->getModelManager()->addIdentifiersToQuery($this->admin->getClass(), $query, $idx);
  372. } elseif (!$all_elements) {
  373. $query = null;
  374. }
  375. return call_user_func(array($this, $final_action), $query);
  376. }
  377. /**
  378. * return the Response object associated to the create action
  379. *
  380. * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
  381. * @return Response
  382. */
  383. public function createAction()
  384. {
  385. // the key used to lookup the template
  386. $templateKey = 'edit';
  387. if (false === $this->admin->isGranted('CREATE')) {
  388. throw new AccessDeniedException();
  389. }
  390. $object = $this->admin->getNewInstance();
  391. $this->admin->setSubject($object);
  392. /** @var $form \Symfony\Component\Form\Form */
  393. $form = $this->admin->getForm();
  394. $form->setData($object);
  395. if ($this->getRestMethod()== 'POST') {
  396. $form->bind($this->get('request'));
  397. $isFormValid = $form->isValid();
  398. // persist if the form was valid and if in preview mode the preview was approved
  399. if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
  400. $this->admin->create($object);
  401. if ($this->isXmlHttpRequest()) {
  402. return $this->renderJson(array(
  403. 'result' => 'ok',
  404. 'objectId' => $this->admin->getNormalizedIdentifier($object)
  405. ));
  406. }
  407. $this->addFlash('sonata_flash_success','flash_create_success');
  408. // redirect to edit mode
  409. return $this->redirectTo($object);
  410. }
  411. // show an error message if the form failed validation
  412. if (!$isFormValid) {
  413. if (!$this->isXmlHttpRequest()) {
  414. $this->addFlash('sonata_flash_error', 'flash_create_error');
  415. }
  416. } elseif ($this->isPreviewRequested()) {
  417. // pick the preview template if the form was valid and preview was requested
  418. $templateKey = 'preview';
  419. $this->admin->getShow();
  420. }
  421. }
  422. $view = $form->createView();
  423. // set the theme for the current Admin Form
  424. $this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme());
  425. return $this->render($this->admin->getTemplate($templateKey), array(
  426. 'action' => 'create',
  427. 'form' => $view,
  428. 'object' => $object,
  429. ));
  430. }
  431. /**
  432. * Returns true if the preview is requested to be shown
  433. *
  434. * @return boolean
  435. */
  436. protected function isPreviewRequested()
  437. {
  438. return ($this->get('request')->get('btn_preview') !== null);
  439. }
  440. /**
  441. * Returns true if the preview has been approved
  442. *
  443. * @return boolean
  444. */
  445. protected function isPreviewApproved()
  446. {
  447. return ($this->get('request')->get('btn_preview_approve') !== null);
  448. }
  449. /**
  450. * Returns true if the request is in the preview workflow
  451. *
  452. * That means either a preview is requested or the preview has already been shown
  453. * and it got approved/declined.
  454. *
  455. * @return boolean
  456. */
  457. protected function isInPreviewMode()
  458. {
  459. return $this->admin->supportsPreviewMode()
  460. && ($this->isPreviewRequested()
  461. || $this->isPreviewApproved()
  462. || $this->isPreviewDeclined());
  463. }
  464. /**
  465. * Returns true if the preview has been declined
  466. *
  467. * @return boolean
  468. */
  469. protected function isPreviewDeclined()
  470. {
  471. return ($this->get('request')->get('btn_preview_decline') !== null);
  472. }
  473. /**
  474. * return the Response object associated to the view action
  475. *
  476. * @param null $id
  477. *
  478. * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
  479. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  480. *
  481. * @return Response
  482. */
  483. public function showAction($id = null)
  484. {
  485. $id = $this->get('request')->get($this->admin->getIdParameter());
  486. $object = $this->admin->getObject($id);
  487. if (!$object) {
  488. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  489. }
  490. if (false === $this->admin->isGranted('VIEW', $object)) {
  491. throw new AccessDeniedException();
  492. }
  493. $this->admin->setSubject($object);
  494. return $this->render($this->admin->getTemplate('show'), array(
  495. 'action' => 'show',
  496. 'object' => $object,
  497. 'elements' => $this->admin->getShow(),
  498. ));
  499. }
  500. /**
  501. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException|\Symfony\Component\Security\Core\Exception\AccessDeniedException
  502. *
  503. * @param mixed $id
  504. *
  505. * @return Response
  506. */
  507. public function historyAction($id = null)
  508. {
  509. if (false === $this->admin->isGranted('EDIT')) {
  510. throw new AccessDeniedException();
  511. }
  512. $id = $this->get('request')->get($this->admin->getIdParameter());
  513. $object = $this->admin->getObject($id);
  514. if (!$object) {
  515. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  516. }
  517. $manager = $this->get('sonata.admin.audit.manager');
  518. if (!$manager->hasReader($this->admin->getClass())) {
  519. throw new NotFoundHttpException(sprintf('unable to find the audit reader for class : %s', $this->admin->getClass()));
  520. }
  521. $reader = $manager->getReader($this->admin->getClass());
  522. $revisions = $reader->findRevisions($this->admin->getClass(), $id);
  523. return $this->render($this->admin->getTemplate('history'), array(
  524. 'action' => 'history',
  525. 'object' => $object,
  526. 'revisions' => $revisions,
  527. ));
  528. }
  529. /**
  530. * @param null $id
  531. * @param string $revision
  532. *
  533. * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
  534. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  535. *
  536. * @return Response
  537. */
  538. public function historyViewRevisionAction($id = null, $revision = null)
  539. {
  540. if (false === $this->admin->isGranted('EDIT')) {
  541. throw new AccessDeniedException();
  542. }
  543. $id = $this->get('request')->get($this->admin->getIdParameter());
  544. $object = $this->admin->getObject($id);
  545. if (!$object) {
  546. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  547. }
  548. $manager = $this->get('sonata.admin.audit.manager');
  549. if (!$manager->hasReader($this->admin->getClass())) {
  550. throw new NotFoundHttpException(sprintf('unable to find the audit reader for class : %s', $this->admin->getClass()));
  551. }
  552. $reader = $manager->getReader($this->admin->getClass());
  553. // retrieve the revisioned object
  554. $object = $reader->find($this->admin->getClass(), $id, $revision);
  555. if (!$object) {
  556. throw new NotFoundHttpException(sprintf('unable to find the targeted object `%s` from the revision `%s` with classname : `%s`', $id, $revision, $this->admin->getClass()));
  557. }
  558. $this->admin->setSubject($object);
  559. return $this->render($this->admin->getTemplate('show'), array(
  560. 'action' => 'show',
  561. 'object' => $object,
  562. 'elements' => $this->admin->getShow(),
  563. ));
  564. }
  565. /**
  566. * @param Request $request
  567. *
  568. * @throws \RuntimeException
  569. * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
  570. *
  571. * @return Response
  572. */
  573. public function exportAction(Request $request)
  574. {
  575. if (false === $this->admin->isGranted('EXPORT')) {
  576. throw new AccessDeniedException();
  577. }
  578. $format = $request->get('format');
  579. $allowedExportFormats = (array) $this->admin->getExportFormats();
  580. if (!in_array($format, $allowedExportFormats) ) {
  581. throw new \RuntimeException(sprintf('Export in format `%s` is not allowed for class: `%s`. Allowed formats are: `%s`', $format, $this->admin->getClass(), implode(', ', $allowedExportFormats)));
  582. }
  583. $filename = sprintf('export_%s_%s.%s',
  584. strtolower(substr($this->admin->getClass(), strripos($this->admin->getClass(), '\\') + 1)),
  585. date('Y_m_d_H_i_s', strtotime('now')),
  586. $format
  587. );
  588. return $this->get('sonata.admin.exporter')->getResponse($format, $filename, $this->admin->getDataSourceIterator());
  589. }
  590. /**
  591. * Adds a flash message for type.
  592. *
  593. * @param string $type
  594. * @param string $message
  595. */
  596. public function addFlash($type, $message)
  597. {
  598. $this->get('session')
  599. ->getFlashBag()
  600. ->add($type, $message);
  601. }
  602. }