<?php
// $Id: content_section.class.php,v 1.5 2003/09/30 23:36:13 eddieajau Exp $
/**
* Administrator menu content section class
* @package Mambo Open Source
* @Copyright (C) 2000 - 2003 Miro International Pty Ltd
* @ All rights reserved
* @ Mambo Open Source is Free Software
* @ Released under GNU/GPL License: http://www.gnu.org/copyleft/gpl.html
* @version $Revision: 1.5 $
**/

// ensure this file is being included by a parent file
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/**
*/
class content_section_menu {
/**
* @param database A database connector object
* @param integer The unique id of the category to edit (0 if new)
*/
	function editSection( $uid, $menutype, $option ) {
		global $database, $my;
		global $mosConfig_absolute_path;

		$menu = new mosMenu( $database );
		$menu->load( $uid );

		// fail if checked out not by 'me'
		if ($menu->checked_out && $menu->checked_out <> $my->id) {
			echo "<script>alert('The module $menu->title is currently being edited by another administrator'); document.location.href='index2.php?option=$option'</script>\n";
			exit(0);
		}

		if ($uid) {
			$menu->checkout( $my->id );
		} else {
			$menu->type = 'content_section';
			$menu->menutype = $menutype;
			$menu->ordering = 9999;
			$menu->parent = intval( mosGetParam( $_POST, 'parent', 0 ) );
		}

	// build the html select list for ordering
		if ($uid) {
			$order = mosGetOrderingList( "SELECT ordering AS value, name AS text"
				. "\nFROM #__menu"
				. "\nWHERE menutype='$menu->menutype' AND parent='$menu->parent'"
				. "\nORDER BY ordering"
			);
			$olist = mosHTML::selectList( $order, 'ordering', 'class="inputbox" size="1"',
				'value', 'text', intval( $menu->ordering ) );
		} else {
			$olist = "<input type=\"hidden\" name=\"ordering\" value=\"$menu->ordering\" />"
				. " New items default to the last place";
		}

	// get list of UNUSED sections in menu
		$database->setQuery( "SELECT s.id AS value,s.title AS text"
			. "\nFROM #__sections AS s "
			. "\nLEFT JOIN #__menu as m on ((s.id=m.componentid) AND m.type='content_section') "
			. "\nWHERE m.componentid is null AND s.count>0 AND s.published=1 ORDER BY s.title"
		);
		$sections = $database->loadObjectList();

// add current section to list
		if ($menu->id > 0) {
			$database->setQuery( "SELECT s.id AS value,s.title AS text"
			. "\nFROM #__sections AS s"
			. "\nLEFT JOIN #__menu as m on ((s.id  =m.componentid) AND m.type = 'content_section') "
			. "\nWHERE m.id=$menu->id"
		);
		$sections = array_merge( $sections,$database->loadObjectList() );
		}
		
		// fail if no more components available
		if (count($sections) == 0) {
			echo "<script>alert('All available content sections are already in use'); document.location.href='index2.php?option=$option'</script>\n";
			exit(0);
		}
		
	// build the html select list for the sections
		$slist = '';
		if ($uid) {
			// existing component, just show name
			foreach ($sections as $section) {
				if ($section->value == $menu->componentid) {
					$slist = "$section->text";
					$slist .= "\n<input type=\"hidden\" name=\"name\" value=\"$section->text\" />";
				}
			}
			$slist .= "\n<input type=\"hidden\" name=\"componentid\" value=\"$menu->componentid\" />";
		} else {
			$slist = mosHTML::selectList( $sections, 'componentid', 'class="inputbox" size="5"',
				'value', 'text', intval( $menu->componentid ) );
			$slist .= "\n<input type=\"hidden\" name=\"name\" value=\"\" />";

		}

	// get list of groups
		$database->setQuery( "SELECT id AS value, name AS text FROM #__groups ORDER BY id" );
		$groups = $database->loadObjectList();

	// build the html select list for the group access
		$glist = mosHTML::selectList( $groups, 'access', 'class="inputbox" size="5"',
			'value', 'text', intval( $menu->access ) );

	// menu parent item
	// get a list of the menu items
		$database->setQuery( "SELECT m.*"
			. "\nFROM #__menu m"
			. "\nWHERE menutype='$menu->menutype'"
			. "\nORDER BY ordering"
		);
		$mitems = $database->loadObjectList();

	// establish the hierarchy of the menu
		$children = array();
	// first pass - collect children
		foreach ($mitems as $v ) {
			$pt = $v->parent;
			$list = @$children[$pt] ? $children[$pt] : array();
			array_push( $list, $v );
			$children[$pt] = $list;
		}
	// second pass - get an indent list of the items
		$list = mosTreeRecurse( 0, '', array(), $children );

	// assemble menu items to the array
		$mitems = array();
		$mitems[] = mosHTML::makeOption( '0', 'Top' );
		$this_treename = '';
		foreach ($list as $item) {
			if ($this_treename) {
				if ($item->id != $menu->id && strpos( $item->treename, $this_treename ) === false) {
					$mitems[] = mosHTML::makeOption( $item->id, $item->treename );
				}
			} else {
				if ($item->id != $menu->id) {
					$mitems[] = mosHTML::makeOption( $item->id, $item->treename );
				} else {
					$this_treename = "$item->treename/";
				}
			}
		}

	// build the html select list
		$parlist = mosHTML::selectList( $mitems, 'parent', 'class="inputbox" size="1"',
			'value', 'text', $menu->parent );

		content_section_menu_html::editSection( $menu, $slist, $olist, $glist, $parlist, $option );
	}
}
?>
