<?php
// $Id$

/**
 * @file
 * Enables categorized real-time feeds using PubSubHubbub.
 */

/**
 * Implements hook_menu_alter().
 */
function pusher_taxonomy_menu_alter(&$items) {
  $items['taxonomy/term/%taxonomy_term/feed']['page callback'] = 'pusher_taxonomy_term_feed_page';
}

/**
 * Page callback for rendering a term feed.
 */
function pusher_taxonomy_term_feed_page($term) {
  drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
  print pusher_taxonomy_term_feed($term);
}

/**
 * Generate the real-time content feed for a taxonomy term.
 *
 * @param $term
 *   The taxonomy term.
 */
function pusher_taxonomy_term_feed($term) {
  $url = 'taxonomy/term/' . $term->tid . '/feed';
  $channel['link'] = url($url, array('absolute' => TRUE));
  $channel['title'] = variable_get('site_name', 'Drupal') . ' - ' . $term->name;
  // Only display the description if we have a single term, to avoid clutter and confusion.
  // HTML will be removed from feed description.
  $channel['description'] = check_markup($term->description, $term->format, '', TRUE);
  $nids = taxonomy_select_nodes($term->tid, FALSE, variable_get('feed_default_items', 10));
  return pusher_feed($nids, $channel, $url);
}

/**
 * Implements hook_node_insert().
 */
function pusher_taxonomy_node_insert($node) {
  if ($node->status) {
    node_load(NULL, NULL, TRUE);

    // Get all tid for node.
    $query = db_select('taxonomy_index', 'r');
    $query->join('taxonomy_term_data', 't', 'r.tid = t.tid');
    $query->condition('r.nid', $node->nid);
    $query->fields('r', array('tid'));
    $tids = $query->execute()->fetchCol(0);

    // Invoke push_hub_notify for each pusher term feed.
    $changed = pusher_feed();
    foreach ($tids as $tid) {
      pusher_notify(array($node->nid), array(), "taxonomy/term/" . $tid . "/feed");
    }
    //pusher_execute_hub_queue();
  }
}

/**
 * Implements hook_node_update().
 */
function pusher_taxonomy_node_update($node) {
  pusher_taxonomy_node_insert($node);
}


//.. TODO: save hooks?
/*

  module_invoke_all('taxonomy_vocabulary_presave', $vocabulary);
  module_invoke_all('entity_presave', $vocabulary, 'taxonomy_vocabulary');
  module_invoke_all('taxonomy_vocabulary_delete', $vocabulary);
  module_invoke_all('entity_delete', $vocabulary, 'taxonomy_vocabulary');
  taxonomy_taxonomy_vocabulary_update($vocabulary)

  module_invoke_all('taxonomy_term_presave', $term);
  module_invoke_all('entity_presave', $term, 'taxonomy_term');

  module_invoke_all("taxonomy_term_$op", $term);
  module_invoke_all("entity_$op", $term, 'taxonomy_term');
  module_invoke_all('taxonomy_term_delete', $term);
  module_invoke_all('entity_delete', $term, 'taxonomy_term');
*/
/*
 * Implements hook_field_insert().
 *
function pusher_taxonomy_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  //TODO insert?
}

/**
 * Implements hook_field_update().
 *
function pusher_taxonomy_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  //TODO PUSH updates?
}

/**
 * Implements hook_node_delete().
 *
function pusher_taxonomy_node_delete($node) {
  //TODO: PUSH delete node?
}

/**
 * Implements hook_taxonomy_term_delete().
 *
function pusher_taxonomy_taxonomy_term_delete($term) {
  //TODO: PUSH delete feed/subscriptions?
}
*/
