AdminExporter.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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\Bridge\Exporter;
  11. use Exporter\Exporter;
  12. use Sonata\AdminBundle\Admin\AdminInterface;
  13. /**
  14. * @author Grégoire Paris <postmaster@greg0ire.fr>
  15. */
  16. final class AdminExporter
  17. {
  18. /**
  19. * @var Exporter service from the exporter bundle
  20. */
  21. private $exporter;
  22. /**
  23. * @param Exporter will be used to get global settings
  24. */
  25. public function __construct(Exporter $exporter)
  26. {
  27. $this->exporter = $exporter;
  28. }
  29. /**
  30. * Queries an admin for its default export formats, and falls back on global settings.
  31. *
  32. * @param AdminInterface $admin the current admin object
  33. *
  34. * @return string[] an array of formats
  35. */
  36. public function getAvailableFormats(AdminInterface $admin)
  37. {
  38. $adminExportFormats = $admin->getExportFormats();
  39. // NEXT_MAJOR : compare with null
  40. if ($adminExportFormats != array('json', 'xml', 'csv', 'xls')) {
  41. return $adminExportFormats;
  42. }
  43. return $this->exporter->getAvailableFormats();
  44. }
  45. /**
  46. * Builds an export filename from the class associated with the provided admin,
  47. * the current date, and the provided format.
  48. *
  49. * @param AdminInterface $admin the current admin object
  50. * @param string $format the format of the export file
  51. */
  52. public function getExportFilename(AdminInterface $admin, $format)
  53. {
  54. $class = $admin->getClass();
  55. return sprintf(
  56. 'export_%s_%s.%s',
  57. strtolower(substr($class, strripos($class, '\\') + 1)),
  58. date('Y_m_d_H_i_s', strtotime('now')),
  59. $format
  60. );
  61. }
  62. }