<?php
/**
 * Implementation of hook_help().
 */
function s5slideshow_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Allows users to submit stories, articles or similar content.');
  }
}

/**
 * Implementation of hook_node_info().
 */
function s5slideshow_node_info() {
  return array(
    's5slideshow' => array(
      'name' => t('s5 slide show'), 
      'base' => 's5slideshow'),
    's5slide' => array(
      'name' => t('s5 slide'), 
      'base' => 's5slide'),      
  );
}

/**
 * Implementation of hook_menu().
 */
function s5slideshow_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array(
      'path' => 'node/add/s5slideshow', 
      'title' => t('s5 slideshow'),
      'access' => user_access('administer nodes')
    );
    $items[] = array(
      'path' => 'node/add/s5slide', 
      'title' => t('s5 slide'),
      'access' => user_access('administer nodes'),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 's5slideshow',
      'title' => t('s5slideshow'),
      'access' => user_access('access content'),
      'callback' => 's5slideshow_page',
      'type' => MENU_CALLBACK,
    );
  }

  return $items;
}

function s5slideshow_link($type, $node = NULL, $teaser = FALSE) {
  $links = array();

  if ($type == 'node' && $node->type == 's5slideshow') {
    $links[] = l('View slideshow', 's5slideshow/'.$node->nid);
    $links[] = l('Add a slide', 'node/add/s5slide/'.$node->nid);
  }
  
  if($type == 'node' && $node->type == 's5slide') {
    $links[] = l('Back to slideshow', 'node/'.$node->slideshow);
  }

  return $links;
}
 
function s5slideshow_view(&$node, $teaser = FALSE, $page = FALSE) {
  $node = node_prepare($node, $teaser);
  $headers = array('weight','title','operations');
  foreach($node->slides as $slide){
    $row = array();
    $row[] = $slide->weight;
    $row[] = $slide->title;
    $row[] = l('Edit', 'node/'.$slide->nid.'/edit');
    $rows[] = $row;
  }
  $node->body .= theme('table',$headers, $rows);
} 

function s5slide_form(&$node) {
  if(!$node->slideshow && !arg(3)){
    drupal_set_message('You must create a slideshow before creating slides.');
    return drupal_goto('node/add/s5slideshow');
  }
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => -5
  );
  $form['body_filter']['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Slide Content'),
    '#default_value' => $node->body,
    '#required' => FALSE
  );
  $form['body_filter']['filter'] = filter_form($node->format);
  $form['handout_filter']['handout'] = array(
    '#type' => 'textarea',
    '#title' => t('Handout Content'),
    '#default_value' => $node->handout,
    '#required' => FALSE
  );
  $form['handout_filter']['handout_format'] = filter_form($node->handout_format,null,array('handout_filter'));
  $form['weight'] = array(
    '#type' => 'weight',
    '#title' => t('Weight'),
    '#default_value' => $node->weight,
    '#required' => TRUE,
    '#delta' => 10,
  );
  $form['slideshow'] = array(
    '#type' => 'value',
    '#value' => ($node->slideshow?$node->slideshow:arg(3)),
  );

  return $form;
} 

function s5slide_insert($node) {
  db_query("INSERT INTO {s5slide} (nid, handout, handout_format, slideshow, weight) VALUES (%d, '%s', %d, %d, %d)", 
    $node->nid, $node->handout, $node->handout_format, $node->slideshow, $node->weight);
}

function s5slide_update($node) {
  db_query("UPDATE {s5slide} SET handout = '%s', handout_format = %d, slideshow = %d, weight = %d WHERE nid = %d", 
    $node->handout, $node->handout_format, $node->slideshow, $node->weight, $node->nid);
}

function s5slide_delete($node) {
  db_query('DELETE FROM {s5slide} WHERE nid = %d', $node->nid);
}

/**
 * Implementation of hook_load().
 *
 * Now that we've defined how to manage the node data in the database, we
 * need to tell Drupal how to get the node back out. This hook is called
 * every time a node is loaded, and allows us to do some loading of our own.
 */
function s5slide_load($node) {
  $additions = db_fetch_object(db_query('SELECT * FROM {s5slide} WHERE nid = %d', $node->nid));
  return $additions;
}

