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 . "training_goals WHERE id=".$this->id; $ret = query_row($q); } if ( $ret) { $this->id = $ret->id; $this->title = $ret->title; //$this->setKeywords($ret->keywords); } } function getURL() { return WWW_ROOT."traininggoals/traininggoal/".$this->id."/view"; } function getId() { return $this->id; } function create() { $this->setCreator(get_logged_in_user()->getId()); $beginning_datetime = strtotime($this->getBeginningDate()); $beginning_date_for_mysql = date( 'Y-m-d', $beginning_datetime); $ending_datetime = strtotime($this->getEndingDate()); $ending_date_for_mysql = date( 'Y-m-d', $ending_datetime); $q = "INSERT INTO " . DB_PREFIX . "training_goals (creator, created, title, beginning_date, ending_date) values (".$this->getCreator().", NOW(), '".mysql_real_escape_string($this->getTitle())."', '".$beginning_date_for_mysql."', '".$ending_date_for_mysql."')"; $uid = query_insert($q); if ($uid) return $uid; return false; } function save() { $update_query = "UPDATE " . DB_PREFIX . "training_plans SET"; $update_query .= " title='".mysql_real_escape_string($this->getTitle())."',"; $update_query .= " keywords='".mysql_real_escape_string($this->getKeywords())."',"; $update_query .= " duration='".mysql_real_escape_string($this->getDuration())."',"; $update_query .= " frequency='".$this->getFrequency()."',"; $update_query .= " notes='".mysql_real_escape_string($this->getNotes())."',"; $update_query .= " status='".$this->getStatus()."',"; $update_query .= " security='".$this->getSecurity()."',"; $update_query .= " type='".$this->getType()."',"; $update_query .= " modified=NOW()"; $update_query .= " WHERE id=".$this->getId(); $update = query_update($update_query); return $this->getId(); } function isExerciseDataEmpty($data) { if (empty($data["exercise"])) { return true; } return false; } function saveGoals($id, $goals) { foreach ($goals as $goal_data) { if (empty($goal_data["id"])) { $goal = new Traininggoal_goal(); $goal->setTrainingGoal($id); $goal->build($goal_data); } else { $fitness = new Fitness(); $nutrition_id = $nutrition_data["id"]; $nutrition = $fitness->loadType("nutrition", $nutrition_id); $nutrition->build($nutrition_data); } } } function build($data) { $this->setTitle($data["title"]); $this->setBeginningDate($data["beginning_date"]); $this->setEndingDate($data["ending_date"]); if ($this->getId() > 0) { $this->saveGoals($this->getId(), $data["goals"]); return $this->save(); } else { $id = $this->create(); $this->saveGoals($id, $data["goals"]); return $id; } } 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 . "training_plans WHERE id=".$this->getId()); } } return false; } function getCreator() { return $this->creator; } function setCreator($creator) { $this->creator = $creator; } function getTitle() { return $this->title; } function setTitle($title) { $this->title = $title; } function getBeginningDate() { if (!$this->beginning_date) { return date("d.m.Y"); } return $this->beginning_date; } function setBeginningDate($beginning_date) { $this->beginning_date = $beginning_date; } function getEndingDate() { return $this->ending_date; } function setEndingDate($ending_date) { $this->ending_date = $ending_date; } function getGoals() { if (empty($this->goals)) { return array("1"=>array("id"=>"", "body_part"=>"", "initial_value"=>"", "final_goal"=>"")); } return $this->goals; } function getInitialisedDayExercises() { $exercises = array("1"=>array("id" => "", "muscle_group"=>"", "exercise"=>"", "series" =>"", "repetitions"=>"", "break_length"=>"", "break_unit"=>"")); return $exercises; } function getSavedGoals() { if ($this->getId()) { $res = query_rows("SELECT * FROM ". DB_PREFIX . "user_goals WHERE training_goal=".$this->getId()); $goals = array(); foreach ($res as $goal) { $Fitness = new Fitness(); //$loaded_exercise = $Fitness->loadType("trainingplan_exercise", $exercise->id); $goal_data = array(); $goal_data["id"] = $goal->id; $goal_data["body_part"] = $goal->body_part; $goal_data["initial_value"] = $goal->initial_value; $goal_data["current_value"] = $goal->current_value; $goal_data["final_goal"] = $goal->final_goal; $goals[$goal->id] = $goal_data; } $this->goals = $goals; } /*if (empty($exercises)) { return $this->getInitialisedDayExercises(); }*/ return $this->goals; } } ?>