id = $id; } $this->load($this->id); } function load($id = NULL) { if (is_numeric($id)) { $this->id = $id; } $ret = false; if (is_numeric($this->id)) { $q = "SELECT * FROM " . DB_PREFIX . "user_nutritions WHERE id=".$this->id; $ret = query_row($q); } if ( $ret) { $this->id = $ret->id; $this->food = $ret->food; } } function getURL() { return WWW_ROOT."trainings/view/".$this->id; } function getId() { return $this->id; } function create() { $this->setCreator(get_logged_in_user()->getId()); $q = "INSERT INTO " . DB_PREFIX . "user_nutritions (creator, training, food, nutrition_time, kcal, proteins, carbohydrates, fat) values (".$this->getCreator().", ".$this->getTraining().", '".$this->getFood()."', '".$this->getNutritionTime()."', '".$this->getKCal()."', '".$this->getProteins()."', '". $this->getCarbohydrates() ."', '".$this->getFat()."')"; $uid = query_insert($q); if ($uid) return $uid; return false; } function save() { $update = query_update("UPDATE " . DB_PREFIX . "user_nutritions SET food='".$this->getFood()."', nutrition_time='".$this->getNutritionTime()."', kcal='".$this->getKCal()."', proteins=".$this->getProteins().", carbohydrates='".$this->getCarbohydrates()."', fat=".$this->getFat()." WHERE id=".$this->getId()); return $this->getId(); } function build($data) { if ($this->isBuildValuesEmpty($data)) { return false; } $this->setFood($data["food"]); $this->setNutritionTimeHH($data["nutrition_time_hh"]); $this->setNutritionTimeMM($data["nutrition_time_mm"]); $this->setKCal($data["kcal"]); $this->setProteins($data["proteins"]); $this->setCarbohydrates($data["carbohydrates"]); $this->setFat($data["fat"]); if ($this->getId() > 0) { return $this->save(); } else { return $this->create(); } } function isBuildValuesEmpty($data) { if (empty($data["food"]) && (empty($data["nutrition_time_hh"]) || $data["nutrition_time_hh"] == "hh") && (empty($data["nutrition_time_mm"]) || $data["nutrition_time_mm"] == "mm") && empty($data["kcal"]) && empty($data["proteins"]) && empty($data["carbohydrates"]) && empty($data["fat"])) { return true; } return false; } function delete() { global $TeKe; if (is_numeric($this->getId()) && $this->getId() > 0) { if ($TeKe->is_logged_in() && ($TeKe->is_admin() || get_logged_in_user()->getId() == $this->getCreator())) { return query("DELETE FROM " . DB_PREFIX . "user_nutritions WHERE id=".$this->getId()); } } return false; } function getCreator() { return $this->creator; } function setCreator($creator) { $this->creator = $creator; } function getTraining() { return $this->training; } function setTraining($training) { $this->training = $training; } function getFood() { return $this->food; } function setFood($food) { $this->food = $food; } function getNutritionTimeHH() { return $this->nutrition_time_hh; } function setNutritionTimeHH($nutrition_time_hh) { $this->nutrition_time_hh = $nutrition_time_hh; } function getNutritionTimeMM() { return $this->nutrition_time_mm; } function setNutritionTimeMM($nutrition_time_mm) { $this->nutrition_time_mm = $nutrition_time_mm; } function getNutritionTime() { $nutrition_time = "{$this->getNutritionTimeHH()}:{$this->getNutritionTimeMM()}"; return $nutrition_time; } function getKCal() { return $this->kcal; } function setKCal($kcal) { $this->kcal = $kcal; } function getProteins() { return $this->proteins; } function setProteins($proteins) { $this->proteins = $proteins; } function getCarbohydrates() { return $this->carbohydrates; } function setCarbohydrates($carbohydrates) { $this->carbohydrates = $carbohydrates; } function getFat() { return $this->fat; } function setFat($fat) { $this->fat = $fat; } } ?>