Location: PHPKode > projects > Enigma > Enigma2/Enigma2_Install/PortalSources/BlockManagement.php
<?php
/*
*****************************************************************

 BlockManagement.php
 
*****************************************************************
LSP: Lunabyte Systems Portal
Open-Source Project Inspired by Zef Hemel (hide@address.com)
*****************************************************************
Software Version:                  LSP 2.0 "Enigma 2"
Software by:                         Lunabyte Systems (http://www.lunabyte.net)
Copyright 2002-2005 by:       Lunabyte Systems (http://www.lunabyte.net)
Support, News, Updates at:    http://www.lunabyte.net
*****************************************************************
This program is free software; you may redistribute it and/or modify it 
under the terms of the provided license as published by Lunabyte Systems.

This program is distributed in the hope that it is and will be useful,                  
but WITHOUT ANY WARRANTIES; without even any implied warranty of           
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                        

See the "LSP_license.txt" file for details of the LSP license.
The latest version can always be found at http://www.lunabyte.net.
*****************************************************************
*/

if (!defined('ENIGMA'))
	die('<b>Access Violation</b><br />Direct Access to this location is not allowed.');

function Blockhub()
{
	global $context, $user_info, $txt;

	// You have to be able to do at least one of the below to see this page.
		isAllowedTo('manage_blocks');

	loadPLanguage('BlockManagement');

	if (!isset($context['page_title']))
		$context['page_title'] = $txt['blk_manage'];

	// Load the common admin stuff... select 'Block_Management'.
	if ($user_info['is_admin'])
		adminIndex('Block_Management');

	$sa = (empty($_REQUEST['sa']) && !empty($_POST['sa'])) ? $_POST['sa'] :
		empty($_REQUEST['sa']) ? 'BlockManagement' : $_REQUEST['sa'];

	$subActions = array(
			'BlockManagement',
			'BlockUp',
			'BlockDown',
			'DeleteBlock',
			'EditBlock',
			'EditBlock2',
			'BlockOnOff',
			'AddBlock',
		);

	if (in_array($sa, $subActions))
		$sa();
	elseif (isset($subActions[$sa]))
		$subActions[$sa]();
	else
		BlockManagement();

	unset($subActions);
}

// this will build a dropdown of built in block types, for all the Block Management functions when called
function blockTypes($currentType = '', $selectname, $add = '')
{
	global $blockdir, $txt;

	$blockTypes = array(
		'custom',
		);

	if (!empty($blockdir))
	{
		if (file_exists($blockdir . '/BlockTypes.php'))
		{
			require_once($blockdir . '/BlockTypes.php');
			$blockTypes = array_merge($blockTypes, installedBlocks());
		}
	
		// read any other files in the block directory that aren't already specified in blocktypes
		$d = dir($blockdir);
		while ($obj = $d->read())
		{	// check for Block_{name}.php files that are not in the array already
			if (preg_match('~(Block_[a-zA-Z0-9]+).php~', $obj, $matches) == 1 && !in_array(str_replace(array('Block_', '.php'), '', $matches[0]) , $blockTypes))
			{	// set a var of just the block name
				$newBlock = str_replace(array('Block_', '.php'), '', $matches[0]);
				// add it to the types array
				$blockTypes[] = $newBlock;
				// if the txt var has not been defined, define it with just the name
				if (empty($txt['blk_' . $newBlock]))
					$txt['blk_' . $newBlock] = '*' . $newBlock;
			}
		}
		$d->close();
	}

	$select = '
		<select name="' . $selectname . '">';

	foreach ($blockTypes AS $type)
	{
		$display = 'blk_' . $type;
		$select .= '
			<option ' . (($currentType == $type || ($add && $type == 'custom')) ? 'selected="selected"' : '') . ' value="' . $type . '">' . $txt[$display] . '</option>';
	}

	$select .= '
		</select>';

	return $select;
}

