Location: PHPKode > projects > Chalange > com_chalange/administrator/models/raceadmin.php
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.model');
 class ChalangeAdminModelRaceAdmin extends JModel{
 	var $dataRT;
 	var $dataC;
 	var $dataL;
 	function getRaceTypesList(){
 		if (empty( $this->dataRT)){
 			$query= 'SELECT * '
 			.'FROM #__gcd_racetypes ';
 			$this->dataRT= $this->_getList($query);
 		}
 		$aux= array();
 		foreach ($this->dataRT as $v){
 			$aux[]=array('text'=>$v->description,'value'=>$v->id);
 		}
 		return $aux;
 	}
 	function getLastId(){
 		if (empty( $this->dataL)){
	 		$query= 'SELECT id '
	 		.'FROM #__gcd_carreras ORDER BY ID DESC ';
	 		$this->dataL= $this->_getList($query);
 		}
 		return $this->dataL[0]->id;
 	}
 	
 	function getCategories($raceId){
 		$aux= array();
 		if (empty( $this->dataC)){
 			//obtengo todas las categorías
 			$query= 'SELECT * '
 			.'FROM #__gcd_categories ';
 			$this->dataC= $this->_getList($query);
 		}
 		//introduzco todas las categorias en el array de retorno
 		foreach ($this->dataC as $v){
 			$aux['category_'.$v->id]=array('name'=>'category_'.$v->id,'id'=>$v->id,'description'=>$v->description,'checked'=>false);
 		}
 		if (!empty($raceId)){
	 		//obtengo las categorías asignadas a esta carrera
	 			$query= 'SELECT * '
	 			.'FROM #__gcd_categoriesrace WHERE idRace='.$raceId;
	 			$this->dataC= $this->_getList($query);
	 		//recorremos las categorias y asignamos checked al array devuelto
	 		foreach ($this->dataC as $v){
	 			$aux['category_'.$v->idCategory]['checked']=true;
	 		}
 		}
 		return $aux;
 	}
 	function setCategoriesToRace($idRace,$categories){
 		foreach ($categories as $v){
 			//miramos si existe esa categoría para la carrera
 			$query= 'SELECT count(*) as esta '
 			.'FROM #__gcd_categoriesrace 	WHERE idCategory='.$v['id'].' AND idRace='.$idRace;
 			$result= $this->_getList($query);
 			if(!$result[0]->esta && $v['checked']){//se está añadiendo una categoría
 				echo 'añadiendo '.$v['description'].'<br>';
	 			$query= 'INSERT INTO #__gcd_categoriesrace (idCategory, idRace) VALUES ('.$v['id'].','.$idRace.')';
	 			$this->_db->setQuery($query);
	 			$this->_db->Query();
 				
 			}else if ($result[0]->esta && !$v['checked']){//se está eliminando una categoría
 				echo 'eliminando '.$v['description'].'<br>';
 				$query='DELETE FROM #__gcd_categoriesrace WHERE idCategory='.$v['id'].' AND idRace='.$idRace;
	 			$this->_db->setQuery($query);
	 			$this->_db->Query();
 			}
 		}	
 	}
 	
 	function asignRaceToChalange($carreraId,$chalangeId){
 		$query= "INSERT INTO #__gcd_carreraschalange (idCarrera, idChalange) VALUES ($carreraId,$chalangeId)";	
		$this->_db->setQuery($query);
		$this->_db->Query();
 	}
 	
  	function getRegistereds($idCarrera){
 		$query= 'SELECT *'
 		.'FROM #__gcd_registered'
 		." WHERE #__gcd_registered.idRace =$idCarrera "
 		.'ORDER BY category, dorsal';
 		$this->data= $this->_getList($query);
 		return $this->data;
 	}
 	function getRaceName($idCarrera){
 		$query='SELECT nombre '
 		.'FROM #__gcd_carreras '
 		."WHERE id=$idCarrera";
 		$aux= $this->_getList($query);
 		return $aux[0]->nombre;
 	}
 }
?>
Return current item: Chalange