CRUDController.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. }
  267. }
  268. $view = $form->createView();
  269. // set the theme for the current Admin Form
  270. $this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme());
  271. return $this->render($this->admin->getTemplate($templateKey), array(
  272. 'action' => 'edit',
  273. 'form' => $view,
  274. 'object' => $object,
  275. ));
  276. }
  277. /**
  278. * redirect the user depend on this choice
  279. *
  280. * @param object $object
  281. *
  282. * @return Response
  283. */
  284. public function redirectTo($object)
  285. {
  286. $url = false;
  287. if ($this->get('request')->get('btn_update_and_list')) {
  288. $url = $this->admin->generateUrl('list');
  289. }
  290. if ($this->get('request')->get('btn_create_and_list')) {
  291. $url = $this->admin->generateUrl('list');
  292. }
  293. if ($this->get('request')->get('btn_create_and_create')) {
  294. $params = array();
  295. if ($this->admin->hasActiveSubClass()) {
  296. $params['subclass'] = $this->get('request')->get('subclass');
  297. }
  298. $url = $this->admin->generateUrl('create', $params);
  299. }
  300. if (!$url) {
  301. $url = $this->admin->generateObjectUrl('edit', $object);
  302. }
  303. return new RedirectResponse($url);
  304. }
  305. /**
  306. * return the Response object associated to the batch action
  307. *
  308. * @throws \RuntimeException
  309. * @return Response
  310. */
  311. public function batchAction()
  312. {
  313. if ($this->getRestMethod() != 'POST') {
  314. throw new \RuntimeException('invalid request type, POST expected');
  315. }
  316. $confirmation = $this->get('request')->get('confirmation', false);
  317. if ($data = json_decode($this->get('request')->get('data'), true)) {
  318. $action = $data['action'];
  319. $idx = $data['idx'];
  320. $all_elements = $data['all_elements'];
  321. $this->get('request')->request->replace($data);
  322. } else {
  323. $this->get('request')->request->set('idx', $this->get('request')->get('idx', array()));
  324. $this->get('request')->request->set('all_elements', $this->get('request')->get('all_elements', false));
  325. $action = $this->get('request')->get('action');
  326. $idx = $this->get('request')->get('idx');
  327. $all_elements = $this->get('request')->get('all_elements');
  328. $data = $this->get('request')->request->all();
  329. }
  330. $batchActions = $this->admin->getBatchActions();
  331. if (!array_key_exists($action, $batchActions)) {
  332. throw new \RuntimeException(sprintf('The `%s` batch action is not defined', $action));
  333. }
  334. $camelizedAction = \Sonata\AdminBundle\Admin\BaseFieldDescription::camelize($action);
  335. $isRelevantAction = sprintf('batchAction%sIsRelevant', ucfirst($camelizedAction));
  336. if (method_exists($this, $isRelevantAction)) {
  337. $nonRelevantMessage = call_user_func(array($this, $isRelevantAction), $idx, $all_elements);
  338. } else {
  339. $nonRelevantMessage = count($idx) != 0 || $all_elements; // at least one item is selected
  340. }
  341. if (!$nonRelevantMessage) { // default non relevant message (if false of null)
  342. $nonRelevantMessage = 'flash_batch_empty';
  343. }
  344. $datagrid = $this->admin->getDatagrid();
  345. $datagrid->buildPager();
  346. if (true !== $nonRelevantMessage) {
  347. $this->addFlash('sonata_flash_info', $nonRelevantMessage);
  348. return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters())));
  349. }
  350. $askConfirmation = isset($batchActions[$action]['ask_confirmation']) ? $batchActions[$action]['ask_confirmation'] : true;
  351. if ($askConfirmation && $confirmation != 'ok') {
  352. $formView = $datagrid->getForm()->createView();
  353. return $this->render('SonataAdminBundle:CRUD:batch_confirmation.html.twig', array(
  354. 'action' => 'list',
  355. 'datagrid' => $datagrid,
  356. 'form' => $formView,
  357. 'data' => $data,
  358. ));
  359. }
  360. // execute the action, batchActionXxxxx
  361. $final_action = sprintf('batchAction%s', ucfirst($camelizedAction));
  362. if (!method_exists($this, $final_action)) {
  363. throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $final_action));
  364. }
  365. $query = $datagrid->getQuery();
  366. $query->setFirstResult(null);
  367. $query->setMaxResults(null);
  368. if (count($idx) > 0) {
  369. $this->admin->getModelManager()->addIdentifiersToQuery($this->admin->getClass(), $query, $idx);
  370. } elseif (!$all_elements) {
  371. $query = null;
  372. }
  373. return call_user_func(array($this, $final_action), $query);
  374. }
  375. /**
  376. * return the Response object associated to the create action
  377. *
  378. * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
  379. * @return Response
  380. */
  381. public function createAction()
  382. {
  383. // the key used to lookup the template
  384. $templateKey = 'edit';
  385. if (false === $this->admin->isGranted('CREATE')) {
  386. throw new AccessDeniedException();
  387. }
  388. $object = $this->admin->getNewInstance();
  389. $this->admin->setSubject($object);
  390. /** @var $form \Symfony\Component\Form\Form */
  391. $form = $this->admin->getForm();
  392. $form->setData($object);
  393. if ($this->getRestMethod()== 'POST') {
  394. $form->bind($this->get('request'));
  395. $isFormValid = $form->isValid();
  396. // persist if the form was valid and if in preview mode the preview was approved
  397. if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
  398. $this->admin->create($object);
  399. if ($this->isXmlHttpRequest()) {
  400. return $this->renderJson(array(
  401. 'result' => 'ok',
  402. 'objectId' => $this->admin->getNormalizedIdentifier($object)
  403. ));
  404. }
  405. $this->addFlash('sonata_flash_success','flash_create_success');
  406. // redirect to edit mode
  407. return $this->redirectTo($object);
  408. }
  409. // show an error message if the form failed validation
  410. if (!$isFormValid) {
  411. if (!$this->isXmlHttpRequest()) {
  412. $this->addFlash('sonata_flash_error', 'flash_create_error');
  413. }
  414. } elseif ($this->isPreviewRequested()) {
  415. // pick the preview template if the form was valid and preview was requested
  416. $templateKey = 'preview';
  417. }
  418. }
  419. $view = $form->createView();
  420. // set the theme for the current Admin Form
  421. $this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme());
  422. return $this->render($this->admin->getTemplate($templateKey), array(
  423. 'action' => 'create',
  424. 'form' => $view,
  425. 'object' => $object,
  426. ));
  427. }
  428. /**
  429. * Returns true if the preview is requested to be shown
  430. *
  431. * @return boolean
  432. */
  433. protected function isPreviewRequested()
  434. {
  435. return ($this->get('request')->get('btn_preview') !== null);
  436. }
  437. /**
  438. * Returns true if the preview has been approved
  439. *
  440. * @return boolean
  441. */
  442. protected function isPreviewApproved()
  443. {
  444. return ($this->get('request')->get('btn_preview_approve') !== null);
  445. }
  446. /**
  447. * Returns true if the request is in the preview workflow
  448. *
  449. * That means either a preview is requested or the preview has already been shown
  450. * and it got approved/declined.
  451. *
  452. * @return boolean
  453. */
  454. protected function isInPreviewMode()
  455. {
  456. return $this->admin->supportsPreviewMode()
  457. && ($this->isPreviewRequested()
  458. || $this->isPreviewApproved()
  459. || $this->isPreviewDeclined());
  460. }
  461. /**
  462. * Returns true if the preview has been declined
  463. *
  464. * @return boolean
  465. */
  466. protected function isPreviewDeclined()
  467. {
  468. return ($this->get('request')->get('btn_preview_decline') !== null);
  469. }
  470. /**
  471. * return the Response object associated to the view action
  472. *
  473. * @param null $id
  474. *
  475. * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
  476. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  477. *
  478. * @return Response
  479. */
  480. public function showAction($id = null)
  481. {
  482. $id = $this->get('request')->get($this->admin->getIdParameter());
  483. $object = $this->admin->getObject($id);
  484. if (!$object) {
  485. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  486. }
  487. if (false === $this->admin->isGranted('VIEW', $object)) {
  488. throw new AccessDeniedException();
  489. }
  490. $this->admin->setSubject($object);
  491. return $this->render($this->admin->getTemplate('show'), array(
  492. 'action' => 'show',
  493. 'object' => $object,
  494. 'elements' => $this->admin->getShow(),
  495. ));
  496. }
  497. /**
  498. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException|\Symfony\Component\Security\Core\Exception\AccessDeniedException
  499. *
  500. * @param mixed $id
  501. *
  502. * @return Response
  503. */
  504. public function historyAction($id = null)
  505. {
  506. if (false === $this->admin->isGranted('EDIT')) {
  507. throw new AccessDeniedException();
  508. }
  509. $id = $this->get('request')->get($this->admin->getIdParameter());
  510. $object = $this->admin->getObject($id);
  511. if (!$object) {
  512. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  513. }
  514. $manager = $this->get('sonata.admin.audit.manager');
  515. if (!$manager->hasReader($this->admin->getClass())) {
  516. throw new NotFoundHttpException(sprintf('unable to find the audit reader for class : %s', $this->admin->getClass()));
  517. }
  518. $reader = $manager->getReader($this->admin->getClass());
  519. $revisions = $reader->findRevisions($this->admin->getClass(), $id);
  520. return $this->render($this->admin->getTemplate('history'), array(
  521. 'action' => 'history',
  522. 'object' => $object,
  523. 'revisions' => $revisions,
  524. ));
  525. }
  526. /**
  527. * @param null $id
  528. * @param string $revision
  529. *
  530. * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
  531. * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  532. *
  533. * @return Response
  534. */
  535. public function historyViewRevisionAction($id = null, $revision = null)
  536. {
  537. if (false === $this->admin->isGranted('EDIT')) {
  538. throw new AccessDeniedException();
  539. }
  540. $id = $this->get('request')->get($this->admin->getIdParameter());
  541. $object = $this->admin->getObject($id);
  542. if (!$object) {
  543. throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
  544. }
  545. $manager = $this->get('sonata.admin.audit.manager');
  546. if (!$manager->hasReader($this->admin->getClass())) {
  547. throw new NotFoundHttpException(sprintf('unable to find the audit reader for class : %s', $this->admin->getClass()));
  548. }
  549. $reader = $manager->getReader($this->admin->getClass());
  550. // retrieve the revisioned object
  551. $object = $reader->find($this->admin->getClass(), $id, $revision);
  552. if (!$object) {
  553. throw new NotFoundHttpException(sprintf('unable to find the targeted object `%s` from the revision `%s` with classname : `%s`', $id, $revision, $this->admin->getClass()));
  554. }
  555. $this->admin->setSubject($object);
  556. return $this->render($this->admin->getTemplate('show'), array(
  557. 'action' => 'show',
  558. 'object' => $object,
  559. 'elements' => $this->admin->getShow(),
  560. ));
  561. }
  562. /**
  563. * @param Request $request
  564. *
  565. * @throws \RuntimeException
  566. * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
  567. *
  568. * @return Response
  569. */
  570. public function exportAction(Request $request)
  571. {
  572. if (false === $this->admin->isGranted('EXPORT')) {
  573. throw new AccessDeniedException();
  574. }
  575. $format = $request->get('format');
  576. $allowedExportFormats = (array) $this->admin->getExportFormats();
  577. if (!in_array($format, $allowedExportFormats) ) {
  578. throw new \RuntimeException(sprintf('Export in format `%s` is not allowed for class: `%s`. Allowed formats are: `%s`', $format, $this->admin->getClass(), implode(', ', $allowedExportFormats)));
  579. }
  580. $filename = sprintf('export_%s_%s.%s',
  581. strtolower(substr($this->admin->getClass(), strripos($this->admin->getClass(), '\\') + 1)),
  582. date('Y_m_d_H_i_s', strtotime('now')),
  583. $format
  584. );
  585. return $this->get('sonata.admin.exporter')->getResponse($format, $filename, $this->admin->getDataSourceIterator());
  586. }
  587. /**
  588. * Adds a flash message for type.
  589. *
  590. * @param string $type
  591. * @param string $message
  592. */
  593. public function addFlash($type, $message)
  594. {
  595. $this->get('session')
  596. ->getFlashBag()
  597. ->add($type, $message);
  598. }
  599. }