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_exercises WHERE id=".$this->id; $ret = query_row($q); } if ( $ret) { $this->id = $ret->id; $this->muscle_group = $ret->muscle_group; $this->exercise = $ret->exercise; $this->setSeries(); } } function getId() { return $this->id; } function create() { $this->setCreator(get_logged_in_user()->getId()); $q = "INSERT INTO " . DB_PREFIX . "user_exercises (creator, training, muscle_group, exercise) values (".$this->getCreator().", ".$this->getTraining().", '".$this->getMuscleGroup()."', '".$this->getExercise()."')"; $uid = query_insert($q); if ($uid) return $uid; return false; } function save() { $update = query_update("UPDATE " . DB_PREFIX . "user_exercises SET muscle_group=".$this->getMuscleGroup().", exercise='".$this->getExercise()."' WHERE id=".$this->getId()); return $this->getId(); } function saveSeries($id, $exercise_series) { foreach ($exercise_series as $number=>$series_data) { $series = new Series(); $series->setExercise($id); $series->setNumber($number); $series->build($series_data); } } function updateSeries($id, $exercise_series) { foreach ($exercise_series as $number=>$series_data) { $fitness = new Fitness(); $series = $fitness->loadType("series", $series_data["id"]); $series->setNumber($number); $series->setExercise($id); $series->build($series_data); } } function build($data) { if ($this->isBuildValuesEmpty($data)) { return false; } $this->setMuscleGroup($data["muscle_group"]); $this->setExercise($data["exercise"]); if ($this->getId() > 0) { $id = $this->save(); $this->updateSeries($id, $data["series"]); } else { $id = $this->create(); $this->saveSeries($id, $data["series"]); } return $id; } function isBuildValuesEmpty($data) { if (empty($data["exercise"])) { 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_exercises 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 getMuscleGroup() { return $this->muscle_group; } function setMuscleGroup($muscle_group) { $this->muscle_group = $muscle_group; } function getExercise() { return $this->exercise; } function setExercise($exercise) { $this->exercise = $exercise; } function getSeries() { if (empty($this->series)) { return $this->initialiseSeries(); } return $this->series; } private function initialiseSeries() { $default_series = array(); foreach(range(1,1) as $number) { $default_series[$number] = array("id" => "", "repetitions" => "", "kg"=>""); } return $default_series; } function setSeries() { $res = query_rows("SELECT * FROM ". DB_PREFIX . "user_exercise_series WHERE exercise=".$this->getId()); $exercise_series = array(); foreach ($res as $series) { $series_data = array(); $series_data["id"] = $series->id; $series_data["repetitions"] = $series->repetitions; $series_data["kg"] = $series->kg; $exercise_series[$series->number] = $series_data; } $this->series = $exercise_series; } } ?>