<?php
/**
* @version 1.0.0
* @package RSEvents! 1.0.0
* @copyright (C) 2009 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

defined('_JEXEC') or die();
 
jimport( 'joomla.application.component.model' );

class rseventsModelUsers extends JModel
{
	var $_query;
	var $_data;
	var $_total=null;
	var $_pagination=null;
	var $_componentList=null;
	function _buildQuery()
	{
		global $mainframe;
		
				
		$this->_query="SELECT `id` ,`name` ,`username` , `email` FROM #__users ";
		//echo $this->_query;
	}
	function __construct()
	{	
		parent::__construct();
		$this->_buildQuery();
		global $mainframe, $option;

		// Get pagination request variables
		$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = $mainframe->getUserStateFromRequest($option.'.limitstart', 'limitstart', 0, 'int');

		// In case limit has been changed, adjust it
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
		

		//echo $this->_db->_errorMsg;
	}
	
	function getData()
	{
		if (empty($this->_data))
		{
			$this->_data=$this->_getList($this->_query,$this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_data;
	}

	function returnUser($email , $name = null)
	{
		global $mainframe;
		
		$db = & JFactory::getDBO();
		$rseventsConfig = $mainframe->getUserState('rseventsConfig');
		$db->setQuery("SELECT `id` FROM #__users WHERE `email` = '".$db->getEscaped($email)."'");
		$userid = $db->loadResult();
		if(empty($userid))
		{
			//Create a new user
			$user = & JFactory::getUser(0);
			$user->name = $name;
			$user->email = $email;

			$username = preg_replace("/[^a-zA-Z0-9\s]/","", trim(strtolower($name)));
			$username = preg_replace('/ {1,}/','.',$username);
			$isDuplicate=true;
			while($isDuplicate)
			{
				$db->setQuery("select count(id) as c from #__users where username='".$username."'");
				$count=$db->loadResult();
				if($count==0) $isDuplicate=false;
				else
				{
					$isDuplicate=true;
					$username=$username.rand(0,99);
				}
			}
			
			$user->username = $username;
			$user->sendEmail = 1;
			
			$password=substr(md5(time()*rand()),0,5);
			$user->password=md5($password);
			
			$user->gid = 18;
			$user->usertype = 'Registered';
			$user->block = 0;
			
			$user->save();
			
			$userid = $user->id;
			
			//auto approve users if CB is installed
			if ($rseventsConfig['cb.autoapprove'] == 1 && file_exists(JPATH_SITE.DS.'components'.DS.'com_comprofiler'.DS.'comprofiler.php'))
			{
				$db->setQuery("INSERT INTO #__comprofiler SET approved = 1 , user_id = ".$userid." , id = ".$userid." , confirmed = 1 ");
				$db->query();
			}
			
			//send mail to new user
			
			$mailfrom 		= $mainframe->getCfg( 'mailfrom' );
			$fromname 		= $mainframe->getCfg( 'fromname' );
			$sitename 		= $mainframe->getCfg( 'sitename' );
			$siteURL		= JURI::base();
			$username		= $user->username;
			$name			= $user->name;
			
			$subject 	= sprintf ( JText::_( 'RSE_MSG_SUBJECT' ), $name, $sitename);
			$subject 	= html_entity_decode($subject, ENT_QUOTES);
			
			$message = sprintf ( JText::_( 'RSE_SEND_MSG' ), $name, $sitename, $siteURL ,$username, $password );
			$message = html_entity_decode($message, ENT_QUOTES);
			
			JUtility::sendMail($mailfrom, $fromname, $email, $subject, $message);
			
		} 
		
		return $userid;
	}
}