PodcastRorController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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', 'rassegna_stampa');
  24. $now = new \Drupal\Core\Datetime\DrupalDateTime('now');
  25. $now_formatted = $now->format(\Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
  26. $query = $baseQuery
  27. ->condition('status', '1')
  28. ->condition('type', $arg, 'IN')
  29. ->condition('field_tx_date', $now_formatted, '<')
  30. ->sort('field_tx_date', 'DESC')
  31. ->range(0, 30);
  32. // \Drupal::logger('podcast_ror')->info(var_export(\Drupal::request()->query->get('trxid'), true));
  33. //$trxid = \Drupal::request()->query->get('trxid');
  34. // TODO: se nell'url c'e' un trx=, allora bisogna
  35. // aggiungere che arg=news_trasmissioni
  36. // fare join con node__field_trasmissione ON // node.nid = node__field_trasmissione.entity_id
  37. // condition: node__field_trasmissione.field_trasmissione_target_id=$trx
  38. $nids = $query->execute();
  39. // TODO: load contenuti_audio referenced entities
  40. $data = array();
  41. $nodes_e = \Drupal\node\Entity\Node::loadMultiple($nids);
  42. $data['nodes'] = array();
  43. foreach($nodes_e as $nid => $node) {
  44. $data['nodes'][$nid] = $node->toArray();
  45. if( count($data['nodes'][$nid]['body']) > 0 ) {
  46. $data['nodes'][$nid]['summary'] = htmlspecialchars(substr(
  47. html_entity_decode(strip_tags($data['nodes'][$nid]['body'][0]['value'])),
  48. 0, 3500), ENT_XML1, 'UTF-8');
  49. } else {
  50. $data['nodes'][$nid]['summary'] = "";
  51. }
  52. }
  53. $first = true;
  54. foreach($nodes_e as $nid => $node) {
  55. $data['nodes'][$nid]['audio'] = array();
  56. $data['nodes'][$nid]['img'] = array();
  57. foreach( $node->get('field_contenuti_audio')->referencedEntities() as $ai => $entity) {
  58. $data['nodes'][$nid]['audio'][$ai] = $entity->toArray();
  59. //$data['nodes'][$nid]['audio'][$ai]['text'] = print_r($data['nodes'][$nid]['audio'][$ai]['field_audio_link'], true);
  60. }
  61. if($node->hasField('field_image')) {
  62. $node_images = $node->get('field_image');
  63. foreach( $node_images->referencedEntities() as $ai => $entity) {
  64. $arr = $entity->toArray();
  65. $data['nodes'][$nid]['img'][] = array(
  66. 'uri' => file_create_url($arr['uri'][0]['value']),
  67. // 'raw' => $arr,
  68. );
  69. }
  70. } else { // alcuni content_type non hanno immagine (rassegna stampa)
  71. $data['nodes'][$nid]['img'][] = [];
  72. }
  73. $data['nodes'][$nid]['tx'] = "";
  74. if($node->getType() == 'news_trasmissioni') {
  75. $tx = \Drupal\taxonomy\Entity\Term::load(
  76. $node->get('field_trasmissione')->target_id
  77. );
  78. if($tx !== null) {
  79. $data['nodes'][$nid]['tx'] = $tx->getName();
  80. }
  81. }
  82. if($first === true) {
  83. $first = false;
  84. }
  85. }
  86. return $data;
  87. }
  88. public function podcast($query, $title) {
  89. // TODO: add $title argument to the function
  90. $template = $this->twig->loadTemplate(drupal_get_path('module', 'podcast_ror') . '/templates/podcast.html.twig');
  91. $tmpl_data = [ 'ror' => array('podcast' => $this->query($query),
  92. 'url' => \Drupal::service('path.current')->getPath(),
  93. 'title' => $title,
  94. )];
  95. //$variables['ror']['url'] =
  96. $xml = $template->render($tmpl_data);
  97. $resp = new Response($xml, 200, array( 'Content-Type' => 'application/rss+xml'));
  98. return $resp;
  99. }
  100. public function podcastAll() {
  101. $query = \Drupal::entityQuery('node');
  102. return $this->podcast($query, 'Tutti gli aggiornamenti, i redazionali, le trasmissioni della radio di chi se la sente');
  103. }
  104. public function podcastByType($contentType) {
  105. $query = \Drupal::entityQuery('node')
  106. ->condition('type', $contentType);
  107. // TODO: get "nice name" for $contentType
  108. return $this->podcast($query, $contentType);
  109. }
  110. public function podcastByTrxID($trxID) {
  111. $query = \Drupal::entityQuery('node')
  112. ->condition('type', 'news_trasmissioni')
  113. ->condition('field_trasmissione', $trxID);
  114. // TODO: discover Trx name, and pass it as podcast title
  115. return $this->podcast($query, 'Trasmissione ' . $trxID);
  116. }
  117. }