<?php
/**
 * @file
 * Code for the Commons Radioactivity Groups feature.
 */

include_once 'commons_radioactivity_groups.features.inc';

/**
 * Implements hook_node_insert().
 */
function commons_radioactivity_groups_node_insert($node) {
  // If the node does not have radioactivity integration then there's nothing to
  // do here.
  if (!isset($node->field_radioactivity)) {
    return;
  }

  // If there are no groups associated with this node then there's nothing to do
  // here.
  if (!isset($node->og_group_ref)) {
    return;
  }

  // If this node is a member of groups, generate incidents for each group.
  commons_radioactivity_groups_incident_groups($node, $node->field_radioactivity[LANGUAGE_NONE][0]['radioactivity_energy']);
}

/**
 * Implements hook_node_delete().
 */
function commons_radioactivity_groups_node_delete($node) {
  // If the node does not have radioactivity integration then there's nothing to
  // do here.
  if (!isset($node->field_radioactivity)) {
    return;
  }

  // If there are no groups associated with this node then there's nothing to do
  // here.
  if (!isset($node->og_group_ref)) {
    return;
  }

  // If this node is a member of groups, generate incidents for each group.
  commons_radioactivity_groups_incident_groups($node, -1 * $node->field_radioactivity[LANGUAGE_NONE][0]['radioactivity_energy']);
}

/**
 * Implements hook_node_update().
 */
function commons_radioactivity_groups_node_update($node) {
  // If the node does not have radioactivity integration then there's nothing to
  // do here.
  if (!isset($node->field_radioactivity)) {
    return;
  }

  // If there are no groups associated with this node then there's nothing to do
  // here.
  if (!isset($node->og_group_ref)) {
    return;
  }

  // If the groups associated with the node changed, generate incidents for each
  // group that was added/removed.
  if ($node->og_group_ref !== $node->original->og_group_ref) {
    commons_radioactivity_groups_process_node_group_membership_change($node);
  }
}

/**
 * Helper function to create Radioactivity incidents for groups to which a node belongs.
 *
 * @param $node
 *   The node which will have its radioactivity modified.
 * @param $value
 *   The value by which to modify the node's radioactivity.
 */
function commons_radioactivity_groups_incident_groups($node, $value) {
  $gids = array();

  if ($items = field_get_items('node', $node, 'og_group_ref')) {
    foreach ($items as $item) {
      $gids[] = $item['target_id'];
    }
  }

  if (count($gids)) {
    // @Todo: We may wish to instead simply select the types of these groups.
    $groups = node_load_multiple($gids);

    foreach ($groups as $group) {
      commons_radioactivity_incident_node($group, $value);
    }
  }
}

/**
 * When a node is moved between groups, create incidents that offset
 * the change in radioactivity for the former and newly containing groups.
 *
 * @param $node
 *   The node which will have its radioactivity modified.
 */
function commons_radioactivity_groups_process_node_group_membership_change($node) {
  $gids_original = array();
  $gids_updated = array();

  // Collect any gids from the original node.
  if (!empty($node->original->og_group_ref[LANGUAGE_NONE])) {
    foreach ($node->original->og_group_ref[LANGUAGE_NONE] as $delta => $field) {
      $gids_original[] = $field['target_id'];
    }
  }

  // Collect any gids from the updated node.
  if (!empty($node->og_group_ref[LANGUAGE_NONE])) {
    foreach ($node->og_group_ref[LANGUAGE_NONE] as $delta => $field) {
      $gids_updated[] = $field['target_id'];
    }
  }

  // Find the gids that are being removed from the node with this update.
  $gids_removed = array_diff($gids_original, $gids_updated);

  // Find the gids that are being added to the node with this update.
  $gids_added = array_diff($gids_updated, $gids_original);

  // Add radioactivity to groups that are newly associated with this node.
  if (!empty($gids_added)) {
    $groups = entity_load('node', $gids_added);

    foreach ($groups as $id => $group) {
      commons_radioactivity_incident_node($group, $node->field_radioactivity[LANGUAGE_NONE][0]['radioactivity_energy']);
    }
  }

  // Remove radioactivity from groups that are now disassociated with this node.
  if (!empty($gids_removed)) {
    $groups = entity_load('node', $gids_removed);

    foreach ($groups as $id => $group) {
      // We create an incident equal to the negative current value of this node.
      commons_radioactivity_incident_node($group, -1 * $node->field_radioactivity[LANGUAGE_NONE][0]['radioactivity_energy']);
    }
  }
}

/**
 * Implements hook_default_page_manager_pages_alter().
 */
function commons_radioactivity_groups_default_page_manager_pages_alter(&$pages) {
  // Add the 'most active groups' block to the groups directory page.
  if (isset($pages['groups_directory'])) {
    $page = $pages['groups_directory'];
    $handler = $page->default_handlers['page_groups_directory_panel_context'];
    $display = $handler->conf['display'];

    $pane = new stdClass();
    $pane->pid = 'new-ab237aa5-5ae7-8fb4-e1ce-20c776a6950b';
    $pane->panel = 'two_66_33_second';
    $pane->type = 'views_panes';
    $pane->subtype = 'commons_radioactivity_groups_most_active-panel_pane_1';
    $pane->shown = TRUE;
    $pane->access = array();
    $pane->configuration = array();
    $pane->cache = array();
    $pane->style = array(
      'settings' => NULL,
    );
    $pane->css = array();
    $pane->extras = array();
    $pane->position = 0;
    $pane->locks = array();
    $pane->uuid = 'ab237aa5-5ae7-8fb4-e1ce-20c776a6950b';
    $display->content['new-ab237aa5-5ae7-8fb4-e1ce-20c776a6950b'] = $pane;
    $display->panels['two_66_33_second'][0] = 'new-ab237aa5-5ae7-8fb4-e1ce-20c776a6950b';
  }

  // Add the 'most active groups' block to the site homepage.
  if (isset($pages['commons_home'])) {
    $page = $pages['commons_home'];
    $handler = $page->default_handlers['page_commons_home_panel_context_2'];
    $display = $handler->conf['display'];

    $pane = new stdClass();
    $pane->pid = 'new-b6e290b7-1d2b-5db4-a181-28befdcde628';
    $pane->panel = 'three_33_second';
    $pane->type = 'views_panes';
    $pane->subtype = 'commons_radioactivity_groups_most_active-panel_pane_1';
    $pane->shown = TRUE;
    $pane->access = array();
    $pane->configuration = array();
    $pane->cache = array();
    $pane->style = array(
      'settings' => NULL,
    );
    $pane->css = array();
    $pane->extras = array();
    $pane->position = 0;
    $pane->locks = array();
    $pane->uuid = 'b6e290b7-1d2b-5db4-a181-28befdcde628';
    $display->content['new-b6e290b7-1d2b-5db4-a181-28befdcde628'] = $pane;
    $display->panels['three_33_second'][0] = 'new-b6e290b7-1d2b-5db4-a181-28befdcde628';
  }
}
