<?php

/**
 * [Discuz!] (C)2001-2099 Discuz! Team
 * This is NOT a freeware, use is subject to license terms
 * https://license.discuz.vip
 */

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

$magicid = intval($_GET['magicid']);
$magic = table_common_magic::t()->fetch($magicid);

if(!submitcheck('magiceditsubmit')) {

	$magicperm = dunserialize($magic['magicperm']);

	$groups = $forums = [];
	foreach(table_common_usergroup::t()->range() as $group) {
		$groups[$group['groupid']] = $group['grouptitle'];
	}

	$typeselect = [$magic['type'] => 'selected'];

	shownav('extended', 'magics', 'admin');
	showchildmenu([['nav_magics', 'magics']], $magic['name'].'('.$magic['identifier'].')');

	echo '<br />';

	$eidentifier = explode(':', $magic['identifier']);
	if(count($eidentifier) > 1 && preg_match('/^[\w\_:]+$/', $magic['identifier'])) {
		include_once DISCUZ_PLUGIN($eidentifier[0]).'/magic/magic_'.$eidentifier[1].'.php';
		$magicclass = 'magic_'.$eidentifier[1];
	} else {
		require_once libfile('magic/'.$magic['identifier'], 'class');
		$magicclass = 'magic_'.$magic['identifier'];
	}

	$magicclass = new $magicclass;
	$magicsetting = $magicclass->getsetting($magicperm);
	echo '<div class="colorbox"><h4>'.lang('magic/'.$magic['identifier'], $magicclass->name).'</h4>'.
		'<table cellspacing="0" cellpadding="3"><tr><td>'.
		(count($eidentifier) > 1 ? (file_exists(DISCUZ_PLUGIN($eidentifier[0]).'/magic/magic_'.$eidentifier[1].'.gif') ? '<img src="source/plugin/'.$eidentifier[0].'/magic/magic_'.$eidentifier[1].'.gif" />' : '')
			: (file_exists(DISCUZ_ROOT.'./static/image/magic/'.$magic['identifier'].'.gif') ? '<img src="static/image/magic/'.$magic['identifier'].'.gif" />' : '')).
		'</td><td valign="top">'.lang('magic/'.$magic['identifier'], $magicclass->description).'</td></tr></table>'.
		'<div style="width:95%" align="right">'.lang('magic/'.$magic['identifier'], $magicclass->copyright).'</div></div>';
	$credits = [];
	foreach($_G['setting']['extcredits'] as $i => $extcredit) {
		$credits[] = [$i, $extcredit['title']];
	}

	showformheader('magics&operation=edit&magicid='.$magicid);
	showtableheader();
	showtitle('');
	showsetting('magics_edit_name', 'namenew', $magic['name'], 'text');
	showsetting('magics_edit_credit', ['creditnew', $credits], $magic['credit'], 'select');
	showsetting('magics_edit_price', 'pricenew', $magic['price'], 'text');
	showsetting('magics_edit_num', 'numnew', $magic['num'], 'text');
	showsetting('magics_edit_supplynum', 'supplynumnew', $magic['supplynum'], 'text');
	showsetting('magics_edit_weight', 'weightnew', $magic['weight'], 'text');
	showsetting('magics_edit_supplytype', ['supplytypenew', [
		[0, $lang['magics_goods_stack_none']],
		[1, $lang['magics_goods_stack_day']],
		[2, $lang['magics_goods_stack_week']],
		[3, $lang['magics_goods_stack_month']],
	]], $magic['supplytype'], 'mradio');
	showsetting('magics_edit_useperoid', ['useperoidnew', [
		[0, $lang['magics_edit_useperoid_none']],
		[1, $lang['magics_edit_useperoid_day']],
		[4, $lang['magics_edit_useperoid_24hr']],
		[2, $lang['magics_edit_useperoid_week']],
		[3, $lang['magics_edit_useperoid_month']],
	]], $magic['useperoid'], 'mradio');
	showsetting('magics_edit_usenum', 'usenumnew', $magic['usenum'], 'text');
	showsetting('magics_edit_description', 'descriptionnew', $magic['description'], 'textarea');

	if(is_array($magicsetting)) {
		foreach($magicsetting as $settingvar => $setting) {
			if(!empty($setting['value']) && is_array($setting['value'])) {
				foreach($setting['value'] as $k => $v) {
					$setting['value'][$k][1] = lang('magic/'.$magic['identifier'], $setting['value'][$k][1]);
				}
			}
			$varname = in_array($setting['type'], ['mradio', 'mcheckbox', 'select', 'mselect']) ?
				($setting['type'] == 'mselect' ? ['perm['.$settingvar.'][]', $setting['value']] : ['perm['.$settingvar.']', $setting['value']])
				: 'perm['.$settingvar.']';
			$value = $magicperm[$settingvar] != '' ? $magicperm[$settingvar] : $setting['default'];
			$comment = lang('magic/'.$magic['identifier'], $setting['title'].'_comment');
			$comment = $comment != $setting['title'].'_comment' ? $comment : '';
			showsetting(lang('magic/'.$magic['identifier'], $setting['title']).':', $varname, $value, $setting['type'], '', 0, $comment);
		}
	}

	showtitle('magics_edit_perm');
	showtablerow('', 'colspan="2" class="td27"', $lang['magics_edit_usergroupperm'].':<input class="checkbox" type="checkbox" name="chkall1" onclick="checkAll(\'prefix\', this.form, \'usergroupsperm\', \'chkall1\', true)" id="chkall1" /><label for="chkall1"> '.cplang('select_all').'</label>');
	showtablerow('', 'colspan="2"', mcheckbox('usergroupsperm', $groups, explode("\t", $magicperm['usergroups'])));

	if(!empty($magicclass->targetgroupperm)) {
		showtablerow('', 'colspan="2" class="td27"', $lang['magics_edit_targetgroupperm'].':<input class="checkbox" type="checkbox" name="chkall2" onclick="checkAll(\'prefix\', this.form, \'targetgroupsperm\', \'chkall2\', true)" id="chkall2" /><label for="chkall2"> '.cplang('select_all').'</label>');
		showtablerow('', 'colspan="2"', mcheckbox('targetgroupsperm', $groups, explode("\t", $magicperm['targetgroups'])));
	}
	showsubmit('magiceditsubmit');
	showtablefooter();
	showformfooter();

} else {

	$namenew = dhtmlspecialchars(trim($_GET['namenew']));
	$identifiernew = dhtmlspecialchars(trim(strtoupper($_GET['identifiernew'])));
	$descriptionnew = dhtmlspecialchars($_GET['descriptionnew']);
	$availablenew = !$identifiernew ? 0 : 1;

	$magicperm['usergroups'] = is_array($_GET['usergroupsperm']) && !empty($_GET['usergroupsperm']) ? "\t".implode("\t", $_GET['usergroupsperm'])."\t" : '';
	$magicperm['targetgroups'] = is_array($_GET['targetgroupsperm']) && !empty($_GET['targetgroupsperm']) ? "\t".implode("\t", $_GET['targetgroupsperm'])."\t" : '';

	$eidentifier = explode(':', $magic['identifier']);
	if(count($eidentifier) > 1 && preg_match('/^[\w\_:]+$/', $magic['identifier'])) {
		include_once DISCUZ_PLUGIN($eidentifier[0]).'/magic/magic_'.$eidentifier[1].'.php';
		$magicclass = 'magic_'.$eidentifier[1];
	} else {
		require_once libfile('magic/'.$magic['identifier'], 'class');
		$magicclass = 'magic_'.$magic['identifier'];
	}

	$magicclass = new $magicclass;
	$magicclass->setsetting($magicperm, $_GET['perm']);
	$magicpermnew = addslashes(serialize($magicperm));

	$supplytypenew = intval($_GET['supplytypenew']);
	$supplynumnew = $_GET['supplytypenew'] ? intval($_GET['supplynumnew']) : 0;
	$usenumnew = intval($_GET['usenumnew']);
	$useperoidnew = $_GET['useperoidnew'] ? intval($_GET['useperoidnew']) : 0;
	$creditnew = intval($_GET['creditnew']);

	if(!$namenew) {
		cpmsg('magics_parameter_invalid', '', 'error');
	}

	if(table_common_magic::t()->check_identifier($identifiernew, $magicid)) {
		cpmsg('magics_identifier_invalid', '', 'error');
	}

	table_common_magic::t()->update($magicid, [
		'name' => $namenew,
		'description' => $descriptionnew,
		'price' => $_GET['pricenew'],
		'num' => $_GET['numnew'],
		'supplytype' => $supplytypenew,
		'supplynum' => $supplynumnew,
		'useperoid' => $useperoidnew,
		'usenum' => $usenumnew,
		'weight' => $_GET['weightnew'],
		'magicperm' => $magicpermnew,
		'credit' => $creditnew
	]);

	updatecache(['setting', 'magics']);
	cpmsg('magics_data_succeed', 'action=magics&operation=admin', 'succeed');

}
	