. * */ /** * CIA Database Access Object. This class provides functionality to * load and save CIA sentences. */ require_once(FILESYSTEMROOTPATH."include/classes/IDao.php"); require_once(FILESYSTEMROOTPATH."jc/include/model/NumberOfVotes.php"); class NumberOfVotesDao implements IDao { /** MySQL object */ private $mysql; /** * Default constructor */ public function __construct(MySQL $mysql) { $this->mysql = $mysql; } public function load($id) { $query = "SELECT * FROM voting_numberofvotes WHERE `id`=".$id; if( $this->mysql->query($query) ) { return new NumberOfVotes($this->mysql->fetchArray()); } else { return array(); } } /** * Load Comments object by id * * @param id * @return */ public function loadByAgora($agora_id) { $query = "SELECT * FROM `voting_numberofvotes` WHERE `agora_id`=".$agora_id." ORDER BY `bodyCode`"; if( $this->mysql->query($query) ) { $ret = array(); while( $row = $this->mysql->fetchAssoc() ) { $ret[] = $row; } return $ret; } else { return array(); } } public function import($filename, $agora_id){ $query1 = "DELETE FROM `voting_numberofvotes` WHERE `agora_id`=".$agora_id; if (!$this->mysql->query($query1)){ return false; } /* //requires special permissions and won't run... $query2 = "LOAD DATA INFILE '$filename' INTO TABLE `voting_numberofvotes` FIELDS TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' (bodyCode, numberOfVotes) SET agora_id=$agora_id"; return $this->mysql->query($query2); */ $query = "INSERT INTO `voting_numberofvotes` (`agora_id`, `bodyCode`, `numberOfVotes`) VALUES "; $i=0; $data = array(); if (($handle = fopen($filename, "r")) !== FALSE) { while (($row = fgetcsv($handle, 1000, "\t")) !== FALSE) { $data[] = $row; if ($i > 0){ $query .= ', '; } $body = $this->mysql->escape($row[0]); $votes = $this->mysql->escape($row[1]); $query .= "($agora_id, '$body', $votes)"; $i = $i+1; } fclose($handle); } return $this->mysql->query($query); } public function saveOrUpdate(IModel $numberOfVotes) { if($numberOfVotes->getId()==NULL) { $query = "INSERT INTO `voting_numberofvotes` SET "; } else { $query = "UPDATE `voting_numberofvotes` SET "; } $query .="`id` = '".$this->mysql->escape($numberOfVotes->getId())."', "; $query .="`agora_id` = '".$this->mysql->escape($numberOfVotes->getAgora_id())."', "; $query .="`bodyCode` = '".$this->mysql->escape($numberOfVotes->getBodyCode())."', "; $query .="`numberOfVotes` = '".$this->mysql->escape($numberOfVotes->getNumberOfVotes())."'"; if($numberOfVotes->getId()!=NULL) { $query.=" WHERE `id` = ".$numberOfVotes->getId(); } return $this->mysql->query($query); } } ?>