function BlockManagement()
{
	global $db_prefix, $context;
	
	$ob = (int) !empty($_REQUEST['ob']) ? $_REQUEST['ob'] : '0';

	$OrderByAllowed = array(
		'0' => 'side,border',
		'1' => 'name',
		'2' => 'contents',
		'3' => 'border',
		'4' => 'side',
	);
	$orderby = $OrderByAllowed[$ob];
	
	$request = db_query("
		SELECT *
		FROM {$db_prefix}blocks
		ORDER BY $orderby ASC", __FILE__, __LINE__);

	while ($block = mysql_fetch_assoc($request))
	{
		$block['contents'] = str_replace (array('<','>'), array('&lt;','&gt;'), $block['contents']);

		$context['listblocks'][] = array(
			'id'	=> $block['id'],
			'type'	=> $block['type'],
			'side'	=> $block['side'],
			'border'	=> $block['border'],
			'contents'	=> (strlen($block['contents']) > 100) ? substr($block['contents'],0,100) . '...' : $block['contents'],
			'name' => $block['name'],
			'active' => $block['active'],
			'template' => $block['templateName'],
		);
	}

	loadPTemplate('BlockManagement');
	$context['sub_template'] = 'block_management';
}

function EditBlock()
{
	global $db_prefix, $context, $txt;

	$blockid = (int) empty($_REQUEST['blockid']) ? '' : $_REQUEST['blockid'];

	$context['page_title'] .= ' - ' . $txt['blk_edit'];

	$request = db_query("
		SELECT *
		FROM {$db_prefix}blocks
		WHERE id = '$blockid'", __FILE__, __LINE__);

	$block = mysql_fetch_assoc($request);
	mysql_free_result($request);

	$context['this_block'] = array(
			'id'			=> $blockid,
			'name'		=> $block['name'],
			// tag fix - the line below is left here just in case there was some < > leftover in previous data.
			// a </textarea> or other html closing tag COULD cause form problems, so let's just make sure it doesn't.
			'contents'	=> str_replace(array('<','>'), array('&#60;', '&#62;'), $block['contents']),
			'border'		=> $block['border'],
			'active'		=> (($block['active'] == 1) ? 'checked /' : '/'),
			'phpe'		=> (($block['phpe'] == 1) ? 'checked /' : '/'),
			'visible'		=> $block['visible'],
			'type'		=> $block['type'],
			'allowance'	=> $block['allowance'],
			'template' 	=> $block['templateName'],
	);

	$context['this_block']['sides'] = array(
		array('name' => 'left',		'selected' => ($block['side'] == 'left')	? 'selected' : ''),
		array('name' => 'right',	'selected' => ($block['side'] == 'right')	? 'selected' : ''),
		array('name' => 'center',	'selected' => ($block['side'] == 'center')	? 'selected' : ''),
		array('name' => 'top',		'selected' => ($block['side'] == 'top')		? 'selected' : ''),
		array('name' => 'bottom',	'selected' => ($block['side'] == 'bottom')	? 'selected' : ''),
	);

	loadPTemplate('BlockManagement');
	$context['sub_template'] = 'edit_block';
}
	
function EditBlock2()
{
	global $db_prefix, $txt;

	$blockid = (int) $_REQUEST['blockid'];

	// make sure that the custom template gets used for the submissions block
	$_REQUEST['template'] = ($_REQUEST['type'] == 'newSubmissions') ? 'newSubmissions' :
		(!empty($_REQUEST['template']) ? $_REQUEST['template'] : '');

	db_query("UPDATE {$db_prefix}blocks
		SET name='" . (!empty($_REQUEST['name']) ? $_REQUEST['name'] : $txt['blk_new']) . "',
		    contents='" . (!empty($_REQUEST['contents']) ? htmlspecialchars($_REQUEST['contents']) : '') . "',
		    active='" . (!empty($_REQUEST['active']) ? '1' : '0') . "',
		    side='" . (!empty($_REQUEST['side']) ? $_REQUEST['side'] : 'left') . "',
		    border='" . (!empty($_REQUEST['border']) ? $_REQUEST['border'] : '0') . "',
		    type='" . (!empty($_REQUEST['type']) ? $_REQUEST['type'] : 'custom') . "',
		    phpe='" . (!empty($_REQUEST['phpe']) ? '1' : '0') . "',
		    visible='" . (empty($_REQUEST['visible']) ? '' : implode(',', $_REQUEST['visible'])) . "',
		    allowance='" . (empty($_REQUEST['blockmg']) || !is_array($_REQUEST['blockmg']) ? '' : implode(',', $_REQUEST['blockmg'])) . "',
		    templateName='$_REQUEST[template]'
		WHERE id=$blockid", __FILE__, __LINE__);

	redirectexit('op=BlockManagement;edit');
}

function BlockUp()
{
	global $db_prefix;

	$blockid = (int) empty($_REQUEST['blockid']) ? '' : $_REQUEST['blockid'];

	db_query("
		UPDATE {$db_prefix}blocks
		SET border = border - 1
		WHERE id = '$blockid'", __FILE__, __LINE__);

	redirectexit('op=BlockManagement;up');
}

function BlockDown()
{
	global $db_prefix;

	$blockid = (int) empty($_REQUEST['blockid']) ? '' : $_REQUEST['blockid'];

	db_query("
		UPDATE {$db_prefix}blocks
		SET border = border + 1
		WHERE id = '$blockid'", __FILE__, __LINE__);

	redirectexit('op=BlockManagement;down');
}

function BlockOnOff()
{
	global $db_prefix;

	$blockid = (int) empty($_REQUEST['blockid']) ? '' : $_REQUEST['blockid'];
	$active = (int) $_REQUEST['active'];

	db_query("
		UPDATE {$db_prefix}blocks
		SET active = '$active'
		WHERE id = '$blockid'", __FILE__, __LINE__);

	redirectexit('op=BlockManagement;onoff');
}

function AddBlock()
{
	global $db_prefix, $txt;

	// make sure that the custom template gets used for the submissions block
	$_REQUEST['template'] = ($_REQUEST['type'] == 'newSubmissions') ? 'newSubmissions' :
		(!empty($_REQUEST['template']) ? $_REQUEST['template'] : '');

	db_query("
		INSERT INTO {$db_prefix}blocks
		(border, name, contents, active, side, phpe, type, visible, allowance, templateName)
		VALUES (
			'" . (!empty($_REQUEST['border']) ? $_REQUEST['border'] : '0') . "',
			'" . (!empty($_REQUEST['name']) ? $_REQUEST['name'] : $txt['blk_new']) . "',
			'" . (!empty($_REQUEST['contents']) ? htmlspecialchars($_REQUEST['contents']) : '') . "',
			'" . (!empty($_REQUEST['active']) ? '1' : '0') . "',
			'" . (!empty($_REQUEST['side']) ? $_REQUEST['side'] : 'left') . "',
			'" . (!empty($_REQUEST['phpe']) ? '1' : '0') . "',
			'" . (!empty($_REQUEST['type']) ? $_REQUEST['type'] : 'custom') . "',
			'" . (empty($_REQUEST['visible']) ? '' : implode(',', $_REQUEST['visible'])) . "',
			'" . (empty($_REQUEST['blockmg']) || !is_array($_REQUEST['blockmg']) ? '' : implode(',', $_REQUEST['blockmg'])) . "',
			'$_REQUEST[template]'
			)", __FILE__, __LINE__);
	
	redirectexit('op=BlockManagement;add');
}

function DeleteBlock()
{
	global $db_prefix;
	
	$blockid = (int) empty($_REQUEST['blockid']) ? '' : $_REQUEST['blockid'];

	db_query("
		DELETE FROM {$db_prefix}blocks
		WHERE id ='$blockid'", __FILE__, __LINE__);
	
	redirectexit('op=BlockManagement;delete');
}

// display selection box for which pages a block will be visable on.
function block_control ($selectname, $current = "all")
{
	global $txt, $db_prefix;

	isAllowedTo('manage_blocks');
	$currentarray = explode(',', $current);

	$blockOptions = array(
		array(	'option' => 'all',			'text' => $txt['blk_350'],	'lineSep' => 'off'),
		array(	'option' => 'pages',		'text' => $txt['blk_357'],	'lineSep' => 'on'),
		array(	'option'	=> 'modules', 		'text' => $txt['blk_367'],	'lineSep' => 'off'),
		array(	'option'	=> 'bridged',		'text' => $txt['blk_368'],	'lineSep' => 'off'),
		array(	'option'	=> 'article',		'text' => $txt['blk_352'],	'lineSep' => 'on'),
		array(	'option'	=> 'calendar', 	'text' => $txt['blk_cal'],	'lineSep' => 'off'),
		array(	'option'	=> 'forum',		'text' => $txt['blk_354'],	'lineSep' => 'off'),
		array(	'option'	=> 'home',		'text' => $txt['blk_362'],	'lineSep' => 'off'),
		array(	'option'	=> 'misc',		'text' => $txt['blk_363'],	'lineSep' => 'off'),
		array(	'option'	=> 'news',		'text' => $txt['blk_356'],	'lineSep' => 'off'),
		array(	'option'	=> 'im',			'text' => $txt['blk_365'],	'lineSep' => 'off'),
		array(	'option'	=> 'profile',		'text' => $txt['blk_359'],	'lineSep' => 'off'),
		array(	'option'	=> 'stats',		'text' => $txt['blk_364'],	'lineSep' => 'off'),
		array(	'option'	=> 'submissions',	'text' => $txt['blk_358'],	'lineSep' => 'off'),
		array(	'option'	=> 'admin',		'text' => $txt['blk_351'],	'lineSep' => 'off'),
	);

	$select = '
		<select name="' . $selectname . '" multiple size="9">';

	foreach ($blockOptions AS $option)
	{
		if ($option['lineSep'] == 'on')
		{
			$select .= '
				<optgroup label="----------"> </optgroup>
				<option ' . (in_array($option['option'], $currentarray) ? 'selected ' : '') . 'value="' . $option['option'] . '">' . $option['text'] . '</option>';
		}
		else
			$select .= '
				<option ' . (in_array($option['option'], $currentarray) ? 'selected ' : '') . 'value="' . $option['option'] . '">' . $option['text'] . '</option>';
	}

	// add boards to selection list
	$boards = getBoardlistMultiple();
	if ($boards > 0)
	{
		$select .= '
				<optgroup label="----------"> </optgroup>
				<optgroup label="' . $txt['blk_369'] . '">';

		foreach ($boards AS $board)
		{
			// is this a new category?
			if ($board['setCat'] == 'new')
			{
				$select .= '
					<optgroup label="' . $board['catName'] . '"> </optgroup>
					<option ' . (in_array($board['id'], $currentarray) ? 'selected ' : '') . 'value="' . $board['id'] . '">' . $board['name'] . '</option>';
			}
			// same category, so no need to show the cat name again.
			else
				$select .= '
					<option ' . (in_array($board['id'], $currentarray) ? 'selected ' : '') . 'value="' . $board['id'] . '">' . $board['name'] . '</option>';
		}

		$select .= '
				</optgroup>';
	}

	// add modules to selection list
	$modules = LoadstartModules();
	if ($modules > 0)
	{
		$select .= '
				<optgroup label="----------"> </optgroup>
				<optgroup label="' . $txt['blk_370'] . '">';

		foreach ($modules AS $module)
			$select .= (strstr($module[0], 'Management')) ? '' : '
					<option ' . (in_array($module[0], $currentarray) ? 'selected ' : '') . 'value="' . $module[0] . '">' . $module[1] . '</option>';
		$select .= '
				</optgroup>';
	}

	// add pages to selection list
	$pages = getPagelist();
	if ($pages > 0)
	{
		$select .= '
				<optgroup label="----------"> </optgroup>
				<optgroup label="' . $txt['blk_371'] . '">';

		foreach ($pages AS $page)
			$select .= '
					<option ' . (in_array('Page_' . $page[0], $currentarray) ? 'selected ' : '') . 'value="Page_' . $page[0] . '">' . $page[1] . '</option>';
		$select .= '
				</optgroup>';
	}

	$BridgeApps = getBridgelist();
	if ($BridgeApps > 0)
	{
		$select .= '
				<optgroup label="----------"> </optgroup>
				<optgroup label="' . $txt['blk_372'] . '">';

		foreach ($BridgeApps AS $BridgeApps)
			$select .= (strstr($module[0], 'Management')) ? '' : '
					<option ' . (in_array($BridgeApps['name'], $currentarray) ? 'selected ' : '') . 'value="' . $BridgeApps['name'] . '">' . $BridgeApps['name'] . '</option>';

		$select .= '
				</optgroup>';
	}

	$select .= '
			</select>';

     return $select;
}

?>
Return current item: Enigma