Merge pull request #112 from ThomasRubini/appreciation_model_refactor
This commit is contained in:
commit
ee7be3d7e2
@ -11,9 +11,8 @@ final class ApprController
|
||||
$S_comment = Utils::getOrDie($A_postParams, "comment");
|
||||
$I_note = Utils::intOrDie(Utils::getOrDie($A_postParams, "note"));
|
||||
|
||||
$O_apprModel = new ApprModel();
|
||||
$O_apprModel->createAppr($_SESSION["ID"], $I_recipe_id, $S_comment, $I_note);
|
||||
|
||||
$O_appr = new ApprModel($S_comment, $I_note,date("Y-m-d"),$_SESSION["ID"],$I_recipe_id);
|
||||
$O_appr->insert();
|
||||
header("Location: ".$_SERVER['HTTP_REFERER']);
|
||||
}
|
||||
|
||||
@ -23,18 +22,17 @@ final class ApprController
|
||||
|
||||
$I_appr_id = Utils::intOrDie($A_urlParams[0]);
|
||||
|
||||
$O_apprModel = new ApprModel();
|
||||
$A_appr = $O_apprModel->getApprById($I_appr_id);
|
||||
$O_appr = ApprModel::getApprById($I_appr_id);
|
||||
|
||||
if ($A_appr === null) {
|
||||
if ($O_appr === null) {
|
||||
throw new HTTPSpecialCaseException(404);
|
||||
}
|
||||
|
||||
if ($A_appr["AUTHOR_ID"] !== $_SESSION["ID"]) {
|
||||
if ($O_appr->I_AUTHOR_ID !== $_SESSION["ID"]) {
|
||||
Session::admin_or_die();
|
||||
}
|
||||
|
||||
$O_apprModel->deleteAppr($I_appr_id);
|
||||
$O_appr->delete();
|
||||
|
||||
header("Location: ".$_SERVER['HTTP_REFERER']);
|
||||
}
|
||||
|
||||
@ -1,8 +1,68 @@
|
||||
<?php
|
||||
|
||||
final class ApprModel {
|
||||
public $I_ID = null;
|
||||
public $S_COMMENT = null;
|
||||
public $I_NOTE = null;
|
||||
public $S_DATE = null;
|
||||
public $I_AUTHOR_ID = null;
|
||||
public $I_RECIPE_ID = null;
|
||||
|
||||
public function searchRecipeApprsWithAuthors($I_recipe_id)
|
||||
public $O_AUTHOR = null;
|
||||
|
||||
public function __construct($S_COMMENT, $I_NOTE, $S_DATE, $I_AUTHOR_ID, $I_RECIPE_ID)
|
||||
{
|
||||
$this->S_COMMENT = $S_COMMENT;
|
||||
$this->I_NOTE = $I_NOTE;
|
||||
$this->S_DATE = $S_DATE;
|
||||
$this->I_AUTHOR_ID = $I_AUTHOR_ID;
|
||||
$this->I_RECIPE_ID = $I_RECIPE_ID;
|
||||
}
|
||||
private function createFromRow($A_row,$I_id){
|
||||
$O_appr = new ApprModel($A_row["COMMENT"], $A_row["NOTE"], $A_row["DATE"], $A_row["AUTHOR_ID"], $A_row["RECIPE_ID"]);
|
||||
$O_appr->I_ID = $I_id;
|
||||
return $O_appr;
|
||||
}
|
||||
|
||||
public function insert(){
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("INSERT INTO APPRECIATION (COMMENT, NOTE, DATE, AUTHOR_ID, RECIPE_ID) VALUES(:comment, :note, :date, :author_id, :recipe_id)");
|
||||
$stmt->bindParam("comment", $this->S_COMMENT);
|
||||
$stmt->bindParam("note", $this->I_NOTE);
|
||||
$stmt->bindParam("date", $this->S_DATE);
|
||||
$stmt->bindParam("author_id", $this->I_AUTHOR_ID);
|
||||
$stmt->bindParam("recipe_id", $this->I_RECIPE_ID);
|
||||
$stmt->execute();
|
||||
$this->I_ID = Model::get()->lastInsertId();
|
||||
}
|
||||
public function update(){
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("UPDATE APPRECIATION SET COMMENT=:comment, NOTE =:note, DATE =:date, AUTHOR_ID =:author_id, RECIPE_ID =:recipe_id WHERE ID = :id");
|
||||
$stmt->bindParam("id", $this->I_ID);
|
||||
$stmt->bindParam("comment", $this->S_COMMENT);
|
||||
$stmt->bindParam("note", $this->I_NOTE);
|
||||
$stmt->bindParam("date", $this->S_DATE);
|
||||
$stmt->bindParam("author_id", $this->I_AUTHOR_ID);
|
||||
$stmt->bindParam("recipe_id", $this->I_RECIPE_ID);
|
||||
$stmt->execute();
|
||||
$this->I_ID = Model::get()->lastInsertId();
|
||||
}
|
||||
public function delete(){
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("DELETE FROM APPRECIATION WHERE ID=:id");
|
||||
$stmt->bindParam("id", $this->I_ID);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
public function getAuthor(){
|
||||
if($this->O_AUTHOR === null){
|
||||
$this->O_AUTHOR = UserModel::getByID($this->I_AUTHOR_ID);
|
||||
}
|
||||
return $this->O_AUTHOR;
|
||||
}
|
||||
|
||||
//DECRECATED
|
||||
public static function searchRecipeApprsWithAuthors($I_recipe_id)
|
||||
{
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("
|
||||
@ -14,59 +74,56 @@ final class ApprModel {
|
||||
");
|
||||
$stmt->bindParam("recipe_id", $I_recipe_id);
|
||||
$stmt->execute();
|
||||
|
||||
$rows = $stmt->fetchAll();
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
public function searchRecipeApprs($I_recipe_id)
|
||||
public static function searchRecipeApprs($I_recipe_id)
|
||||
{
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("SELECT * FROM APPRECIATION WHERE RECIPE_ID = :recipe_id");
|
||||
$stmt->bindParam("recipe_id", $I_recipe_id);
|
||||
$stmt->execute();
|
||||
return $stmt->fetchAll();
|
||||
$A_apprs = array();
|
||||
foreach($stmt->fetchAll() as $row){
|
||||
array_push($A_apprs, self::createFromRow($row, $row["ID"]));
|
||||
}
|
||||
return $A_apprs;
|
||||
}
|
||||
|
||||
public function createAppr($I_user_id, $I_recipe_id, $S_Comment, $I_note)
|
||||
{
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("
|
||||
INSERT INTO APPRECIATION (COMMENT,NOTE,DATE,AUTHOR_ID,RECIPE_ID) VALUES (:comment, :note, :date, :author_id, :recipe_id)
|
||||
");
|
||||
$stmt->bindParam("comment",$S_Comment);
|
||||
$stmt->bindParam("note",$I_note);
|
||||
$_date = date("Y-m-d");
|
||||
$stmt->bindParam("date",$_date);
|
||||
$stmt->bindParam("author_id",$I_user_id);
|
||||
$stmt->bindParam("recipe_id",$I_recipe_id);
|
||||
$stmt->execute();
|
||||
return $stmt->fetch();
|
||||
}
|
||||
|
||||
public function deleteAppr($I_appr_id)
|
||||
public static function deleteById($I_id)
|
||||
{
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("DELETE FROM APPRECIATION WHERE ID = :appr_id");
|
||||
$stmt->bindParam("appr_id", $I_appr_id);
|
||||
$stmt->bindParam("appr_id", $I_id);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
public function getApprByID($I_appr_id)
|
||||
public static function getApprById($I_id)
|
||||
{
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("SELECT * FROM APPRECIATION WHERE ID = :appr_id");
|
||||
$stmt->bindParam("appr_id", $I_appr_id);
|
||||
$stmt->bindParam("appr_id", $I_id);
|
||||
$stmt->execute();
|
||||
|
||||
$row = $stmt->fetch();
|
||||
if ($row === false) return null;
|
||||
return $row;
|
||||
$O_appr = new ApprModel($row["COMMENT"], $row["NOTE"], $row["DATE"], $row["AUTHOR_ID"], $row["RECIPE_ID"]);
|
||||
$O_appr->I_ID = $I_id;
|
||||
return $O_appr;
|
||||
}
|
||||
|
||||
public function updateAppreciation($I_appr_id)
|
||||
public static function getAppsrByRecipeId($I_id)
|
||||
{
|
||||
$O_model = Model::get();
|
||||
$stmt = $O_model->prepare("SELECT * FROM APPRECIATION WHERE RECIPE_ID = :recipe_id");
|
||||
$stmt->bindParam("recipe_id", $I_id);
|
||||
$stmt->execute();
|
||||
|
||||
$row = $stmt->fetch();
|
||||
if ($row === false) return null;
|
||||
$O_appr = new ApprModel($row["COMMENT"], $row["NOTE"], $row["DATE"], $row["AUTHOR_ID"], $row["RECIPE_ID"]);
|
||||
$O_appr->I_ID = $I_id;
|
||||
return $O_appr;
|
||||
}
|
||||
}
|
||||
|
||||
@ -142,8 +142,7 @@ final class RecipeModel
|
||||
|
||||
public function getApprs(){
|
||||
if ($this->A_APPRS === null) {
|
||||
$O_apprModel = new ApprModel();
|
||||
$this->A_APPRS = $O_apprModel->searchRecipeApprsWithAuthors($this->I_ID);
|
||||
$this->A_APPRS = ApprModel::searchRecipeApprs($this->I_ID);
|
||||
}
|
||||
return $this->A_APPRS;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user