### Eclipse Workspace Patch 1.0
#P leaguesite
Index: leaguesite_match/leaguesite_match.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/leaguesite/leaguesite_match/leaguesite_match.module,v
retrieving revision 1.35
diff -u -r1.35 leaguesite_match.module
--- leaguesite_match/leaguesite_match.module	30 Aug 2010 22:15:17 -0000	1.35
+++ leaguesite_match/leaguesite_match.module	2 Sep 2010 17:02:27 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: leaguesite_match.module,v 1.35 2010/08/30 22:15:17 alun Exp $
+// $Id: leaguesite_match.module,v 1.22 2010/05/09 21:08:45 alun Exp $
 
 module_load_include('inc', 'date', 'date_api_elements');
 
@@ -186,8 +186,8 @@
  * @return unknown_type
  */
 function leaguesite_match_form(&$node, $form_state) {
-	$form['#cache'] = TRUE;
-	$form['#validate'][] = 'leaguesite_match_custom_validate';
+  $form['#cache'] = TRUE;
+  $form['#validate'][] = 'leaguesite_match_custom_validate';
   
   
   
@@ -197,7 +197,7 @@
   //this is for the first time the form is built.
   $default_value = $node->season_league;
   if(!isset($default_value)){
-  	$default_value = $first_season;
+    $default_value = $first_season;
   }
   
   $hours = array();
@@ -209,16 +209,16 @@
   }
   
   if(isset($form_state['match_selection'])){
-  	
-  	//This means the form is being resubmitted because of AJAX. We need to know the selected values.
-  	$match_selection = $form_state['match_selection'];
-  	$default_value = $match_selection['season_league'];
-  	
-  	//find out if the season relation is a friendly or not, get the teams related to it if not.
-  	$type = db_fetch_array(db_query('SELECT relation_type FROM {leaguesite_relation} WHERE relation_id = %d;', intval($default_value)));
-  	if($type['relation_type'] == 'friendly'){
-  		//get all the teams for the team list
-  	  $teams = db_query("SELECT title, nid FROM {node} WHERE type ='leaguesite_team' ORDER BY title;");
+    
+    //This means the form is being resubmitted because of AJAX. We need to know the selected values.
+    $match_selection = $form_state['match_selection'];
+    $default_value = $match_selection['season_league'];
+    
+    //find out if the season relation is a friendly or not, get the teams related to it if not.
+    $type = db_fetch_array(db_query('SELECT relation_type FROM {leaguesite_relation} WHERE relation_id = %d;', intval($default_value)));
+    if($type['relation_type'] == 'friendly'){
+      //get all the teams for the team list
+      $teams = db_query("SELECT title, nid FROM {node} WHERE type ='leaguesite_team' ORDER BY title;");
       $team_list = array();
       while($team = db_fetch_array($teams)){
         $team_list[$team['nid']] = $team['title'];
@@ -228,16 +228,16 @@
         '#type' => 'hidden',
         '#default_value' => -1,
       );
-  	}else{
-  		//get the number of weeks in the selected league.
-  		//if there are no weeks, leave the matchday selection out.
-  		$matchdays = array();
-  		$weeks = db_fetch_array(db_query('SELECT weeks FROM {leaguesite_league} LEFT JOIN {leaguesite_relation} ON {leaguesite_league}.lid = {leaguesite_relation}.lid WHERE {leaguesite_relation}.relation_id = %d', $default_value));
-  		if($weeks['weeks'] > 0){
-  		  for($count = 1; $count <= $weeks['weeks']; ++$count){
-  			  $matchdays[$count] = $count;
-  		  }
-  		  $matchday = array(
+    }else{
+      //get the number of weeks in the selected league.
+      //if there are no weeks, leave the matchday selection out.
+      $matchdays = array();
+      $weeks = db_fetch_array(db_query('SELECT weeks FROM {leaguesite_league} LEFT JOIN {leaguesite_relation} ON {leaguesite_league}.lid = {leaguesite_relation}.lid WHERE {leaguesite_relation}.relation_id = %d', $default_value));
+      if($weeks['weeks'] > 0){
+        for($count = 1; $count <= $weeks['weeks']; ++$count){
+          $matchdays[$count] = $count;
+        }
+        $matchday = array(
         '#type' => 'select',
         '#title' => t('Matchday'),
         '#required' => FALSE,
@@ -245,22 +245,22 @@
         '#weight' => 0,
         '#default_value' => $node->matchday,
       );
-  		}else{
-  			$matchday = array(
+      }else{
+        $matchday = array(
           '#type' => 'hidden',
           '#default_value' => -1,
         );
-  		}
-  		//just get the teams in the league for the team list
-  	  $teams = db_query("SELECT title, nid FROM {node} LEFT JOIN {leaguesite_standings} ON {node}.nid = {leaguesite_standings}.team_id WHERE {node}.type = 'leaguesite_team' AND {leaguesite_standings}.relation_id = %d ORDER BY title;", $default_value);
+      }
+      //just get the teams in the league for the team list
+      $teams = db_query("SELECT title, nid FROM {node} LEFT JOIN {leaguesite_standings} ON {node}.nid = {leaguesite_standings}.team_id WHERE {node}.type = 'leaguesite_team' AND {leaguesite_standings}.relation_id = %d ORDER BY title;", $default_value);
       $team_list = array();
       while($team = db_fetch_array($teams)){
         $team_list[$team['nid']] = $team['title'];
       }
-  	  if(count($team_list) == 0){
+      if(count($team_list) == 0){
         $team_list[] = 'No teams!';
       }
-  	}
+    }
   }else{
     //get the number of weeks in the selected league.
     //if there are no weeks, leave the matchday selection out.
@@ -293,10 +293,10 @@
         $team_list[$team['nid']] = $team['title'];
       }
       if(count($team_list) == 0){
-      	$team_list[] = 'No teams!';
+        $team_list[] = 'No teams!';
       }
     }else{
-    	//get all the teams for the team list
+      //get all the teams for the team list
       $teams = db_query("SELECT title, nid FROM {node} WHERE type ='leaguesite_team' ORDER BY title;");
       $team_list = array();
       while($team = db_fetch_array($teams)){
@@ -365,12 +365,12 @@
   );
   
   if(isset($node->match_time)){
-  	$current_date = date_make_date($node->match_time, 'UTC', DATE_ISO);
-  	date_timezone_set($current_date, date_default_timezone());
+    $current_date = date_make_date($node->match_time, 'UTC', DATE_ISO);
+    date_timezone_set($current_date, date_default_timezone());
     $current_date = date_format_date($current_date, 'custom', 'Y-m-d H:i');
   }else{
-  	$current_date = date_format_date(date_make_date('now', 'NULL', 'DATE_UNIX'), 'custom', 'Y-m-d');
-  	$current_date .= ' '.variable_get('leaguesite_match_start_time_hour', '14').':'.variable_get('leaguesite_match_start_time_minute', '00'); 
+    $current_date = date_format_date(date_make_date('now', 'NULL', 'DATE_UNIX'), 'custom', 'Y-m-d');
+    $current_date .= ' '.variable_get('leaguesite_match_start_time_hour', '14').':'.variable_get('leaguesite_match_start_time_minute', '00'); 
   }
   
   
@@ -422,11 +422,11 @@
     $node->away_name = $teams['away_name'];
     $node->season_league = $teams['relation_id'];
     if($teams['relation_type'] == 'friendly'){
-      $node->sport = $teams['sport'];	
+      $node->sport = $teams['sport']; 
     }else{
-    	//Get the sport of the match from its league.
-    	$node->sport = $teams['sport_id'];
-    	$node->matchday = $teams['matchday'];
+      //Get the sport of the match from its league.
+      $node->sport = $teams['sport_id'];
+      $node->matchday = $teams['matchday'];
     }
     $node->match_type = $teams['relation_type'];
     //get DateTime object
@@ -476,7 +476,7 @@
   
   db_query("INSERT INTO {leaguesite_match} (nid, home_team, away_team, match_time, home_score, away_score, is_result, relation_id, matchday) VALUES (%d, %d, %d, %d, '%s', '%s', %d, %d, %d)", intval($node->nid), intval($node->home_team), intval($node->away_team), $match_time, check_plain($node->home_score), check_plain($node->away_score), intval($is_result), intval($node->season_league), intval($node->matchday));
   if($node->op == t('Save and enter players')){
-  	drupal_goto('node/'.$node->nid.'/edit/match_players');
+    drupal_goto('node/'.$node->nid.'/edit/match_players');
   }
 }
 
@@ -485,9 +485,9 @@
  * @param Object $node
  */
 function leaguesite_match_update($node){
-	
-	if(variable_get('leaguesite_match_update_table_update', '') == TRUE && $node->match_type == 'league'){
-		$match = db_fetch_array(db_query("SELECT * FROM {leaguesite_match} WHERE nid = %d", intval($node->nid)));
+  
+  if(variable_get('leaguesite_match_update_table_update', '') == TRUE && $node->match_type == 'league'){
+    $match = db_fetch_array(db_query("SELECT * FROM {leaguesite_match} WHERE nid = %d", intval($node->nid)));
     if($node->home_score != '' && $node->away_score != ''){
       //after checking if the user wants the standings table updated, get the old score to see if it has changed
       $is_result = 1;
@@ -583,15 +583,15 @@
     // was it a win, draw or loss? figure out the standings results
     
     if($home_score > $away_score){
-    	
+      
       ++$home_standing['won'];
       ++$away_standing['lost'];
     }else if($home_score == $away_score){
-    	
+      
       ++$home_standing['drawn'];
       ++$away_standing['drawn'];
     }else{
-    	
+      
       ++$home_standing['lost'];
       ++$away_standing['won'];
     }
@@ -613,19 +613,19 @@
  * @param Array $form
  */
 function leaguesite_match_custom_validate($form, &$form_state) {
-	if($form_state['clicked_button']['#submit'][0] == 'leaguesite_get_matchdetails_submit'){
+  if($form_state['clicked_button']['#submit'][0] == 'leaguesite_get_matchdetails_submit'){
     //skip validation as we are doing an ahah callback.
-	}else{
+  }else{
     if($form_state['values']['home_team'] == $form_state['values']['away_team']){
       form_set_error('away_team', t('A team cannot play itself!'));
     }
-	  //Check that both results are entered if any are entered
+    //Check that both results are entered if any are entered
     if($node->home_score != '' && $node->away_score == ''){
       form_set_error('away_score', t('If you enter a home team score, you should enter an away team score.'));
     }else if($node->home_score == '' && $node->away_score != ''){
       form_set_error('home_score', t('If you enter an away team score, you should enter a home team score.'));
     }
-	}
+  }
 }
 
 function leaguesite_match_validate($node, $form){
@@ -656,7 +656,7 @@
  * @param Object $node
  */
 function leaguesite_match_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
-	if($op == 'presave' && $node->type == 'leaguesite_match'){
+  if($op == 'presave' && $node->type == 'leaguesite_match'){
     //Check that a team is not playing itself
     //update the extra details
     $is_result = 0;
@@ -671,24 +671,24 @@
         $node->match_type = 'friendly';
         break;
     }
-  	
+    
     //get the title. query the database to get the title of the teams.
     $team = db_fetch_array(db_query("SELECT title FROM {node} WHERE nid = %d;", $node->home_team));
     $home_team = $team['title'];
     $team = db_fetch_array(db_query("SELECT title FROM {node} WHERE nid = %d;", $node->away_team));
     $away_team = $team['title'];
     
-	  
+    
     
     switch($node->match_type){
-    	case 'league':
+      case 'league':
       $season = db_fetch_array(db_query('SELECT {leaguesite_league}.name AS league_name, {leaguesite_league}.abbreviation, {leaguesite_season}.name AS season_name FROM {leaguesite_relation} LEFT JOIN {leaguesite_league} ON {leaguesite_relation}.lid = {leaguesite_league}.lid LEFT JOIN {leaguesite_season} ON {leaguesite_relation}.sid = {leaguesite_season}.sid WHERE {leaguesite_relation}.relation_id = %d', $node->season_league));
       $search = array('[league]', '[league_abbreviation]', '[season]', '[home_team]', '[away_team]');
       $replace = array($season['league_name'], $season['abbreviation'], $season['season_name'], $home_team, $away_team);
       break;
       case 'friendly':
-    	$season = db_fetch_array(db_query('SELECT name FROM {leaguesite_season} WHERE sid = %d;', $node->season_league));
-    	$search = array('[season]', '[home_team]', '[away_team]');
+      $season = db_fetch_array(db_query('SELECT name FROM {leaguesite_season} WHERE sid = %d;', $node->season_league));
+      $search = array('[season]', '[home_team]', '[away_team]');
       $replace = array($season['name'], $home_team, $away_team);
       break;
       default:
@@ -698,7 +698,7 @@
       break;
     }
     
-	  
+    
     //If is_result is set, then the match has been submitted from the results tab.
     if($node->home_score != ''){
       
@@ -710,7 +710,7 @@
       if($node->match_type == 'league'){
         $title = str_replace($search, $replace, variable_get('leaguesite_match_league_result_name', '[home_score] [home_team] - [away_score] [away_team]'));
       }else if($node->match_type == 'friendly'){
-      	$title = str_replace($search, $replace, variable_get('leaguesite_match_friendly_result_name', '[home_score] [home_team] - [away_score] [away_team]'));
+        $title = str_replace($search, $replace, variable_get('leaguesite_match_friendly_result_name', '[home_score] [home_team] - [away_score] [away_team]'));
       }
       //$title = $home_team.' '.$node->home_score.' - '.$away_team.' '.$node->away_score;
       //the standings are updated in hook_update and hook_insert
@@ -750,45 +750,48 @@
   
   //if the scoring module is enabled and the match is a result, we should check for scoring details
   if($teaser == FALSE && ($node->is_result == TRUE) && module_exists('leaguesite_scoring') && variable_get('leaguesite_match_display_scoring', TRUE)){
-  	//get the match details and construct the scoring details
-  	$breakdown = db_query("SELECT {leaguesite_event}.*, player_table.title AS player_name, {leaguesite_method}.name AS method_name FROM {leaguesite_event} LEFT JOIN {node} player_table ON {leaguesite_event}.player = player_table.nid LEFT JOIN {leaguesite_method} ON {leaguesite_event}.pen_score_id = {leaguesite_method}.method_id WHERE {leaguesite_event}.match_id = %d AND {leaguesite_event}.event_type = 'score' ORDER BY {leaguesite_method}.points DESC, {leaguesite_event}.time ASC", $node->nid);
-  	$count = 0;
-  	$home_details = array();
-  	$away_details = array();
-  	while($row = db_fetch_array($breakdown)){
-  		++$count;
-  		if($row['team_id'] == $node->home_team){
-  		  $home_details[$row['pen_score_id']][] = array('time' => $row['time'], 'player' => $row['player_name'], 'type' => $row['method_name']);
-  		}else if($row['team_id'] == $node->away_team){
-  			$away_details[$row['pen_score_id']][] = array('time' => $row['time'], 'player' => $row['player_name'], 'type' => $row['method_name']);
-  		}
-  	}
-  	//check if we got any breakdown results. If not, we should check for breakdown details just to display the breakdown of the score.
-  	//If that is not present, we won't display anything.
-  	$home_scoring_times = '';
-  	$away_scoring_times = '';
-  	if($count == 0){
-  		$details = $node->match_attributes;
-  		$home_scoring_times = '';
-  		foreach($details['home_team'] as $score){
-  			$home_scoring_times .= $score['name'].' ('.$score['quantity'].') ';
-  		}
-  		
-  		$away_scoring_times = '';
-  		foreach($details['away_team'] as $score){
-  			$away_scoring_times .= $score['name'].' ('.$score['quantity'].') ';
-  		}
-  	}else{
-  		foreach($home_details as $id => $data){
-  			$home_scoring_times .= $data[0]['type'].' ('.count($data).'): ';
-  			foreach($data as $num => $data2){
-  				if(isset($data[$num+1])){
-  				  $home_scoring_times .= $data2['player'].' '.$data2['time'].', ';
-  				}else{
-  					$home_scoring_times .= $data2['player'].' '.$data2['time'].'. ';
-  				}
-  			}
-  		}
+    //get the match details and construct the scoring details
+    $breakdown = db_query("SELECT {leaguesite_event}.*, player_table.title AS player_name, {leaguesite_method}.name AS method_name FROM {leaguesite_event} LEFT JOIN {node} player_table ON {leaguesite_event}.player = player_table.nid LEFT JOIN {leaguesite_method} ON {leaguesite_event}.pen_score_id = {leaguesite_method}.method_id WHERE {leaguesite_event}.match_id = %d AND {leaguesite_event}.event_type = 'score' ORDER BY {leaguesite_method}.points DESC, {leaguesite_event}.time ASC", $node->nid);
+    $count = 0;
+    $home_details = array();
+    $away_details = array();
+    while($row = db_fetch_array($breakdown)){
+      ++$count;
+      if($row['team_id'] == $node->home_team){
+        $home_details[$row['pen_score_id']][] = array('time' => $row['time'], 'player' => $row['player_name'], 'type' => $row['method_name']);
+      }else if($row['team_id'] == $node->away_team){
+        $away_details[$row['pen_score_id']][] = array('time' => $row['time'], 'player' => $row['player_name'], 'type' => $row['method_name']);
+      }
+    }
+    //check if we got any breakdown results. If not, we should check for breakdown details just to display the breakdown of the score.
+    //If that is not present, we won't display anything.
+    $home_scoring_times = '';
+    $away_scoring_times = '';
+    if($count == 0){
+    if(isset($node->match_attributes)){
+        $details = $node->match_attributes;
+        $home_scoring_times = '';
+        foreach($details['home_team'] as $score){
+          $home_scoring_times .= $score['name'].' ('.$score['quantity'].') ';
+        }
+      
+      
+        $away_scoring_times = '';
+        foreach($details['away_team'] as $score){
+          $away_scoring_times .= $score['name'].' ('.$score['quantity'].') ';
+        }
+      }
+    }else{
+      foreach($home_details as $id => $data){
+        $home_scoring_times .= $data[0]['type'].' ('.count($data).'): ';
+        foreach($data as $num => $data2){
+          if(isset($data[$num+1])){
+            $home_scoring_times .= $data2['player'].' '.$data2['time'].', ';
+          }else{
+            $home_scoring_times .= $data2['player'].' '.$data2['time'].'. ';
+          }
+        }
+      }
       foreach($away_details as $id => $data){
         $away_scoring_times .= $data[0]['type'].' ('.count($data).'): ';
         foreach($data as $num => $data2){
@@ -799,12 +802,12 @@
           }
         }
       }
-  	}
+    }
     if($home_scoring_times != '' && $away_scoring_times != ''){
       $content['home']['home_team'] = array('#value' => $node->home_name.' '.$node->home_score, '#prefix' => '<div id="leaguesite_home_team_scoring_header">', '#suffix' => '</div>', '#weight' => -3);
       $content['home']['home_details'] = array('#value' => $home_scoring_times, '#prefix' => '<div id ="leaguesite_home_team_scoring_details">', '#suffix' => '</div>', '#weight' => -2);
-    	
-    	$content['away']['away_team'] = array('#value' => $node->away_name.' '.$node->away_score, '#prefix' => '<div id="leaguesite_away_team_scoring_header">', '#suffix' => '</div>', '#weight' => -2);
+      
+      $content['away']['away_team'] = array('#value' => $node->away_name.' '.$node->away_score, '#prefix' => '<div id="leaguesite_away_team_scoring_header">', '#suffix' => '</div>', '#weight' => -2);
       $content['away']['away_details'] = array('#value' => $away_scoring_times, '#prefix' => '<div id ="leaguesite_away_team_scoring_details">', '#suffix' => '</div>', '#weight' => -1);
     }
   }
@@ -894,7 +897,7 @@
  * Menu callback for AHAH additions.
  */
 function leaguesite_match_options_js() {
-	//copied from the POLL module. I need to modify this for correct AHAH Drupal implementation
+  //copied from the POLL module. I need to modify this for correct AHAH Drupal implementation
   module_load_include('inc', 'node', 'node.pages');
   $form_state = array('storage' => NULL, 'submitted' => FALSE);
   $form_build_id = $_POST['form_build_id'];
@@ -930,9 +933,9 @@
 }
 
 function theme_leaguesite_match_options($form){
-	//$trash = drupal_render($form['match_options']['get_details']);
-	drupal_add_css(drupal_get_path('module', 'leaguesite_match') .'/leaguesite_match.css', 'module', 'all', FALSE);
-	return drupal_render($form);
+  //$trash = drupal_render($form['match_options']['get_details']);
+  drupal_add_css(drupal_get_path('module', 'leaguesite_match') .'/leaguesite_match.css', 'module', 'all', FALSE);
+  return drupal_render($form);
 }
 
 /**
@@ -940,11 +943,11 @@
  * @param $relation_id - the ID of the relation being removed.
  */
 function leaguesite_match_leaguesite_relation_delete($relation_id){
-	//for the moment, we will delete all the matches in a season, however we should perhaps provide the option of keeping the matches as friendlies.
-	$matches = db_query('SELECT nid FROM {leaguesite_match} WHERE relation_id = %d', intval($relation_id));
-	while($match = db_fetch_array($matches)){
-		node_delete($match['nid']);
-	}
+  //for the moment, we will delete all the matches in a season, however we should perhaps provide the option of keeping the matches as friendlies.
+  $matches = db_query('SELECT nid FROM {leaguesite_match} WHERE relation_id = %d', intval($relation_id));
+  while($match = db_fetch_array($matches)){
+    node_delete($match['nid']);
+  }
 }
 
 /**
@@ -956,9 +959,9 @@
  *   raw name of the token and values containing its user-friendly name.
  */
 function leaguesite_match_token_list(){
-	$tokens = array();
-	$tokens['leaguesite_match']['match_date'] = t("The date of the match in the format yyyy-mm-dd.");
-	return $tokens;
+  $tokens = array();
+  $tokens['leaguesite_match']['match_date'] = t("The date of the match in the format yyyy-mm-dd.");
+  return $tokens;
 }
 
 /**
@@ -978,13 +981,13 @@
  *   $values['title-raw'] = 'My new node';
  */
 function leaguesite_match_token_values ($type, $object = NULL, $options = array()){
-	$values = array();
-	if($type == 'node' && $object->type == 'leaguesite_match'){
-		//node will have been loaded so it is the time in UTC format. We need to convert it to local time and then to a displayable format.
-		$current_date = date_make_date($object->match_time, 'UTC', DATE_ISO);
+  $values = array();
+  if($type == 'node' && $object->type == 'leaguesite_match'){
+    //node will have been loaded so it is the time in UTC format. We need to convert it to local time and then to a displayable format.
+    $current_date = date_make_date($object->match_time, 'UTC', DATE_ISO);
     date_timezone_set($current_date, date_default_timezone());
     $current_date = date_format_date($current_date, 'custom', 'Y-m-d');
-		$tokens['match_date'] = $current_date;
-	}
-	return $values;
+    $tokens['match_date'] = $current_date;
+  }
+  return $values;
 }
\ No newline at end of file
