Files
FHC-Core/application/controllers/api/frontend/v1/Ampeln.php
T
SimonGschnell d4e33eb1df refactor
2024-08-01 15:04:18 +02:00

184 lines
5.7 KiB
PHP

<?php
/**
* Copyright (C) 2024 fhcomplete.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
if (! defined('BASEPATH')) exit('No direct script access allowed');
class Ampeln extends FHCAPI_Controller
{
/**
* Object initialization
*/
public function __construct()
{
parent::__construct([
'open' => self::PERM_LOGGED,
'all' => self::PERM_LOGGED,
'confirm' => self::PERM_LOGGED,
'alleAmpeln' => self::PERM_LOGGED,
]);
$this->load->model('content/Ampel_model', 'AmpelModel');
$this->load->model('system/Sprache_model','SpracheModel');
$this->uid = getAuthUID();
$this->pid = getAuthPersonID();
}
//------------------------------------------------------------------------------------------------------------------
// Public methods
/**
* confirms ampel and inserts ampel_id in public.tbl_ampel_benutzer_bestaetigt
* @access public
*
*/
public function confirm($ampel_id)
{
$this->load->library('form_validation');
$this->form_validation->set_data(['ampel_id'=> $ampel_id]);
$this->form_validation->set_rules('ampel_id', 'Ampel ID', 'required|integer');
if($this->form_validation->run() == FALSE) $this->terminateWithValidationErrors($this->form_validation->error_array());
$insert_into_result = $this->AmpelModel->confirmAmpel($ampel_id,$this->uid);
$insert_into_result = $this->getDataOrTerminateWithError($insert_into_result);
$this->terminateWithSuccess($insert_into_result);
}
/**
* queries active and not confirmed ampeln by the user
* @access public
*
*/
public function open()
{
$userAmpeln = array();
// fetch active ampeln
$activeAmpeln = $this->AmpelModel->active();
$activeAmpeln = $this->getDataOrTerminateWithError($activeAmpeln);
foreach($activeAmpeln as $ampel){
// check if ampel is confirmed by user
$confirmedByUser = $this->AmpelModel->isConfirmed($ampel->ampel_id,$this->uid);
$ampel->bestaetigt = $confirmedByUser;
// only include non confirmed active ampeln in the result
if(!$confirmedByUser){
// check if the user was assigned to the ampel
$zugeteilt = $this->AmpelModel->isZugeteilt($this->uid, $ampel->benutzer_select);
$zugeteilt = $this->getDataOrTerminateWithError($zugeteilt);
if($zugeteilt) $userAmpeln[] = $this->translateAmpel($ampel);
}
}
$this->terminateWithSuccess($userAmpeln);
}
/**
* queries all ampeln of the user
* @access public
*
*/
public function all()
{
$userAmpeln = array();
$ampel_result = $this->AmpelModel->active();
$ampel_result = $this->getDataOrTerminateWithError($ampel_result);
foreach($ampel_result as $ampel){
$confirmedByUser = $this->AmpelModel->isConfirmed($ampel->ampel_id,$this->uid);
$ampel->bestaetigt = $confirmedByUser;
// check if the ampel was assigned to the user
$zugeteilt = $this->AmpelModel->isZugeteilt($this->uid, $ampel->benutzer_select);
$zugeteilt = $this->getDataOrTerminateWithError($zugeteilt);
if($zugeteilt) $userAmpeln[] = $this->translateAmpel($ampel);
}
$this->terminateWithSuccess($userAmpeln);
}
/**
* queries all ampeln that were assigned to the user until start of first work day
* @access public
*
*/
public function alleAmpeln(){
//fetch all ampeln
$alle_ampeln = $this->AmpelModel->alleAmpeln($this->uid);
$alle_ampeln = $this->getDataOrTerminateWithError($alle_ampeln);
$alle_ampeln = array_map(function($ampel){
// check if ampel is confirmed by user
$confirmedByUser = $this->AmpelModel->isConfirmed($ampel->ampel_id,$this->uid);
$ampel->bestaetigt = $confirmedByUser;
// translate ampeln
return $this->translateAmpel($ampel);
}, $alle_ampeln);
$this->terminateWithSuccess($alle_ampeln);
}
//------------------------------------------------------------------------------------------------------------------
// Private methods
/**
* translate ampel description and button text
* @access private
*
*/
public function translateAmpel($ampel){
// fetch user language
$userLanguage = getUserLanguage();
$userLanguage = $this->SpracheModel->loadWhere(["sprache" => $userLanguage]);
$userLanguage = $this->getDataOrTerminateWithError($userLanguage)[0]->index - 1; // why does the index start at 1?
// translate the ampel description and button text
if(isset($ampel->beschreibung) && count($ampel->beschreibung)>=($userLanguage+1))
$ampel->beschreibung = $ampel->beschreibung[$userLanguage];
if(isset($ampel->buttontext) && count($ampel->buttontext)>=($userLanguage+1))
$ampel->buttontext = $ampel->buttontext[$userLanguage];
return $ampel;
}
}