<?php	
define('IN_OUN', true);
include_once( "includes/command.php");
include_once(ROOT_PATH . 'class/cls_image.php');
$image = new cls_image($_CFG['bgcolor']);
include_once(ROOT_PATH.'includes/ckeditor/ckeditor.php'); 

if($Aconf['priveMessage'] != '')
{
   echo showMessage($Aconf['priveMessage']);
   exit;
}

if(!$_SESSION[$un_apraid])
{
   $strMessage = '此账号没有绑定经销商，不能操作！请通过管理员设置.<br/><br/><a href="adminuser.php">用户权限->基本资料编辑 ->账号属于经销商</a>';
   echo  showMessage($strMessage);
   exit;
}

if($_POST['act'] == 'insert' || $_POST['act'] == 'update' )
{
	$is_insert   = $_POST['act'] == 'insert';
	$myuser_id   = $_SESSION[$un_auser_id];
    if(trim($_POST[name]) == '' )
	{
		$strMessage = '标题及内容不能为空';
	}
	else
	{

 	    /* 处理主图 */
		$arti_thumb = $_POST['old_arti_thumb'];
		$min_thumb = $_POST['old_min_thumb'];
	    if($_FILES["arti_thumb"]["size"] > 0 )
	    {
		    /* 判断图像类型 */
            if (!$image->check_img_type($_FILES['arti_thumb']['type']))
            {
                $strMessage =  '图片类型错误,只支持 .jpg,.gif,.png格式.\n';
            }
		    else
		    {
			    if($_FILES["arti_thumb"]["size"] > settype($_POST['MAX_FILE_SIZE'], "integer"))
			    {
				    $strMessage =  '文件太大，不能上传：最大为2M.\n';
			    }
			    else
			    {		
                    /* 生成缩略图 */
                    $arti_thumb = $image->make_thumb($_FILES["arti_thumb"]['tmp_name'], 240,180);
                    /* 像册 */
					$thumb_url = $image->make_thumb($_FILES["arti_thumb"]['tmp_name'], 90, 60);
					$min_thumb = $thumb_url;
				    /* 原图 */
                    $filename = $image->upload_image($_FILES["arti_thumb"]);
			     }
		     }		  
	     } 

	     /* 像册图片处理 star */
         /* 检查图片：如果有错误，检查尺寸是否超过最大值；否则，检查文件类型 */
         if (isset($_FILES['img_url']['error'][0])) // php 4.2 版本才支持 error
         {
             // 最大上传文件大小
             $php_maxsize = ini_get('upload_max_filesize');
             $htm_maxsize = '2M';
             // 相册图片
             foreach ($_FILES['img_url']['error'] AS $key => $value)
             {				
                 if ($value == 0)
                {		
                    if (!$image->check_img_type($_FILES['img_url']['type'][$key]))
                    {
                       $strMessage = '文件类型错误:'.$key;
					   break;
                    }
                 }
                 elseif ($value == 1)
                {
                    $strMessage = '文件太大:'.$key.' '.$php_maxsize;
				    break;
                }
                elseif ($_FILES['img_url']['error'] == 2)
               {
                   $strMessage = '文件太大:'.$key.' '. $htm_maxsize;
			       break;
               }
             }
	      }
          /* 4。1版本 */
          else
          {
              // 相册图片
		      while( @list( $key, $value ) = @each( $_FILES['img_url']['tmp_name']) )
              {			
                  if ($value != 'none')
                  {				
                      if (!$image->check_img_type($_FILES['img_url']['type'][$key]))
                      {
                          $strMessage = '文件无效:'. $key + 1;
					      break;
                       }
                    }
                }
           }
	      /* 像册图片处理 end */

      //数据添加
	  if($is_insert)
	  {
	    /* 入库 */
		$db_table = $pre.'pravail_artitxt';
		$sql = "INSERT INTO " . $db_table . " ( `user_id` ,`praid` , `name` ,  `dateadd` ,`min_thumb`,`arti_thumb`, `states` , `domain_id` )" .
                 "VALUES ('$myuser_id','$_SESSION[$un_apraid]','$_POST[name]',  '".gmtime()."', '$min_thumb','$arti_thumb','0','$_SESSION[$un_domain_id]')"; 
        $oPub->query($sql);
        /* 编号 */
        $arid = $is_insert ? $oPub->insert_id() : $_POST['arid'];

        $db_table = $pre.'pravail_article';
		$sql = "INSERT INTO " . $db_table . " ( `arid` , `user_id` , `praid`, `name` , `descs` ,`dateadd` , `domain_id`  )" .
                 "VALUES ($arid,'$myuser_id','$_SESSION[$un_apraid]','$_POST[name]','$_POST[descs]', '".gmtime()."', '$_SESSION[$un_domain_id]')"; 
        $oPub->query($sql);
		/* tag */

		/* 相册 */
		if($filename && $thumb_url)
		{
		     $db_table = $pre.'pravail_arti_file';
		     $sql = "INSERT INTO " . $db_table . " ( `arid` ,  `filename` , `thumb_url`,`domain_id`  )" .
                    "VALUES ($arid,'$filename', '$thumb_url',$_SESSION[$un_domain_id])"; 
             $oPub->query($sql);
		 }
		$strMessage = '添加成功!';

	  }
      else if($_POST['act'] == 'update' && $_POST['arid'] > 0)
	  {
		$db_table = $pre.'pravail_artitxt';
		$arid = $_POST['arid']+0;
        $sql = "UPDATE " . $db_table . " SET 
			   `user_id`='$myuser_id' ,
               `name`='$_POST[name]' , 
			   `min_thumb`='$min_thumb',
			   `arti_thumb`='$arti_thumb' 
		        WHERE `arid` =".$arid." and `domain_id`=".$_SESSION[$un_domain_id];
        $oPub->query($sql);

		$db_table = $pre.'pravail_article';
        $sql = "UPDATE " . $db_table . " SET 
		       `user_id`='$myuser_id' ,
               `name` ='$_POST[name]', 
               `descs`= '$_POST[descs]'
		        WHERE `arid` =".$arid." and `domain_id`=".$_SESSION[$un_domain_id];
        $oPub->query($sql);

		/* 相册 */
		if($filename && $thumb_url)
		{
		     $db_table = $pre.'pravail_arti_file';
		     $sql = "INSERT INTO " . $db_table . " ( `arid` ,  `filename` , `thumb_url`,`domain_id`  )" .
                    "VALUES ($arid,'$filename', '$thumb_url',$_SESSION[$un_domain_id])"; 
             $oPub->query($sql);
		 }
		/* 编辑图片描述 old_img_desc */
		if (isset($_POST['old_img_desc']))
        {
			foreach ($_POST['old_img_desc'] AS $key => $val)
           {
				$db_table = $pre.'pravail_arti_file';
				$sql = "UPDATE " . $db_table . " SET `descs` = '$val' WHERE `fileid` =".$key;
                $oPub->query($sql);              
		   }
		}
        $strMessage = '修改成功！';
	  }
 	  /* 处理相册图片 */
      handle_gallery_imageprav($arid, $_FILES['img_url'], $_POST['namedesc']);
    }//if(trim($_POST[name]) == '')
	$_REQUEST[arid] = $arid;
	$_REQUEST['action'] = 'edit';

}


