<?php
include_once("header.php");
if($login==1)
{
if($action=="add")
{
  if(isset($submit))
  {
   if(in_array(strtolower(end(explode(".",$_FILES['pic']['name']))), $pic_types)
            && strlen($_FILES['pic']['name'])!=0
            &&move_uploaded_file($_FILES["pic"]["tmp_name"],"../up/works/".time().$_FILES["pic"]["name"]))
               {
                $pic_name = time().$_FILES["pic"]["name"];
               }
    mysql_query("insert into works values('','$name','$pic_name','$link','$type',
    '$start','$end','$client_name','$plan','$comment','1')");
    $msg="تم الاضافه بنجاح";
   $sub=works();
    header( 'refresh: 2; url=works.php' );
  }
  else
  {
     $planquery=mysql_query("select * from host_plan");
     while($planrow=mysql_fetch_array($planquery))
     {
        extract($planrow);
        $plans .="<option value=$host_id>$host_name</option>";
     } 
    $sub=Template_Array("$site_template/admin/works_add.html",0);
  }
}
elseif($action=="edit")
{
  if(isset($submit))
  {
     
      mysql_query("update works set name='$name', link='$link',
       type='$type',start_host='$start',end_host='$end',
      client_name='$client_name',client_plan='$plan',comment='$comment' where work_id=$id");
    $msg="تم التعديل";
   $sub=works();
  }
  else
  {
    $editqu=mysql_query("select * from works where work_id='$id'");
    while($row=mysql_fetch_array($editqu))
    {
      extract($row);
    }
    $planquery=mysql_query("select * from host_plan");
     while($planrow=mysql_fetch_array($planquery))
     {
        extract($planrow);
        $plans .="<option value=$host_id>$host_name</option>";
     }
     $sub = Template_Array("$site_template/admin/works_edit.html",0);
  }
}
elseif($action=="editpic")
{
  if(isset($submit))
  {
       if(in_array(strtolower(end(explode(".",$_FILES['pic']['name']))), $pic_types)
            && strlen($_FILES['pic']['name'])!=0
            &&move_uploaded_file($_FILES["pic"]["tmp_name"],"../up/works/".time().$_FILES["pic"]["name"]))
              {
                $pic_name = time().$_FILES["pic"]["name"];
               }
    mysql_query("update works set pic='$pic_name' where work_id='$id'");
    $msg="تم تعديل الصوره بنجاح";
    $sub=works();
  }
  else
  {
      $sub = Template_Array("$site_template/admin/works_edit.html",1);
  }
}
elseif($action=="delete")
{
  mysql_query("delete from works where work_id='$id'");
  $msg="تم الحذف بنجاح";
  $sub=works();
}
else
{
  $sub = works(); 
}
}
else
{
    echo Template_Array("$site_template/admin/login.html");
    die();
}
 include_once("footer.php");
 ?>