CRUDController.php 24 KB

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