<?php
/*
// "Frontpage Slideshow" Component for Joomla! 1.5.x - Version 1.7.1
// Copyright (c) 2006 - 2008 JoomlaWorks, a Komrade LLC company.
// This code cannot be redistributed without permission from JoomlaWorks - http://www.joomlaworks.gr.
// More info at http://www.joomlaworks.gr and http://www.frontpageslideshow.net
// Developers: Fotis Evangelou - George Chouliaras
// ***Last update: February 27th, 2008***
*/

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

//////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                  //
//              Classes and helper functions to the Frontpage SlideShow system                      //
//                                                                                                  //
//////////////////////////////////////////////////////////////////////////////////////////////////////

class mosFPSlideShow {


	function builtDropDownMenu( &$lookup, $unassigned=1 )
	{
		$db =& JFactory::getDBO();

		// get a list of the menu items
		$query = "SELECT m.*"
		. "\n FROM #__menu AS m"
		. "\n WHERE m.published = 1"
		//. "\n AND m.type != 'separator'"
		//. "\n AND NOT ("
		//	. "\n ( m.type = 'url' )"
		//	. "\n AND ( m.link LIKE '%index.php%' )"
		//	. "\n AND ( m.link LIKE '%Itemid=%' )"
		//. "\n )"
		. "\n ORDER BY m.menutype, m.parent, m.ordering"
		;
		$db->setQuery( $query );
		$mitems = $db->loadObjectList();
		$mitems_temp = $mitems;

		// establish the hierarchy of the menu
		$children = array();
		// first pass - collect children
		foreach ( $mitems as $v ) {
			$id = $v->id;
			$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 = mosFPSlideShow::mosTreeRecurse( intval( $mitems[0]->parent ), '', array(), $children, 20, 0, 0 );

		// Code that adds menu name to Display of Page(s)
		$text_count 	= 0;
		$mitems_spacer 	= $mitems_temp[0]->menutype;
		foreach ($list as $list_a) {
			foreach ($mitems_temp as $mitems_a) {
				if ($mitems_a->id == $list_a->id) {
					// Code that inserts the blank line that seperates different menus
					if ($mitems_a->menutype != $mitems_spacer) {
						$list_temp[]    = JHTML::_('select.option', '-999', '-----');
						$mitems_spacer 	= $mitems_a->menutype;
					}

					// do not display `url` menu item types that contain `index.php` and `Itemid`
					if (!($mitems_a->type == 'url' && strpos($mitems_a->link, 'index.php') !== false && strpos($mitems_a->link, 'Itemid=') !== false)) {
						$text 			= $mitems_a->menutype .' | '. $list_a->treename;
						$list_temp[]    = JHTML::_('select.option', $list_a->id, $text);
						if ( strlen($text) > $text_count) {
							$text_count = strlen($text);
						}
					}
				}
			}
		}
		$list = $list_temp;

		$mitems = array();
		$mitems[] = JHTML::_('select.option', '0', JText::_('FPSS_IMAGES_SELECT_MENU2'));
		// append the rest of the menu items to the array
		foreach ($list as $item) {
			$mitems[] = JHTML::_('select.option', $item->value, $item->text);
		}

		$pages = JHTML::_('select.genericlist',  $mitems, 'menu2', 'class="inputbox" size="1" onclick="this.form.linkto[1].checked=true" onchange="this.form.linkto[2].checked=true"', 'value', 'text', $lookup);
		return $pages;
	}

	function mosTreeRecurse( $id, $indent, $list, &$children, $maxlevel=9999, $level=0, $type=1,$parent='parent') {
		if (@$children[$id] AND $level <= $maxlevel) {
			$newindent = $indent.($type ? '.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' : '&nbsp;&nbsp;');
			$pre = $type ? '<sup>L</sup>&nbsp;' : '- ';
			foreach ($children[$id] as $v) {
				$id = $v->id;
				$list[$id] = $v;
				$list[$id]->treename = $indent.($v->$parent == 0 ? '' : $pre).$v->name;
				$list[$id]->children = count( @$children[$id] );
				$list[$id]->level = $level;
				$list = mosFPSlideShow::mosTreeRecurse( $id, $newindent, $list, $children, $maxlevel, $level+1, $type );
			}
		}
		return $list;
	}

	function getAccessgroups($registers)
	{

		$db =& JFactory::getDBO();

		// build list of categories
		$query = 'SELECT g.id, g.name' .
		' FROM #__groups AS g' .
		' ORDER BY g.id';
		$groups[] = JHTML::_('select.option', '-1', '- '.JText::_('FPSS_SEL_ACCESS').' -', 'id', 'name');
		$db->setQuery($query);
		$groups = array_merge($groups, $db->loadObjectList());
		$groups = JHTML::_('select.genericlist',  $groups, 'registers', 'class="inputbox" size="1" ', 'id', 'name', $registers);

		return $groups;

	}

	function getJSections( $name, $active=NULL, $javascript=NULL )
	{

		$db =& JFactory::getDBO();

		$query = "SELECT id AS id, title AS name"
		. "\n FROM #__sections"
		. "\n WHERE published = '1'"
		. "\n ORDER BY ordering"
		;
		$sections[] = JHTML::_('select.option', '0', JText::_('FPSS_SEL_SECTION'), 'id', 'name');
		$db->setQuery($query);
		$sections = array_merge( $sections, $db->loadObjectList() );
		$sections = JHTML::_('select.genericlist',  $sections, $name, 'class="inputbox" size="1" '. $javascript, 'id', 'name', $active);

		return $sections;

	}

	function getJCategories( $name, $active=NULL, $section=NULL, $javascript=NULL )
	{

		$db =& JFactory::getDBO();

		$query = "SELECT cc.id AS id, cc.title AS name, section"
		. "\n FROM #__categories AS cc"
		. "\n WHERE published = '1'"
		. "\n AND section = '$section'"
		. "\n ORDER BY cc.ordering"
		;
		$categories[] = JHTML::_('select.option', '0', JText::_('FPSS_SEL_CATEGORY'), 'id', 'name');
		$db->setQuery( $query );
		$categories = array_merge( $categories, $db->loadObjectList() );
		$categories = JHTML::_('select.genericlist',  $categories, $name, 'class="inputbox" size="1" '. $javascript, 'id', 'name', $active);

		return $categories;

	}

	function getJContents( $name, $active=NULL, $categoryid, $javascript=NULL )
	{

		$db =& JFactory::getDBO();

		$query = "SELECT c.id AS id, c.title AS name"
		. "\n FROM #__content AS c"
		. "\n WHERE state = '1'"
		. "\n AND catid = '$categoryid'"
		. "\n ORDER BY c.ordering"
		;
		$contents[] = JHTML::_('select.option', '0', JText::_('FPSS_SEL_CONTENT'), 'id', 'name');
		$db->setQuery( $query );
		$contents = array_merge( $contents, $db->loadObjectList() );
		$contents = JHTML::_('select.genericlist',  $contents, $name, 'class="inputbox" size="1" '. $javascript, 'id', 'name', $active);

		return $contents;
	}

	function AccessProcessing( &$row, $i ) {
		if ( !$row->registers ) {
			$color_access = 'style="color: green;"';
			$task_access = 'accessregistered';
		} else if ( $row->registers == 1 ) {
			$color_access = 'style="color: red;"';
			$task_access = 'accessspecial';
		} else {
			$color_access = 'style="color: black;"';
			$task_access = 'accesspublic';
		}

		$href = '
		<a href="javascript: void(0);" onclick="return listItemTask(\'cb'. $i .'\',\''. $task_access .'\')" '. $color_access .'>
		'. $row->groupname .'
		</a>'
		;

		return $href;
	}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                  //
//                               Classes of structure                                               //
//                                                                                                  //
//////////////////////////////////////////////////////////////////////////////////////////////////////

class mosFPSlideShowImages extends JTable {
	var $id		          = null;
	var $name		      = null;
	var $path		      = null;
	var $path_type	      = null;
	var $state 		      = null;
	var $publish_up   	  = null;
	var $publish_down  	  = null;
	var $menu	          = null;
	var $menu2	          = null;
	var $target           = null;
	var $clink	          = null;
	var $ctext	          = null;
	var $plaintext        = null;
	var $registers        = null;
	var $showtitle        = null;
	var $showseccat       = null;
	var $showcustomtext   = null;
	var $showplaintext    = null;
	var $showreadmore     = null;
	var $ordering         = null;

	function __construct(&$db) {
		parent::__construct( _FPSS_TABLE_SLIDES, 'id', $db );
	}
}

class mosFPSlideShow_toolbars {

	function startTable() {
		?>
		<table cellpadding="0" cellspacing="8" border="0">
		<tr>
		<?php
	}

	function publishList( $task='publish', $alt=null ) {
		if (is_null($alt)) $alt = T_('Published');

		$mainframe =& mosMainFrame::getInstance();
		$image = $mainframe->ImageCheck( 'publish.png', '/administrator/images/', NULL, NULL, $alt, $task );
		$image2 = $mainframe->ImageCheck( 'publish_f2.png', '/administrator/images/', NULL, NULL, $alt, $task, 0 );
		?>
		<td width="25" align="center"><a href="javascript:if (document.adminForm.boxchecked.value == 0){ alert('<?php echo T_('Please make a selection from the list to publish')?> '); } else {submitbutton('<?php echo $task;?>', '');}" onmouseout="MM_swapImgRestore();"  onmouseover="MM_swapImage('<?php echo $task;?>','','<?php echo $image2; ?>',1);"> <?php echo $image; ?></a></td>
		<?php
	}

	function unpublishList( $task='unpublish', $alt=null ) {
		if (is_null($alt)) $alt = T_('Unpublished');

		$mainframe =& mosMainFrame::getInstance();
		$image = $mainframe->ImageCheck( 'unpublish.png', '/administrator/images/', NULL, NULL, $alt, $task );
		$image2 = $mainframe->ImageCheck( 'unpublish_f2.png', '/administrator/images/', NULL, NULL, $alt, $task, 0 );
		?>
		<td width="25" align="center"><a href="javascript:if (document.adminForm.boxchecked.value == 0){ alert('<?php T_('Please make a selection from the list to unpublish') ?>'); } else {submitbutton('<?php echo $task;?>', '');}" onmouseout="MM_swapImgRestore();"  onmouseover="MM_swapImage('<?php echo $task;?>','','<?php echo $image2; ?>',1);" > <?php echo $image; ?></a></td>
		<?php
	}

	function addNew( $task='new', $alt=null ) {
		if (is_null($alt)) $alt = T_('New');

		$mainframe =& mosMainFrame::getInstance();
		$image = $mainframe->ImageCheck( 'new.png', '/administrator/images/', NULL, NULL, $alt, $task );
		$image2 = $mainframe->ImageCheck( 'new_f2.png', '/administrator/images/', NULL, NULL, $alt, $task, 0 );
		?>
		<td width="25" align="center"><a href="javascript:submitbutton('<?php echo $task;?>');" onmouseout="MM_swapImgRestore();"  onmouseover="MM_swapImage('<?php echo $task;?>','','<?php echo $image2; ?>',1);"><?php echo $image; ?></a></td>
		<?php
	}

	function deleteList( $msg='', $task='remove', $alt=null ) {
		if (is_null($alt)) $alt = T_('Delete');

		$mainframe =& mosMainFrame::getInstance();
		$image = $mainframe->ImageCheck( 'delete.png', '/administrator/images/', NULL, NULL, $alt, $task );
		$image2 = $mainframe->ImageCheck( 'delete_f2.png', '/administrator/images/', NULL, NULL, $alt, $task, 0 );
		?>
		<td width="25" align="center"><a href="javascript:if (document.adminForm.boxchecked.value == 0){ alert('<?php echo 'Please make a selection from the list to delete'; ?>'); } else if (confirm('<?php echo 'Are you sure you want to delete selected items?'; ?> <?php echo $msg;?>')){ submitbutton('<?php echo $task;?>');}" onmouseout="MM_swapImgRestore();"  onmouseover="MM_swapImage('<?php echo $task;?>','','<?php echo $image2; ?>',1);"><?php echo $image; ?></a></td>
		<?php
	}

	function save( $task='save', $alt=null ) {
		if (is_null($alt)) $alt = T_('Save');

		$mainframe =& mosMainFrame::getInstance();
		$image = $mainframe->ImageCheck( 'save.png', '/administrator/images/', NULL, NULL, $alt, $task );
		$image2 = $mainframe->ImageCheck( 'save_f2.png', '/administrator/images/', NULL, NULL, $alt, $task, 0 );
		?>
		<td width="25" align="center">
		<a href="javascript:submitbutton('<?php echo $task;?>');" onmouseout="MM_swapImgRestore();"  onmouseover="MM_swapImage('<?php echo $task;?>','','<?php echo $image2;?>',1);">
		<?php echo $image;?>
		</a>
		</td>
		<?php
	}

	function savenew() {
		$mainframe =& mosMainFrame::getInstance();
		$image = $mainframe->ImageCheck( 'save.png', '/administrator/images/', NULL, NULL, T_('save'), 'save' );
		$image2 = $mainframe->ImageCheck( 'save_f2.png', '/administrator/images/', NULL, NULL, T_('save'), 'save', 0 );
		?>
		<td width="25" align="center">
		<a href="javascript:submitbutton('savenew');" onmouseout="MM_swapImgRestore();"  onmouseover="MM_swapImage('save','','<?php echo $image2;?>',1);">
		<?php echo $image;?>
		</a>
		</td>
		<?php
	}

	function saveedit() {
		$mainframe =& mosMainFrame::getInstance();
		$image = $mainframe->ImageCheck( 'save.png', '/administrator/images/', NULL, NULL, T_('save'), 'save' );
		$image2 = $mainframe->ImageCheck( 'save_f2.png', '/administrator/images/', NULL, NULL, T_('save'), 'save', 0 );
		?>
		<td width="25" align="center">
		<a href="javascript:submitbutton('saveedit');" onmouseout="MM_swapImgRestore();"  onmouseover="MM_swapImage('save','','<?php echo $image2;?>',1);">
		<?php echo $image;?>
		</a>
		</td>
		<?php
	}

	function cancel( $task='cancel', $alt=null ) {
		if (is_null($alt)) $alt = T_('Cancel');

		$mainframe =& mosMainFrame::getInstance();
		$image = $mainframe->ImageCheck( 'cancel.png', '/administrator/images/', NULL, NULL, $alt, $task );
		$image2 = $mainframe->ImageCheck( 'cancel_f2.png', '/administrator/images/', NULL, NULL, $alt, $task, 0 );
		?>
		<td width="25" align="center">
		<a href="javascript:submitbutton('<?php echo $task;?>');" onmouseout="MM_swapImgRestore();"  onmouseover="MM_swapImage('<?php echo $task;?>','','<?php echo $image2;?>',1);">
		<?php echo $image;?>
		</a>
		</td>
		<?php
	}

	function spacer( $width='' ) {
		if ($width != '') {
		?>
		<td width="<?php echo $width;?>">&nbsp;</td>
		<?php
		} else {
		?>
		<td>&nbsp;</td>
		<?php
		}
	}

	function divider() {
		$image = $mainframe->ImageCheck( 'menu_divider.png', '/administrator/images/' );
		?>
		<td width="25" align="center">
		<?php echo $image; ?>
		</td>
		<?php
	}

	function endTable() {
		?>
		</tr>
		</table>
		<?php
}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                  //
//                               Get Config Class                                                   //
//                                                                                                  //
//////////////////////////////////////////////////////////////////////////////////////////////////////

// INCLUDE CONFIGURATION
include_once(JPATH_SITE."/administrator/components/com_fpslideshow/configuration.php");
$fpss_config = new FPSSConfig();
// Table names (DO NOT edit if you don't know what you are doing!)
if(!defined("_FPSS_TABLE_SLIDES")) DEFINE("_FPSS_TABLE_SLIDES","#__fpslideshow_slides");

?>