You are here

function nodejs_subscribe_nodeapi in Node.js integration 6

Implements hook_nodeapi().

File

nodejs_subscribe/nodejs_subscribe.module, line 6

Code

function nodejs_subscribe_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case 'load':
      $result = db_fetch_object(db_query('SELECT nid, subscribe FROM {nodejs_subscribe_node} WHERE nid = "%d"', $node->nid));
      $node->nodejs_subscribe = $result->subscribe;
      break;
    case 'delete':
      db_query('DELETE FROM {nodejs_subscribe_node} WHERE nid = %d', $node->nid);
      if (nodejs_subscribe_is_node_subscribed_to($node->nid)) {
        nodejs_subscribe_send_notification($node, 'deleted');
      }
      break;
    case 'insert':
      if (isset($node->nodejs_subscribe)) {
        if ($node->nodejs_subscribe['nodejs_subscriptions_allowed']) {
          db_query('INSERT INTO {nodejs_subscribe_node} (nid, subscribe) VALUES (%d, %d)', $node->nid, 1);
        }
      }
      break;
    case 'update':
      global $user;
      if (isset($node->nodejs_subscribe)) {
        if ($node->nodejs_subscribe['nodejs_subscriptions_allowed']) {
          $transaction = db_transaction();
          try {
            db_query('DELETE FROM {nodejs_subscribe_node} WHERE nid = %d', $node->nid);
            db_query('INSERT INTO {nodejs_subscribe_node} (nid, subscribe) VALUES (%d, %d)', $node->nid, $node->nodejs_subscribe['nodejs_subscriptions_allowed']);
          } catch (Exception $e) {
            $transaction
              ->rollback();
            watchdog('nodejs_subscribe', t('Error processing node subscription for nid %nid', array(
              ':nid' => $node->nid,
            )));
          }
        }
        else {
          db_query('DELETE FROM {nodejs_subscribe_node} WHERE nid = %d', $node->nid);
        }
      }
      if (nodejs_subscribe_is_node_subscribed_to($node->nid)) {
        nodejs_subscribe_send_notification($node, 'update');
      }
      break;
    case 'view':
      global $user;

      //var_dump($node);

      // If node allows nodejs subscriptions and user has permission to subscribe,
      // then provide "Subscribe" or "Unsubscribe" links.
      if (isset($node->nodejs_subscribe) && $node->nodejs_subscribe && user_access('Subscribe to realtime content updates')) {
        $links = array();
        $node_title_stripped = strip_tags($node->title);
        if (nodejs_subscribe_is_user_subscribed_to_node($user->uid, $node->nid)) {
          $links['nodejs-subscribe-unsubscribe-node'] = array(
            'title' => t('Unsubscribe'),
            'href' => "nodejs/unsubscribe/{$node->nid}",
            'attributes' => array(
              'class' => array(
                'nodejs-subscribe-node-link',
              ),
              'title' => t('Unsubscribe to @title', array(
                '@title' => $node_title_stripped,
              )),
            ),
          );
        }
        else {
          $links['nodejs-subscribe-subscribe-node'] = array(
            'title' => t('Subscribe'),
            'href' => "nodejs/subscribe/{$node->nid}",
            'attributes' => array(
              'class' => array(
                'nodejs-subscribe-node-link',
              ),
              'title' => t('Subscribe to @title', array(
                '@title' => $node_title_stripped,
              )),
            ),
          );
        }
        $node->content['links']['nodejs_subscribe'] = array(
          '#theme' => 'links__node__nodejs_subscribe',
          '#links' => $links,
          '#attributes' => array(
            'class' => array(
              'links',
              'inline',
            ),
          ),
        );
      }
      break;
  }
}