<?php
include_once("header.php");
if($login==1)
{
if($action=="add")
{
  if(isset($submit)&&$place!=0)
  {
   if(in_array(strtolower(end(explode(".",$_FILES['pic']['name']))), $pic_types)
            && strlen($_FILES['pic']['name'])!=0
            &&move_uploaded_file($_FILES["pic"]["tmp_name"],"../up/banner/".time().$_FILES["pic"]["name"]))
               {
                $pic_name = time().$_FILES["pic"]["name"];
               }
    mysql_query("insert into ads values('','$name','$link','$pic_name','$comment','$place','1')");
    $msg="تم الاضافه بنجاح";
    $sub =ads();
    header( 'refresh: 2; url=ads.php' );
  }
  else
  {
     if(isset($submit)&&$place==0)
     {
       $msg="يرجى تحديد مكان العرض";
     }
      $textarea="comment";
    $sub=Template_Array("$site_template/admin/ads_add.html",0);
  }
}
elseif($action=="edit")
{
  if(isset($submit)&&$place!=0)
  {
      if(in_array(strtolower(end(explode(".",$_FILES['pic']['name']))), $pic_types)
            && strlen($_FILES['pic']['name'])!=0
            &&move_uploaded_file($_FILES["pic"]["tmp_name"],"../up/banner/".time().$_FILES["pic"]["name"]))
              {
                $pic_name = time().$_FILES["pic"]["name"];
               }
      mysql_query("update ads set name='$name',link='$link',
    pic='$pic_name',comment='$comment',view_place='$place' where id=$id");
    $msg="تم التعديل";
    $sub=ads();
  }
  else
  {
    $editqu=mysql_query("select * from ads where id='$id'");
    while($row=mysql_fetch_array($editqu))
    {
      extract($row);
    }
    if(isset($submit)&&$place==0)
     {
       $msg="يرجى تحديد مكان العرض";
     }
    $textarea="comment";
     $sub = Template_Array("$site_template/admin/ads_add.html",0);
  }
}
elseif($action=="delete")
{
  mysql_query("delete from ads where id='$id'");
  $msg="تم الحذف بنجاح";
  $sub=ads();
}
elseif($action=="state")
{
    $pages_query = mysql_query("SELECT * FROM ads where id='$id'",$connection);
        $pages_fetch = mysql_fetch_array($pages_query);
        extract($pages_fetch);
        if($state==1)
         {
            $state=0;
            $msg = "تم التعطيل";
         }
        else
         {
           $state=1;
           $msg = "تم التنشيط ";
         }
        mysql_query("update ads set state='$state' where id='$id'"); 
        $sub = ads();
}
else
{
  $sub = ads(); 
}
}
else
{
    echo Template_Array("$site_template/admin/login.html");
    die();
}
 include_once("footer.php");
 ?>