<?php
/**
 * Adodis Blog package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2012 - 2013 J-Extension
 * @license GNU/GPL, see LICENSE.php for full license.
**/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

class AdodisBlogModelUsers extends JModelList
{
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'id', 'ur.id',
				'name', 'ur.name',
				'username', 'ur.username',
				'email', 'ur.email',
				'user_id', 'ur.user_id',
				'block', 'ur.block'
			);
		}
		parent::__construct($config);
	}

	/**
	 * Method to auto-populate the model state.
	 */
	protected function populateState($ordering = null, $direction = null)
	{

		// Initialise variables.
		$app	= JFactory::getApplication();

		// Adjust the context to support modal layouts.
		if ($layout = JRequest::getVar('layout')) {
			$this->context .= '.'.$layout;
		}

		$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
		$this->setState('filter.search', $search);
		$user 	= $this->getUserStateFromRequest($this->context.'.filter.user', 'filter_user');
		$this->setState('filter.user', $user);
		$block 	= JRequest::getVar('blockid','0');
		$this->setState('block', $block);

		// List state information.
		parent::populateState('ur.user_id', 'asc');
	}

	/**
	 * Method to get a store id based on model configuration state.
	 */
	protected function getStoreId($id = '')
	{
		// Compile the store id.
		$id	.= ':'.$this->getState('filter.search');
		$id	.= ':'.$this->getState('filter.user');

		return parent::getStoreId($id);
	}

	/**
	 * Build an SQL query to load the list data.
	 */
	protected function getListQuery()
	{
		//Check clicked block or unblock
		$block			= $this->getState('block');
		if($block){
			$val 		= JRequest::getVar('blockval','0');
			$result 	= $this->block($block , $val);
		}

		// Create a new query object.
		$db				= $this->getDbo();
		$query			= $db->getQuery(true);

		// Select the required fields from the table.
		$query->select( $this->getState(
			'list.select',
			'ur.*'
			) );
		$query->from( '#__adodisblog_users as ur' );

		// Filter by search in title & microblog.
		$search			= $this->getState('filter.search');
		if (!empty($search)) {
			if (stripos($search, 'id:') === 0) {
				$query->where('ur.id = '.(int) substr($search, 3));
			} else {
				$search = $db->Quote('%'.$db->getEscaped($search, true).'%');
				$query->where('(ur.name LIKE '.$search.' OR ur.username LIKE '.$search.')');
			}
		}

		// Filter by user
		$user			= $this->getState('filter.user');
		if(is_numeric($user)){
			$query->where('(ur.block ='.$user.')');
		}

		// Add the list ordering clause.
		$orderCol		= $this->state->get('list.ordering');
		$orderDirn		= $this->state->get('list.direction');

		$query->order($db->getEscaped($orderCol.' '.$orderDirn));

		return $query;
	}

	function block($id , $val)
	{
		if (empty( $id )) {
			return 0;
		}
		if($val=='0'){
			$val	= 1;
		} else {
			$val=0;
		}
		$db			= $this->getDbo();
		$query		= $db->getQuery(true);
        $query->clear();
		$query->update(' #__adodisblog_users ');
		$query->set(' block =' .(int) $val);
		$query->where(' id = ' . (int) $id );

		$db->setQuery((string)$query);

        if (!$db->query()) {
            JError::raiseError(500, $db->getErrorMsg());
        	return false;
        } else {
        	return true;
		}
	}
	function filterUser($active = NULL)
	{
		$options	= array();
		$options[]  = JHTML::_('select.option','', "--".JText::_( 'COM_ADODISBLOG_SELECT_USER' )."--");
		$options[]  = JHTML::_('select.option','1', JText::_( 'COM_ADODISBLOG_SELECT_USER_UNBLOCKED_USER' ));
		$options[]  = JHTML::_('select.option','0', JText::_( 'COM_ADODISBLOG_SELECT_USER_BLOCKED_USER' ));
		$user 		= JHTML::_('select.genericlist',  $options, 'filter_user', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text',$active);

		return $user;
	}

	//UnBlock Group of users
	function unBlockUsers ()
	{
		$postvalue 	= JRequest::get('post');
		$checked 	= $postvalue['cid'];
		$string 	= implode(",",$checked);
		$db			= $this->getDbo();
		$query		= $db->getQuery(true);
        $query->clear();
		$query->update(' #__adodisblog_users ');
		$query->set(' block = 1' );
		$query->where(' id IN ( ' . $string . ' )' );
		$db->setQuery((string)$query);
        if (!$db->query()) {
            JError::raiseError(500, $db->getErrorMsg());
        	return false;
        } else {
        	return true;
		}
	}

	//Block Group of users
	function blockUsers()
	{
		$postvalue 	= JRequest::get('post');
		$checked 	= $postvalue['cid'];
		$string 	= implode(",",$checked);
		$db			= $this->getDbo();
		$query		= $db->getQuery(true);
        $query->clear();
		$query->update(' #__adodisblog_users ');
		$query->set(' block = 0' );
		$query->where(' id IN ( ' . $string . ' )' );
		$db->setQuery((string)$query);
        if (!$db->query()) {
            JError::raiseError(500, $db->getErrorMsg());
        	return false;
        } else {
        	return true;
		}
	}
}
?>
