You are here

function sheetnode_find_sheet in Sheetnode 7

Same name and namespace in other branches
  1. 6 sheetnode.module \sheetnode_find_sheet()
  2. 7.2 sheetnode.module \sheetnode_find_sheet()

API function to return a sheet given a name.

3 calls to sheetnode_find_sheet()
_sheetnode_ajax_load in ./sheetnode.module
AJAX function to return a sheet value.
_sheetnode_phpexcel_export_reference in modules/sheetnode_phpexcel/sheetnode_phpexcel.export.inc
Helper to export referenced sheet.
_sheetnode_phpexcel_export_sheet in modules/sheetnode_phpexcel/sheetnode_phpexcel.export.inc
Helper function to export a single spreadsheet.

File

./sheetnode.module, line 959
Module file for the sheetnode module.

Code

function sheetnode_find_sheet($sheetname) {
  module_load_include('inc', 'sheetnode', 'socialcalc');
  $args = explode('/', $sheetname);
  $sheetname = array_shift($args);

  // No value found by default.
  $value = NULL;
  $title = NULL;

  // Try to find a node.
  $node = NULL;
  if (is_numeric($sheetname)) {
    $node = node_load(intval($sheetname));
  }
  else {
    $nid = db_query("SELECT nid FROM {node} WHERE title = :title LIMIT 1", array(
      ':title' => $sheetname,
    ))
      ->fetchField();
    if ($nid) {
      $node = node_load($nid);
    }
  }

  // Do we have access to this node?
  if ($node && !node_access('view', $node)) {
    return array(
      NULL,
      NULL,
    );
  }

  // Is it a sheetnode?
  if ($node && $node->type === 'sheetnode' && empty($args[0])) {
    $value = $node->sheetnode['value'];
    $title = $node->title;
  }

  // Is it a sheetfield?
  if (!$value && $node) {
    $target_field = empty($args[0]) ? FALSE : $args[0];
    $target_delta = empty($args[1]) ? 0 : intval($args[1]);
    foreach (sheetnode_get_sheetfields($node->type) as $field_name => $field_info) {

      // Check that it's the field we want:
      // * It's accessible to this user.
      // * Its label matches the input.
      // * Its delta matches the input.
      if (field_access('view', $field_info, 'node', $node) && (!$target_field || 0 === strcasecmp($target_field, $field_name) || 0 === strcasecmp($target_field, $field_info['instance']['label']) || 0 === strcasecmp($target_field, @$node->{$field_name}[LANGUAGE_NONE][$target_delta]['name'])) && !empty($node->{$field_name}[LANGUAGE_NONE][$target_delta])) {
        $value = $node->{$field_name}[LANGUAGE_NONE][$target_delta]['value'];
        $title = trim(sprintf("%s %s %s", $node->title, $field_info['instance']['label'], isset($node->{$field_name}[LANGUAGE_NONE][$target_delta]['name']) ? $node->{$field_name}[LANGUAGE_NONE][$target_delta]['name'] : ($target_delta ? strval($target_delta) : '')));
        break;
      }
    }
  }

  // Is it 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_id) {
        $display = $view->display[$display_id];
        if (isset($display->display_options['style_plugin']) && $display->display_options['style_plugin'] === 'sheet_raw' && $view
          ->access($display_id)) {
          if (!empty($args)) {
            $view
              ->set_arguments($args);
          }
          $value = $view
            ->render($display_id);
          $title = $display->handler
            ->get_option('title');

          // Our style plugin sets this to text/plain, so reset it.
          drupal_add_http_header('Content-type', 'text/html; charset=utf-8');
        }
      }
    }
  }
  return array(
    $value,
    $title,
  );
}