<?php /**/ /*cleaned*/?><?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

require_once( JApplicationHelper::getPath( 'admin_html' ) );
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_2j_tabs'.DS.'tables');


$cid 		= JRequest::getVar( 'cid', array(0), '', 'array' );
JArrayHelper::toInteger($cid, array(0));

$task 	= JRequest::getCmd('task');
$act 	= JRequest::getCmd('act');

switch ($task) {

	case 'css':
		twoj_css_edit( $option );
		break;

	case 'add': twoj_tabs_edit( 0, $option );
		break;

	case 'edit': twoj_tabs_edit( intval( $cid[0] ), $option );
		break;

	case 'editA': twoj_tabs_edit( intval( $cid[0] ), $option );
		break;

	case 'apply':
	case 'save':
		if($act =='css') twoj_tabs_css_save( $option, ($task=='apply'?1:0) );
			else twoj_tabs_save( $option, ($task=='apply'?1:0) );
		break;

	case 'remove': twoj_tabs_remove( $cid, $option );
		break;

	case 'cancel':
		if($act =='css') twoj_tabs_css_cancel( $option, $act );
			else twoj_tabs_cancel( $option );
		break;

	default:
		if($act=='css') twoj_css_edit($option );
			else twoj_tabs_show( $option );
		break;
}


function twoj_tabs_show( $option ) {
	global $mainframe;

	$db					=& JFactory::getDBO();
	$limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
	$limitstart	= $mainframe->getUserStateFromRequest( $option.'.limitstart', 'limitstart', 0, 'int' );

	$query = "SELECT COUNT(*) "
	. "\n FROM #__2j_tabs"
	;
	$db->setQuery( $query );
	$total = $db->loadResult();

	jimport('joomla.html.pagination');
	$pageNav = new JPagination( $total, $limitstart, $limit );

	$query = "SELECT a.* "
	. "\n FROM #__2j_tabs AS a"
	. "\n ORDER BY a.name"
	;
	$db->setQuery( $query, $pageNav->limitstart, $pageNav->limit );

	$rows = $db->loadObjectList();
	if ($db->getErrorNum()) {
		echo $db->stderr();
		return false;
	}

	for($i=0;$i<count($rows);$i++){
		$params = new JParameter( $rows[$i]->params );
		$cat_id = $params->get('catid', 0);
		$sec_id = $params->get('secid', 0);
		$cat_or_sec = $params->get('cat_or_sec', 0);

		if(!$cat_or_sec){
			if($cat_id > 0){
				$query = "SELECT c.title as cat_name FROM #__categories AS c WHERE c.id = ".intval($cat_id );
				$db->setQuery( $query );
				$rows[$i]->cat_name  = $db->loadResult();
			}
		} else {
			if($sec_id > 0){
				$query = "SELECT c.title as cat_name FROM #__sections AS c WHERE c.id = ".intval($sec_id );
				$db->setQuery( $query );
				$rows[$i]->cat_name  = $db->loadResult();
			}
		}

	}
	HTML_2J_TABS::show( $rows, $lists, $pageNav, $option );
}


function twoj_tabs_edit( $id, $option ) {
	$db 		=& JFactory::getDBO();
	$user 		=& JFactory::getUser();
	$row =& JTable::getInstance( '2j_tabs', 'tab_' );
	if($id) $row->load( (int)$id );
	$file = JPATH_ADMINISTRATOR.DS.'components'.DS.'com_2j_tabs'.DS.'2j_tabs.xml';
	$params = new JParameter( $row->params, $file, 'component' );
	HTML_2J_TABS::edit( $row, $lists, $option, $params );
}

function twoj_tabs_save( $option, $apply=0 ) {
	global $mainframe;
	JRequest::checkToken() or die( 'Invalid Token' );
	$db 		=& JFactory::getDBO();
	$task 		= JRequest::getVar( 'task');
	$row 		=& JTable::getInstance( '2j_tabs', 'tab_' );
	if (!$row->bind(JRequest::get('post'))) {
		JError::raiseError(500, $row->getError() );
	}
	$params = JRequest::getVar( 'params', array(), 'post', 'array' );

	if ( is_array( $params ) ){
		$registry = new JRegistry();
		$registry->loadArray($params);
		$row->params = $registry->toString();
	}


	if (!$row->check()) {
		JError::raiseError(500, $row->getError() );
	}
	if (!$row->store()) {
		JError::raiseError(500, $row->getError() );
	}
	$row->checkin();
	$mainframe->redirect( 'index.php?option='.$option.($apply?'&task=editA&cid[0]='.$row->id:''), JText::_( 'Item saved' ) );
}

