<?php

$plugins->add_hook('member_profile_end', 'profilemycode_run');
$plugins->add_hook('admin_profilefields_do_add', 'profilemycode_admin_do_edit');
$plugins->add_hook('admin_profilefields_do_edit', 'profilemycode_admin_do_edit');
$plugins->add_hook('admin_profilefields_add', 'profilemycode_admin_add');
$plugins->add_hook('admin_profilefields_edit', 'profilemycode_admin_edit');

function profilemycode_info()
{
	return array(
		'name'			=> 'MyCode in Profile Fields',
		'description'	=> 'Gives admins the option to allow MyCode to be used in custom profile fields.',
		'website'		=> 'http://mybbhacks.zingaburga.com/',
		'author'		=> 'ZiNgA BuRgA',
		'authorsite'	=> 'http://zingaburga.com/',
		'version'		=> '1.0'
	);
}

function profilemycode_activate()
{
	global $db;
	$db->query('ALTER TABLE '.TABLE_PREFIX.'profilefields ADD (`allowmycode` CHAR(3) NOT NULL DEFAULT "no")');
}

function profilemycode_deactivate()
{
	global $db;
	$db->query('ALTER TABLE '.TABLE_PREFIX.'profilefields DROP `allowmycode`');
}

function profilemycode_admin_add()
{
	global $lang;
	cpheader();
	startform("profilefields.php", "" , "do_add");
	starttable();
	tableheader($lang->new_custom_field);
	makeinputcode($lang->field_name, "name");
	maketextareacode($lang->field_description, "description");
	makeinputcode($lang->field_max_length, "maxlength");
	makeinputcode($lang->field_length, "length", 20);
	makeinputcode($lang->field_disporder, "disporder", "", 4);
	makelabelcode($lang->field_type, "<select name=\"type\"><option value=\"text\">$lang->field_type_textbox</option><option value=\"textarea\">$lang->field_type_textarea</option><option value=\"select\">$lang->field_type_select</option><option value=\"multiselect\">$lang->field_type_multiselect</option><option value=\"radio\">$lang->field_type_radio</option><option value=\"checkbox\">$lang->field_type_checkbox</option></select>");
	maketextareacode($lang->field_options, "options", "", 6, 50);
	makeyesnocode($lang->field_required, "required", "no");
	makeyesnocode($lang->field_editable, "editable", "yes");
	makeyesnocode($lang->field_hidden, "hidden", "no");
	makeyesnocode('Allow MyCode<br /><small>Allow users to enter MyCode in the field (only works if field type is Textbox or Textarea)</small>', "allowmycode", "no");
	endtable();
	endform($lang->add_field, $lang->reset_button);
	cpfooter();
	
	exit;
}

function profilemycode_admin_edit()
{
	global $lang, $profilefield, $typesel;
	cpheader();
	startform("profilefields.php", "" , "do_edit");
	makehiddencode("fid", $profilefield['fid']);
	starttable();
	$lang->edit_custom_field = sprintf($lang->edit_custom_field, $profilefield['name']);
	tableheader($lang->edit_custom_field);
	makeinputcode($lang->field_name, "name", "$profilefield[name]");
	maketextareacode($lang->field_description, "description", $profilefield['description']);
	makeinputcode($lang->field_max_length, "maxlength", $profilefield['maxlength']);
	makeinputcode($lang->field_length, "length", $profilefield['length']);
	makeinputcode($lang->field_disporder, "disporder", $profilefield['disporder'], 4);
	makelabelcode($lang->field_type, "<select name=\"type\"><option value=\"text\"{$typesel['text']}>$lang->field_type_textbox</option><option value=\"textarea\"{$typesel['textarea']}>$lang->field_type_textarea</option><option value=\"select\"{$typesel['select']}>$lang->field_type_select</option><option value=\"multiselect\"{$typesel['multiselect']}>$lang->field_type_multiselect</option><option value=\"radio\"{$typesel['radio']}>$lang->field_type_radio</option><option value=\"checkbox\"{$typesel['checkbox']}>$lang->field_type_checkbox</option></select>");
	maketextareacode($lang->field_options, "options", $options, 6, 50);
	makeyesnocode($lang->field_required, "required", $profilefield['required']);
	makeyesnocode($lang->field_editable, "editable", $profilefield['editable']);
	makeyesnocode($lang->field_hidden, "hidden", $profilefield['hidden']);
	makeyesnocode('Allow MyCode<br /><small>Allow users to enter MyCode in the field (only works if field type is Textbox or Textarea)</small>', "allowmycode", $profilefield['allowmycode']);
	endtable();
	endform($lang->edit_field, $lang->reset_button);
	cpfooter();

	exit;
}

function profilemycode_admin_do_edit()
{
	global $sqlarray, $mybb;
	if($mybb->input['allowmycode'] == 'yes') $sqlarray['allowmycode'] = 'yes';
	else $sqlarray['allowmycode'] = 'no';
}

function profilemycode_run()
{
	// unfortunately, we have to redo all the profile processing code... :(
	global $db, $profilefields, $field_hidden, $templates, $lang, $theme, $userfields;
	if(!$profilefields) return;
	$query = $db->simple_select(TABLE_PREFIX."profilefields", "*", "{$field_hidden}", array('order_by' => 'disporder'));
	$bgcolor = "trow1";
	while($customfield = $db->fetch_array($query))
	{
		$thing = explode("\n", $customfield['type'], "2");
		$type = trim($thing[0]);

		$field = "fid$customfield[fid]";
		$useropts = explode("\n", $userfields[$field]);
		$customfieldval = $comma = '';
		if(is_array($useropts) && ($type == "multiselect" || $type == "checkbox"))
		{
			foreach($useropts as $val)
			{
				if($val != '')
					$customfieldval .= "<li style=\"margin-left: 0;\">{$val}</li>";
			}
			if($customfieldval != '')
				$customfieldval = "<ul style=\"margin: 0; padding-left: 15px;\">{$customfieldval}</ul>";
		}
		elseif($customfield['allowmycode'] == 'yes')
		{
			global $parser;
			if(!is_object($parser)) // compatibiliy with preparser mod
			{
				require_once MYBB_ROOT.'inc/class_parser.php';
				$parser = new postParser;
			}
			$customfieldval = $parser->parse_message($userfields[$field], array(
				'allow_html' => 'no',
				'allow_mycode' => 'yes',
				'allow_smilies' => 'yes',
				'allow_imgcode' => 'yes'
			));
			if($customfield['type'] != "textarea")
				// strip out <br> tags
				$customfieldval = strtr($customfieldval, array('<br>' => '', '<br />' => ''));
		}
		else
		{
			if($customfield['type'] == "textarea")
				$customfieldval = nl2br(htmlspecialchars_uni($userfields[$field]));
			else
				$customfieldval = htmlspecialchars_uni($userfields[$field]);
		}
		eval("\$customfields .= \"".$templates->get("member_profile_customfields_field")."\";");
		$bgcolor = alt_trow();
	}
	if($customfields)
	{
		eval("\$profilefields = \"".$templates->get("member_profile_customfields")."\";");
	}
}

?>