<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'artexpand' );
if ( !isset( $_REQUEST['action'] ) ) $_REQUEST['action'] = '';
include SAXUE_ROOT_PATH . "/model/article_expand.php";
$expand_handler = &saxuearticleexpandhandler :: getinstance( "saxuearticleexpandhandler" );
switch ( $_REQUEST['action'] ) {
		case "add" :
				if ( isset( $_REQUEST['eid'] ) && is_numeric( $_REQUEST['eid'] ) ) {
						$expand = $expand_handler -> get( $_REQUEST['eid'] );
						if ( !$expand ) {
								saxue_printfail( '扩展属性不存在' );
						} 
						$row = $expand -> getvars( 'n' );
						$_isnew = false;
				} else {
						$_isnew = true;
				}
				if ( isset( $_POST['dosubmit'] ) ) {
						$_POST['name'] = strtolower( trim( $_POST['name'] ) );
						if ( strlen( $_POST['name'] ) == 0 ) {
								saxue_printfail( '属性名称不能为空' );
						} 
						if ( !preg_match( "/^[a-z]+$/i", $_POST['name'] ) ) {
								saxue_printfail( '属性名称格式错误' );
						} 
						if ( $_POST['name'] != $row['name'] ) {
								if ( 0 < $expand_handler -> getcount( new criteria( 'name', $_POST['name'] ) ) ) {
										saxue_printfail( '属性名称已存在' );
								} 
						} 
						$_POST['title'] = trim( $_POST['title'] );
						if ( strlen( $_POST['title'] ) == 0 ) {
								saxue_printfail( '属性标题不能为空' );
						} 
						if ( $_POST['type'] > 6 && empty( $_POST['options'] ) ) {
								saxue_printfail( '请填写可用选项' );
						} 
						if ( !isset( $_POST['cids'] ) || !is_array( $_POST['cids'] ) ) {
								$_POST['cids'] = '';
						} else {
								$_POST['cids'] = ',' . implode( ',', $_POST['cids'] ) . ',';
						} 
						$_POST['intro'] = trim( $_POST['intro'] );
						$_POST['listorder'] = intval( $_POST['listorder'] );
						$_POST['options'] = trim( $_POST['options'] );
						if ( !empty( $_POST['options'] ) ) {
								$options_ary = explode( ',', $_POST['options'] );
								$options = array();
								foreach( $options_ary as $val ) {
										$val = trim( $val );
										if ( strlen( $val ) == 0 ) continue;
										$options[] = $val;
								} 
								$_POST['options'] = implode( ',', $options );
						} else {
								$_POST['options'] = '';
						} 
						if ( $_POST['name'] != $row['name'] || $_POST['title'] != $row['title'] || $_POST['intro'] != $row['intro'] || $_POST['options'] != $row['options'] ) {
								$repack = true;
						} else {
								$repack = false;
						}
						$_POST['adminform'] = trim( $_POST['adminform'] );
						if ( $repack || strlen( $_POST['adminform'] ) == 0 ) {
								$_POST['adminform'] = $expand_handler -> adminform( $_POST );
						}
						$_POST['userform'] = trim( $_POST['userform'] );
						if ( $repack || strlen( $_POST['userform'] ) == 0 ) {
								$_POST['userform'] = $expand_handler -> userform( $_POST );
						} 
						if ( $_isnew ) {
								$newexpand = $expand_handler -> create();
						} else {
								$newexpand = $expand_handler -> create( false );
						} 
						if ( !$expand_handler -> insert( $newexpand ) ) {
								saxue_printfail( LANG_ERROR_DATABASE );
						} 
						saxue_jumppage( 'expand.php?action=cache', LANG_DO_SUCCESS . '<br>开始生成表单缓存' );
				} 
				saxue_getconfigs( 'cate', 'article' );
				$categorys = array();
				foreach ( $saxueCate['article'] as $cid => $cat ) {
						if ( $cat['child'] ) {
								$cat['selected'] = 'disabled';
						} elseif ( false !== strpos( $row['cids'], ',' . $cid . ',' ) ) {
								$cat['selected'] = 'selected';
						} else {
								$cat['selected'] = '';
						}
						$categorys[$cid] = $cat;
				}
				include SAXUE_ROOT_PATH . "/lib/util/tree.php";
				$tree = new tree;
				$tree -> icon = array( '&nbsp;│&nbsp;', '&nbsp;├&nbsp;', '&nbsp;└&nbsp;' );
				$tree -> nbsp = '&nbsp;';
				$tree -> mid = 'cid';
				$tree -> pid = 'pid';
				$tree -> init( $categorys );
				$str = "<option value=\$cid \$selected>\$spacer\$cname</option>";
				$cates = $tree -> get_tree( 0, $str );
				include SAXUE_ADMIN_PATH . "/header.php";
				$saxueTpl -> assign( "cates", $cates );
				$saxueTpl -> assign_by_ref( "row", $row );
				$saxueTpl -> assign_by_ref( "types", $expand_handler -> type() );
				$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/article/templates/expand_add.html";
				include SAXUE_ADMIN_PATH . "/footer.php";
				exit();
		case "delete" :
				if ( isset( $_REQUEST['eid'] ) && is_numeric( $_REQUEST['eid'] ) ) {
						$expand_handler -> delete( $_REQUEST['eid'] );
				} 
				saxue_jumppage( 'expand.php?action=cache', LANG_DO_SUCCESS . '<br>开始生成表单缓存' );
		case "listorder" :
				foreach( $_POST['listorder'] as $k => $v ) {
						$v = intval( $v );
						$expand_handler -> updatefields( array( 'listorder' => $v ), 'eid=' . $k );
				} 
				saxue_jumppage( 'expand.php?action=cache', LANG_DO_SUCCESS . '<br>开始生成表单缓存' );
		case "cache" :
				$criteria = new criteriacompo();
				$criteria -> setsort( "listorder" );
				$criteria -> setorder( "ASC" );
				$expand_handler -> queryobjects( $criteria );
				$expand = $adminform = $userform = array();
				while ( $v = $expand_handler -> getobject() ) {
						$cids = $v -> getvar( 'cids' );
						$cids = trim( $cids, ',' );
						if ( empty( $cids ) ) continue;
						$name = $v -> getvar( 'name' );
						$expand['info'][$name]['title'] = $v -> getvar( 'title' );
						$expand['info'][$name]['type'] = $v -> getvar( 'type' );
						$expand['info'][$name]['required'] = $v -> getvar( 'required' );
						$options = $v -> getvar( 'options' );
						$expand['info'][$name]['options'] = explode( ',', $options );
						$cids = explode( ',', $cids );
						$_adminform = $v -> getvar( 'adminform', 'n' );
						$_userform = $v -> getvar( 'userform', 'n' );
						foreach ( $cids as $cid ) {
								$expand['cate'][$cid][] = $name;
								$adminform[$cid] .= $_adminform . "\r\n";
								$userform[$cid] .= $_userform . "\r\n";
						}
				}
				if ( count( $adminform ) > 0 ) {
						$adminformpath = SAXUE_ROOT_PATH . '/data/adminform';
						saxue_checkdir( $adminformpath, true );
						foreach ( $adminform as $cid => $form ) {
								saxue_writefile( $adminformpath . '/' . $cid . '.html', $form );
						}
						$userformpath = SAXUE_ROOT_PATH . '/data/userform';
						saxue_checkdir( $userformpath, true );
						foreach ( $userform as $cid => $form ) {
								saxue_writefile( $userformpath . '/' . $cid . '.html', $form );
						}
				}
				saxue_setconfigs( "expand", $expand, 'article', 'saxueExpand', true );
				saxue_jumppage( 'expand.php', LANG_DO_SUCCESS );
} 
saxue_getconfigs( 'cate', 'article' );
if ( empty( $_REQUEST['pagesize'] ) || !is_numeric( $_REQUEST['pagesize'] ) || !in_array( $_REQUEST['pagesize'], array( '10', '30', '50', '100' ) ) ) $_REQUEST['pagesize'] = 30;
if ( empty( $_REQUEST['page'] ) || !is_numeric( $_REQUEST['page'] ) ) $_REQUEST['page'] = 1;
$sql = saxue_dbprefix( 'article_expand' ) . " WHERE 1";
if ( !empty( $_REQUEST['cid'] ) ) {
		$sql .= " AND cids LIKE '%," . $_REQUEST['cid'] . ",%'";
} 
if ( !empty( $_REQUEST['type'] ) ) {
		$sql .= " AND type=" . $_REQUEST['type'];
} 
if ( !empty( $_REQUEST['keyword'] ) && !empty( $_REQUEST['keytype'] ) ) {
		$sql .= " AND " . $_REQUEST['keytype'] . " LIKE '%" . $_REQUEST['keyword'] . "%'";
} 
$_res = $expand_handler -> db -> query( "SELECT COUNT(*) FROM " . $sql );
if ( !$_res ) {
		$resrows = 0;
} else {
		list( $resrows ) = $expand_handler -> db -> fetchrow( $_res );
} 
$sql = "SELECT * FROM " . $sql . " ORDER BY listorder ASC LIMIT " . ( ( $_REQUEST['page'] - 1 ) * $_REQUEST['pagesize'] ) . ", " . $_REQUEST['pagesize'];
$res = $expand_handler -> db -> query( $sql );
$rows = array();
$k = 0;
while ( $v = $expand_handler -> getobject( $res ) ) {
		$rows[$k] = $v -> getvars( "n" );
		$rows[$k]['catnum'] = substr_count( $rows[$k]['cids'], ',' );
		if ( $rows[$k]['catnum'] == 2 ) {
				$cid = str_replace( ',', '', $rows[$k]['cids'] );
				$rows[$k]['catname'] = $saxueCate['article'][$cid]['cname'];
		} 
		++$k;
} 
include_once( SAXUE_ROOT_PATH . "/lib/util/tree.php" );
$tree = new tree;
$tree -> icon = array( '&nbsp;│&nbsp;', '&nbsp;├&nbsp;', '&nbsp;└&nbsp;' );
$tree -> nbsp = '&nbsp;';
$tree -> mid = 'cid';
$tree -> pid = 'pid';
$tree -> init( $saxueCate['article'] );
$str = "<option value=\$cid \$selected>\$spacer\$cname</option>";
$cates = $tree -> get_tree( 0, $str, intval( $_REQUEST['cid'] ) );
include_once( SAXUE_ADMIN_PATH . "/header.php" );
$saxueTpl -> assign( "cates", $cates );
$saxueTpl -> assign_by_ref( "rows", $rows );
$saxueTpl -> assign_by_ref( "types", $expand_handler -> type() );
include_once( SAXUE_ROOT_PATH . "/lib/util/page.php" );
$jumppage = new saxuepage( $resrows, $_REQUEST['pagesize'], $_REQUEST['page'] );
$jumppage -> setlink( "", true, true );
$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/article/templates/expand.html";
include_once( SAXUE_ADMIN_PATH . "/footer.php" );
