<?php
defined('IN_HS') or exit('Access Denied');
$catid or showmessage($LANG['category_id_not_null'],$referer);
if($CAT['child'] && !$CAT['enableadd'])
{
	showmessage($LANG['appoint_category_not_allowed_add_picture'],'goback');
}

require HS_ROOT.'/admin/include/position.class.php';
$pos = new position($channelid);

if($dosubmit)
{
	if(empty($picture['title']))
	{
		showmessage($LANG['sorry_short_title_not_null'],'goback');
	}
	if(empty($picture['pictureurls']))
	{
		showmessage($LANG['sorry_picture_url_not_null'],'goback');
	}
	$picture['pictureurls'] = trim($picture['pictureurls']);

  if(strpos($picture['pictureurls'], "://"))
  {
  $picture['thumb'] = $picture['pictureurls'];
  }else{
  $picture['thumb'] = '/'.$HS['uploaddir'].'/'.$CHA['channeldir'].'/'.$MOD['upload_dir'].'/'.$picture['pictureurls'];
  }
	
	$picture['islink'] = isset($picture['islink']) ? 1 : 0;
	$picture['arrgroupidview'] = empty($picture['arrgroupidview']) ? '' : implode(',', $picture['arrgroupidview']);
	$picture['catid'] = $catid;
	$picture['username'] = $picture['editor'] = $picture['checker'] = $_username;
	$picture['urlruleid'] = $picture['ishtml'] ? $html_urlrule : $php_urlrule;
	$picture['addtime'] = $picture['edittime'] = $picture['checktime'] = preg_match('/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/', $picture['addtime']) ? strtotime($picture['addtime']) : $PHP_TIME;
	if(isset($picture['arrposid']))
	{
		$arrposid = $picture['arrposid'];
		$picture['arrposid'] = ','.implode(',', $arrposid).',';
	}

	$pictureid = $pic->add($picture);
	if($pictureid)
	{

		require HS_ROOT.'/include/attachment.class.php';
		$att = new attachment;
		$att->attachment($pictureid, $channelid, $catid);
		$att->add($picture['introduce']);

		if(isset($arrposid) && $arrposid) $pos->add($pictureid, $arrposid);


		$forward = "?mod=$mod&file=$file&action=add&channelid=$channelid&catid=$catid";
		if($picture['status'] == 3)
		{
			require HS_ROOT.'/include/create_related_html.inc.php';
		}
		showmessage($LANG['picture_add_success'], $forward);
	}
	else
	{
		showmessage($LANG['fail_add_picture'],'goback');
	}
}
else
{
	$today=date('Y-m-d H:i:s',$PHP_TIME);
	
	$style_edit = style_edit("picture[style]","");

	$this_category = str_replace("<option value='0'></option>",'',category_select('catid','',$catid));
	$showgroup = showgroup('checkbox','picture[arrgroupidview][]');
	$showskin = showskin('picture[skinid]');
	$showtpl = showtpl($mod,'content','picture[templateid]');
	$html_urlrule = urlrule_select('html_urlrule','html','item',$CAT['item_html_urlruleid']);
	$php_urlrule = urlrule_select('php_urlrule','php','item',$CAT['item_php_urlruleid']);
	$position = $pos->checkbox('picture[arrposid][]');
	include admintpl($mod.'_add');
}
?>