You are here

function sheetnode_find_sheet in Sheetnode 6

Same name and namespace in other branches
  1. 7.2 sheetnode.module \sheetnode_find_sheet()
  2. 7 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 876

Code

function sheetnode_find_sheet($sheetname) {
  require_once drupal_get_path('module', 'sheetnode') . '/socialcalc.inc';
  $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_result(db_query("SELECT nid FROM {node} WHERE title = '%s' LIMIT 1", db_escape_string($sheetname)));
    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 (content_access('view', $field_info, NULL, $node) && (!$target_field || 0 === strcasecmp($target_field, $field_name) || 0 === strcasecmp($target_field, $field_info['widget']['label']) || 0 === strcasecmp($target_field, @$node->{$field_name}[$target_delta]['name'])) && !empty($node->{$field_name}[$target_delta])) {
        $value = $node->{$field_name}[$target_delta]['value'];
        $title = trim(sprintf("%s %s %s", $node->title, $field_info['widget']['label'], isset($node->{$field_name}[$target_delta]['name']) ? $node->{$field_name}[$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_set_header('Content-type: text/html; charset=utf-8');
        }
      }
    }
  }
  return array(
    $value,
    $title,
  );
}