function s5slideshow_form(&$node) {
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => -5
  );
  $form['body_filter']['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Body'),
    '#default_value' => $node->body,
    '#required' => FALSE
  );
  $form['body_filter']['filter'] = filter_form($node->format);
  $form['theme'] = array(
    '#type' => 'textfield', 
    '#title' => t('Theme Path'), 
    '#default_value' => $node->theme ? $node->theme : '%h%m/ui',
    '#description' => t('Enter the path to the theme directory (example: "/themes/mytheme").<br />Macros:<ul><li>%h (host name: http://www.example.com/)</li><li>%t (path to theme: theme/yourtheme/)</li><li>%m (path to s5slideshow module)</li></ul>')        
  );
  $form['subtitle'] = array(
    '#type' => 'textfield',
    '#title' => t('Subtitle'),
    '#required' => FALSE,
    '#default_value' => $node->subtitle,
  );
  $form['author_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Author'),
    '#required' => FALSE,
    '#default_value' => $node->author_name,
  );
  $form['author_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Authors URL'),
    '#required' => FALSE,
    '#default_value' => $node->author_url,
  );

  return $form;
} 

function s5slideshow_insert($node) {
  db_query("INSERT INTO {s5slideshow} (nid, theme, subtitle, author_name, author_url) VALUES (%d, '%s', '%s', '%s', '%s')", 
    $node->nid, $node->theme, $node->subtitle, $node->author_name, $node->author_url);
}

function s5slideshow_update($node) {
  db_query("UPDATE {s5slideshow} SET theme = '%s', subtitle='%s', author_name='%s', author_url='%s' WHERE nid = %d", 
    $node->theme, $node->subtitle, $node->author_name, $node->author_url, $node->nid);
}

function s5slideshow_delete($node) {
  db_query('DELETE FROM {s5slideshow} WHERE nid = %d', $node->nid);
}

function s5slideshow_load($node) {
  $additions = db_fetch_object(db_query('SELECT * FROM {s5slideshow} WHERE nid = %d', $node->nid));
  $result = s5slideshow_select_slides($node->nid);
  while($slide = db_fetch_object($result)){
    $additions->slides[] = node_load($slide->nid);
  }
    
  return $additions;
}

function s5slideshow_page(){
  $slideshow = node_load(arg(1));
  print theme('s5slideshow',$slideshow);
  exit;
}

function s5slideshow_select_slides($pid){
//  return taxonomy_select_nodes(array($pid));
  $sql = 'SELECT * FROM {node} n INNER JOIN {s5slide} s on n.nid=s.nid WHERE slideshow = %d';
  return db_query($sql,$pid);
}  

function _s5slideshow_get_vid() {
  $vid = variable_get('s5slideshow_vocabulary', '');
  if (empty($vid)) {
    // Check to see if an image gallery vocabulary exists
    $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module='s5slideshow'"));
    if (!$vid) {
      $vocabulary = array(
        'name' => t('S5 Slideshows'), 
        'multiple' => '0', 
        'required' => '1', 
        'hierarchy' => '1', 
        'relations' => '0', 
        'module' => 's5slideshow', 
        'nodes' => array('s5slideshow' => 1)
      );
      taxonomy_save_vocabulary($vocabulary);
      $vid = $vocabulary['vid'];
    }
    variable_set('s5slideshow_vocabulary', $vid);
  }

  return $vid;
}

function theme_s5slideshow($slideshow){
  $theme = str_replace(array('%h', '%t', '%m'), 
    array(base_path(), path_to_theme(), drupal_get_path('module','s5slideshow')),
    $slideshow->theme);
  $author = theme('username',user_load(array('uid'=>$node->uid)));
  $site = url();
  $output = <<< OUTPUT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{$slideshow->title}</title>
<meta name="version" content="S5 1.0" />
<link rel="stylesheet" href="{$theme}/slides.css" type="text/css"
   media="projection" id="slideProj" />
<link rel="stylesheet" href="{$theme}/outline.css" type="text/css" 
  media="screen" id="outlineStyle" />
<link rel="stylesheet" href="{$theme}/opera.css" type="text/css"
   media="projection" id="operaFix" />
<link rel="stylesheet" href="{$theme}/print.css" type="text/css"
   media="print" id="slidePrint" />
<script src="{$theme}/slides.js" type="text/javascript"></script>
</head>
<body>

<div class="layout">
  <div id="controls"><!-- DO NOT EDIT --></div>
  <div id="currentSlide"><!-- DO NOT EDIT --></div>
  <div id="header"></div>
  
  <div id="footer">
  <h1>{$slideshow->title}</h1>
  <h2>Your computer &#8226; Today's date</h2>
  </div>
</div>

<div class="presentation">
  <div class="slide">
    <h1>{$slideshow->title}</h1>
    <h2>{$slideshow->subtitle}</h2>
    <h3>{$slideshow->author_name}</h3>
    <h4>{$slideshow->author_url}</h4>
  </div>
OUTPUT;

  foreach($slideshow->slides as $slide){
    $output .= '<div class="slide">'
            .  '<h1>'.$slide->title.'</h1>'
            .  $slide->body
            .  '</div>';
  }

  $output .= '</div></body></html>';
  return $output;
}
