<commit_before><?php
/**
 * Administration panel - Extra fields editor for structure part
 *
 * @package Cotonti
 * @version 0.9.0
 * @author Cotonti Team
 * @copyright Copyright (c) Cotonti Team 2008-2011
 * @license BSD
 */

(defined('COT_CODE') && defined('COT_ADMIN')) or die('Wrong URL.');

list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = cot_auth('users', 'a');
cot_block($usr['isadmin']);

require_once cot_incfile('extrafields');

$t = new XTemplate(cot_tplfile(array('admin', 'extrafields', $n), 'core'));

$a = cot_import('a', 'G', 'ALP');
$id = (int) cot_import('id', 'G', 'INT');
$name = cot_import('name', 'G', 'ALP');
list($pg, $d) = cot_import_pagenav('d', $cfg['maxrowsperpage']);

$parse_type = array('HTML', 'Text');

/* === Hook === */
foreach (cot_getextplugins('admin.extrafields.first') as $pl)
{
	include $pl;
}
/* ===== */

if ($n == 'structure')
{
	$adminpath[] = array(cot_url('admin', 'm=extrafields&n=structure'), $L['adm_extrafields']. " (".$L['Categories'].")");
	$adminhelp = $L['adm_help_structure_extrafield'];
	$extra_path = 'm=extrafields&n=structure';
	$location = $db_structure;
}

if ($n == 'users')
{
	$adminpath[] = array(cot_url('admin', 'm=users'), $L['Users']);
	$adminpath[] = array(cot_url('admin', 'm=extrafields&n=users'), $L['adm_extrafields']);
	$adminhelp = $L['adm_help_users_extrafield'];
	$extra_path = 'm=extrafields&n=users';
	$location = $db_users;
}

if (empty($extra_path) || empty($n))
{
	cot_redirect(cot_url('message', 'msg=950', '', true));
}

if ($a == 'add')
{
	$field['field_name'] = cot_import('field_name', 'P', 'ALP');
	$field['field_type'] = cot_import('field_type', 'P', 'ALP');
	$field['field_html'] = str_replace("'", "\"", htmlspecialchars_decode(cot_import('field_html', 'P', 'HTM')));
	$field['field_variants'] = cot_import('field_variants', 'P', 'HTM');
	$field['field_description'] = cot_import('field_description', 'P', 'HTM');
	$field['field_default'] = cot_import('field_default', 'P', 'HTM');
	$field['field_required'] = cot_import('field_required', 'P', 'BOL');
	$field['field_parse'] = cot_import('field_parse', 'P', 'ALP');
	$field['field_noalter'] = cot_import('field_noalter', 'P', 'BOL');
	if (empty($field['field_html']))
	{
		$field['field_html'] = cot_default_html_construction($field['field_type']);
	}

	/* === Hook === */
	foreach (cot_getextplugins('admin.extrafields.add') as $pl)
	{
		include $pl;
	}
	/* ===== */

	if (!empty($field['field_name']) && !empty($field['field_type']))
	{
		if (cot_extrafield_add($location, $field['field_name'], $field['field_type'], $field['field_html'], $field['field_variants'], $field['field_default'], $field['field_required'], $field['field_parse'], $field['field_description'], $field['field_noalter']))
		{
			cot_message('adm_extrafield_added');
		}
		else
		{
			cot_error('adm_extrafield_not_added');
		}
	}
}
elseif ($a == 'upd')
{
	$field_name = cot_import('field_name', 'P', 'ARR');
	$field_type = cot_import('field_type', 'P', 'ARR');
	$field_html = cot_import('field_html', 'P', 'ARR');
	$field_variants = cot_import('field_variants', 'P', 'ARR');
	$field_description = cot_import('field_description', 'P', 'ARR');
	$field_default = cot_import('field_default', 'P', 'ARR');
	$field_required = cot_import('field_required', 'P', 'ARR');
	$field_parse = cot_import('field_parse', 'P', 'ARR');

	/* === Hook - Part1 : Set === */
	$extp = cot_getextplugins('admin.extrafields.update');
	/* ===== */
	if(is_array($field_name))
	{
		foreach ($field_name as $k => $v)
		{
			$field['field_name'] = cot_import($field_name[$k], 'D', 'ALP');
			$field['field_type'] = cot_import($field_type[$k], 'D', 'ALP');
			$field['field_html'] = str_replace("'", "\"", htmlspecialchars_decode(cot_import($field_html[$k], 'D', 'HTM')));
			$field['field_variants'] = cot_import($field_variants[$k], 'D', 'HTM');
			$field['field_description'] = cot_import($field_description[$k], 'D', 'HTM');
			$field['field_default'] = cot_import($field_default[$k], 'D', 'HTM');
			$field['field_required'] = cot_import($field_required[$k], 'D', 'BOL');
			$field['field_parse'] = cot_import($field_parse[$k], 'D', 'ALP');
			$field['field_location'] = $location;

			if ($field != $cot_extrafields[$location][$field['field_name']] && !empty($field['field_name']) && !empty($field['field_type']))
			{
				if (empty($field['field_html']) || $field['field_type'] != $cot_extrafields[$location][$field['field_name']]['field_type'])
				{
					$field['field_html'] = cot_default_html_construction($field['field_type']);
				}

				/* === Hook - Part2 : Include === */
				foreach ($extp as $pl)
				{
					include $pl;
				}
				/* ===== */

				if (cot_extrafield_update($location, $k, $field['field_name'], $field['field_type'], $field['field_html'], $field['field_variants'], $field['field_default'], $field['field_required'], $field['field_parse'], $field['field_description']))
				{
					cot_message(sprintf($L['adm_extrafield_updated'], $k));
				}
				else
				{
					cot_error(sprintf($L['adm_extrafield_not_updated'], $k));
				}
			}
		}
	}
}
elseif ($a == 'del' && isset($name))
{
	/* === Hook === */
	foreach (cot_getextplugins('admin.extrafields.delete') as $pl)
	{
		include $pl;
	}
	/* ===== */

	if (cot_extrafield_remove($location, $name))
	{
		cot_message('adm_extrafield_removed');
	}
	else
	{
		cot_error('adm_extrafield_not_removed');
	}
}

