<?php

require_once('scripts/sb_functions.php');
if (!signed_in()){
  global $logged_in;
  $logged_in = logged_in( true, true );
}
else{
    global $blog_permissions;
    if (! $blog_permissions['move']==1 ){ 
	redirect_to_url('index.php');
    }
}

//global $logged_in;
//$logged_in = logged_in( true, true );
	
read_config();
sb_language( 'events' );

html_header('',false);



		
function page_content() {

    global $lang_string;

  if (isset($_REQUEST['m'])){$m=$_REQUEST['m'];}
  else{$m=date('m');}
  if (isset($_REQUEST['y'])){$y=$_REQUEST['y'];}
  else{$y=date('y');}

  // this is the entry-array

  $entry_array = array();

  // make the subject line

  $table = '<table width="100%"><tr><td>';
	    
  $linkM = $m-1;
  $linkY = $y;
  if ($linkM < 1){
    $linkM = 12;
    $linkY -= 1;
    if ($linkY < 10){ $linkY = '0'.$linkY; }
  }
  if ($linkM < 10){ $linkM = '0'.$linkM; }
	    
  $table .= '<a href="?y='.$linkY.'&m='.$linkM.'">';
  $table .= '&lt;&lt; / 20'.$linkY .' / '.$linkM.' / ';
  $table .= '</td><td> '.$lang_string['title'].' ( 20'.$y.' / '.$m.' / )';
  $table .= '</td><td>';
  
  $linkM = $m+1;
  $linkY = $y;
  if ($linkM > 12){
    $linkM = 1;
    $linkY += 1;
    if ($linkY < 10){ $linkY = '0'.$linkY; }
  }
  if ($linkM < 10){ $linkM = '0'.$linkM; }

  $table .= '<a href="?y='.$linkY.'&m='.$linkM.'">';
  $table .= ' / 20'.$linkY .' / '.$linkM.' / &gt;&gt;';
  $table .= '</td></tr></table>';

  $entry_array['subject'] = $table;


  /////////////////////////////////////////////////////////////
  // change dates and the repeat settings
  /////////////////////////////////////////////////////////////

  if (isset($_REQUEST['change'])){
    $entry=$_POST['entry'];
    $year=$_POST['ny'];
    $month=$_POST['nm'];
    $day=$_POST['nd'];
    $rep=$_POST['repeat'];

    // move the file )only of necessary)

    $file = move_entry($entry,$year,$month,$day);

    // update the repeat_events file
    
    update_repeat_event($entry,$file,$rep);

    unset($_REQUEST['entry']);

  }

  /////////////////////////////////////////////////////////////


  $entries = array();
  get_blog_entries($entries,$y,$m);
  get_recurrent_events($entries);


  $entry_array['entry'] .= 
    '<form action="events.php" method="post" name="dateform">';
  $entry_array['entry'].= $lang_string['instructions'];
  //$entry_array['entry'].=
  //'The event will not be repeated anymore if you select "--" for repeat!<br>';
  $entry_array['entry'].='<br><p>';


  //////////////////////////////////////////////////////////////////
  // display the blog entries and events
  //////////////////////////////////////////////////////////////////

  if ( count($entries) ){

    $table = '<table border="0" align="center" width="100%">';
    $table .= '<tr><th>year</th>';
    $table .= '<th>'.$lang_string['month'].'</th>';
    $table .= '<th>'.$lang_string['day'].'</th>';
    // $table .= '<th>blog entry</th>';
    $table .= '<th>'.$lang_string['subject'].'</th>';
    $table .= '<th>'.$lang_string['repeat'].'</th></tr>';
  

    krsort ($entries);
    foreach ($entries as $filename => $repeat ){

      list($dir,$year,$month,$file) = explode('/',$filename);
      $day = substr($file,9,2);
      $path = 'content/' . $year . '/' . $month . '/' . $file;
      $entry_data = blog_entry_to_array( $path );
      $subject = blog_to_html( $entry_data["SUBJECT"], false, false );
      $entry = sb_strip_extension( $file );

      $entry_link = 'preview_cgi.php?y='.$year.'&m='.$month.'&entry='.$entry;
      //$edit_link = '?y='.$year.'&m='.$month.'&entry='.$file;
      $edit_link = '?y='.$year.'&m='.$month.'&entry='.$entry;

      $table .= '<tr>';
    



      ////////////////////////////////////////////////////////////////////////
      // if there is a selected entry --> display a form for chenging dates

      if (isset($_REQUEST['entry']) && ($_REQUEST['entry']==$entry)){
	       
	$table .= '<td>';
	$table .= '<input type="hidden" name="y" value="'.$_REQUEST['y'].'">';
	$table .= '<input type="hidden" name="m" value="'.$_REQUEST['m'].'">';
	$table .= 
	  '<input type="hidden" name="entry" value="'.$_REQUEST['entry'].'">';

	// a new year

	$table .=  '<select name="ny">';
	for ( $j=0; $j<=20; $j++ ) {
	  $y = $j;
	  if ($y < 10){$y = '0'.$j;}
	  if ($y == $year){
	    $table .= '<option selected label="20'.$y.'" value="'.$y.'">20'.$y.'</option>';
	  }
	  else{
	    $table.='<option label="20'.$y.'" value="'.$y.'">20'.$y.'</option>';
	  }
	}
	$table .=  '</select></td>';
      

	// a new month

	$table .=  '<td><select name="nm">';
	for ( $j=1; $j<=12; $j++ ) {
	  $m = $j;
	  if ($m < 10){$m = '0'.$j;}
	  if ($m == $month){
	    $table .= '<option selected label="'.$m.'" value="'.$m.'">'.$m.'</option>';
	  }
	  else{
	    $table .=  '<option label="'.$m.'" value="'.$m.'">'.$m.'</option>';
	  }
	}
	$table .=  '</select></td>';
		
	// a new day
		
	$table .=  '<td><select name="nd">';
	for ( $j=1; $j<=31; $j++ ) {
	  $d = $j;
	  if ($d < 10){$d = '0'.$j;}
	  if ($d == $day){
	    $table .=
	      '<option selected label="'.$d.'" value="'.$d.'">'.$d.'</option>';
		  }
	  else{
	    $table .= '<option label="'.$d.'" value="'.$d.'">'.$d.'</option>';
	  }
	}
	$table .=  '</select>';
      
	// $table .= '<td>'.$entry.'</td>';
	$table .= '<td><a href="'.$entry_link.'">'.$subject.'</a></td>';


	// repeat event selection box

	$selected = '--';
	if ($repeat != NULL){
	  $selected=$repeat;
	}

	$table .= repeat_form($selected);
	$table.='<td><input type="submit" name="change" value="change"/></td>';

      }

      // end of cgi-form
      ////////////////////////////////////////////////////////////////
      // display a normal entry with links otherwise
      

      else{
		
	$table .= '<td><a href="'.$edit_link.'">20'.$year.'</a></td>';
	$table .= '<td><a href="'.$edit_link.'">'.$month.'</a></td>';
	$table .= '<td><a href="'.$edit_link.'">'.$day.'</a></td>';
	//$table .= '<td><a href="'.$edit_link.'">'.$entry.'</a></td>';
	$table .= '<td><a href="'.$entry_link.'">'.$subject.'</a></td>';
      

	if ($repeat != NULL){
	  if ($repeat == '1:0'){
	    $table .= '<td><a href="'.$edit_link.'">every year</a></td>';
	  }
	  elseif ($repeat == '0:1'){
	    $table .= '<td><a href="'.$edit_link.'">every month</a></td>';
	  }
	}
      }

      $table .= '</tr>';
    }
    $table .= '</table>';

    $entry_array['entry'] .= $table;
    $entry_array['entry'] .= '</form>';
    
	
  }
  
  // finally - show it
  //////////////////////////////////////////////////////////////////

  echo( theme_staticentry( $entry_array ) );

}


theme_pagelayout();
html_footer();

?>
