PodcastRorController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Drupal\podcast_ror\Controller;
  3. use Drupal\Core\Controller\ControllerBase;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. use Drupal\Core\Template\TwigEnvironment;
  7. use Drupal\node\NodeInterface;
  8. class PodcastRorController extends ControllerBase {
  9. protected $state;
  10. protected $twig;
  11. public function __construct($state, TwigEnvironment $twig) {
  12. $this->state = $state;
  13. $this->twig = $twig;
  14. }
  15. public static function create(ContainerInterface $container) {
  16. return new static(
  17. $container->get('state'),
  18. $container->get('twig')
  19. );
  20. }
  21. private function query($baseQuery) {
  22. //TODO: estrai argomento dall'url
  23. $arg = array('ror_news', 'redazionali', 'news_trasmissioni');
  24. $query = $baseQuery
  25. ->condition('status', '1')
  26. ->condition('type', $arg, 'IN')
  27. ->sort('nid', 'DESC')
  28. ->range(0, 20);
  29. \Drupal::logger('podcast_ror')->info(var_export(\Drupal::request()->query->get('trxid'), true));
  30. //$trxid = \Drupal::request()->query->get('trxid');
  31. // TODO: se nell'url c'e' un trx=, allora bisogna
  32. // aggiungere che arg=news_trasmissioni
  33. // fare join con node__field_trasmissione ON // node.nid = node__field_trasmissione.entity_id
  34. // condition: node__field_trasmissione.field_trasmissione_target_id=$trx
  35. $nids = $query->execute();
  36. // TODO: load contenuti_audio referenced entities
  37. $data = array();
  38. $nodes_e = \Drupal\node\Entity\Node::loadMultiple($nids);
  39. $data['nodes'] = array();
  40. foreach($nodes_e as $nid => $node) {
  41. $data['nodes'][$nid] = $node->toArray();
  42. $data['nodes'][$nid]['summary'] = htmlspecialchars(substr(
  43. html_entity_decode(strip_tags($data['nodes'][$nid]['body'][0]['value'])),
  44. 0, 3500), ENT_XML1, 'UTF-8');
  45. }
  46. $first = true;
  47. foreach($nodes_e as $nid => $node) {
  48. $data['nodes'][$nid]['audio'] = array();
  49. $data['nodes'][$nid]['img'] = array();
  50. foreach( $node->get('field_contenuti_audio')->referencedEntities() as $ai => $entity) {
  51. $data['nodes'][$nid]['audio'][$ai] = $entity->toArray();
  52. //$data['nodes'][$nid]['audio'][$ai]['text'] = print_r($data['nodes'][$nid]['audio'][$ai]['field_audio_link'], true);
  53. }
  54. foreach( $node->get('field_image')->referencedEntities() as $ai => $entity) {
  55. $arr = $entity->toArray();
  56. $data['nodes'][$nid]['img'][] = array(
  57. 'uri' => file_create_url($arr['uri'][0]['value']),
  58. // 'raw' => $arr,
  59. );
  60. }
  61. if($first === true) {
  62. // print_r($data['nodes'][$nid]['img']);
  63. $first = false;
  64. }
  65. }
  66. return $data;
  67. }
  68. public function podcast($query) {
  69. $template = $this->twig->loadTemplate(drupal_get_path('module', 'podcast_ror') . '/templates/podcast.html.twig');
  70. $tmpl_data = [ 'ror' => array('podcast' => $this->query($query),
  71. 'url' => \Drupal::service('path.current')->getPath(),
  72. )];
  73. //$variables['ror']['url'] =
  74. $xml = $template->render($tmpl_data);
  75. $resp = new Response($xml, 200, array( 'Content-Type' => 'application/rss+xml'));
  76. return $resp;
  77. }
  78. public function podcastAll() {
  79. $query = \Drupal::entityQuery('node');
  80. return $this->podcast($query);
  81. }
  82. //public function podcastByType(NodeInterface $ctype) {
  83. public function podcastByType($contentType) {
  84. $query = \Drupal::entityQuery('node')
  85. ->condition('type', $contentType);
  86. return $this->podcast($query);
  87. }
  88. public function podcastByTrxID($trxID) {
  89. $query = \Drupal::entityQuery('node')
  90. ->condition('type', 'news_trasmissioni')
  91. ->condition('field_trasmissione', $trxID);
  92. return $this->podcast($query);
  93. }
  94. }