if($_REQUEST['action'] == 'edit' && $_REQUEST[arid])
{ 
	$arid = $_REQUEST[arid];
	$sql = "SELECT a.*,b.descs    
	        FROM ".$pre."pravail_artitxt as a,".$pre."pravail_article as b 
			where a.arid = b.arid 
			AND a.arid = '$arid'
			AND a.domain_id=$_SESSION[$un_domain_id]";
    $work = $oPub->selectrow($sql);
	/* 关联文章格式调整 */


	/* 像册列表 */
	$db_table = $pre.'pravail_arti_file';
    $sql = "SELECT * FROM " . $db_table . " WHERE arid = '$arid'";
    $img_list = $oPub->select($sql);
}

?>
<?php
include_once( "header.php"); 
if ($strMessage != '') {
	echo  '<SCRIPT language="javascript"> alert( "'.$strMessage.'");</script>';
}
?>

 
<table width="800" border="0" cellspacing="0" cellpadding="0" class="button">
<tr>
  <td align="middle">
	       <span style="float: left"><a href="prav_articlesend.php"> [添加促销信息]</a> </span>
	   <span style="float: right"> <a href="prav_articlelist.php"> [促销信息文章列表]</a></span>
 </td>
</tr>
</table>
<form action="" method="post" name="theForm" enctype="multipart/form-data" onsubmit="return validate();">
<TABLE width="800" border=0> 
  <TR class=bg1>
    <TD align=left> 
         <b>标题：</b><input type="text" name="name" value="<?php echo ($work["arid"] > 0)?$work["name"]:'';?>" size="50"/>
         <br/>
		 <b>缩图：</b>
		 <input type="file" name="arti_thumb"  size="20"/>
		 <span id="arti_thumb_show">
         <?php 
		 if($work["arti_thumb"])
		 {
			 $tmp = '<A HREF="../'.$work["arti_thumb"].'" target="_blank">';
			 $tmp .= '<IMG SRC="images/icon_view.gif" WIDTH="16" HEIGHT="16" BORDER="0" ALT="显示缩图"></A> ';
			 $tmp .= '<a href="javascript:;" onclick="if (confirm(\'删除\')) drop_pravail_artitxtImg(\''.$work["arid"].'\',\''.$work["arti_thumb"].'\')">';
			 $tmp .= '<IMG SRC="images/b_drop.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT="删除缩图"></A> ';
		     $tmp .= '<input type="hidden" name="old_arti_thumb" value="'.$work["arti_thumb"].'" />';
			 $tmp .= '<input type="hidden" name="old_min_thumb" value="'.$work["min_thumb"].'" />';
		 }
		 else
		 {
             $tmp = '<input type="hidden" name="old_arti_thumb" value="" />';
			 $tmp .= '<input type="hidden" name="old_min_thumb" value="" />';
		 }
		 echo $tmp;
		 ?>
		 </span>
		 (注：自动缩图尺寸240 × 180px)
 
	</TD> 
  </TR>
  <TR class=bg1>
    <TD align=left>
      <?php
		echo create_html_ck('descs',$work["descs"]);
	  ?> 
    </TD> 
  </TR>
