<?php
/**
* This plugin depends on XThreads.
* Most method used here uses XThreads method.
*
* XThreads official release thread:
*
*		http://mybbhacks.zingaburga.com/showthread.php?tid=288
*		Coded by: Yumi/ZiNgA BuRgA
*		@ http://mybbhacks.zingaburga.com
*/

if(!defined('IN_MYBB')) die();
function xt_ucp_main_info(){
	return array(
		'name'			=> 'UCP Main Page',
		'description'	=> '<strong style="color: red"><em><a href="http://mybbhacks.zingaburga.com/showthread.php?tid=288">XThreads</a> is required for this plugin.</em></strong><br />Load Custom Profile Fields input into User CP main page.',
		'website'		=> 'http://mybbhacks.zingaburga.com',
		'author'		=> 'XThreads Mania',
		'authorsite'	=> 'http://mybbhacks.zingaburga.com',
		'version'		=> '1.0',
		'compatibility' => '*',
		'guid'        	=> ''
	);
}

function xt_ucp_main_activate(){}
function xt_ucp_main_deactivate(){}

$plugins->add_hook('usercp_start','xt_ucp_main_usercp_start');
function xt_ucp_main_usercp_start(){
	global $mybb;
	if($mybb->settings['xt_proffields_ucp'] != 0){
		global $xtpfc,$cache;
		if(!$xtpfc) $xtpfc = $cache->read('xt_proffields');
		if($xtpfc){
			global $mybb,$user,$errors,$xtpf_minp,$xtpf_data,$lang;
			if($mybb->input['action'] == 'do_update_xt_proffields' && $mybb->request_method == 'post'){
				verify_post_check($mybb->input['my_post_key']);
				require_once MYBB_ROOT.'inc/datahandlers/user.php';
				$userhandler = new UserDataHandler('update');
				$userxtpf = array('uid'=>$mybb->user['uid']);
				foreach($xtpfc as $pfields => &$pfval){
					if(xthreads_user_in_groups($pfval['xt_proffields_editable']) && !$pfval['xt_proffields_cinp'] && !$mybb->input['profile_fields']['fid'.$pfval['fid']]){
						$mybb->input['profile_fields']['fid'.$pfval['fid']] = $mybb->user['fid'.$pfval['fid']];
					}
				}
				$userxtpf['profile_fields'] = $mybb->input['profile_fields'];
				$userhandler->set_data($userxtpf);
				if(!$userhandler->validate_user()){
					$errors = $userhandler->get_friendly_errors();
					$errors = inline_error($errors);
					$mybb->input['action'] = '';
				}else{
					$userhandler->update_user();
					if(!$lang->xt_proffields_updated) $lang->load('xt_proffields');
					redirect('usercp.php',$lang->xt_proffields_updated);
				}
			}
			$xtpf_minp = array();
			if(!$user) $user = $mybb->user;
			foreach($xtpfc as $uf => &$xtpf){
				if($xtpf['editable'] == 1){
					$code = '';
					if($xtpf['postnum'] && $xtpf['postnum'] > $user['postnum']){
						continue;
					}
					if(xthreads_user_in_groups($xtpf['xt_proffields_editable'])){
						$code = xt_proffields_inp($xtpf,$user,$errors,$vars);
						$xtpf_minp['fid'.$xtpf['fid']] = xt_proffields_cinp($xtpf,$vars);
					}
				}
			}
		}
	}
}
?>