<?php


/**
 * Minimum CTools version needed.
 */
define('FB_SOCIAL_REQUIRED_CTOOLS_API', '1.7.1');

/**
 * Implements hook_menu().
 */
function fb_social_menu() {
  $items = array();

  $items['fb_social'] = array(
    'title' => t('fb_social'),
    'page callback' => 'fb_social_fb_social',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  $items['admin/config/fb_social'] = array(
    'title' => 'Facebook social',
    'description' => 'Settings for facebook social plugins.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fb_social_admin_settings_form',
    ),
    'file' => 'fb_social.admin.inc',
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer site configuration',
    ),
  );

  return $items;
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function fb_social_fb_social() {
  dpm(fb_social_preset_load());
  return 'fb_social';
}

/**
 * load the preset
 * @param $name
 *    the preset name
 * @param $reset
 */
function fb_social_preset_load($name = NULL, $reset = FALSE) {
  ctools_include('export');
  if ($reset) {
    ctools_export_load_object_reset('fb_social_preset');
  }
  $presets = ctools_export_load_object('fb_social_preset', 'all');

  if (isset($name)) {

    return isset($presets[$name]) ? $presets[$name] : FALSE;
  }
  return $presets;
}

/**
 * filter presets based on come conditions $args
 * @see ctools_export_load_object()
 */
function fb_social_enabled_presets($type = NULL) {
  $result = array();
  foreach (fb_social_preset_load() as $preset) {
    if (empty($preset->disabled) && (empty($type) || ($preset->plugin_type == $type))) {
      $result[$preset->name] = $preset;
    }
  }
  return $result;
}


/**
 * Implements hook_block_info().
 */
