You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
4.0 KiB

<?php
namespace Gateway;
use Config\ConnectClass;
use Config\Connection;
use PDO;
use PDOException;
class GatewayForm
{
private Connection $connection;
public function __construct()
{
try{
$this->connection = (new ConnectClass)->connect();
}catch(PDOException $e){
throw new PDOException($e->getMessage(), $e->getCode(), $e);
}
}
public function insertForm(array $parameters): void
{
if(empty($this->getForm(array())))
{
$query = "INSERT INTO Form(title, description) VALUES(:title, :description)";
$this->connection->executeQuery($query, array(
':title' => array($parameters[0], PDO::PARAM_STR), //parameters[0] = title of the form
':description' => array($parameters[1], PDO::PARAM_STR) //parameters[1] = description of the form
));
}
}
public function getForm(array $ignore): array //parameters never used cause every function require this parameter
{
$query = "SELECT * FROM `form`";
$this->connection->executeQuery($query);
return $this->connection->getResults();
}
public function deleteForm(array $idform): void
{
$query = "DELETE FROM Form WHERE id = :id";
$this->connection->executeQuery($query, array(
':id' => array($idform[0], PDO::PARAM_INT)
));
}
public function selectForDeleteAndInsert(int $idQuestion, string $response){
$query = "SELECT pr.id FROM Propose p, PossibleResponse pr WHERE p.question = :id AND p.possibleResponse = pr.id AND pr.content = :response";
$this->connection->executeQuery($query, array(
':id' => array($idQuestion, PDO::PARAM_INT),
':response' => array($response, PDO::PARAM_STR)
));
return $this->connection->getResults()[0][0];
}
public function assignKeywordToQuestion(array $parameters): void
{
$query = "INSERT INTO Reference(possibleResponse, keyword) VALUES(:possibleResponse, :keyword)";
$this->connection->executeQuery($query, array(
':possibleResponse' => array($this->selectForDeleteAndInsert($parameters[2],$parameters[1]), PDO::PARAM_INT),
':keyword' => array($parameters[0], PDO::PARAM_STR) //parameters[0] = keyword
));
}
public function deleteKeywordFromQuestion(array $parameters): void
{
$query = "DELETE FROM Reference WHERE response = :idResponse AND keyword = :idKeword";
$this->connection->executeQuery($query, array(
':possibleResponse' => array($this->selectForDeleteAndInsert($parameters[2],$parameters[1]), PDO::PARAM_INT),
':keyword' => array($parameters[0], PDO::PARAM_STR) //parameters[0] = keyword
));
}
public function updateTitleToForm(array $parameters): void
{
$query = "UPDATE Form SET title = :title WHERE id = :id";
$this->connection->executeQuery($query, array(
':title' => array($parameters[0], PDO::PARAM_STR), //parameters[0] = title
':id' => array($parameters[1], PDO::PARAM_INT) //parameters[1] = idForm
));
}
public function updateDescriptionToForm(array $parameters): void
{
$query = "UPDATE Form SET title = :title WHERE description = :description";
$this->connection->executeQuery($query, array(
':description' => array($parameters[0], PDO::PARAM_STR), //parameters[0] = description
':id' => array($parameters[1], PDO::PARAM_INT) //parameters[1] = idForm
));
}
public function deleteDescriptionToForm(array $idForm): void
{
$query = "UPDATE Form SET title = :title WHERE description = :description";
$this->connection->executeQuery($query, array(
':description' => array('', PDO::PARAM_STR),
':id' => array($idForm[0], PDO::PARAM_INT)
));
}
}