Weight.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. ApplicationSettings::RegisterDefaultSetting("weight", "graph_history_duration", "1M");
  3. class Weight extends Controller {
  4. private function GetUserReadings(IWeightReadingRepository $weightReadingRepo, IUserSettingsRepository $userSettingRepo){
  5. $user=Session::GetLoggedInUser();
  6. $graphDuration=ApplicationSettings::GetSetting("weight", "graph_history_duration");
  7. $userDurationSetting=$userSettingRepo->GetSetting($user, "weight_graph_duration")->Value;
  8. if ($userDurationSetting!=null)
  9. $graphDuration=$userDurationSetting.'M';
  10. $dateTo=new DateTime();
  11. $dateFrom=clone $dateTo;
  12. $dateFrom->sub(new DateInterval("P".$graphDuration));
  13. return $weightReadingRepo->GetReadingsInDateRange($user->UserId,$dateFrom,$dateTo);
  14. }
  15. public function Index(IWeightReadingRepository $weightReadingRepo, IUserSettingsRepository $userSettingRepo) {
  16. return new View("Weight/index.view",array("readings"=>$this->GetUserReadings($weightReadingRepo, $userSettingRepo)));
  17. }
  18. public function GetReadings(IWeightReadingRepository $weightReadingRepo, IUserSettingsRepository $userSettingRepo) {
  19. return json_encode($this->GetUserReadings($weightReadingRepo, $userSettingRepo));
  20. }
  21. public function Add($weight, $fat, IUserSettingsRepository $userSettingsRepo){
  22. $reading=new WeightReading($userSettingsRepo);
  23. foreach (array($weight,$fat) as $arg)
  24. if ($arg=="" || $arg<=0)
  25. return;
  26. $reading->Weight=$weight;
  27. $reading->Fat=$fat;
  28. $reading->Save(Session::GetLoggedInUser());
  29. return json_encode($reading);
  30. }
  31. public function Delete($id,IWeightReadingRepository $weightReadingRepo){
  32. $weightReadingRepo->Delete($id);
  33. }
  34. }