<?php
function _digett_block_html_pod_configure($block=array()){
  $form['digett_block_html_pod_title'] = array(
    '#type' => 'textfield', 
    '#title' => t('Block Title'), 
    '#default_value' => digett_block_variable_get($block, 'html_pod_title', ''),
  );
  $form['digett_block_html_pod_nid'] = array(
    '#type' => 'textfield', 
    '#title' => t('Node ID'), 
    '#description' => t('The node that contains the html'),
    '#default_value' => digett_block_variable_get($block, 'html_pod_nid', '0'),
  );
  $form['digett_block_html_pod_selectable'] = array(
    '#type' => 'select', 
    '#title' => t('User Selectable'), 
    '#description' => t('Should the user be able to select a node from the block itself?'),
    '#default_value' => digett_block_variable_get($block, 'html_pod_selectable', '0'),
    '#options' => array(0=>'No',1=>'Yes'),
  );
  $form['digett_block_html_pod_teaser'] = array(
    '#type' => 'select', 
    '#title' => t('Node Body'), 
    '#default_value' => digett_block_variable_get($block, 'html_pod_teaser', 'body'),
    '#options' => array('body'=>'Full Body','teaser'=>'Teaser'),
  );
  
  return $form;
}

function _digett_block_html_pod_view($block=array()){
  $node = node_load(digett_block_variable_get($block,'html_pod_nid', '0'));
  $title = trim(digett_block_variable_get($block,'html_pod_title', ''));
  if($title == '<node>'){
    $title = $node->title;
  }
  // unpublished node
  if($node->status == 0 && !user_access('administer content')){
    return array();
  }

  $return['subject'] = $title;
  $return['content'] = theme('html_pod', $node, (digett_block_variable_get($block,'html_pod_teaser', 'body') == 'teaser'));
  $return['module'] = 'digett-html-pod';
  $return['node'] = $node;

  if(user_access('administer blocks') && digett_block_variable_get($block,'html_pod_selectable', 0) == 1){
    $form = _digett_block_html_pod_configure($block);
    foreach($form as $key=>$field){
      $form[$key]['#type'] = 'hidden';
      $form[$key]['#value'] = $form[$key]['#default_value'];
    } 
    $form['delta'] = array(
      '#type' => 'hidden',
      '#value' => $block['delta'],
    );
    $form['destination'] = array(
      '#type' => 'hidden',
      '#value' => $_GET['q'],
    );
    unset($form['digett_block_html_pod_nid']['#value']);
    $form['digett_block_html_pod_nid']['#type'] = 'select';
    $form['digett_block_html_pod_nid']['#options'] = _digett_block_html_pod_get_options($block);
    
    $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
    
    $return['content'] .= drupal_get_form('digett_block_html_pod_inline_'.$block['delta'], $form, 'digett_block_html_pod_inline');
    //$return['content'] .= '<pre>'.print_r($form,true).'</pre>';
  }
  
  return $return;  
}

function digett_block_html_pod_inline_submit($form_id, $form_values){
  digett_block_save($form_values, $form_values['delta']);
  drupal_goto();
}
  
function _digett_block_html_pod_get_options($block){
  $result = db_query('SELECT nid, title FROM {node}');
  $options = array();
  while($row = db_fetch_object($result)){
    $options[$row->nid] = $row->title;
  }
  return $options;
}

function theme_html_pod($node,$teaser=false){
  // accept $node as a node object or a node id
  if(!is_object($node)){
    $node = node_load($node);
  }
  $tabs = theme('html_pod_links',$node);
  return $tabs . node_view($node,$teaser,true);
}

function theme_html_pod_links(&$node){
  if(node_access('update',$node)){
    $links[] = l('Edit',"node/{$node->nid}/edit",array(),'destination='.$_GET['q']);
  }
  if($links){
    $tabs = '<ul class="tabs primary"><li>'.theme('links',$links,'</li><li>').'</li></ul>';
  }

  return $tabs;
}