<?php
// $Id$

/**
 * @file
 * TODO: Enter file description here.
 */

/**
 * Implementation of hook_block().
 */
function hcc_review_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('New Review Form');
    // OPTIONAL: Add additional block descriptions here, if required.
    return $blocks;
  }
  else if ($op == 'configure') {
    // OPTIONAL: Enter form elements to add to block configuration screen, if required.    
  }
  else if ($op == 'save') {
    // OPTIONAL: Add code to trigger when block configuration is saved, if required.
  }
  else if ($op == 'view') {
    switch ($delta) {
      case 0:
      	;;
      // OPTIONAL: Enter additional cases for each additional block, if defined.
    }
    return $block;
  }
}


/**
 * Implementation of hook_help().
 */
function hcc_review_help($section) {
  /* INFO:
   * The help hook is for displaying helpful messages at the top of pages indicated
   * by $section to further explain how they work. Adding certain "keywords" to the end of 
   * a given path (like admin/modules#description) will cause this text to display elsewhere
   * in the page as well (in this case, in the description section for the given module).
   */
  switch ($section) {
    case 'admin/help#hcc_review':
      return t('Review Node Type for HillCountryCritic.com');
    case 'admin/modules#description':
      return t('Review Node Type for HillCountryCritic.com');
    case 'node/add#hcc_review':
      return t('Review a Business');
    // OPTIONAL: Add additional cases for other paths that should display help text.
  }
}


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

  if ($may_cache) {
    $items[] = array(
      'path' => 'node/add/hcc_review',
      'title' => t('Business Review'),
      'access' => user_access('create Business Review'),
      // OPTIONAL: Fill in other properties
    );
    // OPTIONAL: Fill in additional static menu items
  }
  // OPTIONAL: Put in else statement for dynamic menu items that can't be cached.

  return $items;
}


/**
 * Implementation of hook_nodeapi().
 */
function hcc_review_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'delete revision':
      // TODO: Enter revision deletion query here, for example:
      // db_query('DELETE FROM {node_example} WHERE vid = %d', $node->vid);
      break;
  }
}


/**
 * Implementation of hook_perm().
 */
function hcc_review_perm() {
  return array('create Business Review', 'edit own Business Review');
}


/**
 * Implementation of hook_access().
 */
function hcc_review_access($op, $node) {
  global $user;

  if ($op == 'create') {
    return user_access('create Business Review');
  }

  if ($op == 'update' || $op == 'delete') {
    if (user_access('edit own Business Review') && ($user->uid == $node->uid)) {
      return TRUE;
    }
  }
}


/**
 * Implementation of hook_delete().
 */
function hcc_review_delete(&$node) {
  // TODO: Enter database deletion query here, for example:
  // db_query('DELETE FROM {node_example} WHERE nid = %d', $node->nid);
  db_query('DELETE FROM {hcc_review} WHERE nid = %d', $node->nid);
}


/**
 * Implementation of hook_form().
 */
function hcc_review_form(&$node, &$param) {

	if(! arg(3)){
		$options = array('0'=>'[ New Business ]');
		$result = db_query('SELECT nid, title FROM node WHERE type="hcc_business"');
		while($row = db_fetch_array($result)){
			$options[$row['nid']] = $row['title'];
		}
		$form['bid'] = array(
			'#type'		=> 'select',
			'#title'	=> t('Select The Business'),
			'#default_value' => $node->bid,
			'#options'	=> $options,
			'#required'	=> TRUE);
		$form['bname'] = array(
			'#type'		=>'textfield',
			'#title'	=> t('or Enter a New One'),
			'#default_value' => $node->bname,
			'#required' => FALSE );
	}else{
		$business = node_load(arg(3));
		$form['NAME'] = array(
			'#type' => 'markup',
			'#value' => '<h2 class="title">'.$business->title.'</h2>',
			'#weight' => -10);	
		$form['bid'] = array(
			'#type'		=> 'hidden',
			'#default_value' => arg(3));
	}
	
	$form['title'] = array(
		'#type'		=> 'hidden',
		'#value' => 'Business Review');		
	$form['body'] = array(
		'#type' => 'textarea',
		'#title' => t('Enter Your Review'),
		'#default_value' => $node->body,
		'#required' => TRUE );
	$options = array('1'=>'1','2'=>'2','3'=>'3','4'=>'4','5'=>'5',
					 '6'=>'6','7'=>'7','8'=>'8','9'=>'9','10'=>'10');
	$form['rating'] = array(
		'#type' => 'select',
		'#title' => t('Rate This Business'),
		'#default_value' => $node->rating,
		'#required' => TRUE,
		'#options' => $options );		

  return $form;
}


