You are here

function _sheetnode_ajax_load in Sheetnode 5

Same name and namespace in other branches
  1. 6 sheetnode.module \_sheetnode_ajax_load()
  2. 7.2 sheetnode.module \_sheetnode_ajax_load()
  3. 7 sheetnode.module \_sheetnode_ajax_load()
1 string reference to '_sheetnode_ajax_load'
sheetnode_menu in ./sheetnode.module
Implementation of hook_menu().

File

./sheetnode.module, line 588

Code

function _sheetnode_ajax_load($sheetname = NULL, $vid = NULL) {
  require_once drupal_get_path('module', 'sheetnode') . '/socialcalc.inc';

  // Try to find a sheetnode.
  if (empty($sheetname)) {
    $sheetname = $_REQUEST['sheetname'];
  }
  if (is_numeric($sheetname)) {
    $value = db_result(db_query("SELECT value FROM {sheetnode} WHERE nid=%d ORDER BY vid DESC LIMIT 1", intval($sheetname)));
  }
  else {
    $value = db_result(db_query("SELECT value FROM {sheetnode} s INNER JOIN {node} n ON s.nid=n.nid WHERE UCASE(n.title)='%s' ORDER BY s.vid DESC LIMIT 1", db_escape_string(strtoupper($sheetname))));
  }

  // Try to find a sheetfield.
  if (!$value && module_exists('content')) {
    if (is_numeric($sheetname)) {
      $node = node_load(intval($sheetname));
      if ($node) {
        $type = content_types($node->type);
        foreach ($type['fields'] as $field_name => $field_info) {
          if ($field_info['type'] == 'sheetfield' && isset($node->{$field_name})) {
            $value = $node->{$field_name}[0]['value'];
          }
        }
      }
    }
  }

  // Try to find a sheetview.
  if (!$value && module_exists('views')) {

    // Try a view feed with our SocialCalc output plugin style
    $view = views_get_view($sheetname);
    if ($view) {
      foreach (array_keys($view->display) as $display_name) {
        $display = $view->display[$display_name];
        if (isset($display->display_options['style_plugin']) && $display->display_options['style_plugin'] == 'sheet_raw') {
          $value = $view
            ->render($display->id);
          header('Content-type: text/html');

          // Our style plugin sets this to text/plain
        }
      }
    }
  }

  // Found a spreadsheet: return it.
  if ($value) {
    $parts = socialcalc_parse_parts($value);
    if (isset($parts['sheet'])) {
      echo $parts['sheet'];
    }
  }
  exit;
}