function fb_social_block_info(){
  $blocks = array();
  
  // load all presets
  $presets = fb_social_enabled_presets();
  
  foreach ( $presets as $preset ) {
    // is any submodule implementing the hook_block ?
    $module = 'fb_social_' . $preset->type;
    $hook = module_hook($module, 'block');
    
    if (! $hook && $preset->settings['block']) {
      $blocks[$preset->name] = array(
          'info' => $preset->name . ' block' 
      );
    }
  }
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function fb_social_block_view($delta){
  $block['subject'] = t('');
  $preset = fb_social_preset_load($name = $delta, $reset = FALSE);
  $block['content'] = theme('fb_social_preset', array('preset' => $preset ));
  return $block;
}

// boxes
/**
 * Implements hook_boxes_plugins().
 */
function fb_social_boxes_plugins() {
  $info = array();

  $info['fbs'] = array(  //@todo it gives an erros if it's longer
    //'title' => '', // important . We dont /box-add/fb_social_boxes_default
    'handler' => array(
      'class' => 'fb_social_boxes_default',
      'file' => 'fb_social_boxes.inc',
      'path' => drupal_get_path('module', 'fb_social'),
      'parent' => 'box',
    ),
  );

  return $info;
}


/**
 * Implements hook_ctools_plugin_api().
 */
function fb_social_ctools_plugin_api($module, $api) {
  if ($module == 'fb_social' && $api == 'fb_social') {
    return array(
      'version' => 1,
    );
  }

  //boxes
  if ($module == 'boxes' && $api == 'plugins') {
    return array(
      'version' => 1,
    );
  }
}

/**
 * Implements hook_ctools_plugin_directory().
 */
function fb_social_ctools_plugin_directory($module, $type) {
  // Safety: go away if CTools is not at an appropriate version.
  if (! module_invoke('ctools', 'api_version', FB_SOCIAL_REQUIRED_CTOOLS_API)) {
    return;
  }

  if ($module == 'fb_social' && $type == 'plugins') {
    return 'plugins/fb_social';
  }
  elseif ($type == 'export_ui') {
    return 'plugins/export_ui';
  }
}


/**
 * Implements hook_theme().
 */
function fb_social_theme() {
  return array(
    'fb_social_preset' => array(
      'variables' => array(
        'preset' => NULL,
        'extra' => NULL,
      ),
    ),
    'fb_social_box' => array(
      'variables' => array(
        'box' => NULL,
      ),
    ),
  );
}

/**
 * returns the fbml content of the plugin
 */
function theme_fb_social_preset($variables) {
  $preset = $variables['preset'];
  $extra = $variables['extra'] ? $variables['extra'] : array();
  $attrs = array_filter($preset->fb_attrs);
  $attrs = array_merge($attrs, $extra);
  $attrs = drupal_attributes($attrs);

  $plugin_type = $preset->plugin_type;
  return '<div class="fb_social-' . $plugin_type . '-plugin"><fb:' . $plugin_type . ' ' . $attrs . '></fb:' . $plugin_type . '></div>';
}

/**
 * returns the fbml content of the plugin
 */
function theme_fb_social_box($variables) {
  $box = $variables['box'];
  $attrs = drupal_attributes(array_filter($box->options));
  $plugin_type = $box->fb_plugin_name;
  return '<div class="fb_social-' . $plugin_type . '-plugin"><fb:' . $plugin_type . ' ' . $attrs . '></fb:' . $plugin_type . '></div>';
}

/**
 * Implements hook_footer().
 */
function fb_social_page_alter(&$page) {
  global $language;

  $languages = _map_active_languages();

  if (fb_social_auto_language()) {
    if (array_key_exists($language->language, $languages)) {
      $fb_locale = $languages[$language->language];
    }
    else {
      drupal_set_message("There is no mapping for the current language. Using the default locale.");
    }
  }
  else {
    $fb_locale = variable_get('fb_social_locale', 'en_US');
  }

  $appid = variable_get('fb_social_appid', '');

  $output = '<div id="fb-root"></div>';
  $output .= "<script type=\"text/javascript\">
     window.fbAsyncInit = function() {
       FB.init({
         appId: " . drupal_json_encode($appid) . ",
         status: true,
         cookie: true,
         xfbml: true});

         jQuery.event.trigger('fb:init');
     };
     (function() {
       var e = document.createElement('script');
       e.async = true;
       e.src = document.location.protocol + '//connect.facebook.net/" . $fb_locale . "/all.js';
       document.getElementById('fb-root').appendChild(e);
     }());
  </script>";

  $page['page_bottom']['fb_social'] = array(
    '#markup' => $output,
  );
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function fb_social_auto_language() {
  return variable_get('fb_social_locale_auto', 0);
}

function _map_active_languages() {
  $languages = language_list();
  $mapped = array();
  foreach ( $languages as $key => $language ) {
    $mapped[$language->language] = variable_get('fb_social_language_' . $language->language, '');
  }
  return $mapped;
}


// API functions

/**
 *  Based on the user settings return the aliased / unaliased version
 *  of a given $url
 */
function fb_social_url($url = NULL) {
  $aliased = variable_get('fb_social_urls_mode', 0);
  return url($url, array(
    'absolute' => TRUE,
    'alias' => $aliased,
  ));
}

/**
 * Return the default vaules of a certain plugin
 * type as defined by facebook
 * @param $type the plugin type/name (i.e. like).
 */
function fb_social_attrs_defaults($type) {
  _fb_social_include_settings_file($type);
  $func = '_fb_social_' . $type . '_fb_defaults';
  return call_user_func($func);
}

/**
 * Returns the definition form of the fb plugin
 * @param $type the plugin type/name (i.e. like).
 */
function fb_social_attrs_form($type) {
  _fb_social_include_settings_file($type);
  $func = '_fb_social_' . $type . '_fb_attrs_form';
  return call_user_func($func);
}

/**
 * Returns the definition form of the (drupal) plugin settings
 * @param $type the plugin type/name (i.e. like).
 */
function fb_social_settings_form($type, $settings) {
  _fb_social_include_settings_file($type);
  $func = '_fb_social_' . $type . '_settings_form';
  return call_user_func($func, $settings);
}


/**
 * Include the orresponding settings.inc file
 * based on the plugin type
 */
function _fb_social_include_settings_file($type) {
  $module = 'fb_social_' . $type;
  $file = 'fb_social_' . $type . '_settings';
  module_load_include('inc', $module, $file);
}



