<?php
	include('../../includes/db.php'); 
	include('_config.php');

	$id= mysql_real_escape_string($_REQUEST[id]); 
	$mode= mysql_real_escape_string($_REQUEST[mode]); 
   
   if($mode=="pages"){   	
		$rrow = mysql_fetch_array(mysql_query("SELECT * FROM `$mode` WHERE `id`='$id'")); 
		$title = $rrow['title'];
		$sql= "SELECT * FROM `files` WHERE parent='$mode' and parent_id='$id' ORDER by `position`  ";
		$folder="/media/files/$mode/".$id."/";
		$limit=15;
   
   }
   
   if($mode=="category"){   	
		$rrow = mysql_fetch_array(mysql_query("SELECT * FROM `$mode` WHERE `id`='$id'"));
		$title = $rrow['title']; 
		$sql= "SELECT * FROM `files` WHERE parent='category' and parent_id='$id' ORDER by `position`  ";
		$folder="/media/files/category/".$id."/";
		$limit=100;
   
   }   
   
   if($mode=="szkolenia"){   	
		$rrow = mysql_fetch_array(mysql_query("SELECT * FROM `$mode` WHERE `id`='$id'"));
		$title = $rrow['title']; 
		$sql= "SELECT * FROM `files` WHERE parent='szkolenia' and parent_id='$id' ORDER by `position`  ";
		$folder="/media/files/szkolenia/".$id."/";
		$limit=1;
   
   }   
   
   
   
   if(!$mode){header("Location: $basehttp/admin/");}
