<?php
// $Id$


/**
 * Implementation of hook_graphmind_plugin_info().
 *
 * @return array
 */
function graphmind_og_graphmind_plugin_info() {
  return array(
    'graphmind_og' => array(
      // Module implement this plugin
      'module' => 'graphmind_og',
      // Name of the plugin in flash (src/plugin/PLUGIN.as)
      'plugin' => 'OrganicGroupsPlugin',
      'name' => t('Graphmind organic groups'),
      'description' => t('Only load nodes from the same group.'),
    ),
  );
}


/**
 * Implementation of hook_services_resources().
 * 
 * @return array
 */
function graphmind_og_services_resources() {
  return array(
    'graphmindOG' => array(
      'getNode' => array(
        'help' => 'Load a node from the same group.',
        'access arguments' => array('access content'),
        'access callback' => 'user_access',
        'access arguments append' => FALSE,
        'callback' => 'graphmind_og_get_node',
        'args' => array(
          array(
            'name' => 'nid',
            'type' => 'int',
            'description' => 'Node ID to load',
            'optional' => FALSE,
            'source' => 'data',
          ),
          array(
            'name' => 'origin_nid',
            'type' => 'int',
            'description' => 'Group of the node',
            'optional' => FALSE,
            'source' => 'data',
          ),
        ),
      ),
    ),
  );
}


/**
 * Services callback - returns a node from a group.
 * 
 * @param type $nid
 * @param type $group_nid
 * @return type 
 */
function graphmind_og_get_node($nid, $origin_nid) {
  $node = node_load((int)$nid);
  $origin_node = node_load((int)$origin_nid);
  
  // Wrong argument
  if (!$node || !$origin_node) return FALSE;
  
  // No simple access rights
  if (!node_access('view', $node)) return FALSE;
  
  // If not in a group then return the node
  if (!isset($node->group_audience)) return $node;

  // Collect groups that can accept the new node.
  $optional_groups = array();

  // If origin node is part of a group.
  // Check if they share a common parent group.
  if (isset($origin_node->group_audience)) {
    foreach ((array)$origin_node->group_audience as $language => $group_settings) {
      foreach ((array)$group_settings as $group) {
        $optional_groups[] = $group['gid'];
      }
    }
  }

  // Check if the origin node is a group and owns the node.
  $origin_group_info = og_get_group('node', $origin_node->nid);
  if ($origin_group_info) {
    $optional_groups[] = $origin_group_info->gid;
  }

  // Membership check.
  foreach ((array)$node->group_audience as $language => $group_settings) {
    foreach ((array)$group_settings as $group) {
      if (in_array($group['gid'], $optional_groups)) {
        return $node;
      }
    }
  }

  return FALSE;
}


/**
 * Implements hook_field_attach_view_alter().
 *
 * @param array $output
 * @param array $context
 */
function graphmind_og_field_attach_view_alter(&$output, $context) {
  foreach (element_children($output) as $field_name) {
    $element = &$output[$field_name];
    if ($element['#field_type'] == 'graphmind') {
      $plugins = unserialize($element['#items'][0]['plugins']);
      // If the OrganicGroupsPlugin plugin has checked the module place javascript.
      if (isset($plugins['OrganicGroupsPlugin']) && $plugins['OrganicGroupsPlugin']) {
        list($id, $vid, $bundle) = entity_extract_ids($context['entity_type'], $context['entity']);
        drupal_add_js(array('graphmind_og' => array('base_node' => $id)), 'setting');
        $element['#attached']['js'][] = drupal_get_path('module', 'graphmind_og') . '/js/script.js';
      }
    }
  }
}


/**
 * Implements hook_field_widget_form_alter().
 *
 * @param $element
 * @param $form_state
 * @param $context
 */
function graphmind_og_field_widget_form_alter(&$element, &$form_state, $context) {
  // If we are creating a node via the flash interface, the created node Groups Audience
  // field will be prefield.
  if ($context['field']['type'] == 'group' && isset($form_state['node']->base_group)) {
    $element['#default_value'][] = $form_state['node']->base_group;
  }
}
