<?php
include("include/config.php");
include("include/protect.class.php");
include("include/jdf.php");
include('include/functions.php');
include("include/thumbnail_create.php");

$prot=new protect();
if ($prot->showPage) {
$db=new DB($globalConfig['dbase'], $globalConfig['dbhost'],$globalConfig['dbuser'], $globalConfig['dbpass']);
mysql_query("SET NAMES utf8");
extract($_GET);
extract($_POST);

if($_POST['edit']){
			if ($_FILES['myfile']['name'] =='') {
				$pic=$_POST['recent'];
				} else {
						move_uploaded_file($_FILES['myfile']['tmp_name'],'../upload/up_article/'.$_FILES['myfile']['name']) or die ("NOT MOVED");
						$picname=explode(".",$_FILES['myfile']['name']);
						$pic=time().'.'.$picname[1];
						$thumb_name1 = new Thumbnail('../upload/up_article/'.$_FILES['myfile']['name'],120,90,'../upload/up_article/k_'.time().'.'.$picname[1],85,'"round_edges(0,\'000000\',1)"');
						
						$motavaset_name1 = new Thumbnail('../upload/up_article/'.$_FILES['myfile']['name'],200,150,'../upload/up_article/big_'.time().'.'.$picname[1],85,'"round_edges(0,\'000000\',1)"');
												
						unlink('../upload/up_article/'.$_FILES['myfile']['name']);
						unlink('../upload/up_article/k_'.$_POST['recent']);
						unlink('../upload/up_article/big_'.$_POST['recent']);
				}	
	$sql="update article set
	title='".$db->safe_string($_POST['title'])."',
	Source='".$db->safe_string($_POST['Source'])."',
	ShortDescription='".$db->safe_string($_POST['ShortDescription'])."',
	description='".$db->safe_string(addslashes($_POST['description']))."',
	file='".$pic."',
	date=now()
	where aID='".$_POST['aID']."' ";
    $query=$db->query($sql);
}elseif($_POST['send']){			
			if ($_FILES['myfile']['name'] =='') {
				$pic=$_POST['recent'];
				} else {
						move_uploaded_file($_FILES['myfile']['tmp_name'],'../upload/up_article/'.$_FILES['myfile']['name']) or die ("NOT MOVED");
						$picname=explode(".",$_FILES['myfile']['name']);
						$pic=time().'.'.$picname[1];
						$thumb_name1 = new Thumbnail('../upload/up_article/'.$_FILES['myfile']['name'],120,90,'../upload/up_article/k_'.time().'.'.$picname[1],85,'"round_edges(0,\'000000\',1)"');
						
						$motavaset_name1 = new Thumbnail('../upload/up_article/'.$_FILES['myfile']['name'],200,150,'../upload/up_article/big_'.time().'.'.$picname[1],85,'"round_edges(0,\'000000\',1)"');
												
						 unlink('../upload/up_article/'.$_FILES['myfile']['name']);
				}				
			
			
			
		$query=$db->execute("insert into article set
													title='".$db->safe_string($_POST['title'])."',
													Source='".$db->safe_string($_POST['Source'])."',
													ShortDescription='".$db->safe_string($_POST['ShortDescription'])."',
													description='".addslashes($_POST['description'])."',
													date=now(),
													file='".$pic."' ");
}

if ($_GET['delid']){
	$filename = "SELECT file FROM article WHERE aID = '".$_GET['delid']."'";
	$picname = $db->query($filename) or die ("Bad Query<br>".$filename);
	$array = $db->fetchNextArray($picname);
	unlink('../upload/up_article/k_'.$array['file']);
	unlink('../upload/up_article/big_'.$array['file']);
	$dell=$db->execute("delete from article where aID='".$_GET['delid']."'");    	
}


$page = $_GET['page'];
$limit = $_GET['limit'];
if (!($limit)){
$limit =20;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
$numresults = $db->query("select * from article  "); // the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
$pages = intval($numrows/$limit); // Number of results pages.
// $pages now contains int of pages, unless there is a remainder from division.
if ($numrows%$limit) {
$pages++;} // has remainder so add one page
$current = ($page/$limit) + 1; // Current page number.
if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.
else {
$total = $pages;} // Else total pages is $pages value.
$first = $page + 1; // The first result.
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
else{
$last = $numrows;} // If last results page, last result equals total number of results.
//escape from PHP mode.
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> مدیریت مقاله</title><link href="css/dropdown/dropdown.vertical.rtl.css" media="all" rel="stylesheet" type="text/css" />
<link href="css/dropdown/themes/default/default.css" media="all" rel="stylesheet" type="text/css" />

<?php include("maintemplate/mainheader.php");?>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
</head>
<body>
<div id="wpwrap">
<div id="wpcontent">
	<?php include("include/menutop.php");?> 
<div id="wpbody">
	<?php include("include/header.php");?>
    <div style="overflow:hidden;" id="wpbody-content">
        <div id="screen-meta">
            <div class="wrap">
                <div id="icon-index" class="icon32"><br></div>
                <h2>مدیریت مقاله </h2>
                <div id="dashboard-widgets-wrap">
                    <div id="dashboard-widgets" class="metabox-holder">
                        <div class="postbox-container" style="width:100%;">
                            <div id="normal-sortables" class="meta-box-sortables ui-sortable"><div id="dashboard_recent_comments" class="postbox ">  
                    <div class="handlediv" title="برای جانشینی کلیک کنید"><br></div><h3 class="hndle"><span>مدیریت مقاله   </span></h3>
<?php
$query=$db->query("select * from article  where aID='".$_GET['aID']."'");
$num=$db->numRows($query);
$row=$db->fetchNextArray($query);
?>
<form  action="adminArticle.php" method="post" enctype="multipart/form-data" name="form1" id="form1">

  <table width="100%" border="0" align="center" class="table_insert" cellpadding="3" cellspacing="3"  dir="rtl">
    <tr>
      <td align="right"   dir="rtl" width="150px">عنوان مقاله</td>
      	<td>
        	<input type="text" name="title" id="title" value="<?=$row['title']?>" />
        </td>
    </tr>       
          
    <tr>
      <td align="right"   dir="rtl">منبع مقاله  :</td>
      <td >
        <input name="Source" type="text" size="43"  value="<?=$row['Source']?>" dir="rtl"  /></td>
    </tr>
    
    
    <tr>
      <td  dir="rtl" align="right">شرح مختصر</td>
      <td >
        <textarea name="ShortDescription"  cols="70" >  <?=$row['ShortDescription']?></textarea></td>
    </tr>
     
    <tr>
      <td  dir="rtl" align="right">شرح </td>
      <td >
        <textarea name="description"  cols="70" >  <?=$row['description']?></textarea>
        
			<script type="text/javascript">
			//<![CDATA[

				CKEDITOR.replace( 'description',
					{
						fullPage : true,
						extraPlugins : 'docprops'
					});

			//]]>
			</script>         
        
        </td>
    </tr>     
     
     <tr>
         <td>
          تصویر :         
         </td>
       <td width="260" ><input name="myfile" id="myfile"  type="file" size="31"  dir="rtl" /><input type="hidden" value="<?php echo $row['file'];?>" name="recent" /></td>
    </tr>

    <tr>
      <td  colspan="2" align="center" >
       <?php if($num){
			  echo ' <input type="hidden" value="'.$_GET['aID'].'"  name="aID" />
      		       <input type="submit" id="button" name="edit" value="ویرایش"  />';}
			 else {  
      		  echo'<input type="submit" id="button" name="send"  value="ذخیره"  />';}
	 ?>
        </td>
    </tr>
  </table>
</form>  
 <table width="100%"  border="0" align="center" cellpadding="0" cellspacing="0"  dir="rtl"  class="table1" >
        <tr>
          <th class="td_header"> تصویر</th>
          <th class="td_header">عنوان</th>
          <th class="td_header">منبع</th>
          <th class="td_header">ویرایش</th>
          <th class="td_header">حذف</th>
          
        </tr>
<?php
$sql="select * from article order by aID desc LIMIT $page, $limit";
$query = $db->query($sql);
while($catarray = $db->fetchNextArray($query)){ 

echo '<tr align=center dir=rtl>
<td class="tablebattom1"><img src="../upload/up_article/k_'.$catarray['file'].'" width="100px" /> </td> 
<td class="tablebattom1">'.$catarray['title'].'</td>
<td class="tablebattom1">'.$catarray['Source'].' </td>
<td class="tablebattom1"><a href="adminArticle.php?aID='.$catarray['aID'].'"><img border="0" src="images/edit.gif"></a></td>
<td class="tablebattom1"><a href="adminArticle.php?delid='.$catarray['aID'].'"><img border="0" src="images/dell.gif"></a></td></tr>';
}
?>
</table>
<?php
            echo '<p align="center" style="margin-top:20px;">';

            if ($page != 0) { // Don't show back link if current page is first page.
                $back_page = $page - $limit;
                echo ("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\" class=\"pages\">قبلی</a>    \n");
            }

            for ($i = 1; $i <= $pages; $i++) // loop through each page and give link to it.
                {
                $ppage = $limit * ($i - 1);
                if ($ppage == $page) {
                    echo ("<b class=\"pages_on\">" . $i . "</b> \n");
                } // If current page don't give link, just text.
                else {
                    echo ("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\" class=\"pages\">" .
                        $i . "</a> \n");
                }
            }

            if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
                $next_page = $page + $limit;
                echo ("<a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\" class=\"pages\">بعدی</a>\n");
            }
            echo '</p>';
?>



                           </div>
                            </div>
                        </div>                        
                    </div>
                    <div class="clear"></div>
                </div><!-- dashboard-widgets-wrap -->
            </div><!-- wrap -->
			<div class="clear"></div></div><!-- wpbody-content -->
			<div class="clear"></div></div><!-- wpbody -->
			<div class="clear"></div></div><!-- wpcontent -->
        </div>
    </div><!-- wpcontent -->
</div><!-- wpwrap -->
<?php include("include/footer.php");?>
</div>
</div>
</body>
</html><?php } ?>