function twoj_css_edit($option, $apply=0){
	global $mainframe;
	$file_css  = JRequest::getVar( 'file_name_cc', '2j_tabs.css', 'string' );
	$file = JPATH_SITE.DS."plugins".DS."system".DS."2j_tabs".DS.$file_css;
	$lists = array(); $css_array = array();
	$lists['cur_file'] = $file_css;
	$css_arr = JFolder::files( JPATH_SITE.DS."plugins".DS."system".DS."2j_tabs".DS, '.css' );
	sort( $css_arr );
	for($i=0;$i<count($css_arr);$i++){
		$css_array[] = JHTML::_('select.option',  $css_arr[$i] );
	}
	$lists['file_select'] = JHTML::_('select.genericlist',  $css_array, 'file_name_cc', 'class="inputbox" size="1" ', 'value', 'text', $file_css );
	if ($fp = fopen( $file, "r" )) {
		$content = fread( $fp, filesize( $file ) );
		$content = htmlspecialchars( $content );
		HTML_2J_TABS_CSS::edit( $content, $option, $lists );
	} else {
		$mainframe->redirect( 'index.php?option=com_2j_tabs', JText::_( "Operation Failed: Could not open " ).$file );
	}
}

function twoj_tabs_css_save($option, $apply=0){
	global $mainframe;
	$file_css  = JRequest::getVar( 'file_name_cc', '2j_tabs.css', 'string' );
	$filecontent 	= JRequest::getVar( 'filecontent', '', 'string', JREQUEST_ALLOWRAW);
	if (!$filecontent || !$file_css ) {
		$mainframe->redirect( 'index.php?option=com_2j_tabs', JText::_( "Operation failed: Content empty" ) );
	}
	$file = JPATH_SITE.DS."plugins".DS."system".DS."2j_tabs".DS.$file_css ;
	clearstatcache();
	if (is_writable( $file ) == false) {
		$mainframe->redirect( 'index.php?option=com_2j_tabs', JText::_( "Operation failed: The file is not writable." ) );
	}
	if ($fp = fopen ($file, "w")) {
		fputs( $fp, stripslashes( $filecontent ) );
		fclose( $fp );
		$mainframe->redirect( 'index.php?option=com_2j_tabs'.($apply?"&task=css&file_name_cc=$file_css":''), JText::_( "CSS file saved successfully" ) );
	} else {
		$mainframe->redirect( 'index.php?option=com_2j_tabs', JText::_( "Operation failed: Failed to open file for writing." ) );
	}
}

function twoj_tabs_css_cancel($option  ) {
	global $mainframe;
	JRequest::checkToken() or die( 'Invalid Token' );
	$mainframe->redirect( 'index.php?option='.$option  );
}

function twoj_tabs_remove( &$cid, $option ) {
	global $mainframe;
	$db 		=& JFactory::getDBO();
	if (!is_array( $cid ) || count( $cid ) < 1) {
		echo "<script> alert('Select an item to delete'); window.history.go(-1);</script>\n";
		exit;
	}
	if (count( $cid )) {
		JArrayHelper::toInteger($cid, array(0));
		$cids = 'id=' . implode( ' OR id=', $cid );
		$query = "DELETE FROM #__2j_tabs WHERE ( $cids )";
		$db->setQuery( $query );
		if (!$db->query()) {
			JError::raiseError(500, $row->getError() );
		}
	}
	$mainframe->redirect( 'index.php?option='.$option  , JText::_( "Item(s) delet successfully" ));
}

function twoj_tabs_cancel( $option ) {
	global $mainframe;
	JRequest::checkToken() or die( 'Invalid Token' );
	$mainframe->redirect( 'index.php?option='.$option  );
}

?>