<?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.
**/

defined('_JEXEC') or die('Restricted access');

jimport('joomla.plugin.plugin');
jimport('joomla.filesystem.file');

class plgUserAdodisBlogUsers extends JPlugin
{
	public function __construct(& $subject, $config)
	{
		parent::__construct($subject, $config);
		$this->loadLanguage();
	}

	function onUserAfterSave($user, $isnew, $success, $msg)
	{
	    $db 				=& JFactory::getDBO();
	    if( is_object($user)) {
	        $user   		= get_object_vars( $user );
	    }

	    if( !isset( $user['id'] ) && empty( $user['id'] ) )
			return;

	    $data 				= new stdClass();
		$data->id 			= null;
		$data->name 		= $user['name'];
		$data->username 	= $user['username'];
		$data->email 		= $user['email'];
		$data->user_id 		= $user['id'];
		$data->block 		= 1;
		
		$db 				= JFactory::getDBO();
		
		$query 				= 'SELECT count(ur.user_id) FROM #__adodisblog_users AS ur WHERE ur.user_id='.$user['id'];
		$db->setQuery( $query );
		$userid				= $db->loadResult();
		if($userid) {} else{
			$db 			= JFactory::getDBO();
			$db->insertObject( '#__adodisblog_users', $data, user_id );
		}
	}
}
