123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace Drupal\podcast_ror\Controller;
- use Drupal\Core\Controller\ControllerBase;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Drupal\Core\Template\TwigEnvironment;
- use Drupal\node\NodeInterface;
- class PodcastRorController extends ControllerBase {
- protected $state;
- protected $twig;
- public function __construct($state, TwigEnvironment $twig) {
- $this->state = $state;
- $this->twig = $twig;
- }
- public static function create(ContainerInterface $container) {
- return new static(
- $container->get('state'),
- $container->get('twig')
- );
- }
- private function query($baseQuery) {
- //TODO: estrai argomento dall'url
- $arg = array('ror_news', 'redazionali', 'news_trasmissioni', 'rassegna_stampa');
- $now = new \Drupal\Core\Datetime\DrupalDateTime('now');
- $now_formatted = $now->format(\Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
- $query = $baseQuery
- ->condition('status', '1')
- ->condition('type', $arg, 'IN')
- ->condition('field_tx_date', $now_formatted, '<')
- ->sort('field_tx_date', 'DESC')
- ->range(0, 30);
- // \Drupal::logger('podcast_ror')->info(var_export(\Drupal::request()->query->get('trxid'), true));
- //$trxid = \Drupal::request()->query->get('trxid');
- // TODO: se nell'url c'e' un trx=, allora bisogna
- // aggiungere che arg=news_trasmissioni
- // fare join con node__field_trasmissione ON // node.nid = node__field_trasmissione.entity_id
- // condition: node__field_trasmissione.field_trasmissione_target_id=$trx
- $nids = $query->execute();
- // TODO: load contenuti_audio referenced entities
- $data = array();
- $nodes_e = \Drupal\node\Entity\Node::loadMultiple($nids);
- $data['nodes'] = array();
- foreach($nodes_e as $nid => $node) {
- $data['nodes'][$nid] = $node->toArray();
- if( count($data['nodes'][$nid]['body']) > 0 ) {
- $data['nodes'][$nid]['summary'] = htmlspecialchars(substr(
- html_entity_decode(strip_tags($data['nodes'][$nid]['body'][0]['value'])),
- 0, 3500), ENT_XML1, 'UTF-8');
- } else {
- $data['nodes'][$nid]['summary'] = "";
- }
- }
- $first = true;
- foreach($nodes_e as $nid => $node) {
- $data['nodes'][$nid]['audio'] = array();
- $data['nodes'][$nid]['img'] = array();
- foreach( $node->get('field_contenuti_audio')->referencedEntities() as $ai => $entity) {
- $data['nodes'][$nid]['audio'][$ai] = $entity->toArray();
- //$data['nodes'][$nid]['audio'][$ai]['text'] = print_r($data['nodes'][$nid]['audio'][$ai]['field_audio_link'], true);
- }
- if($node->hasField('field_image')) {
- $node_images = $node->get('field_image');
- foreach( $node_images->referencedEntities() as $ai => $entity) {
- $arr = $entity->toArray();
- $data['nodes'][$nid]['img'][] = array(
- 'uri' => file_create_url($arr['uri'][0]['value']),
- // 'raw' => $arr,
- );
- }
- } else { // alcuni content_type non hanno immagine (rassegna stampa)
- $data['nodes'][$nid]['img'][] = [];
- }
- $data['nodes'][$nid]['tx'] = "";
- if($node->getType() == 'news_trasmissioni') {
- $tx = \Drupal\taxonomy\Entity\Term::load(
- $node->get('field_trasmissione')->target_id
- );
- if($tx !== null) {
- $data['nodes'][$nid]['tx'] = $tx->getName();
- }
- }
- if($first === true) {
- $first = false;
- }
- }
- return $data;
- }
- public function podcast($query, $description) {
- // TODO: add $title argument to the function
- $template = $this->twig->loadTemplate(drupal_get_path('module', 'podcast_ror') . '/templates/podcast.html.twig');
- $query_results = $this->query($query);
- $tmpl_data = [ 'ror' => array('podcast' => $query_results,
- 'url' => \Drupal::service('path.current')->getPath(),
- 'description' => $description,
- 'title' => 'Radio Onda Rossa',
- )];
- $headers = array(
- 'Content-Type' => 'application/rss+xml',
- 'Access-Control-Allow-Origin' => '*',
- 'Access-Control-Allow-Methods' => 'GET',
- );
- $nodes = $tmpl_data['ror']['podcast']['nodes'];
- $lastmodtime = 0;
- if( !empty($nodes) ) {
- foreach($nodes as $nid => $node) {
- $modtime = intval($node['changed'][0]['value']);
- if($modtime > $lastmodtime) {
- $lastmodtime = $modtime;
- }
- }
- }
- $xml = $template->render($tmpl_data);
- $resp = new Response($xml, 200, $headers);
- $resp->setPublic();
- $resp->setTtl(300);
- if($lastmodtime !== 0) {
- $resp->setLastModified(\DateTime::createFromFormat('U',$lastmodtime));
- }
- return $resp;
- }
- public function podcastAll() {
- $query = \Drupal::entityQuery('node');
- return $this->podcast($query, 'Tutti gli aggiornamenti, i redazionali, le trasmissioni della radio di chi se la sente');
- }
- public function podcastByType($contentType) {
- $query = \Drupal::entityQuery('node')
- ->condition('type', $contentType);
- // TODO: get "nice name" for $contentType
- return $this->podcast($query, str_replace('+', ', ', $contentType));
- }
- public function podcastByTrxID($trxID) {
- $query = \Drupal::entityQuery('node')
- ->condition('type', 'news_trasmissioni')
- ->condition('field_trasmissione', $trxID);
- // TODO: discover Trx name, and pass it as podcast title
- return $this->podcast($query, 'Trasmissione ' . $trxID);
- }
- }
|