$cfg['cache'] && $cache->db->remove('cot_extrafields', 'system');

$totalitems = $db->query("SELECT COUNT(*) FROM $db_extra_fields WHERE field_location = '$location'")->fetchColumn();
$res = $db->query("SELECT * FROM $db_extra_fields WHERE field_location = '$location' ORDER BY field_name ASC LIMIT $d, ".$cfg['maxrowsperpage']);

$pagenav = cot_pagenav('admin',$extra_path, $d, $totalitems, $cfg['maxrowsperpage'], 'd', '', $cfg['jquery'] && $cfg['turnajax']);

$field_types = array('input', 'inputint', 'currency', 'textarea', 'select', 'checkbox', 'radio', 'datetime', 'file', 'filesize');

$ii = 0;
/* === Hook - Part1 : Set === */
$extp = cot_getextplugins('admin.extrafields.loop');
/* ===== */
foreach ($res->fetchAll() as $row)
{
	$t->assign(array(
		'ADMIN_EXTRAFIELDS_ROW_NAME' => cot_inputbox('text', 'field_name['.$row['field_name'].']', $row['field_name']),
		'ADMIN_EXTRAFIELDS_ROW_DESCRIPTION' => cot_textarea('field_description['.$row['field_name'].']', $row['field_description'], 1, 30),
		'ADMIN_EXTRAFIELDS_ROW_SELECT' => cot_selectbox($row['field_type'], 'field_type['.$row['field_name'].']', $field_types, $field_types, false),
		'ADMIN_EXTRAFIELDS_ROW_VARIANTS' => cot_textarea('field_variants['.$row['field_name'].']', $row['field_variants'], 1, 60),
		'ADMIN_EXTRAFIELDS_ROW_HTML' => cot_textarea('field_html['.$row['field_name'].']', $row['field_html'], 1, 60),
		'ADMIN_EXTRAFIELDS_ROW_DEFAULT' => cot_textarea('field_default['.$row['field_name'].']', $row['field_default'], 1, 60),<commit_msg>cache fix<commit_after><?php
/**
 * Administration panel - Extra fields editor for structure part
 *
 * @package Cotonti
 * @version 0.9.0
 * @author Cotonti Team
 * @copyright Copyright (c) Cotonti Team 2008-2011
 * @license BSD
 */

(defined('COT_CODE') && defined('COT_ADMIN')) or die('Wrong URL.');

list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = cot_auth('users', 'a');
cot_block($usr['isadmin']);

require_once cot_incfile('extrafields');

$t = new XTemplate(cot_tplfile(array('admin', 'extrafields', $n), 'core'));

$a = cot_import('a', 'G', 'ALP');
$id = (int) cot_import('id', 'G', 'INT');
$name = cot_import('name', 'G', 'ALP');
list($pg, $d) = cot_import_pagenav('d', $cfg['maxrowsperpage']);

$parse_type = array('HTML', 'Text');

/* === Hook === */
foreach (cot_getextplugins('admin.extrafields.first') as $pl)
{
	include $pl;
}
/* ===== */

if ($n == 'structure')
{
	$adminpath[] = array(cot_url('admin', 'm=extrafields&n=structure'), $L['adm_extrafields']. " (".$L['Categories'].")");
	$adminhelp = $L['adm_help_structure_extrafield'];
	$extra_path = 'm=extrafields&n=structure';
	$location = $db_structure;
}

if ($n == 'users')
{
	$adminpath[] = array(cot_url('admin', 'm=users'), $L['Users']);
	$adminpath[] = array(cot_url('admin', 'm=extrafields&n=users'), $L['adm_extrafields']);
	$adminhelp = $L['adm_help_users_extrafield'];
	$extra_path = 'm=extrafields&n=users';
	$location = $db_users;
}

if (empty($extra_path) || empty($n))
{
	cot_redirect(cot_url('message', 'msg=950', '', true));
}

if ($a == 'add')
{
	$field['field_name'] = cot_import('field_name', 'P', 'ALP');
	$field['field_type'] = cot_import('field_type', 'P', 'ALP');
	$field['field_html'] = str_replace("'", "\"", htmlspecialchars_decode(cot_import('field_html', 'P', 'HTM')));
	$field['field_variants'] = cot_import('field_variants', 'P', 'HTM');
	$field['field_description'] = cot_import('field_description', 'P', 'HTM');
	$field['field_default'] = cot_import('field_default', 'P', 'HTM');
	$field['field_required'] = cot_import('field_required', 'P', 'BOL');
	$field['field_parse'] = cot_import('field_parse', 'P', 'ALP');
	$field['field_noalter'] = cot_import('field_noalter', 'P', 'BOL');
	if (empty($field['field_html']))
	{
		$field['field_html'] = cot_default_html_construction($field['field_type']);
	}

	/* === Hook === */
	foreach (cot_getextplugins('admin.extrafields.add') as $pl)
	{
		include $pl;
	}
	/* ===== */

	if (!empty($field['field_name']) && !empty($field['field_type']))
	{
		if (cot_extrafield_add($location, $field['field_name'], $field['field_type'], $field['field_html'], $field['field_variants'], $field['field_default'], $field['field_required'], $field['field_parse'], $field['field_description'], $field['field_noalter']))
		{
			cot_message('adm_extrafield_added');
		}
		else
		{
			cot_error('adm_extrafield_not_added');
		}
	}
}
elseif ($a == 'upd')
{
	$field_name = cot_import('field_name', 'P', 'ARR');
	$field_type = cot_import('field_type', 'P', 'ARR');
	$field_html = cot_import('field_html', 'P', 'ARR');
	$field_variants = cot_import('field_variants', 'P', 'ARR');
	$field_description = cot_import('field_description', 'P', 'ARR');
	$field_default = cot_import('field_default', 'P', 'ARR');
	$field_required = cot_import('field_required', 'P', 'ARR');
	$field_parse = cot_import('field_parse', 'P', 'ARR');

	/* === Hook - Part1 : Set === */
	$extp = cot_getextplugins('admin.extrafields.update');
	/* ===== */
	if(is_array($field_name))
	{
		foreach ($field_name as $k => $v)
		{
			$field['field_name'] = cot_import($field_name[$k], 'D', 'ALP');
			$field['field_type'] = cot_import($field_type[$k], 'D', 'ALP');
			$field['field_html'] = str_replace("'", "\"", htmlspecialchars_decode(cot_import($field_html[$k], 'D', 'HTM')));
			$field['field_variants'] = cot_import($field_variants[$k], 'D', 'HTM');
			$field['field_description'] = cot_import($field_description[$k], 'D', 'HTM');
			$field['field_default'] = cot_import($field_default[$k], 'D', 'HTM');
			$field['field_required'] = cot_import($field_required[$k], 'D', 'BOL');
			$field['field_parse'] = cot_import($field_parse[$k], 'D', 'ALP');
			$field['field_location'] = $location;

			if ($field != $cot_extrafields[$location][$field['field_name']] && !empty($field['field_name']) && !empty($field['field_type']))
			{
				if (empty($field['field_html']) || $field['field_type'] != $cot_extrafields[$location][$field['field_name']]['field_type'])
				{
					$field['field_html'] = cot_default_html_construction($field['field_type']);
				}

				/* === Hook - Part2 : Include === */
				foreach ($extp as $pl)
				{
					include $pl;
				}
				/* ===== */

				if (cot_extrafield_update($location, $k, $field['field_name'], $field['field_type'], $field['field_html'], $field['field_variants'], $field['field_default'], $field['field_required'], $field['field_parse'], $field['field_description']))
				{
					cot_message(sprintf($L['adm_extrafield_updated'], $k));
				}
				else
				{
					cot_error(sprintf($L['adm_extrafield_not_updated'], $k));
				}
			}
		}
	}
}
elseif ($a == 'del' && isset($name))
{
	/* === Hook === */
	foreach (cot_getextplugins('admin.extrafields.delete') as $pl)
	{
		include $pl;
	}
	/* ===== */

	if (cot_extrafield_remove($location, $name))
	{
		cot_message('adm_extrafield_removed');
	}
	else
	{
		cot_error('adm_extrafield_not_removed');
	}
}

$cache && $cache->db->remove('cot_extrafields', 'system');

$totalitems = $db->query("SELECT COUNT(*) FROM $db_extra_fields WHERE field_location = '$location'")->fetchColumn();
$res = $db->query("SELECT * FROM $db_extra_fields WHERE field_location = '$location' ORDER BY field_name ASC LIMIT $d, ".$cfg['maxrowsperpage']);

$pagenav = cot_pagenav('admin',$extra_path, $d, $totalitems, $cfg['maxrowsperpage'], 'd', '', $cfg['jquery'] && $cfg['turnajax']);

$field_types = array('input', 'inputint', 'currency', 'textarea', 'select', 'checkbox', 'radio', 'datetime', 'file', 'filesize');

$ii = 0;
/* === Hook - Part1 : Set === */
$extp = cot_getextplugins('admin.extrafields.loop');
/* ===== */
foreach ($res->fetchAll() as $row)
{
	$t->assign(array(
		'ADMIN_EXTRAFIELDS_ROW_NAME' => cot_inputbox('text', 'field_name['.$row['field_name'].']', $row['field_name']),
		'ADMIN_EXTRAFIELDS_ROW_DESCRIPTION' => cot_textarea('field_description['.$row['field_name'].']', $row['field_description'], 1, 30),
		'ADMIN_EXTRAFIELDS_ROW_SELECT' => cot_selectbox($row['field_type'], 'field_type['.$row['field_name'].']', $field_types, $field_types, false),
		'ADMIN_EXTRAFIELDS_ROW_VARIANTS' => cot_textarea('field_variants['.$row['field_name'].']', $row['field_variants'], 1, 60),
		'ADMIN_EXTRAFIELDS_ROW_HTML' => cot_textarea('field_html['.$row['field_name'].']', $row['field_html'], 1, 60),
		'ADMIN_EXTRAFIELDS_ROW_DEFAULT' => cot_textarea('field_default['.$row['field_name'].']', $row['field_default'], 1, 60),
{'fraction_non_alphanumeric': 0.2395417965634742, 'fraction_numerical': 0.0043500326252446896, 'mean_word_length': 6.706145251396648, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3933575', 'n_tokens_mistral': 5427, 'n_tokens_neox': 5076, 'n_words': 790}