1. Michael Manfre
  2. xpattern

Source

xpattern / textpattern / include / txp_group.php

<?php

if (!defined('txpinterface'))
{
	die('txpinterface is undefined.');
}

if (empty($groups))
{
	$groups = array();
	$rs = safe_rows('`id`,`name`', 'group');
	if ($rs)
	{
		foreach ($rs as $r)
		{
			$groups[$r['id']] = gTxt($r['name']);
		}
	}
}

if ($event == 'group')
{
	require_privs('group');

	if (is_callable($step))
		$step();
	else
		group();
}

function group($message='')
{
	pageTop($message);
	
	group_list();
}

function group_list()
{
	global $txp_user;

	if (!has_privs('group.list'))
		return;

	echo n.n.hed(gTxt('tab_groups'), 3, ' style="text-align: center;margin-top:2em;"').
		n.n.startTable('list').
		n.tr(
			n.hCell(gTxt('id')).
			n.hCell(gTxt('name')).
			n.hCell().
			n.hCell()
		);

	$rs = safe_rows_start('*', 'group', '1=1 ORDER BY `id`');

	if ($rs)
	{
		while ($a = nextRow($rs))
		{
			if (has_privs('group.edit')) {
				echo n.tr(
					n.'<form method="post" action="index.php">'.
					n.td( ' '.$a['id'] ).
					td( ($a['id'] <= 6 ? htmlspecialchars(gTxt($a['name'])) : fInput('text', 'name', $a['name'], 'edit')) ).
					n.td(
						$a['id'] <= 6 ? '' :
							fInput('submit', 'save', gTxt('save'), 'smallerbox').
							hInput('group_id', $a['id']).
							eInput('group').
							sInput('group_save')
					).
  				'</form>'.
					td(
						$a['id'] <= 6 ? '' :
							dLink('group', 'group_delete', 'group_id', $a['id'])
					)
				);
			} else {
				echo n.tr(
					td( $a['id'] ).
					td( htmlspecialchars($a['name']) ).
					td().
					td()
				);
			}
		}
	}

	if (has_privs('group.edit'))
	{
		echo n.tr(
			form(
				td( fInput('text', 'group_id', '', 'edit', '', '', '2') ).
				td( fInput('text', 'name', '', 'edit') ).
				td( fInput('submit', 'add', gTxt('add'), 'smallerbox') ).
				td().
				n.eInput('group').
				n.sInput('group_save_new')
			)
		);
	}

	echo n.endTable();
}
// -------------------------------------------------------------
function group_save()
{
	global $groups;

	require_privs('group.edit');

	extract(doSlash(psa(array('group_id', 'name'))));
	$group_id = assert_int($group_id);

	if ($name && $group_id > 6)
	{
		$rs = safe_update('group', "`name` = '$name'","`id` = $group_id");

		if ($rs)
		{
			$groups[$group_id] = gTxt($name);

			group(
				gTxt('saved_group', array('{name}' => $name))
			);

			return;
		}
	}

	group(gTxt('error_adding_new_level'));
}
// -------------------------------------------------------------
function group_save_new()
{
	global $groups;

	require_privs('group.edit');

	extract(doSlash(psa(array('group_id', 'name'))));
	$group_id = assert_int($group_id);

	if ($name && $group_id > 6)
	{
		$rs = safe_insert('group', "
			`id`		= $group_id,
			`name`	= '$name'
		");

		if ($rs)
		{
			$groups[$group_id] = gTxt($name);

			group(
				gTxt('added_group', array('{name}' => $name))
			);

			return;
		}
	}

	group(gTxt('error_adding_new_level'));
}
// -------------------------------------------------------------
function group_delete()
{
	global $groups;

	require_privs('group.edit');

	$group_id = assert_int(ps('group_id'));

	if ($group_id > 6) {
		if (isset($groups[$group_id]))
		{
			$name = $groups[$group_id];

			$rs = safe_delete('group', "`id` = $group_id");

			if ($rs) {
				unset($groups[$group_id]);
				group( gTxt('group_deleted', array('{name}' => $name)) );
			}
		}
	}
}

?>