</TABLE>
<!-- 附件 start -->
<DIV id=tabbar-div></DIV><!-- tab body -->
<TABLE id=gallery-table width="100%" align=center cellspacing="0" cellpadding="0">
 <tr>
   <td>
   相册：(<U>1.点“加号”可以批量上传多张图片；2.点提交按钮提交图片；3.单击相册列表中的缩图，可把原图添加到编辑器中</U>) 
     <div id="delimg_show" style="margin: 0">
    <?php
       while( @list( $k, $v ) = @each( $img_list) )
		{
    ?>
              <div id="gallery_<?php echo $v['fileid'];?>" style="float:left; text-align:center; border: 1px solid #DADADA; margin: 4px; padding:2px;">
                <a href="javascript:;" onclick="if (confirm('删除')) dropImg('<?php echo $v['fileid'];?>','<?php echo $v['arid'];?>')" title="删除">[-]</a><br />                
				<?php if($v['thumb_url'] != '') { ?>
				     <img src="../<?php  echo $v['filename'];?>" width="90" height="60"  border="0" title="插入编辑器" onclick="InsertHTML('insert<?php echo  $v['fileid'];?>');" id="insert<?php echo   $v['fileid'];?>" />
				<?php } else {?>
				     <div style="width:60;height:60"><br/><br/><a href="../<?php  echo $v['filename'];?>" target="_blank">查阅>></a></div>
				<?php  }?>
	                <br />
                <input type="text" value="<?php echo $v['descs'];?>" size="15" name="old_img_desc[<?php echo $v['fileid'];?>]" />
              </div>
   <?php
		}
	?>
	 </div>
    </TD> 
  </TR> 
  <TR>
    <TD>
	<A onclick=addImg(this) href="javascript:;">[+]</A> 
	描述:<INPUT TYPE="text" NAME=namedesc[] value="" size="30"/>
	地址:<INPUT type=file name=img_url[]> 
	</TD>
  </TR>
</TABLE>
  <!-- 附件 end -->
<TABLE width="96%" border=0>
  <TR class=even>
    <TD align=left>
	 <div id="cltion">
	 <?php echo $strCltion;?>
	 </div>
	 <div id="cltion_product">
	 <?php echo $strCltion_product;?>
	 </div>	
    </TD> 
  </TR>
  <TR class=bg1>
    <TD align=left>
       <input type="submit" value="<?php echo ($work["arid"] > 0)?'修改文章':'提交新文章';?>" style="background-color: #FFCC66;margin-left: 100px"/>
	   <input type="hidden" name="arid" value="<?php echo ($work["arid"] > 0)?$work["arid"]:0;?>" id="arid" />
       <input type="hidden" name="act" value="<?php echo ($work["arid"] > 0)?'update':'insert';?>" /> 
    </TD> 
  </TR> 
 </table>
