<script src='tinymce/tinymce.min.js'></script>
<script>
tinymce.init({
    selector: '#deskripsi',
    height: 500,
    theme: 'modern',
    plugins: [
      'advlist autolink lists link image charmap print preview hr anchor pagebreak',
      'searchreplace wordcount visualblocks visualchars code fullscreen',
      'insertdatetime media nonbreaking save table contextmenu directionality',
      'emoticons template paste textcolor colorpicker textpattern imagetools'
    ],
    toolbar1: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
    toolbar2: 'print preview media | forecolor backcolor emoticons',
    image_advtab: true
});
</script>
<script type="text/javascript">
<!--
function confirmation(noid) {
	var answer = confirm("Are You sure to delete this Article?")
	if (answer){
		//alert("Bye bye!")
		window.location = "?m=product&page=delete&no=" + noid;
		
	}
	
}
//-->
</script>

<h2><img src="images/icon-48-article.png" width="48" height="48" align="absmiddle"> Product Manager</h2>
<?
switch($page) {
	default :
?>	
<div id="menu_button">
  <ul>
    <li><a href="?m=product&page=addnew">Tambah Produk Baru <br>
      <br>
    </a></li>
  </ul>
</div>
<table width="1000" border="0" align="center" cellpadding="5" cellspacing="0">
  <tr bgcolor="#FFFF33" class="tbl_header">
    <td width="4%" align="center"><strong>#</strong></td>
    <td width="4%" align="center"><strong>ID</strong></td>
    <td width="30%" align="center"><strong>Title</strong></td>
    <td width="9%" align="center"><strong>Published</strong></td>
    <td width="11%" align="center"><strong>Harga</strong></td>
    <td width="7%" align="center"><strong>Diorder</strong></td>
    <td width="8%" align="center">&nbsp;</td>
  </tr>
<?
$db->select("id, nama, harga, published, created, catid, file, hit, expire, poin, berat", "product");

$j=$db->num_rows();
for($i=0;$i<$j;$i++) {
	$nom = $i + 1;
	$lid = $i - 1;
	if(is_odd($i) == 0) {
		$class = "tblrow_ganjil";
	} else {
		$class = "tblrow_genap";
	} 	
	if($db->result($i, "published") > 0) {
		$img = "<a href='?m=product&page=publish&pub=0&no=".$db->result($i, "id")."'><img src='images/tick.png' border=0 title='Click to Unpublish'></a>";
	} else {
		$img = "<a href='?m=product&page=publish&pub=1&no=".$db->result($i, "id")."'><img src='images/publish_x.png' border=0 title='Click to Publish'></a>";
	} 	
	
?>
 
  <tr class="<?= $class; ?>">
    <td width="4%"><?= $nom; ?> </td>
    <td width="4%"><?= $db->result($i, "id"); ?></td>
    <td><a href="?m=product&page=addnew&edit=1&no=<?= $db->result($i, "id"); ?>"><?= $db->result($i, "nama"); ?></a></td>
    <td align="center"><?= $img; ?></td>
    <td align="right"><?= rupiah($db->result($i, "harga")); ?></td>
    <td align="center"><?= $db->result($i, "hit"); ?></td>
    <td align="center"><div id="delete"><a href="#" onClick='confirmation(<?= $db->result($i, "id"); ?>)' style='cursor:hand; padding-left:15px'>Delete</a></div></td>
  </tr>
<?
	}
?>	  
</table>
<p>&nbsp;</p>
<?
	break;
	case addnew :
	//include("fckeditor/fckeditor.php") ;
		if($edit > 0) {
			$db->select("id, nama, deskripsi, created, published, harga, catid, gambar, file, expire, poin, berat", "product", "id='$no'");
			$title = $db->result($i, "nama"); 
			$harga = $db->result($i, "harga"); 
			$crdate = $db->result($i, "created"); 
			$publish = $db->result($i, "published"); 
			$gbr = $db->result($i, "gambar");
			$catid = $db->result($i, "catid"); 
			$myfile = $db->result($i, "file"); 
			$maintext = $db->result($i, "deskripsi"); 
			$expire = $db->result($i, "expire"); 
			$poin = $db->result($i, "poin"); 
			$berat = $db->result($i, "berat"); 
			$judul = "Edit Produk";
		} else {
			$author = $valid_admin; 
			$crdate = $clientdate; 
			$judul = "Create New Product";
		}		
?>
<form action="?m=product&page=submit" method="post" enctype="multipart/form-data" name="form1">
  <table width="700" border="0" align="center" cellpadding="5" cellspacing="0">
    <tr>
      <td colspan="4"><h4><?= $judul; ?></h4></td>
    </tr>
    <tr>
      <td width="13%" align="right"><strong>Nama Produk</strong> :</td>
      <td><label>
        <input name="title" type="text" id="title" value="<?= $title; ?>" size="50">
      </label></td>
      <td align="right"><strong>Harga</strong> :</td>
      <td><input name="harga" type="text" id="harga" value="<?= $harga; ?>" size="10"></td>
    </tr>
    <tr>
      <td align="right">Category :</td>
      <td width="38%"><select name="cat" id="cat">
        <option value="0">- Select Category -</option>
        <?
	 
	$db->select("id, parent_id, title, published", "categories", "section='product' and parent_id=0"); 
	while($data = $db->fetch_row()) {
		if($catid == $data[0]) {
			$sel = "selected";
		} else {
			$sel = "";
		}		
		echo "<option value=$data[0] $sel>$data[2]</option>";
		$sql = mysql_query("select id, parent_id, title, published from categories where section='product' and parent_id=$data[0]");
		while($row=mysql_fetch_row($sql)) {
			if($catid == $row[0]) {
				$sel2 = "selected";
			} else {
				$sel2 = "";
			}
			echo "<option value=$row[0] $sel2>&nbsp;| $row[2]</option>";
		}	
	}
	 
	 ?>
            </select></td>
      <td align="right"><strong>Poin</strong> :</td>
      <td><input name="poin" type="text" id="poin" value="<?= $poin; ?>" size="10"></td>
    </tr>
    <tr>
      <td align="right" valign="top"></td>
      <td><?
		  if($gbr) {
		  	echo "<img src='../produk/images/thumb/$gbr'  border=0>";
			}
		?></td>
      <td align="right"><strong>Berat</strong> :</td>
      <td><input name="berat" type="text" id="berat" value="<?= $berat; ?>" size="10"> 
      Kg </td>
    </tr>
    <tr>
      <td align="right" valign="top"> Gambar Produk : </td>
      <td><label>
        <input name="my_field" type="file" size="40" id="my_field" />
      </label></td>
      <td align="right">&nbsp;</td>
      <td>        <input name="my_file" type="hidden" id="my_file" value="<?= $myfile; ?>" size="20" /></td></tr>
    <tr>
      <td align="right" valign="top">Published :</td>
      <td colspan="3">
        
          <input name="publish" type="radio" id="publish_0" value="1" checked>Yes
          <input type="radio" name="publish" value="0" id="publish_1">No
        <input name="no" type="hidden" id="no" value="<?= $no; ?>" size="20">
        <input name="edit" type="hidden" id="edit" value="<?= $edit; ?>" size="20">      </td>
    </tr>
    <tr>
      <td align="right" valign="top">Deskripsi :</td>
      <td colspan="3"><textarea name="deskripsi" cols="70" rows="10" id="deskripsi"><?= $maintext; ?></textarea></td>
    </tr>
    <tr>
      <td align="right">&nbsp;</td>
      <td colspan="3"><label>
        <input type="submit"  value="SAVE" class="button">
        
      </label><label><input type="button" name="cancel" id="cancel" value="CANCEL" onClick="javascript:history.go(-1)" class="cancelbutton">
      </label></td>
    </tr>
  </table>
</form>
  <script language="JavaScript" type="text/javascript">
 var frmvalidator = new Validator("form1");
 frmvalidator.addValidation("title","req","Nama Produk harus diisi, silahkan ulangi lagi!");
 frmvalidator.addValidation("cat","dontselect=0","Pilih katagori Produk!");
 frmvalidator.addValidation("harga","req","Harga harus diisi, silahkan ulangi lagi!");
 frmvalidator.addValidation("expire","req","Berapa lama password akan expire? Tulis 0 jika unlimited!");

</script>  
<?	
	break;
	case submit :
		include('class.upload.php');
	   // All the uploads are accessible from the PHP object $_FILES
if($my_field) {
   $handle = new Upload($_FILES['my_field']);

    // then we check if the file has been uploaded properly
    // in its *temporary* location in the server (often, it is /tmp)
    if ($handle->uploaded) {
       
        // yes, the file is on the server
        // below are some example settings which can be used if the uploaded file is an image.
       $info = getimagesize($handle->file_src_pathname);	
       if($info[0] >= 400) {
	    $handle->image_resize            = true;
        $handle->image_ratio_y           = true;
        $handle->image_x                 = 200;
		} else {
			$handle->image_resize            = false;
		}	

        // now, we start the upload 'process'. That is, to copy the uploaded file
        // from its temporary location to the wanted location
        // It could be something like $handle->Process('/home/www/my_uploads/');
        $handle->Process('../produk/images/');
        
        // we check if everything went OK
      
        // we now process the image a second time, with some other settings
        $handle->image_resize            = true;
        $handle->image_ratio_y           = false;
        $handle->image_x                 = 80;
		$handle->image_y                 = 80;
        $handle->image_reflection_height = '0';
        $handle->image_contrast          = 0;

        $handle->Process('../produk/images/thumb/');
        
      

        // we delete the temporary files
        $handle-> Clean();

    } else {
        // if we're here, the upload file failed for some reasons
        // i.e. the server didn't receive the file
        echo '<fieldset>';
        echo '  <legend>file not uploaded on the server</legend>';
        echo '  Error: ' . $handle->error . '';
        echo '</fieldset>';
    //}

	}
}	
  $gambar = $handle->file_dst_name;
  if(!$gambar) {
 	$db->select("gambar", "product", "id='$no'");
	if($db->num_rows() > 0) {
		$gambar = $db->result(0, "gambar");
	}
 }
	if(!$img1) {
		$myfile = $my_file;
	} else {		
		$file = "../produk/file/$img1_name";
		@copy("$img1" , $file);
		$myfile = $img1_name;
	}		
		if($edit > 0) {
			$db->update("product", "nama='$title', file='$myfile', catid='$cat', harga='$harga', deskripsi='$deskripsi', published='$publish', expire='$expire', poin='$poin', berat='$berat', gambar='$gambar'", "id='$no'");
	} else {
			$db->insert("product", "catid, nama, deskripsi, harga, gambar, published, created, file, expire, poin, berat", "'$cat', '$title', '$deskripsi', $harga, '$gambar', '$publish', '$clientdate', '$myfile', '$expire', '$poin', '$berat'");
	}		
			echo "<meta http-equiv='refresh' content='0;URL=?m=product'>";
	break;
	
	case publish :
		$db->update("product", "published='$pub'", "id='$no'");
		echo "<meta http-equiv='refresh' content='0;URL=?m=product'>";
	break;
	case delete :
		//echo "delete no $no";
		$db->delete("product", "id=$no");
		echo "<meta http-equiv='refresh' content='0;URL=?m=product'>";
	break;	
	
	case frontpage :
		$db->update("product", "frontpage='$pub'", "id='$no'");
		echo "<meta http-equiv='refresh' content='0;URL=?m=product'>";
	break;
}
?>	