Robert Marshall 9 rokov pred
rodič
commit
1f1a71cb75

+ 1 - 1
Controller/Blog.php

@@ -1,7 +1,7 @@
 <?php
 ApplicationSettings::RegisterDefaultSetting("blog", "posts_per_page", 10);
 
-class Blog {
+class Blog extends Controller {
 	public function Index($page=1, IBlogPostRepository $repo=null){
 		$count=(int)ApplicationSettings::GetSetting("blog", "posts_per_page");
 		$offset=($page-1)*$count;

+ 1 - 1
Controller/Desktop.php

@@ -5,7 +5,7 @@ ApplicationSettings::RegisterDefaultSetting("desktop", "wake_command", "wakeonla
 ApplicationSettings::RegisterDefaultSetting("desktop", "ip_command", "/usr/sbin/arp -n | grep <mac> | awk '{print $1}'");
 ApplicationSettings::RegisterDefaultSetting("desktop", "hardware_monitor_port", "8085");
 
-class Desktop {
+class Desktop extends Controller {
 
 	public function Index() {
 		return new View("Desktop/index.view");

+ 1 - 1
Controller/E404.php

@@ -1,5 +1,5 @@
 <?php
-class E404{
+class E404 extends Controller{
 	public function Index() {
 		return new View("E404/index.view");
 	}

+ 1 - 1
Controller/E500.php

@@ -1,5 +1,5 @@
 <?php
-class E500{
+class E500 extends Controller{
 	public function Index() {
 		return new View("E500/index.view");
 	}

+ 1 - 1
Controller/Gallery.php

@@ -1,5 +1,5 @@
 <?php
-class Gallery {
+class Gallery extends Controller {
 	public function Index(IAlbumRepository $repo) {
 		$albums=$repo->GetAlbums();
 		return new View("Gallery/index.view",array("albums"=>$albums));

+ 1 - 1
Controller/Home.php

@@ -1,5 +1,5 @@
 <?php
-class Home{
+class Home extends Controller{
 	public function Index(IBlogPostRepository $blogRepo){
 		return new View("Home/index.view",array("post"=>$blogRepo->GetLatest()[0]));
 	}

+ 1 - 1
Controller/Member.php

@@ -1,5 +1,5 @@
 <?php
-class Member {
+class Member extends Controller {
 	public function Index($permission_error,$submit_form,$email,$password,$confirm_password) {
 		if (Session::IsUserLoggedIn())
 			return $this->Manage($email,$password,$confirm_password);

+ 1 - 1
Controller/Projects.php

@@ -1,5 +1,5 @@
 <?php
-class Projects {
+class Projects extends Controller {
 	public function Index($params) {
 		return new View("Projects/index.view");
 	}

+ 1 - 1
Controller/SettingsEditor.php

@@ -1,5 +1,5 @@
 <?php
-class SettingsEditor {
+class SettingsEditor extends Controller {
 	const KEY_SPLITTER='|';
 
 	public function Index() {

+ 1 - 1
Controller/SiteMap.php

@@ -1,5 +1,5 @@
 <?php
-class SiteMap {
+class SiteMap extends Controller {
 	public function Index() {
 		$controllers=Navigation::Get();
 		return new View("SiteMap/index.view",array("controllers"=>$controllers));

+ 8 - 10
Controller/Weight.php

@@ -1,28 +1,26 @@
 <?php
 ApplicationSettings::RegisterDefaultSetting("weight", "graph_history_duration", "1M");
 
-class Weight {
-	public function Index(IWeightReadingRepository $repo) {
+class Weight extends Controller {	
+	public function Index(IWeightReadingRepository $weightReadingRepo) {
 		$userId=Session::GetLoggedInUser()->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)));
+		return new View("Weight/index.view",array("readings"=>$weightReadingRepo->GetReadingsInDateRange($userId,$dateFrom,$dateTo)));
 	}
 	
-	public function Add($weight,$fat,$bmi){
-		$reading=new WeightReading();
+	public function Add($weight, $fat, IUserSettingsRepository $userSettingsRepo){
+		$reading=new WeightReading($userSettingsRepo);
 		
-		foreach (func_get_args() as $arg)
+		foreach (array($weight,$fat) as $arg)
 			if ($arg=="" || $arg<=0)
 				return;
-		
+
 		$reading->Weight=$weight;
 		$reading->Fat=$fat;
-		$reading->Bmi=$bmi;
-		$reading->UserId=Session::GetLoggedInUser()->UserId;
 		
-		$reading->Save();
+		$reading->Save(Session::GetLoggedInUser());
 		
 		return '{"Weight":'.$reading->Weight.',"BMI":'.$reading->Bmi.',"Fat":'.$reading->Fat.'}';
 	}

+ 9 - 0
Database/BaseRepository.php

@@ -5,6 +5,11 @@ ApplicationSettings::RegisterDefaultSetting("database", "username", "root");
 ApplicationSettings::RegisterDefaultSetting("database", "password", "");
 
 class BaseRepository {
+
+	/**
+	 * @var DependencyInjector
+	 */
+	private $DependencyInjector;
 	protected static $PDO=null;
 
 	protected static function SetupPDO(){
@@ -44,4 +49,8 @@ class BaseRepository {
 		$prep->execute($execData);
 		return $prep->fetchAll(PDO::FETCH_COLUMN);
 	}
+	
+	protected function ResolveDependency($dependency) {
+		return $this->DependencyInjector->Resolve($dependency);
+	}
 }

+ 4 - 0
Database/IUserSettingsRepository.php

@@ -0,0 +1,4 @@
+<?php
+interface IUserSettingsRepository {
+	public function GetSetting(User $user, $settingName);
+}

+ 10 - 0
Database/UserSettingsRepository.php

@@ -0,0 +1,10 @@
+<?php
+class UserSettingsRepository implements IUserSettingsRepository {
+	public function GetSetting(User $user, $settingName) {
+		$PDO=BaseRepository::GetPDO();
+		$prep=$PDO->prepare("SELECT setting_id FROM user_settings WHERE user_id=? AND key=?");
+		$prep->execute(array($user->UserId,$settingName));
+		$settingId=$prep->FetchColumn();
+		return new UserSetting($settingId);
+	}
+}

+ 2 - 2
Database/WeightReadingRepository.php

@@ -4,7 +4,7 @@ class WeightReadingRepository extends BaseRepository implements IWeightReadingRe
 		$readings=array();
 		$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);
+			$readings[]=new WeightReading(parent::ResolveDependency("IUserSettingsRepository") ,$id);
 		return $readings;
 	}
 	
@@ -18,7 +18,7 @@ class WeightReadingRepository extends BaseRepository implements IWeightReadingRe
 		));
 		$results=$prep->fetchAll(PDO::FETCH_COLUMN);
 		foreach ($results as $id)
-			$readings[]=new WeightReading($id);
+			$readings[]=new WeightReading(parent::ResolveDependency("IUserSettingsRepository") ,$id);
 		return $readings;
 	}
 }

+ 10 - 0
Model/DataTypes.php

@@ -0,0 +1,10 @@
+<?php
+abstract class DataTypes {
+	const Null=0;
+	const Integer=1;
+	const Float=2;
+	const Boolean=3;
+	const String=4;
+	const Collection=5;
+	const Object=6;
+}

+ 52 - 0
Model/UserSetting.php

@@ -0,0 +1,52 @@
+<?php
+class UserSetting extends DBObject {	
+	public function __construct($settingId) {
+		parent::__construct("user_settings", "setting_id", $settingId);
+	}
+
+	public function GetValue(){
+		switch ($this->Type){
+			case DataTypes::Integer:
+				return (int)$this->Value;
+			case DataTypes::Float:
+				return (float)$this->Value;
+			case DataTypes::Boolean:
+				return (bool)$this->Value;
+			case DataTypes::String:
+				return (string)$this->Value;
+			case DataTypes::Collection:
+			case DataTypes::Object:
+				return json_decode($this->Value);
+		}
+		return null;
+	}
+	
+	public function Save() {		
+		switch (gettype($this->Value)){
+			case "integer":
+				$this->Type=DataTypes::Integer;
+				break;
+			case "double":
+				$this->Type=DataTypes::Float;
+				break;
+			case "boolean":
+				$this->Type=DataTypes::Boolean;
+				break;
+			case "string":
+				$this->Type=DataTypes::String;
+				break;
+			case "Array":
+				$this->Type=DataTypes::Collection;
+				$this->Value=json_encode($this->Value);
+				break;
+			case "object":
+				$this->Type=DataTypes::Object;
+				$this->Value=json_encode($this->Value);
+				break;
+			default:
+				$this->Type=DataTypes::Null;
+		}
+		
+		parent::Save();
+	}
+}

+ 24 - 2
Model/WeightReading.php

@@ -1,10 +1,32 @@
 <?php
-class WeightReading extends DBObject {	
-	public function __construct($id=0) {
+class WeightReading extends DBObject {
+
+	/**
+	 * @var IUserSettingsRepository
+	 */
+	private $_userSettingsRepo;
+
+	public static function CalculateBMI($weight, $height){
+		if ($weight==0 || $height==0)
+			return 0;
+		return ($weight/$height)/$height;
+	}
+	
+	public function __construct(IUserSettingsRepository $userSettingsRepo, $id=0) {
 		parent::__construct("weight_readings", "reading_id", $id);
+		$this->_userSettingsRepo=$userSettingsRepo;
 	}
 	
 	public function GetParsedDate(){
 		return date_parse($this->Timestamp);
 	}
+	
+	public function Save(User $user=null) {
+		if ($user==null)
+			throw new Exception("Please specify user");
+		$height=$this->_userSettingsRepo->GetSetting($user, "height")->GetValue;
+		$this->Bmi=self::CalculateBMI($this->Weight, $height);
+		$this->UserId=$user->UserId;
+		parent::Save();
+	}
 }

+ 2 - 6
View/Weight/index.view

@@ -98,15 +98,11 @@
 	<div class="row col-md-4">
 		<div class="col input">
 			<label for="weight">Weight (KG)</label>
-			<input type="text" id="weight" name="weight" />
-		</div>
-		<div class="col input">
-			<label for="bmi">BMI</label>
-			<input type="text" id="bmi" name="bmi" />
+			<input type="number" id="weight" name="weight" />
 		</div>
 		<div class="col input">
 			<label for="fat">Fat %</label>
-			<input type="text" id="fat" name="fat" />
+			<input type="number" id="fat" name="fat" />
 		</div>
 		<div class="col input">
 			<label for="save">Action</label>

+ 17 - 5
base/Application.php

@@ -45,17 +45,29 @@ class Application{
 		return $callArgs;
 	}
 	
-	protected function LoadPage($page,$action,$params){
+	private function SetupController(Controller $controller){
+		$controller->DependencyInjector=$this->_dependencyInjector;
+	}
+	
+	private function LoadController($page) {
 		$controller=self::FindControllerPath($page);
-		if ($controller===false){
+		if ($controller===false) {
 			$page="E404";
 			$controller="Controller/E404.php";
-			$params=array($this->_url);
+			//$params=array($this->_url);
 			http_response_code(404);
 		}
-		
+
 		include_once $controller;
-		$this->_controller=new $page();
+		
+		$controllerObject=new $page();
+		$this->SetupController($controllerObject);
+		
+		return $controllerObject;
+	}
+	
+	protected function LoadPage($page,$action,$params){
+		$this->_controller=$this->LoadController($page);
 		if (!method_exists($this->_controller, $action))
 			$action="Index";
 		

+ 4 - 0
base/Controller.php

@@ -0,0 +1,4 @@
+<?php
+class Controller {
+	public $DependencyInjector;
+}

+ 3 - 1
base/DependencyInjector.php

@@ -10,6 +10,8 @@ class DependencyInjector {
 	}
 	
 	public function Resolve($interface) {
-		return new $this->_dependencyArray[$interface];
+		$item = new $this->_dependencyArray[$interface];
+		$item->DependencyInjector=$this;
+		return $item;
 	}
 }

+ 2 - 1
settings.ini

@@ -23,4 +23,5 @@ mac_address=c8:60:00:16:a8:39
 IBlogPostRepository=BlogPostRepository
 IWeightReadingRepository=WeightReadingRepository
 IAlbumRepository=AlbumRepository
-IImageRepository=ImageRepository
+IImageRepository=ImageRepository
+IUserSettingsRepository=UserSettingsRepository