| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Grocy\Controllers; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-12 21:13:38 +02:00
										 |  |  | use \Grocy\Services\BatteriesService; | 
					
						
							| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | class BatteriesController extends BaseController | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	public function __construct(\Slim\Container $container) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		parent::__construct($container); | 
					
						
							|  |  |  | 		$this->BatteriesService = new BatteriesService(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	protected $BatteriesService; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-12 21:13:38 +02:00
										 |  |  | 	public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) | 
					
						
							| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-04-12 21:13:38 +02:00
										 |  |  | 		$nextChargeTimes = array(); | 
					
						
							|  |  |  | 		foreach($this->Database->batteries() as $battery) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			$nextChargeTimes[$battery->id] = $this->BatteriesService->GetNextChargeTime($battery->id); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-12 15:30:13 +02:00
										 |  |  | 		$nextXDays = 5; | 
					
						
							|  |  |  | 		$countDueNextXDays = count(FindAllItemsInArrayByValue($nextChargeTimes, date('Y-m-d', strtotime("+$nextXDays days")), '<')); | 
					
						
							|  |  |  | 		$countOverdue = count(FindAllItemsInArrayByValue($nextChargeTimes, date('Y-m-d', strtotime('-1 days')), '<')); | 
					
						
							| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | 		return $this->AppContainer->view->render($response, 'batteriesoverview', [ | 
					
						
							|  |  |  | 			'batteries' => $this->Database->batteries(), | 
					
						
							|  |  |  | 			'current' => $this->BatteriesService->GetCurrent(), | 
					
						
							| 
									
										
										
										
											2018-05-12 15:30:13 +02:00
										 |  |  | 			'nextChargeTimes' => $nextChargeTimes, | 
					
						
							|  |  |  | 			'nextXDays' => $nextXDays, | 
					
						
							|  |  |  | 			'countDueNextXDays' => $countDueNextXDays - $countOverdue, | 
					
						
							|  |  |  | 			'countOverdue' => $countOverdue | 
					
						
							| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | 		]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-12 21:13:38 +02:00
										 |  |  | 	public function TrackChargeCycle(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) | 
					
						
							| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return $this->AppContainer->view->render($response, 'batterytracking', [ | 
					
						
							|  |  |  | 			'batteries' =>  $this->Database->batteries() | 
					
						
							|  |  |  | 		]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-12 21:13:38 +02:00
										 |  |  | 	public function BatteriesList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) | 
					
						
							| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return $this->AppContainer->view->render($response, 'batteries', [ | 
					
						
							|  |  |  | 			'batteries' => $this->Database->batteries() | 
					
						
							|  |  |  | 		]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-12 21:13:38 +02:00
										 |  |  | 	public function BatteryEditForm(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) | 
					
						
							| 
									
										
										
										
											2018-04-11 19:49:35 +02:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if ($args['batteryId'] == 'new') | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			return $this->AppContainer->view->render($response, 'batteryform', [ | 
					
						
							|  |  |  | 				'mode' => 'create' | 
					
						
							|  |  |  | 			]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			return $this->AppContainer->view->render($response, 'batteryform', [ | 
					
						
							|  |  |  | 				'battery' =>  $this->Database->batteries($args['batteryId']), | 
					
						
							|  |  |  | 				'mode' => 'edit' | 
					
						
							|  |  |  | 			]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |