<?php session_start();
	if (!$_SESSION['nama']){
		echo "<script language=JavaScript>";
		echo "location.href = 'login.php';";
		echo "</script>";
	}
	include ("../koneksi.php");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title> WEBSITE BACKOFFICE </title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<link href="style.css" rel="stylesheet" type="text/css">
		<script language="JavaScript" type="text/javascript" src="richtext.js"></script>
		<script language="JavaScript" type="text/javascript">
		<!--
		function submitForm() {
			//make sure hidden and iframe value are in sync before submitting form
			//to sync only 1 rte, use updateRTE(rte)
			//to sync all rtes, use updateRTEs
			updateRTE('deskripsi');
			updateRTE('deskripsi2');
			//updateRTEs();
			//alert("rte1 = " + document.RTEDemo.content.value);
			
			//change the following line to true to submit form
			return true;
		}
		
		//Usage: initRTE(imagesPath, includesPath, cssFile)
		initRTE("images/", "", "");
		//-->
		</script>				
	</head>	
	<body>
		<center>
			<table width="800">
				<tr valign="top">
					<td width="162" class="bg">
						<?php include("kiri.php"); ?>
					</td>
					<td>
						<?php include ("logo.php"); ?>
						<div id="spasi1c"></div>
						<?php
						if ($action) {
							$type = $_REQUEST["type"];
							$subtype = $_REQUEST["subtype"];
							
							if ($subtype)
								$query2="select tanggal, gambar, dokumen1, title, description, a.id as a_id, type, subtype, c.tipe as c_tipe, b.tipe as b_tipe from publications_trans as a, publications_submaster as b, publications_master as c where a.type=c.id and a.subtype=b.id and a.id=".$id." order by tanggal desc";
							else 
								$query2="select tanggal, gambar, dokumen1, title, description, a.id as a_id, type, subtype, b.tipe as b_tipe from publications_trans as a, publications_master as b where a.type=b.id and a.id=".$id." order by tanggal desc";
								
							// echo $query2;
							if($hasil2=mysql_query($query2)) {
								while($data2=mysql_fetch_array($hasil2)) {
									$id = $data2["a_id"];
									$c_tipe = $data2["c_tipe"];
									$b_tipe = $data2["b_tipe"];
									$title = $data2["title"];
									$gambar = $data2["gambar"];
									$dokumen1 = $data2["dokumen1"];
								
									$deskripsi = $data2["description"];
									$deskripsi = str_replace("'", "&acute;", $deskripsi);	
									$deskripsi = str_replace(chr(13), "", $deskripsi);
									$deskripsi = str_replace(chr(10), "", $deskripsi);								
								
									$tanggal = explode("-", $data2["tanggal"]);					
								
									if ($type) $lokasi = $lokasi.$type."/"; 
									if ($subtype) $lokasi = $lokasi.$subtype."/"; 

									$lokasi = "../download/publications/";
									if ($type) $lokasi = $lokasi.$type."/"; 
									if ($subtype) $lokasi = $lokasi.$subtype."/"; 
									$lokasi = $lokasi.$tanggal[0]."/";
									//echo "Lokasi : ".$lokasi;
								}
							}
							?>
							<table width="100%" cellpadding="5" cellspacing="0" border="0">
								<tr><td><strong>PUBLICATIONS --> <?=$action;?> PUBLICATION</strong></td></tr>
								<tr valign="top">
									<td>									
										<form method="post" action="proses.php" enctype="multipart/form-data" name="form1" onSubmit="return submitForm();">
											<?php
											if ($action=="DELETE") {
												?>
												<div>Are you sure you want to delete the following data? </div>
												<table width="100%" cellpadding="3" cellspacing="1" bgcolor="#999999">
													<tr bgcolor="#FFFFFF">
														<td>
															<table width="100%" cellpadding="3">
																<tr>
																	<td width="15%">Date</td>
																	<td width="5%">:</td>
																	<td width="80%">
																		<?php echo $tanggal[2]." - ".$tanggal[1]." - ".$tanggal[0]; ?>
																	</td>
																</tr>												
																<tr>
																	<td>Title</td>
																	<td>:</td>
																	<td><?=$title;?></td>
																</tr>
																<?php
																if ($deskripsi) {
																	?>
																	<tr valign="top">
																		<td>Descriptions</td>
																		<td>:</td>
																		<td><?php echo $deskripsi; ?></td>
																	</tr>
																	<?php
																}
																?>
																<tr>
																	<td colspan="2"></td>
																	<td>
																		<input type="submit" value="Submit">
																		<input type="button" value="Cancel" onclick="javascript:location='publication.php?menu=6&id2=<?=$subtype?>'">
																		<input type="hidden" name="action" value="<?=$action?>">
																		<input type="hidden" name="module" value="publications">
																		<input type="hidden" name="id" value="<?=$id?>">
																		<input type="hidden" name="type" value="<?=$type?>">
																		<input type="hidden" name="subtype" value="<?=$subtype?>">
																		<input type="hidden" name="thn" value="<?=$tanggal[0]?>">																		
																		<input type="hidden" name="old_gambar" value="<?=$gambar?>">
																		<input type="hidden" name="old_dokumen" value="<?=$dokumen1?>">
																	</td>
																</tr>
															</table>											
														</td>
													</tr>
												</table>
												<?php
											} else {
												?>
												<table width="100%" cellpadding="3">
													<tr>
														<td width="15%">Date</td>
														<td width="5%">:</td>
														<td width="80%">
															<select name="tgl">
																<?php
																for($i=1; $i<=31; $i++) {
																	?>
																	<option <?php if($i == (int)$tanggal[2]) echo "selected";?> value="<?=$i?>"><?=$i?></option>
																	<?php
																}
																?>
															</select>
															<select name="bln">
																<?php
																for($i=1; $i<=12; $i++) {
																	?>
																	<option <?php if($i == (int)$tanggal[1]) echo "selected";?> value="<?=$i?>"><?=$i?></option>
																	<?php
																}
	
																?>
															</select>
															<select name="thn">
																<?php
																for($i=2007; $i<=2020; $i++) {
																	?>
																	<option <?php if($i == (int)$tanggal[0]) echo "selected";?> value="<?=$i?>"><?=$i?></option>
																	<?php
																}
																?>
															</select>														
														</td>
													</tr>												
													<tr>
														<td>Type</td>
														<td>:</td>
														<td>
															<select name="type">
																<?php
																$sql = "select * from publications_master order by id";
																if ($hasil = mysql_query($sql)) {
																	while ($data=mysql_fetch_array($hasil)) {
																		?><option <?php if($data["id"]==$type) echo "selected"; ?> value="<?=$data["id"];?>"><?=$data["tipe"];?></option><?php
																	}
																}
																?>
															</select>														
														</td>
													</tr>
													<tr>
														<td>Sub Type</td>
														<td>:</td>
														<td>
															<select name="subtype">
																<option value="">None</option>
																<?php
																$sql = "select * from publications_submaster order by id";
																if ($hasil = mysql_query($sql)) {
																	while ($data=mysql_fetch_array($hasil)) {
																		?><option <?php if($data["id"]==$subtype) echo "selected"; ?> value="<?=$data["id"];?>"><?=$data["tipe"];?></option><?php
																	}
																}
																?>
															</select>														
														</td>
													</tr>
													<tr>
														<td>Title</td>
														<td>:</td>
														<td><input type="text" name="title" size="70" value="<?=$title;?>"></td>
													</tr>
													<tr valign="top">
														<td>Descriptions</td>
														<td>:</td>
														<td>
															<script language="JavaScript" type="text/javascript">
																<!--
																	//Usage: writeRichText(fieldname, html, width, height, buttons, readOnly)
																	writeRichText('deskripsi', '<?php echo $deskripsi; ?>', 10, 200, true, false);
																//-->
															</script>															
														</td>
													</tr>
													<tr valign="top">
														<td>Image</td>
														<td>:</td>
														<td>
															<?php if ($gambar) echo "<img src='".$lokasi.$gambar."' width='200' border='0'>"."<br>"; ?>
															<input type="file" name="gambar" size="15">
														</td>
													</tr>
													<tr valign="top">
														<td>File</td>
														<td>:</td>
														<td>
															<?php if ($dokumen1) echo $dokumen1."<br>"; ?>
															<input type="file" name="dokumen1" size="15">
														</td>
													</tr>
													<tr>
														<td>Show on Home</td>
														<td>:</td>
														<td>
															<select name="tampil">
																<option value="">-select-</option>
																<option value="1" <?php if ($data2["tampil"]=="1") echo "selected"; ?>>YES</option>
																<option value="0" <?php if ($data2["tampil"]=="0") echo "selected"; ?>>NO</option>
															</select>
														</td>
													</tr>
													<tr>
														<td colspan="2"></td>
														<td>
															<div id="grs-lurus"></div>
															<input type="submit" value="Submit">
															<input type="button" value="Cancel" onclick="javascript:location='publication.php?menu=6&id2=<?=$subtype?>'">
															<input type="hidden" name="action" value="<?=$action?>">
															<input type="hidden" name="module" value="publications">
															<input type="hidden" name="id" value="<?=$id?>">
															<input type="hidden" name="old_gambar" value="<?=$gambar?>">
															<input type="hidden" name="old_dokumen" value="<?=$dokumen1?>">
														</td>
													</tr>
												</table>											
												<?php
											}
											?>
										</form>
									</td>
								</tr>
							</table>																																					
							<?php
						} else {
							if ($id) {
								$query="select count(a.id) as total from publications_master as a, publications_submaster as b where a.id=b.parent_id and b.parent_id=".$id;
								$hasil=mysql_query($query);
								$data = mysql_fetch_array($hasil);
								$total = $data["total"];
								if ($total > 0) {
									$query2="select a.id as aid, b.id as bid, a.tipe as a_tipe, b.tipe as b_tipe  from publications_master as a, publications_submaster as b where a.id=b.parent_id and b.parent_id=".$id;
									if ($hasil2=mysql_query($query2)) {
										$cek=0;
										?>
										<table width="100%" cellpadding="5" cellspacing="0" border="0">
											<?php
											$nomor = 0;
											while($data2=mysql_fetch_array($hasil2)){
												if ($nomor==0) {
													?>
													<tr><td><strong>PUBLICATIONS > <?=strtoupper($data2["a_tipe"]);?></strong></td></tr>
													<tr valign="top">
														<td>
															<table width="100%" cellpadding="6" cellspacing="1" bgcolor="#CCCCCC">
																<tr bgcolor="#EEEEEE">
																	<td width="8%" align="center"><strong>ID</strong></td>
																	<td width="72%"><strong>Title</strong></td>
																	<td width="25%" align="center"><strong>Action</strong></td>
																</tr>										
																<?php
												}
																$nomor=$nomor+1;
																?>
																<tr bgcolor="#FFFFFF">
																	<td align="center"><?php echo $nomor; ?> </td>
																	<td><?php echo $data2["b_tipe"]; ?></td>
																	<td align="center">
																		<a href="publication.php?menu=6&id2=<?=$data2["bid"]?>">Edit</a>
																	</td>
																</tr>
																<?php
	
											}
											?>
													</table>
												</td>
											</tr>
										</table>
										<?php
									}
								} else {
									$query2="select * from publications_master where id=".$id;
									if($hasil2=mysql_query($query2)) {
										while($data2=mysql_fetch_array($hasil2)) {
											?>
											<table width="100%" cellpadding="5" cellspacing="0" border="0">
												<tr><td><strong>PUBLICATIONS -> <?=strtoupper($data2["tipe"]);?></strong></td></tr>
												<tr valign="top">
													<td>
														<div align="right"><a href="?menu=6&type=<?=$data2["type"];?>&subtype=<?=$data2["subtype"];?>&action=ADD"><strong>ADD</strong></a></div>
														<table width="100%" cellpadding="6" cellspacing="1" bgcolor="#CCCCCC">
															<tr bgcolor="#EEEEEE">
																<td width="8%" align="center"><strong>ID</strong></td>
																<td width="75%"><strong>Title</strong></td>
																<td width="25%" align="center"><strong>Action</strong></td>
															</tr>										
															<?php
										}
															// Detil
															$query3="select title, description, a.id as a_id, type, subtype, b.tipe as b_tipe from publications_trans as a, publications_master as b where a.type=b.id and a.type=".$id." order by tanggal desc";
															// echo $query2;
															if($hasil3=mysql_query($query3)) {
																$nomor=0;
																while($data3=mysql_fetch_array($hasil3)) {
																	$nomor++;
																	?>
																	<tr bgcolor="#FFFFFF">
																		<td align="center"><?php echo $nomor; ?> </td>
																		<td><?php echo $data3["title"]; ?></td>
																		<td align="center">
																			<a href="?menu=6&id=<?=$data3["a_id"];?>&type=<?=$data3["type"];?>&subtype=<?=$data3["subtype"];?>&action=EDIT">Edit</a>
																			&nbsp; | &nbsp;
																			<a href="?menu=6&id=<?=$data3["a_id"];?>&type=<?=$data3["type"];?>&subtype=<?=$data3["subtype"];?>&action=DELETE">Delete</a>
																		</td>
																	</tr>
																	<?php
																}
															}
															// Detil
															?>
														</table>
													</td>
												</tr>
											</table>
											<?php
									}
								}
							} else if ($id2) {
								$query2="select title, description, a.id as a_id, type, subtype, c.tipe as c_tipe, b.tipe as b_tipe from publications_trans as a, publications_submaster as b, publications_master as c where a.type=c.id and a.subtype=b.id and a.subtype=".$id2." order by tanggal desc";
								//echo $query2;
								if($hasil2=mysql_query($query2)) {
									$nomor=0;
									while($data2=mysql_fetch_array($hasil2)) {
										if ($nomor==0) {
										?>
									<table width="100%" cellpadding="5" cellspacing="0" border="0">
										<tr><td><strong>PUBLICATIONS > <?=strtoupper($data2["c_tipe"]);?> > <?=strtoupper($data2["b_tipe"]);?></strong></td></tr>
										<tr valign="top">
											<td>
												<div align="right"><a href="?menu=6&type=<?=$data2["type"];?>&subtype=<?=$data2["subtype"];?>&action=ADD"><strong>ADD</strong></a></div>
												<table width="100%" cellpadding="6" cellspacing="1" bgcolor="#CCCCCC">
													<tr bgcolor="#EEEEEE">
														<td width="8%" align="center"><strong>ID</strong></td>
														<td width="75%"><strong>Title</strong></td>
														<td width="25%" align="center"><strong>Action</strong></td>
													</tr>										
										<?php
										}
										$nomor++;
													?>
													<tr bgcolor="#FFFFFF">
														<td align="center"><?php echo $nomor; ?> </td>
														<td><?php echo $data2["title"]; ?></td>
														<td align="center">
															<a href="?menu=6&id=<?=$data2["a_id"];?>&type=<?=$data2["type"];?>&subtype=<?=$data2["subtype"];?>&action=EDIT">Edit</a>
															&nbsp; | &nbsp;
															<a href="?menu=6&id=<?=$data2["a_id"];?>&type=<?=$data2["type"];?>&subtype=<?=$data2["subtype"];?>&action=DELETE">Delete</a>
														</td>
													</tr>
													<?php
									}
									?>
												</table>
											</td>
										</tr>
									</table>
									<?php
								}
							} else {
								?>
								<table width="100%" cellpadding="5" cellspacing="0" border="0">
									<tr><td><strong>PUBLICATIONS</strong></td></tr>
									<tr valign="top">
										<td>
											<table width="100%" cellpadding="6" cellspacing="1" bgcolor="#CCCCCC">
												<tr bgcolor="#EEEEEE">
													<td width="8%" align="center"><strong>ID</strong></td>
													<td width="72%"><strong>Title</strong></td>
													<td width="25%" align="center"><strong>Action</strong></td>
												</tr>										
												<?php 
												if ($koneksi){
													@ $nomor = 0;
													$query="select * from publications_master ";
													$hasil=mysql_query($query);												
													while($data=mysql_fetch_array($hasil)){													
														$nomor=$nomor+1;
														?>
														<tr bgcolor="#FFFFFF">
															<td align="center"><?php echo $nomor; ?> </td>
															<td><?php echo $data["tipe"]; ?></td>
															<td align="center">
																<a href="publication.php?menu=6&id=<?=$data["id"]?>">Edit</a>
															</td>
														</tr>
														<?php
													}
												}	
												?>
											</table>
										</td>
									</tr>
								</table>				
								<?php
							} 
						}
						?>
					</td>
				</tr>
			</table>
		</center>
	</body>
</html>	