<?php

/**
* @version 2.8.1
* @package Joomla 2.5
* @subpackage DT Register
* @copyright Copyright (C) 2006 DTH Development
* @copyright contact dthdev@dthdevelopment.com
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/

class DtregisterModelAro extends DtrModel {

     function __construct($config = array()){

       parent::__construct($config);

	   $this->table = new TableAro($this->getDBO());

	 }

}

class TableAro extends DtrTable {

    var $id;
	var $type = 'joomlaAro';
    var $aro_id;
	var $alias;

    function __construct( &$db = null ) {

		$db = &JFactory::getDBO();

	    $this->db =&$db;
		include_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_dtregister'.DS.'models'.DS.'user.php');

	    $this->TableJuser = & DtrTable::getInstance('Juser','Table');

		parent::__construct( '#__dtregister_aros', 'id', $db );

  }

  function save($data){
     
	 unset($this->AroGroup);
	  if(!isset($data['alias']) || $data['alias'] == ""){

		   $user = $this->TableJuser->find(' id = '.$data['aro_id']);

		   $user = $user[0];

		   $data['alias'] = $user->username;

	  }  

	  parent::save($data);

  }

  function findaroByUser($user){
     
	 $groups = $user->getAuthorisedGroups();
	 $groups = $user->groups;
	 $aro = $this->find(' aro_id ='.$user->id.' and type="joomlaUser"');
     pr("user Groups are : - ".implode(",",$groups));
	 if($aro ){

		 return array($aro[0]->id);

	 }else{
		
		 if(count($groups)) 
		 {
		    $con = "aro_id in(".implode(",",$groups).")";
		 }else
		 {
		    return array(0);
		 }
		 $aros = $this->find($con);
		 
		 if($aros){
             $temp = array();
			 foreach($aros as $aro) {
			 	$temp[] = $aro->id;
			 }
			 return $temp;

		 }else{

		     return false;	 

		 }

	 }

  }

  function delete($id=0){
	   
	   $this->rawquery("delete from #__dtregister_permissions where aro_id=".$id);
	   
	   parent::delete($id);
	   
  }
  
	function sync_auto(){
	   
		$sql="TRUNCATE TABLE `#__dtregister_aros` ";
		$this->_db->setQuery($sql);
		$this->_db->query();
		
		$query = "select id, title from #__usergroups ";
		$this->_db->setQuery($query);
		$groups = $this->_db->loadObjectlist();
		
		foreach ($groups as $group) {
		
			$sql="INSERT INTO `#__dtregister_aros` (aro_id, type , alias) VALUES(".$group->id.", 'joomlaAro', '".$group->title."')";
			$this->_db->setQuery($sql);
			$this->_db->query();
		
		}	
	
	}
	
	function sync(){
		
		$query = "SELECT u.id, u.title , a.aro_id FROM #__usergroups u 
				 	LEFT JOIN #__dtregister_aros a ON u.id = a.aro_id 
					
				 ";
		$this->_db->setQuery($query);
		$groups_id = $this->_db->loadObjectlist();
		// pr($groups_id);
		
		if (isset($groups_id)) {
			foreach ($groups_id as $group) {
				
				if(intval($group->aro_id)) {
					$sql="Update `#__dtregister_aros` set  alias = '".$group->title."' where aro_id = '".$group->aro_id."' ";
				} else {
					
					$sql="INSERT INTO `#__dtregister_aros` (aro_id, type, alias) VALUES(".$group->id.",'joomlaAro' , '".$group->title."')";
				
				}
				$this->_db->setQuery($sql);
				$this->_db->query();
			
			}
		}
		
		$query = "SELECT a.aro_id, a.alias FROM #__dtregister_aros a 
				 	LEFT JOIN #__usergroups u ON a.aro_id = u.id 
					WHERE u.id IS NULL  and a.type = 'joomlaAro' 
				 ";
		$this->_db->setQuery($query);
		$aros_id = $this->_db->loadObjectlist();
		// pr($aros_id);
		
		if (isset($aros_id)) {
			foreach ($aros_id as $aros) {
				
				$sql= "DELETE FROM `#__dtregister_aros` WHERE aro_id = ".$aros->aro_id;
				$this->_db->setQuery($sql);
				$this->_db->query();
			
			}
		}
			
	}

}

?>