<?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_section` FROM `'.TBL_USER.'` WHERE `uid` = '.$_SESSION['Logged_Uid']);
$row_right = mysql_fetch_assoc($sql_right);

if($row_right['manage_section'] != 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 = 'ep_name';
	$lang_btn = 'btn_lang_switch_en';
} elseif ($row_lang['first_language'] == 2){
	//tc
	$lang_table = 'tc_table';
	$must_subject = 'cp_name';
	$lang_btn = 'btn_lang_switch_tc';
} elseif ($row_lang['first_language'] == 3){
	//sc
	$lang_table = 'sc_table';
	$must_subject = 'sp_name';
	$lang_btn = 'btn_lang_switch_sc';
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "fHtmlEditor")){
	$type = isNumber(trim($_POST['type']));
	$link = htmlPurify($_POST['link']);
	$ext_link = htmlPurify($_POST['ext_link']);
	$cp_name = htmlPurify($_POST['cp_name']);
	$ep_name = htmlPurify($_POST['ep_name']);
	$sp_name = htmlPurify($_POST['sp_name']);
	$seo_meta_key = htmlPurify($_POST['seo_meta_key']);
	$seo_description = htmlPurify($_POST['seo_description']);
	
	if(!get_magic_quotes_gpc()){ 
		$link = mysql_real_escape_string($link);
		$ext_link = mysql_real_escape_string($ext_link);
		$cp_name = mysql_real_escape_string($cp_name);
		$ep_name = mysql_real_escape_string($ep_name);
		$sp_name = mysql_real_escape_string($sp_name);
		$seo_meta_key = mysql_real_escape_string($seo_meta_key);
		$seo_description = mysql_real_escape_string($seo_description);
	}
	$sql_rank = mysql_query('SELECT `rank` FROM `section` ORDER BY `rank` DESC LIMIT 0, 1');
	$row_rank = mysql_fetch_assoc($sql_rank);
	$insert_rank = $row_rank['rank']+1;
	
	mysql_query("INSERT INTO `cdc_section` (type, cp_name, ep_name, sp_name, link, ext_link, seo_meta_key, seo_description, last_update, rank) VALUES ('$type', '$cp_name', '$ep_name', '$sp_name', '$link', '$ext_link', '$seo_meta_key', '$seo_description', NOW(), '$insert_rank')", $connePaper) or die(mysql_error());
	header("Location: close_iframe.html");
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "fHtmlEditor")){
	$id = isNumber(trim($_POST['section_id']));
	$type = isNumber(trim($_POST['type']));
	$link = htmlPurify($_POST['link']);
	$ext_link = htmlPurify($_POST['ext_link']);
	$cp_name = htmlPurify($_POST['cp_name']);
	$ep_name = htmlPurify($_POST['ep_name']);
	$sp_name = htmlPurify($_POST['sp_name']);
	$seo_meta_key = htmlPurify($_POST['seo_meta_key']);
	$seo_description = htmlPurify($_POST['seo_description']);
	
	if(!get_magic_quotes_gpc()){ 
		$link = mysql_real_escape_string($link);
		$ext_link = mysql_real_escape_string($ext_link);
		$cp_name = mysql_real_escape_string($cp_name);
		$ep_name = mysql_real_escape_string($ep_name);
		$sp_name = mysql_real_escape_string($sp_name);
		$seo_meta_key = mysql_real_escape_string($seo_meta_key);
		$seo_description = mysql_real_escape_string($seo_description);
	}

	mysql_query("UPDATE `cdc_section` SET `type` = '$type', `cp_name` = '$cp_name', `ep_name` = '$ep_name', `sp_name` = '$sp_name', `seo_meta_key` = '$seo_meta_key', `seo_description` = '$seo_description', `link` = '$link', `ext_link` = '$ext_link', `last_update` = NOW() WHERE `section_id` = '$id'", $connePaper) or die(mysql_error());
	header("Location: close_iframe.html");
}


if ($action == 'edit'){
	$sql = mysql_query("SELECT * FROM `cdc_section` WHERE `section_id` = ".$id);
	$row = mysql_fetch_assoc($sql);
	$web = $row['type'];
} else {
	$web=1;
}

?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AWESOME CMS by Allpeople Ltd,.</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>
<!--[if IE]>
    <script src="../js/html5.js"></script>
<![endif]--> 
<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');
	
	$('#btn_lang_switch_content a').click(function(){
		$('#btn_lang_switch_content a').removeClass('selected');
		$(this).addClass('selected');
	})
})

function switch_lang(id){
	$('.lang_content_table:not(#'+id+'_table)').css('display', 'none');
	$('#'+id+'_table').css('display', 'block');
}
function OnFormSubmit(){
	var error = false;
	var must_subject = $('#<?php echo $must_subject;?>').val();
	var must_link = $('#link').val();
	if (!must_link){
		$('#error_link').css('display', 'block');
		error = true;
	}
	if (!must_subject){
		$('#error_subject').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();
		error = true;
	} 
	if (error == true){
		return
	} 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><?php 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 width="120">Section Type:</td>
                <td>
                  <select name="type" id="type" style="width:200px">
                      <option value="1" <?php if ($web==1){echo 'selected';}?>>News</option>
                      <option value="2" <?php if ($web==2){echo 'selected';}?>>Content</option>
                      <option value="3" <?php if ($web==3){echo 'selected';}?> >Photo</option>
                       <!-- added 2019-10-08-->
     <!--                  <option value="4" <?php if ($web==4){echo 'selected';}?>>Product</option> -->
                       <option value="4" <?php if ($web==4){echo 'selected';}?>>Doctor</option>
                      <option value="5" <?php if ($web==5){echo 'selected';}?>>Home</option>
                      <option value="6" <?php if ($web==6){echo 'selected';}?>>External Link</option>
                      <!-- added 2019-10-08-->
                      <option value="7" <?php if ($web==4){echo 'selected';}?>>Contact</option>
                  </select>
                  </td>
              </tr>
              <tr>
                <td width="120">Section Link:</td>
                <td>
                  <input name="link" type="text" size="50" id="link" value="<? if ($action=='add'){ echo '';} else {echo $row['link'];}?>"> (input "#" if it is an external link)  <span class="er_msg" id="error_link">Please fill in section link</span>
                  </td>
              </tr>
              <tr>
                <td width="120">External Link:</td>
                <td>
                  <input name="ext_link" type="text" size="50" id="ext_link" value="<? if ($action=='add'){ echo '';} else {echo $row['ext_link'];}?>"> <span class="er_msg" id="error_link">Please fill in section link</span>
                  (<strong style="color:red">http://</strong> or <strong style="color:red">https://</strong> is require to enter)</td>
              </tr>
              <tr>
                <td width="120">SEO Keywords:</td>
                <td>
                  <input name="seo_meta_key" type="text" size="80" value="<? if ($action=='add'){ echo '';} else {echo $row['seo_meta_key'];}?>"> (Seperate with comma)<br>
(leave it empty if you want to follow "General Setting&quot;s' meta tag)
                  </td>
              </tr>
              <tr>
                <td width="120">SEO Description:</td>
                <td><textarea name="seo_description" cols="80" rows="5"><? if ($action=='edit'){echo $row['seo_description'];}?>
                </textarea><br>
                  (leave it empty if you want to follow "General Setting&quot;s' meta tag) </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" id="error_subject">Please fill in name</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">分區標題:<br>
                    (建議最多12字)
                    </td>
                    <td><input name="cp_name" type="text" id="cp_name" size="50"   value="<? if ($action=='add'){ echo '';} else {echo $row['cp_name'];}?>"> </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">分區标题：<br>(建议最多12字)</td>
                    <td><input name="sp_name" type="text" id="sp_name" size="50"   value="<? if ($action=='add'){ echo '';} else {echo $row['sp_name'];}?>"></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">Section Name:<br>(Suggesting <br>Max. 30 words)</td>
                    <td><input name="ep_name" type="text" id="ep_name" size="50"  value="<? if ($action=='add'){ echo '';} else {echo $row['ep_name'];}?>">
                    </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="section_id" value="<?php echo $row['section_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>