/**
 * Implementation of hook_insert().
 */
function hcc_review_insert($node) {
  // TODO: Enter database insertion query here, for example:
  // db_query("INSERT INTO {node_example} (vid, nid, color, quantity) VALUES (%d, %d, '%s', %d)", $node->vid, $node->nid, $node->color, $node->quantity);
  	db_query("INSERT INTO {hcc_review} (nid, bid, bname, rating) VALUES (%d, %d, '%s', %d)", 
  		$node->nid, $node->bid, $node->bname, $node->rating);
}


/**
 * Implementation of hook_load().
 */
function hcc_review_load($node) {
  // TODO: Obtain and return additional fields added to the node type, for example:
  // $additions = db_fetch_object(db_query('SELECT color, quantity FROM {node_example} WHERE vid = %d', $node->vid));
  // return $additions;
  $sql = 'SELECT * FROM {hcc_review} WHERE nid = %d';
  $review = db_fetch_object(db_query($sql, $node->nid));
  if($review->bid){
  	$row = db_fetch_object(db_query('SELECT title FROM {node} WHERE nid=%d',$review->bid));
	$review->bname = $row->title;
  }
  return $review;  
}


/**
 * Implementation of hook_node_info().
 */
function hcc_review_node_info() {
  return array('hcc_review' => array('name' => t('Business Review'), 'base' => 'hcc_review'));
}


/**
 * Implementation of hook_update().
 */
function hcc_review_update($node) {
  if ($node->revision) {
    hcc_review_insert($node);
  }
  else {
    // TODO: Enter database update query here, for example:
    // db_query("UPDATE {node_example} SET color = '%s', quantity = %d WHERE vid = %d", $node->color, $node->quantity, $node->vid);
  	db_query("UPDATE {hcc_review} SET bid = %d, bname = '%s', rating = %d WHERE nid = %d", 
  		$node->bid, $node->bname, $node->rating, $node->nid);  	
  }
}


/**
 * Implementation of hook_validate().
 */
function hcc_review_validate(&$node) {
  // TODO: Enter form validation code here
  	if($node->bid == 0){
  		if(! $node->bname){
  			form_set_error('bname', 'Please Select a business or enter the name of a new one.');
  		}
	}else{
		$node->bname = null;
	}
}


/**
 * Implementation of hook_view().
 */
function hcc_review_view(&$node, $teaser = FALSE, $page = FALSE) {
  // TODO: Insert additional code (call to theme functions, etc.) to execute when viewing a node, for example:
  // $node = node_prepare($node, $teaser);
  // $order_info = theme('node_example_order_info', $node);
  // $node->body .= $order_info;
  // $node->teaser .= $order_info;
  $node = node_prepare($node, $teaser);
  $review_info = theme('hcc_review_info', $node);
  $node->body = $review_info;
}

function theme_hcc_review_info($node) {
  $output = '<div class="hcc_review_info">';
  $output .= '<h2 class="title"><a href="/?q=node/'.$node->bid.'">Review of "' . $node->bname . '"</a></h2>';
  $output .= '<p>' . $node->body . '</p>';
  $output .= '<p>Rating: ' . $node->rating . '/10</p>';
  $output .= '</div>';
  return $output;
} 

function theme_hcc_review_list($nodes) {
	$title = '<h2 class="title">'.t('Reviews').'</h2>';
	foreach($nodes as $node){
		$list .= '<li class="hcc_review_list_item">';
		$list .= '<p class="hcc_review_meta">Submitted on: '. format_date($node->created) . '<br />';
		$list .= 'Rating: ' . $node->rating . '/10</p>';
		$list .= '<p class="hcc_review_body">' . $node->body . '</p>';
		$list .= '</li>';
		
		$sum += $node->rating;
		$count++;
	}
	if($list){
		$output = $title . '<b>Average Rating: ' . number_format($sum/$count, 1) . '/10</b>'
				. '<ul class="hcc_review_list">' . $list . '</ul>';
	}else{
		$output = $title . '<p>'.t('No reviews submitted yet.').'</p>';
	}

  return '<div class="hcc_review_list">'.$output.'</div>';
	
}