<?php
// taskDescription.php - subtask details 
// ------------------------------------------------------------------------
// Copyright (c) 2007 Jerome Nivard
// Copyright (c) 2015 Denis Guinnepain
// ------------------------------------------------------------------------
// This file is part of Diamentis

// Diamentis is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.

// Diamentis is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with phpBugTracker; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// ------------------------------------------------------------------------

if ( !defined('INC_FILE_TASK_DESCRIPTION') ) {
      define ('INC_FILE_TASK_DESCRIPTION',1);

include_once('Inc/ConstantesDB.php');
      
class TaskDescription
{
  var $id;
  var $title;
  var $description;
  var $name;
  var $type;
  var $group;
  
function TaskDescription ($id=-1) {
  $this->id  = $id;
}

function GetFromId(&$tab, $targetID ) {
  $index=-1;
  foreach($tab as $desc) {
    $index = $index +1;
    If ($desc->id==$targetID) break;
  }
  return $index;
}
		
function loadFromId( &$mdb2 ) {
    $ret=-1;
    $req="select * from " . TBL_PREFIX . "taskdescription where id=".$this->id;
    $result=mysql_query($req,$mdb2);
    if ($result==false) {
      $ret=-1;
			echo "$req a échoué";
    } else {
      $data=mysql_fetch_object($result);
      $this->id             =$data->id;
      $this->name           =$data->name;
      $this->type           =$data->type;
      $this->title          =$data->title;
      $this->description    =$data->description;
			$this->group					=$data->group;
      $ret=0;
    }
    return $ret;
  }
	
  function loadFromDBData( &$mdb2, $data )
  {
		$this->id             =$data->id;
		$this->name           =$data->name;
		$this->type           =$data->type;
		$this->title          =$data->title;
		$this->description    =$data->description;
		$this->group          =$data->group;
    return 1;
  }
  
  function loadFromRstData( &$mdb2, $data )
  {
  	$this->name           =$data['name'];
  	$this->type           =$data['type'];
  	$this->title          =$data['title'];
  	$this->description    =$data['description'];
  	$this->group          =$data['group'];
  	return 1;
  }
  
  
Function loadList( &$mdb2, &$tab, $tabTasks)
  {
    $ret=-1;
    if (count($tabTasks)<=0)
    {
    	return $ret;
    }
    $i=0;
    $tokenizedTasks = "";
    foreach ( $tabTasks as $task){
    	if ($i>0){
    		$tokenizedTasks.=",";
    	}
    	$tokenizedTasks.="$task->idTaskDesc";
    	$i+=1;
    }
	$req="select * from " . TBL_PREFIX . "taskdescription WHERE id in ( " . $tokenizedTasks . ") order by id asc";    
	$result=mysql_query($req,$mdb2);
    if ($result==false) {
		  echo "$req a échoué";
      $ret=-1;
    } else {
      $i=0;
      while ($data=mysql_fetch_object($result)) {
        $tab[]=new taskDescription;
        $tab[$i]->initAttributes();
        $tab[$i]->loadFromDBData($mdb2, $data);
        $i++;
      }
      $ret=$i;
    }
    return $ret;
  }

Function initAttributes( ) {
  $this->id  = -1;
}
function Save(&$mdb2) {
	$ret=-1;
	
	if ($this->title==""){
		printf("Title field empty");
		return $ret;
		}
	$reqNames = "insert into " . TBL_PREFIX . "taskdescription (  `title`  ";
	$reqValues = " values (\"$this->title\"";
	
	If ($this->description<>'') {
		//printf("description : %d", $this->description);
		$reqNames = $reqNames.", `description`";
		$reqValues = $reqValues.", \"$this->description\"";
	}
	If ($this->name<>'') {
		$reqNames = $reqNames.", `name`";
		$reqValues = $reqValues.", \"$this->name\"";
	}
	If ($this->type<>'') {
		$reqNames = $reqNames.", `type`";
		$reqValues = $reqValues.", \"$this->type\"";
	}
	If ($this->group<>'') {
		$reqNames = $reqNames.", `group`";
		$reqValues = $reqValues.", \"$this->group\"";
	}
	$reqNames = $reqNames.")";
	$reqValues = $reqValues.")";
	$req = $reqNames.$reqValues;
	$result=mysql_query($req,$mdb2);
	if ($result==false) {
		printf("<br>La requête $req a échoué !!!");
		$ret = -1;
	} else {
		//printf("<br>La requête $req a réussi !");
		$req2="select id from " . TBL_PREFIX . "taskdescription order by id desc";
		$result2=mysql_query($req2,$mdb2);
		$data=mysql_fetch_object($result2);
		$ret = $data->id;
		
	}
	//mysql_free_result($result);
	return $ret;
}
function Delete(&$mdb2) {
	$req="delete from " . TBL_PREFIX . "taskdescription where id=$this->id";
	$result=mysql_query($req,$mdb2);
	if ($result==false) {
		printf("<br>La requête $req a échoué !");
		$ret = -1;
	} else {
		printf("<br>La requête $req a réussi !");
		$ret = 1;
	}
}

function Update(&$mdb2) {
	If (trim($this->title)=="") {
		$req = " `title`=NULL";
	} else {
		$req = " `title`=\"".$this->title . "\"";
	}
	If (trim($this->description)=="") {
		$req .= ", `description`=NULL";
	} else {
		$req .= ", `description`=\"".$this->description . "\"";
	}
	If (trim($this->name)=="") {
		$req .= ", `name`=NULL";
	} else {
		$req .= ", `name`=\"".$this->name . "\"";
	}
	If (trim($this->type)=="") {
		$req .= ", `type`=NULL";
	} else {
		$req .= ", `type`=\"".$this->type . "\"";
	}
	If (trim($this->group)=="") {
		$req .= ", `group`=NULL";
	} else {
		$req .= ", `group`=\"".$this->group ."\"";
	}
	$req="update " . TBL_PREFIX . "taskdescription set $req where id=$this->id";
	$result=mysql_query($req,$mdb2);
	if ($result==false) {
		printf("<br>La requête $req a échoué !");
		$ret = -1;
	} else {
		//printf("<br>La requête $req a réussi !");
		//mysql_free_result($result);
		$ret = $this->id;
	}
	return $ret;
}


}

} // ifndef
?>
