<?php

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

//include(DISCUZ_ROOT.'source/discuz_version.php');
//
//if(version_compare(DISCUZ_VERSION, 'X2.5') <= 0) {
//	include 'includes/editpost/editpost_25.php';
//	exit;
//}
include 'includes/editpost/get_editpost.php';
require_once libfile('function/forumlist');



odz_loadforum();

//$tmp = array_keys($_G['forum']['threadsorts']['types']);

//print_R($_G['forum']['threadsorts']);exit;

//$sortid = isset($tmp[0])?$tmp[0]:'';


$thread = array('readperm' => '', 'pricedisplay' => '', 'hiddenreplies' => '');

$threadtypes = array();

    if($_G['forum']['ismoderator'] || empty($_G['forum']['threadtypes']['moderators'][$thread['typeid']])) {

        $arr = $_G['forum']['threadsorts'];

        $threadtypes['required'] = $arr['required'];

        if($threadtypes['required'] == 0){

                $threadtypes['required'] = 'false';

        }

        if($threadtypes['required'] == 1){

                $threadtypes['required'] = 'true';

        }

        foreach($arr['types'] as $key => $value) {

            if( $arr['moderators'][$key] == 1 && !$_G['forum']['ismoderator']){

                continue;

            }

            $threadtypes['types'][] = array('sortid'=>$key,'optionlist'=>getoptionlist($key),'name'=>strip_tags($value));
            // $threadtypes['types']['optionlist']=getoptionlist($key);

        }

    }

$tmp = array_keys($_G['forum']['threadsorts']['types']);

$sortid = isset($tmp[0])?$tmp[0]:'';



$thread = array('readperm' => '', 'pricedisplay' => '', 'hiddenreplies' => '');

$threadtypes1 = array();

    $threadtypes1['sortid'] = $sortid;

    if($_G['forum']['ismoderator'] || empty($_G['forum']['threadtypes']['moderators'][$thread['typeid']])) {

        $arr = $_G['forum']['threadtypes'];

        $threadtypes1['required'] = $arr['required'];

            if($threadtypes1['required'] == 0){

                $threadtypes1['required'] = 'false';

            }

            if($threadtypes1['required'] == 1){

                $threadtypes1['required'] = 'true';

            }

        foreach($arr['types'] as $key => $value) {

            if( $arr['moderators'][$key] == 1 && !$_G['forum']['ismoderator']){

                continue;

            }

            $threadtypes1['types'][] = array('id'=>$key, 'name'=>strip_tags($value));

        }

    }

 odz_result(array_merge(array('fronttypes'=>$threadtypes,'threadtypes'=>$threadtypes1),$result));
//echo json_encode(array_merge(array('errcode'=>0,'errmsg'=>'','fronttypes'=>$threadtypes,'threadtypes'=>$threadtypes1),$result));
	//odz_result(array('fronttypes'=>$threadtypes,'threadtypes'=>$threadtypes1));


function genCate_select($array,$parentid=0){

            $result = array();

            foreach($array as $key => $val){

                if($val['parentid'] == $parentid) {

                $tmp = $array[$key];

                unset($array[$key]);

                count(genCate_select($array,$val['id'])) > 0 && $tmp['children'] = genCate_select($array,$val['id']);

                $result[] = $tmp;

                }

            }

            return $result;

        }

function getoptionlist($sortid){
        $optionlist=array();

        if($sortid){

            $typevar= C::t('forum_typevar')->fetch_all_by_sortid($sortid,'ASC');

            $optionlist = array();

            foreach($typevar as $key=>$option) {

                $query= DB::fetch_all('SELECT * FROM '.DB::table('forum_typeoption')." WHERE optionid = ".$option['optionid']);

//              $query = C::t('forum_typeoption')->fetch_all_by_optionid($option['optionid']);

                $query[0]['required']=   $option['required'];

                $query[0]['protect']=   unserialize($query[0]['protect']);

                if($query[0]['rules']){

                    $query[0]['rules']  =   unserialize($query[0]['rules']);

                }else{

                    unset($query[0]['rules']);

                }

                if($query[0]['type']=='radio'||$query[0]['type']=='checkbox'){



                   $string= explode("\r", $query[0]['rules']['choices']);

                   $strings='';

                   foreach($string as $ks=>$val) {

                       $string1= explode("=", $val);

                       if(!$string1[1]) continue;

                        $strings['choices'][$ks]=$string1[1];

                   }

                   $query[0]['rules']=$strings;

                }

                if($query[0]['type']=='select'){

                   $string= explode("\r", $query[0]['rules']['choices']);

                   $strings='';

                   foreach($string as $ks=>$val) {

                       $choices=$val;

                       $p=$ks;

                       if(!$choices) continue;

                       $strings[$p]=$choices;

                   }

                   $query[0]['rules']['choices']=$strings;

                }

                $optionlist[]=$query[0];
            }


        }
         return $optionlist;

}


// function clear_blank($str, $glue=''){
//     $replace = array(" ", "\r", "\n", "\t"); return str_replace($replace, $glue, $str);
// }
?>