</form>
<SCRIPT src="js/tab.js" type="text/javascript"></SCRIPT>
<SCRIPT src="js/utils.js" type="text/javascript"></SCRIPT>	
<SCRIPT src="../js/ajax.js" type="text/javascript"></SCRIPT>
<SCRIPT language=JavaScript>
  function addImg(obj)
  {
      var src  = obj.parentNode.parentNode;
      var idx  = rowindex(src);
      var tbl  = document.getElementById('gallery-table');
      var row  = tbl.insertRow(idx + 1);
      var cell = row.insertCell(-1);
      cell.innerHTML = src.cells[0].innerHTML.replace(/(.*)(addImg)(.*)(\[)(\+)/i, "$1removeImg$3$4-");
  } 
  /**
   * ~{I>3}M<F,IO4+~}
   */
  function removeImg(obj)
  {
      var row = rowindex(obj.parentNode.parentNode);
      var tbl = document.getElementById('gallery-table');

      tbl.deleteRow(row);
  } 

  /**
   * ~{I>3}M<F,~}
   */
  function dropImg(fileid,arid)
  {
    obj = "delimg_show";
	var strTemp = "ajax_prav_arti_delimg.php?fileid=" + fileid + "&arid=" + arid + "&op=delimg&action=edit";
	//alert(strTemp);
	//document.getElementById('gallery_' + fileid).style.display = 'none';
	send_request(strTemp);	
  }
  function drop_pravail_artitxtImg(arid,arti_thumb_file)
  {
    obj = "arti_thumb_show";
	var strTemp = "ajax_prav_arti_delimg.php?arid=" + arid + "&arti_thumb_file=" + arti_thumb_file;
	//alert(strTemp);
	//document.getElementById('gallery_' + fileid).style.display = 'none';
	send_request(strTemp);	
  }
function InsertHTML(a)
{
	var oEditor = CKEDITOR.instances.descs;
	//var value = document.getElementById( 'plainArea' ).value;
	//var value = document.getElementById( a ).innerHTML  
	var value = '<img src="' + document.images(a).src + '"/>';  
	if ( oEditor.mode == 'wysiwyg' )
	{
		// Insert the desired HTML.
		oEditor.insertHtml( value );
	}
	else
		alert( 'You must be on WYSIWYG mode!' );
} 
function validate()
{
  var frm          = document.forms['theForm'];
  var name = document.getElementById("name").value;  

  var msg = '';
  var reg = null;

  if( name.length < 4) {
	msg += '请输入文章标题' + '\n';
  } 
  if (msg.length > 0) {
	alert(msg);
	return false;
  }  else
  {
	return true;
  }
} 
</SCRIPT>
<?php 
/*
 * 保存某商品的相册图片
 * @param   int     $workid
 * @param   array   $image_files
 * @param   array   $image_descs
 * @return  void
 */
function handle_gallery_imageprav($arid, $image_files, $image_descs)
{
	global $image,$oPub,$pre,$_SESSION,$un_domain_id;
	$imgType = array(1 => 'image/gif', 2 => 'image/jpeg', 3 => 'image/png',4 => 'image/pjpeg');
	while( @list( $key, $img_desc ) = @each( $image_descs) )
    {
        /* 是否成功上传 */
        $flag = false;
        if (isset($image_files['error']))
        {
            if ($image_files['error'][$key] == 0)
            {
                $flag = true;
            }
        }
        else
        {
            if ($image_files['tmp_name'][$key] != 'none')
            {
                $flag = true;
            }
        }

        if ($flag)
        {
            // 生成缩略图
			//if( in_array($image_files['type'][$key],$imgType) )
			if($image->check_img_type($image_files['type'][$key]))
			{
				
               $thumb_url = $image->make_thumb($image_files['tmp_name'][$key], 80,  80);
			}
            $thumb_url = is_string($thumb_url) ? $thumb_url : '';

            $upload = array(
                'name' => $image_files['name'][$key],
                'type' => $image_files['type'][$key],
                'tmp_name' => $image_files['tmp_name'][$key],
                'size' => $image_files['size'][$key],
            );
            if (isset($image_files['error']))
            {
                $upload['error'] = $image_files['error'][$key];
            }
            $img_original = $image->upload_image($upload);
            $img_url = $img_original;

            $db_table = $pre.'pravail_arti_file';
            $sql = "INSERT INTO " . $db_table. " (arid, filename,thumb_url,descs,domain_id) " .
                    "VALUES ('$arid', '$img_url', '$thumb_url','$img_desc',$_SESSION[$un_domain_id])";
            $oPub->query($sql);
        }
    }
}

?>
<?php
include_once( "footer.php");
?>

