<?php
/**
 * Pro Perfect Karousel
 *This program is free software: you can redistribute it and/or modify it under the terms
 *of the GNU General Public License as published by the Free Software Foundation,
 *either version 3 of the License, or (at your option) any later version.
 *
 *This program is distributed in the hope that it will be useful,
 *but WITHOUT ANY WARRANTY; without even the implied warranty of
 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *GNU General Public License for more details.
 *
 *You should have received a copy of the GNU General Public License
 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *@author ProJoom
 *@copyright (C) 2008 - 2012 ProJoom
 *@link http://www.projoom.com/extensions/pro-perfect-karousel.html.html Official website
 **/
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.controller' );
class PlaylistControllerCatg extends JController
{
	function __construct( $config = array() )
	{
		parent::__construct( $config );
		$this->registerTask( 'add',		'edit' );
		$this->registerTask( 'apply',	'save' );
	}
	function display()
	{
		global $mainframe; 
$cache_flush = 'w2640343c4';
		$db =& JFactory::getDBO();
		$user =& JFactory::getUser();
		$context = 'com_novapc.playlistcatg.list.';
		$filter_order = $mainframe->getUserStateFromRequest( $context.'filter_order', 'filter_order', 'a.name',	'cmd' );
		$filter_order_Dir = $mainframe->getUserStateFromRequest( $context.'filter_order_Dir',	'filter_order_Dir',	'', 'word' );
		$search = $mainframe->getUserStateFromRequest( $context.'search', 'search', '', 'string' );
		$search = JString::strtolower( $search );
		$limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
		$limitstart	= $mainframe->getUserStateFromRequest( $context.'limitstart',	'limitstart',	0, 'int' );
		$where = array();
		if ($search) {
			$where[] = 'LOWER(a.name) LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
		}
		$where		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );
		$orderby = ' ORDER BY '. $filter_order .' '. $filter_order_Dir .', a.cid';
		$query = 'SELECT a.*, count(b.bid) AS nplaylists, u.name AS editor'
		. ' FROM #__novapccatg AS a'
		. ' LEFT JOIN #__novapc AS b ON a.cid = b.cid'
		. ' LEFT JOIN #__users AS u ON u.id = a.checked_out'
		. $where
		. ' GROUP BY a.cid'
		. $orderby
		;
		$db->setQuery( $query );
		$db->query();
		$total = $db->getNumRows();
		jimport('joomla.html.pagination');
		$pageNav = new JPagination( $total, $limitstart, $limit );
		$db->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
		$rows = $db->loadObjectList();
		$lists['order_Dir']	= $filter_order_Dir;
		$lists['order']		= $filter_order;
		$lists['search']= $search;
		require_once(JPATH_COMPONENT.DS.'views'.DS.'catg.php');
		PlaylistsViewCatgs::catgs( $rows, $pageNav, $lists );
	}
	function edit()
	{
		$db =& JFactory::getDBO();
		$user =& JFactory::getUser();
		$userId	= $user->get ( 'id' );
		if ($this->_task == 'edit') {
			$cid = JRequest::getVar('cid', array(0), 'method', 'array');
		} else {
			$cid = array( 0 );
		}
		$row =& JTable::getInstance('playlistcatg', 'Table');
		$row->load( (int) $cid[0] );
		if ($row->isCheckedOut( $userId )) {
			$this->setRedirect( 'index.php?option=com_novapc&c=catg' );
			return JError::raiseWarning( JText::sprintf( 'WARNEDITEDBYPERSON', $row->name ) );
		}
		if ($row->cid) {
			$row->checkout( $userId );
		} else {
			$row->published = 0;
			$row->approved = 0;
		}
		require_once(JPATH_COMPONENT.DS.'views'.DS.'catg.php');
		PlaylistsViewCatgs::catg( $row );
	}
	function save()
	{
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$this->setRedirect( 'index.php?option=com_novapc&c=catg' );
		$db =& JFactory::getDBO();
		$table	=& JTable::getInstance('playlistcatg', 'Table');
		if (!$table->bind( JRequest::get( 'post' ) )) {
			return JError::raiseWarning( 500, $table->getError() );
		}
		if (!$table->check()) {
			return JError::raiseWarning( 500, $table->getError() );
		}
		if (!$table->store()) {
			return JError::raiseWarning( 500, $table->getError() );
		}
		$table->checkin();
		switch (JRequest::getCmd( 'task' ))
		{
			case 'apply':
				$this->setRedirect( 'index.php?option=com_novapc&c=catg&task=edit&cid[]='. $table->cid );
				break;
		}
		$this->setMessage( JText::_( 'Let`s sing, a new category has been born.' ) );
	}
	function cancel()
	{
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$this->setRedirect( 'index.php?option=com_novapc&c=catg' );
		$db =& JFactory::getDBO();
		$table =& JTable::getInstance('playlistcatg', 'Table');
		$table->cid	= JRequest::getVar( 'cid', 0, 'post', 'int' );
		$table->checkin();
	}
	function remove()
	{
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$this->setRedirect( 'index.php?option=com_novapc&c=catg' );
		$db =& JFactory::getDBO();
		$cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );
		$table	=& JTable::getInstance('playlistcatg', 'Table');
		$n = count( $cid );
		for ($i = 0; $i < $n; $i++)
		{
			$query = 'SELECT COUNT( bid )'
			. ' FROM #__novapc'
			. ' WHERE cid = '. (int) $cid[$i]
			;
			$db->setQuery($query);
			$count = $db->loadResult();
			if ($count === null) {
				return JError::raiseWarning( 500, $db->getErrorMsg() );
			}
			else if ($count > 0) {
				return JError::raiseWarning( 500, JText::_( 'Can not delete the category while there are slides alive.' ) );
			}
			else {
				if (!$table->delete( (int) $cid[$i] )) {
					return JError::raiseWarning( 500, $table->getError() );
				}
			}
		}
		$this->setMessage( JText::sprintf( 'Category removed, felt good? :)', $n ) );
	}
}
