Robert Marshall преди 9 години
родител
ревизия
8411675851
променени са 5 файла, в които са добавени 42 реда и са изтрити 5 реда
  1. 6 1
      Controller/Weight.php
  2. 18 0
      Database/BaseRepository.php
  3. 2 1
      Database/IWeightReadingRepository.php
  4. 15 2
      Database/WeightReadingRepository.php
  5. 1 1
      View/Weight/index.view

+ 6 - 1
Controller/Weight.php

@@ -1,8 +1,13 @@
 <?php
+ApplicationSettings::RegisterDefaultSetting("weight", "graph_history_duration", "1M");
+
 class Weight {
 	public function Index(IWeightReadingRepository $repo) {
 		$userId=Session::GetLoggedInUser()->UserId;
-		return new View("Weight/index.view",array("readings"=>$repo::GetAll($userId)));
+		$dateTo=new DateTime();
+		$dateFrom=clone $dateTo;
+		$dateFrom->sub(new DateInterval("P".ApplicationSettings::GetSetting("weight", "graph_history_duration")));
+		return new View("Weight/index.view",array("readings"=>$repo->GetReadingsInDateRange($userId,$dateFrom,$dateTo)));
 	}
 	
 	public function Add($weight,$fat,$bmi){

+ 18 - 0
Database/BaseRepository.php

@@ -26,4 +26,22 @@ class BaseRepository {
 	public function __construct() {
 		self::SetupPDO();
 	}
+	
+	protected function DoBasicSelectQuery($table, $field, array $keyValuePair=null){
+		$sql="SELECT `$field` FROM `$table`";
+		$execData=array();
+		
+		if ($keyValuePair!=null && count($keyValuePair)>0){
+			$sql.=" WHERE ";
+			foreach ($keyValuePair as $key=>$value){
+				$sql.="$key=:$key";
+				$execData[":$key"]=$value;
+			}
+		}
+		
+		self::SetupPDO();
+		$prep=self::$PDO->prepare($sql);
+		$prep->execute($execData);
+		return $prep->fetchAll(PDO::FETCH_COLUMN);
+	}
 }

+ 2 - 1
Database/IWeightReadingRepository.php

@@ -1,4 +1,5 @@
 <?php
 interface IWeightReadingRepository {
-	public static function GetAll($userId);
+	public function GetAll($userId);
+	public function GetReadingsInDateRange($userId, DateTime $from, DateTime $to);
 }

+ 15 - 2
Database/WeightReadingRepository.php

@@ -1,11 +1,24 @@
 <?php
 class WeightReadingRepository extends BaseRepository implements IWeightReadingRepository {
-	public static function GetAll($userId){
+	public function GetAll($userId){
 		$readings=array();
-		self::SetupPDO();
 		$ids=self::$PDO->query("SELECT reading_id FROM weight_readings WHERE user_id=".(int)$userId)->fetchAll(PDO::FETCH_COLUMN);
 		foreach ($ids as $id)
 			$readings[]=new WeightReading($id);
 		return $readings;
 	}
+	
+	public function GetReadingsInDateRange($userId, DateTime $from, DateTime $to){
+		$readings=array();
+		$prep=self::$PDO->prepare("SELECT reading_id FROM weight_readings WHERE user_id=:user_id AND (`timestamp`>=:from AND `timestamp`<=:to)");
+		$prep->execute(array(
+			":user_id"=>$userId,
+			":from"=>$from->format('Y-m-d 00:00:00'),
+			":to"=>$to->format('Y-m-d 23:59:59')
+		));
+		$results=$prep->fetchAll(PDO::FETCH_COLUMN);
+		foreach ($results as $id)
+			$readings[]=new WeightReading($id);
+		return $readings;
+	}
 }

+ 1 - 1
View/Weight/index.view

@@ -94,7 +94,7 @@
 	});
 }@
 @Body{
-<form ajaxForm action="/weight/add" method="post" onsuccess="AddWeightReading" onpost="DisableControls">
+<form ajaxForm action="/weight/add" method="post" onsuccess="AddWeightReading" onpost="DisableControls" autocomplete="off">
 	<div class="row col-md-4">
 		<div class="col input">
 			<label for="weight">Weight (KG)</label>