?>

       <div id="right_column">
        <!-- RIGHT -->
        <div id="right_top">
        	<div id="right_home"></div>
            <div id="right_right">            
            <a href="#">Pliki</a>            
            <span><a href="#">Lista</a></span>            
            </div>
        </div>
        
        
        
        <div id="right_bg">        
      	<h3>Lista plików dla : "<? echo $title; ?>"</h3>
            
            
         <!--table-->  
         <div id="table">
          
        <table class="pagetable" width="100%" cellspacing="0" >
        <thead>
          <tr>
            <th width="2%">Lp.</th>
            <th width="72%">Nazwa</th>
            <th width="10%" style="text-align:center;"><div align="center">Plik</div></th>
            <th width="11%"  style="text-align:center;"><div align="center">Typ</div></th>
            <th colspan="2" ><div align="center">Opcje</div></th>
            </tr>
        </thead>
        <tbody>

		<?php

		$first= mysql_real_escape_string($_REQUEST[first]); 
		if (empty($first)) { $first = 0; }
		$ltmp = $settings['adminResults'];
		
		// zapytanie zlicza ile jest rekordow w bazie, potrzebne do porcjowania
		$sql2=$sql."LIMIT $first,$ltmp; "; 
		
		$wynik = mysql_query($sql2) or die('Blad zapytania: '.mysql_error()); 
		// operacje porcjowania
		$licznik=0;
		$count = mysql_num_rows(mysql_query($sql));
		$count = $count/$ltmp;
		$ile = ceil($count);
		
		if ($count==0){
		echo "
		  <tr>
			<td colspan='5'>Brak wyników.</td>
		  </tr>
		  ";
		}					
		
		while($row = mysql_fetch_array($wynik)) {

		$licznik=$licznik+1;
		$reszta = $licznik % 2;


	
		if ($reszta == 0) {?>
		<tr class="row1" id="item<? echo $row[id]; ?>" onmouseover="this.className='row1hover';" onmouseout="this.className='row1';">
		<?php } else { ?>
		<tr class="row2" id="item<? echo $row[id]; ?>" onmouseover="this.className='row2hover';" onmouseout="this.className='row2';">
		<?php } ?>
	
		<td><?php echo $licznik; ?></td>
		<td><?php if ($row['title']) { echo $row['title']; } else { echo $row['file'];} ?></td>
		<td><div align="center"><a href="<? echo $folder.$row['file']; ?>">Pobierz</a></div></td>        
        <td><div align="center"><img src="<? echo $basehttp;?>/admin/gfx/files/file-<?php echo $row['type'];?>.png" border="0" alt="<?php echo $row['type'];?>" /></div></td>
        <td width="8">
        <a class="tooltip" title="edytuj nazwę" href='<? echo $basehttp; ?>/admin/modules/files/files.list.php?mode=<? echo $mode; ?>&id=<? echo $id; ?>&edit=<?php echo $row[id]; ?>#edit'>
        <img src="<? echo $basehttp; ?>/admin/gfx/icons/page_edit.png" border="0" alt="edytuj">
        </a>
		</td>
        <td><? echo generateCMSitem('delete','files', $row['id']); ?></td>
        </tr>
		<?php }?>
        
        </tbody>
        </table>
        
         </div>
         </div>
        
        <div id="right_bottom"></div>        
        <div class="pagination">   
		<?
         $link="files.list.php?typ=".$typ."&id=".$id."&first=";
         include($basepath.'/admin/includes/pagination.php');
        
        ?>
        </div> 


        <div id="right_top2"></div>        
        <div id="right_bg">
		<? if($_REQUEST[edit]) {
		$wynikk = mysql_query("SELECT * FROM `files` WHERE id='$_REQUEST[edit]'");
		$rrow = mysql_fetch_array($wynikk); 
		if(isset($_POST['save']))	{  
			generateSql($moduleTable, $_POST, 'update', "WHERE id = $_REQUEST[edit]",$_REQUEST[edit]);			
			header("Location: $basehttp/admin/modules/files/files.list.php?mode={$mode}&id={$id}");
		}
	
		?>
      	<h3>
            <span style="float:left;">
            <img src="<? echo $basehttp; ?>/admin/gfx/icons/page_edit.png" style="vertical-align:middle; padding-right:3px;" />
            </span>
            <span style="padding-top:0px; display:block; float:left;">Edytuj</span>
        </h3>
            
		<div id="table">         
        <form name="formAdd" enctype="multipart/form-data" action="" method="post">
        <input type="hidden" name="save" value="1" />
          <table width="100%" border="0">
            <? 
            $row= mysql_fetch_array(mysql_query("SELECT * FROM `$moduleTable` WHERE id = $_REQUEST[edit]"));
            echo generateForm($formSettings, $row);
            ?>
            <tr>
                <td></td>
                <td><p><input name="save" type="image" src="<? echo $basehttp; ?>/admin/gfx/buttons/button_submit.gif" style="border:none;" /></p></td>
            </tr>  
            </table>
        </form>
        </div>
		<? } else { ?>
		
		
      	<h3>
            <span style="float:left;">
            <img src="<? echo $basehttp; ?>/admin/gfx/icons/report_disk.png" style="vertical-align:middle; padding-right:3px;" />
            </span>
            <span style="padding-top:0px; display:block; float:left;">Dodaj nowy plik</span>
        </h3>        
		<div id="table"> 
			<? if($licznik < $limit){?>
			<a href="<? echo $basehttp; ?>/admin/uploadFileForm.php?mode=<? echo $mode; ?>&id=<? echo $id; ?>"><img src="<? echo $basehttp; ?>/admin/gfx/buttons/button_dodaj.gif" border="0" /></a>
			<? }  else { ?>
			<p>Osiągnąłeś limit plików dla tego modułu. Proszę skasuj jakiś plik i odśwież stronę.</p>
			<? } ?>
		</div>
		<? }?>



        </div>

    	<div id="right_bottom"></div>   
		<? if($licznik < $limit){?>		
			<? showSort('files',$id."|".$mode); ?>	
		<? } ?>
        </div>

</div></div></div>

<? include($_SERVER['DOCUMENT_ROOT'].'/admin/templates/footer.php'); ?>