<?php 
require_once 'library/top_application.php';
require_once 'library/logged.php';

if (isset($_GET['action'])) {
  $action=$_GET['action'];
}
if (isset($_GET['id'])) {
  $id=$_GET['id'];
}

if ($action != 'add' && $action != 'edit'){
	$action='add';
}
//權限 
$sql_right = mysql_query('SELECT `manage_blogcat` FROM `'.TBL_USER.'` WHERE `uid` = '.$_SESSION['Logged_Uid']);
$row_right = mysql_fetch_assoc($sql_right);

if($row_right['manage_blogcat'] != 1){
	header("Location: listUser.php");
}

$sql_lang = mysql_query('SELECT `first_language` FROM `general_setting` WHERE `id` = 1');
$row_lang = mysql_fetch_assoc($sql_lang);

if ($row_lang['first_language'] == 1){
	//eng
	$lang_table = 'en_table';
	$must_subject = 'cat_name';
	// $must_subject = 'ep_subject';
	$lang_btn = 'btn_lang_switch_en';
} elseif ($row_lang['first_language'] == 2){
	//tc
	$lang_table = 'tc_table';
	$must_subject = 'cp_cat_name';
	// $must_subject = 'cp_subject';
	$lang_btn = 'btn_lang_switch_tc';
} elseif ($row_lang['first_language'] == 3){
	//sc
	$lang_table = 'sc_table';
	$must_subject = 'sp_cat_name';
	// $must_subject = 'sp_subject';
	$lang_btn = 'btn_lang_switch_sc';
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "fHtmlEditor")){
	// $target = isNumber(trim($_POST['target']));
	$cp_subject = txt_only($_POST['cp_subject']);
	$ep_subject = txt_only($_POST['ep_subject']);
	$sp_subject = txt_only($_POST['sp_subject']);
	$cp_brief = htmlPurify($_POST['cp_brief']);
	$ep_brief = htmlPurify($_POST['ep_brief']);
	$sp_brief = htmlPurify($_POST['sp_brief']);
	$cp_link = htmlPurify($_POST['cp_link']);
	$ep_link = htmlPurify($_POST['ep_link']);
	$sp_link = htmlPurify($_POST['sp_link']);
	
	$cat_name = htmlPurify($_POST['cat_name']);
	$cp_cat_name = htmlPurify($_POST['cp_cat_name']);
	$sp_cat_name = htmlPurify($_POST['sp_cat_name']);

	
	if(!get_magic_quotes_gpc()){ 
		$cp_subject = mysql_real_escape_string($cp_subject);
		$ep_subject = mysql_real_escape_string($ep_subject);
		$sp_subject = mysql_real_escape_string($sp_subject);
		$cp_brief = mysql_real_escape_string($cp_brief);
		$ep_brief = mysql_real_escape_string($ep_brief);
		$sp_brief = mysql_real_escape_string($sp_brief);
		$cp_link = mysql_real_escape_string($cp_link);
		$ep_link = mysql_real_escape_string($ep_link);
		$sp_link = mysql_real_escape_string($sp_link);

		$cat_name = mysql_real_escape_string($cat_name);
		$cp_cat_name = mysql_real_escape_string($cp_cat_name);
		$sp_cat_name = mysql_real_escape_string($sp_cat_name);

	}
	$sql_rank = mysql_query('SELECT `rank` FROM `nchmcblog_cat` ORDER BY `rank` DESC LIMIT 0, 1');
	$row_rank = mysql_fetch_assoc($sql_rank);
	$insert_rank = $row_rank['rank']+1;
	
	// mysql_query("INSERT INTO `banner` (target, cp_subject, ep_subject, sp_subject, cp_brief, ep_brief, sp_brief, cp_link, ep_link, sp_link, last_update, rank) VALUES ('$target', '$cp_subject', '$ep_subject', '$sp_subject', '$cp_brief', '$ep_brief', '$sp_brief', '$cp_link', '$ep_link', '$sp_link', NOW(), '$insert_rank')", $connePaper) or die(mysql_error());
	mysql_query("INSERT INTO `nchmcblog_cat` (cat_name,cp_cat_name,sp_cat_name, rank) VALUES ('$cat_name', '$cp_cat_name', '$sp_cat_name', '$insert_rank')", $connePaper) or die(mysql_error());
	header("Location: close_iframe.html");
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "fHtmlEditor")){
	// $target = isNumber(trim($_POST['target']));
	$cp_subject = txt_only($_POST['cp_subject']);
	$ep_subject = txt_only($_POST['ep_subject']);
	$sp_subject = txt_only($_POST['sp_subject']);
	$cp_brief = htmlPurify($_POST['cp_brief']);
	$ep_brief = htmlPurify($_POST['ep_brief']);
	$sp_brief = htmlPurify($_POST['sp_brief']);
	$cp_link = htmlPurify($_POST['cp_link']);
	$ep_link = htmlPurify($_POST['ep_link']);
	$sp_link = htmlPurify($_POST['sp_link']);
	// $banner_id = isNumber(trim($_POST['banner_id']));
	
		$cat_name = htmlPurify($_POST['cat_name']);
		$cp_cat_name = htmlPurify($_POST['cp_cat_name']);
		$sp_cat_name = htmlPurify($_POST['sp_cat_name']);
		$cat_id = isNumber(trim($_POST['cat_id']));

	if(!get_magic_quotes_gpc()){ 
		$cp_subject = mysql_real_escape_string($cp_subject);
		$ep_subject = mysql_real_escape_string($ep_subject);
		$sp_subject = mysql_real_escape_string($sp_subject);
		$cp_brief = mysql_real_escape_string($cp_brief);
		$ep_brief = mysql_real_escape_string($ep_brief);
		$sp_brief = mysql_real_escape_string($sp_brief);
		$cp_link = mysql_real_escape_string($cp_link);
		$ep_link = mysql_real_escape_string($ep_link);
		$sp_link = mysql_real_escape_string($sp_link);

		$cat_name = mysql_real_escape_string($cat_name);
		$cp_cat_name = mysql_real_escape_string($cp_cat_name);
		$sp_cat_name = mysql_real_escape_string($sp_cat_name);
		$cat_id = mysql_real_escape_string($cat_id);
	}
	
	
	mysql_query("UPDATE `nchmcblog_cat` SET `cat_name` = '$cat_name', `cp_cat_name` = '$cp_cat_name', `sp_cat_name` = '$sp_cat_name' WHERE `cat_id` = '$cat_id'", $connePaper) or die(mysql_error());
	echo "UPDATE `nchmcblog_cat` SET `cat_name` = '$cat_name', `cp_cat_name` = '$cp_cat_name', `sp_cat_name` = '$sp_cat_name' WHERE `cat_id` = '$cat_id'";
	header("Location: close_iframe.html");
}

$sql_size = mysql_query('SELECT `banner_w`, `banner_h` FROM `general_setting` WHERE `id` = 1');
$row_size = mysql_fetch_assoc($sql_size);
if(!$row_size['banner_w']){
	$pic_w = DEFAULT_SLIDE_W;
} else {
	$pic_w = $row_size['banner_w'];
}
if(!$row_size['banner_h']){
	$pic_h = DEFAULT_SLIDE_H;
} else {
	$pic_h = $row_size['banner_h'];
}

if ($action == 'edit'){
	$sql = mysql_query("SELECT * FROM `nchmcblog_cat` WHERE `cat_id` = ".$id);
	$row = mysql_fetch_assoc($sql);
} 
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Artech Creative</title>

<link href="css/css_admin.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui-min.js"></script>
<script type="text/javascript" src="../js/tinybox.js"></script>
<!--[if IE]>
    <script src="../js/html5.js"></script>
<![endif]--> 
<link href="css/anytime.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../js2/anytime.js"></script>
<script type="text/javascript" src="../js2/droparea.js"></script>
<script type="text/javascript" src="../js2/BlobBuilder.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>

<script type="text/javascript">
$(document).ready(function(){
	$('.lang_content_table').css('display', 'none');
  $('#<?php echo $lang_table;?>').css('display', 'block');
  $('#<?php echo $lang_btn;?>').addClass('selected');
  $("#ep_date").AnyTime_picker(
    { format: "%Y-%m-%d", labelTitle: "日期"} );
	
	$('#btn_lang_switch_content a').click(function(){
		$('#btn_lang_switch_content a').removeClass('selected');
		$(this).addClass('selected');
	})
	
	// Calling jQuery "droparea" plugin
	$('.droparea').droparea({
		'instructions': 'drop a file or click to here to upload photo',
		'init' : function(result){
		//console.log('custom init',result);
		},
		'start' : function($this){
		$this.area.find('.error').remove(); 
		
		},
		'error' : function(result, input, area){
		$('<div class="error">').html(result.error).prependTo(area); 
		return 0;
		//console.log('custom error',result.error);
		},
		'complete' : function($this, file){
		console.log('this',$this);
		if((/image/i).test(file.type)){
			var del_img = $this.area.find('img').attr('src');
			
			if(del_img){
				$.ajax({
					type: "POST",
					url: "ajax/instant_photo_function.php",
					data: {"del_slide" : del_img},
					cache: false
				});
			}
			$this.area.find('img').remove();
			//area.data('value',result.filename);
			$this.area.append($('<img>',{
				//'src': $this.result.path + $this.result.filename + '?' + Math.random()					
				'src': $this.result.filename .replace("../../","../"),
				'width': 200,
				'id': 'cur_thumb'
			}));
			<?php if($action=='add'){?>
			var image_name = $this.result.filename.replace("../../upload/slide_images/","");
			var target = $('#target').val();
			$.ajax({
				type: "POST",
				url: "ajax/instant_photo_function.php",
				data: {"new_slide_image" : image_name, "target" : target},
				cache: false,
				success: function(html){
					$('body').html(html);
				}
			});
			<?php }?>
			
			<?php if($action=='edit'){?>
			var del_img = $('#cur_thumb').attr('src');
			if(del_img){
				$.ajax({
					type: "POST",
					url: "ajax/instant_photo_function.php",
					data: {"del_slide" : del_img},
					cache: false
				});
			}
			var image_name = $this.result.filename.replace("../../upload/slide_images/","");
			var target = $('#target').val();
			$.ajax({
				type: "POST",
				url: "ajax/instant_photo_function.php",
				data: {"updates_slide_image" : image_name, "target" : target, "banner_id" : <?php echo $id;?>},
				cache: false,
				success: function(html){
					$('#upload_success').html('<strong style="color:red">Photo updated</strong>,  Click or drag photo a photo on the image to re-upload the image.');
					$('#upload_success').css('display', 'block');
					
				}
			});
			$('#cur_image, #cur_image_2').remove();
			<?php }?>
		} 
		//console.log('custom complete',result);
		}
	});
})

function switch_lang(id){
	$('.lang_content_table:not(#'+id+'_table)').css('display', 'none');
	$('#'+id+'_table').css('display', 'block');
}

function OnFormSubmit(){
	var must_subject = $.trim($('#<?php echo $must_subject;?>').val());
	var image_link = $('#cur_thumb').attr('src');
	if (!must_subject){
		$('.er_msg').css('display', 'block');
		
		$('.lang_content_table').css('display', 'none')
		$('#<?php echo $lang_table;?>').css('display', 'block')
		$('#btn_lang_switch_content a').removeClass('selected');
		$('#<?php echo $lang_btn;?>').addClass('selected');
		$('#<?php echo $must_subject;?>').focus();
	// } else if (!image_link){
	// 	$('#txt_must_photo').css('display', 'block');
	} else {
	  if(confirm("This Document is about to be submitted\nAre you sure you have finished editing?")){
		document.fHtmlEditor.submit();
	  }
	}
}
</script>

</head>
<body style="background:#fff">
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="padding:20px;" >
  <tr>
    <td><h2><? if ($action=='add'){ echo '新增專欄種類';} else { echo '修改專欄種類';}?></h2></td>
  </tr>
  <tr>
    <td><hr></td>
  </tr>
  <tr>
    <td><table width="100%" border="0" cellpadding="4" cellspacing="0" >
      <tr>
        <td>
          <form name="fHtmlEditor" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <table width="100%"  border="0" cellpadding="2" cellspacing="1" class="content">
              <!-- <tr>
                <td>Target window</td>
                <td>
                  <select name="target" id="target" style="width:144px">
                    <option value="1" <?php if ($row['target']==1){echo 'selected';}?>>Self</option>
                    <option value="2" <?php if ($row['target']==2){echo 'selected';}?>>New Window</option>
                    </select>
                  
                  </td>
              </tr> -->
              <?php if($action=='add'){?>
              <!-- <tr>
              <td width="120">Slide:<br>
              (<?php echo $pic_w;?>px * <?php echo $pic_h;?>px)</td>
                  <td> --><!-- <input type="file" class="droparea spot" name="xfile" data-post="upload/drop_Photo_banner.php" data-width="<?php echo $pic_w;?>" data-height="<?php echo $pic_h;?>" data-crop="true" data-canvas="true"/> -->
                <!--   	<span style="display:none; color:red" id="txt_must_photo">必須上傳圖片</span></td>
              </tr> -->
			 <!--  <tr>
              <td width="120"></td>
                  <td><div id="upload_success" style="display:none"><strong style="color:red">Photo updated</strong>,  Click or drag photo a photo on the image to re-upload the image.</div></td>
              </tr> -->
			  <?php
			  } elseif($action=='edit'){ ?>
             <!--  <tr >
              <td width="120" height="144">Current Slide:<br>
              (<?php echo $pic_w;?>px * <?php echo $pic_h;?>px)</td>
              <td>
              <table width="100%" border="0" cellpadding="0" cellspacing="0" class="content">
                <tr>
                  <td width="230" id="cur_image"><img src="../upload/slide_images/<?=$row['ep_pic']?>" id="cur_thumb" width="200"/><br>
</td>
                  <td width="120" id="cur_image_2">Change Slide:<br>
              (<?php echo $pic_w;?>px * <?php echo $pic_h;?>px)</td> -->
                 <!--  <td> --><!-- <input type="file" class="droparea spot" name="xfile" data-post="upload/drop_Photo_banner.php" data-width="<?php echo $pic_w;?>" data-height="<?php echo $pic_h;?>" data-crop="true" data-canvas="true"/> </td>-->
               <!--  </tr>
              </table></td>
              </tr> -->
			<!--   <tr>
              <td width="120"></td>
                  <td>
                  <div id="upload_success" style="display:none"><strong style="color:red">Photo updated</strong>,  Click or drag photo a photo on the image to re-upload the image.</div></td>
              </tr>--><? }?> 
              <tr>
                <td width="120" valign="top">選擇修改語言:</td>
                <td>
                  <div id="btn_lang_switch_content"><a href="javascript:switch_lang('en')" id="btn_lang_switch_en">ENG</a> | <a href="javascript:switch_lang('tc')" id="btn_lang_switch_tc">繁</a> | <a href="javascript:switch_lang('sc')" id="btn_lang_switch_sc">簡</a></div><span class="er_msg">必須輸入標題</span></td>
              </tr>
              <tr>
                <td valign="top" bgcolor="#CCCCCC" height="1"></td>
                <td bgcolor="#CCCCCC" height="1"></td>
              </tr>
              </table>
            <table width="100%" border="0" id="tc_table" class="lang_content_table">
              <tr>
                <td><table width="100%" border="0">
                <!--   <tr class="content">
                    <td width="120">連結:
                    </td>
                    <td><input name="cp_link" type="text" id="cp_link" size="50"   value="<? if ($action=='add'){ echo '';} else {echo $row['cp_link'];}?>">
                      (<strong style="color:red">http://</strong> or <strong style="color:red">https://</strong> is require to enter)</td>
                    </tr> -->
                   <tr class="content">
                    <td width="120">
                    	專欄名稱
                    	<!-- 最新消息標題:<br>
                      (建議最多12字) -->
                    </td>
                    <td><input name="cp_cat_name" type="text" id="cp_cat_name" size="50"   value="<? if ($action=='add'){ echo '';} else {echo $row['cp_cat_name'];}?>"></td>
                    </tr>
                <!--   <tr class="content">
                    <td valign="top">簡介:<br>
                      (建議最多100字)</td>
                    <td><textarea name="cp_brief" cols="80" rows="4" id="cp_brief" class="content"><? if ($action=='add'){ echo '';} else {echo $row['cp_brief'];}?></textarea></td>
                    </tr> -->
                  </table></td>
              </tr>
            </table>
            
            
            <table width="100%" border="0" id="sc_table" class="lang_content_table">
              <tr>
                <td><table width="100%" border="0">
              <!--   <tr class="content">
                    <td width="120">连结:
                    </td>
                    <td><input name="sp_link" type="text" id="sp_link" size="50"   value="<? if ($action=='add'){ echo '';} else {echo $row['sp_link'];}?>">
                      (<strong style="color:red">http://</strong> or <strong style="color:red">https://</strong> is require to enter)</td>
                    </tr> -->
                  <tr class="content">
                    <td width="120">
                    	專欄名稱
                    	<!-- 最新消息标题：<br>(建议最多12字) --></td>
                    
                    <td><input name="sp_cat_name" type="text" id="sp_cat_name" size="50" value="<? if ($action=='add'){ echo '';} else {echo $row['sp_cat_name'];}?>"></td>
                    </tr>
                <!--   <tr class="content">
                    <td valign="top">简介:<br>
                      (建议最多100字)</td>
                    <td><textarea name="sp_brief" cols="80" rows="4" id="sp_brief"  class="content"><? if ($action=='add'){ echo '';} else {echo $row['sp_brief'];}?></textarea></td>
                    </tr> -->
                  </table></td>
              </tr>
            </table>
            
            
            <table width="100%" border="0" id="en_table" class="lang_content_table">
              <tr>
                <td><table width="100%" border="0">
              <!--   <tr class="content">
                    <td width="120">Link:
                    </td>
                    <td><input name="ep_link" type="text" id="ep_link" size="50"   value="<? if ($action=='add'){ echo '';} else {echo $row['ep_link'];}?>">
                      (<strong style="color:red">http://</strong> or <strong style="color:red">https://</strong> is require to enter)</td>
                    </tr> -->
                  <tr class="content">
                    <td width="120">
                    	Blog Category<!-- News Title:<br>(Suggesting Max. 30 words) --></td>
                                        
                    <td><input name="cat_name" type="text" id="cat_name" size="50"value="<? if ($action=='add'){ echo '';} else {echo $row['cat_name'];}?>"></td>
                    </tr>
                <!--   <tr class="content">
                    <td valign="top">Brief:<br>
                      (Suggesting <br>
						Max. 100 words)</td>
                    <td><textarea name="ep_brief" cols="80" rows="4" id="ep_brief" class="content"><? if ($action=='add'){ echo '';} else {echo $row['ep_brief'];}?></textarea></td>
                    </tr> -->
                  </table></td>
              </tr>
            </table>
           
          <table width="100%" border="0" cellpadding="0" cellspacing="0" class="content">
            <tr>
              <td height="15"></td>
            </tr>
          </table>
           <?php if ($action =='edit'){?><input type="hidden" name="cat_id" value="<?php echo $row['cat_id'];?>"><?php }?>
           <input type="hidden" name="<? if ($action =='add'){ echo 'MM_insert';} else { echo 'MM_update';}?>" value="fHtmlEditor">
            <input type="button" name="Submit"  onClick="OnFormSubmit()" id="btn_submit">
          </form>
        </td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
    </table></td>
  </tr>
</table>
<?php include 'library/bottom.php'; ?>
</body>
</html>
