<?php
/**
 * CodeIgniter Controller
 *
 * @category CodeIgniter
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Koller
 * @copyright 2009 untermStrich
 * @package controllers
 * @version $Id: start.php 16153 2018-07-11 09:58:46Z christian $
*/

require_once APPPATH . 'us_libraries/vendor/autoload.php';
use OpenApi\Attributes as OA;

/**
 * The language fe controller for untermStrich
 */
class Lang extends My_Controller
{
    private $_cache_path = null;
    
    public function __construct()
    {
        $this->in_simple_mode = true;
        parent::__construct();
        $this->_cache_path = $this->config->item('cache_path');
    }
    
    /**
     * All translations for english
     */
    #[OA\Get(path: '/fe/lang/english', tags: ['lang'])]
    #[OA\Response(response: '200', description: 'key : translation for the choosen language', content: new OA\JsonContent(
        properties: [
            new OA\Property(
                property: "whatiswhere_article_fulltitle",
                type: "string",
                example: "Folder / Version"
            ),
            new OA\Property(
                property: "whatiswhere_article",
                type: "string",
                example: "Folder"
            ),
            new OA\Property(
                property: "*",
                type: "string",
                example: "..."
            )
        ],
        type: 'object'
    ))]
    public function english()
    {
        $this->_json_all_lang_files(__FUNCTION__);
    }
    
    /**
     * All translations for french
     */
    #[OA\Get(path: '/fe/lang/french', tags: ['lang'])]
    #[OA\Response(response: '200', description: 'key : translation for the choosen language', content: new OA\JsonContent(
        properties: [
            new OA\Property(
                property: "whatiswhere_article_fulltitle",
                type: "string",
                example: "Dossier / version"
            ),
            new OA\Property(
                property: "whatiswhere_article",
                type: "string",
                example: "Dossier"
            ),
            new OA\Property(
                property: "*",
                type: "string",
                example: "..."
            )
        ],
        type: 'object'
    ))]
    public function french()
    {
        $this->_json_all_lang_files(__FUNCTION__);
    }
    
    /**
     * All translations for german
     */
    #[OA\Get(path: '/fe/lang/german', tags: ['lang'])]
    #[OA\Response(response: '200', description: 'key : translation for the choosen language', content: new OA\JsonContent(
        properties: [
            new OA\Property(
                property: "whatiswhere_article_fulltitle",
                type: "string",
                example: "Ordner/Ausgabe"
            ),
            new OA\Property(
                property: "whatiswhere_article",
                type: "string",
                example: "Ordner"
            ),
            new OA\Property(
                property: "*",
                type: "string",
                example: "..."
            )
        ],
        type: 'object'
    ))]
    public function german()
    {
        $this->_json_all_lang_files(__FUNCTION__);
    }
    
    /**
     * All translations for germanarch
     */
    #[OA\Get(path: '/fe/lang/germanarch', tags: ['lang'])]
    #[OA\Response(response: '200', description: 'key : translation for the choosen language', content: new OA\JsonContent(
        properties: [
            new OA\Property(
                property: "whatiswhere_article_fulltitle",
                type: "string",
                example: "ordner/ausgabe"
            ),
            new OA\Property(
                property: "whatiswhere_article",
                type: "string",
                example: "ordner"
            ),
            new OA\Property(
                property: "*",
                type: "string",
                example: "..."
            )
        ],
        type: 'object'
    ))]
    public function germanarch()
    {
        $this->_json_all_lang_files(__FUNCTION__);
    }
    
    /**
     * All translations for germanch
     */
    #[OA\Get(path: '/fe/lang/germanch', tags: ['lang'])]
    #[OA\Response(response: '200', description: 'key : translation for the choosen language', content: new OA\JsonContent(
        properties: [
            new OA\Property(
                property: "whatiswhere_article_fulltitle",
                type: "string",
                example: "Ordner/Ausgabe"
            ),
            new OA\Property(
                property: "whatiswhere_article",
                type: "string",
                example: "Ordner"
            ),
            new OA\Property(
                property: "*",
                type: "string",
                example: "..."
            )
        ],
        type: 'object'
    ))]
    public function germanch()
    {
        $this->_json_all_lang_files(__FUNCTION__);
    }
    
    /**
     * Empty response for startup
     */
    #[OA\Get(path: '/fe/lang/dev', tags: ['lang'])]
    #[OA\Response(response: '200', description: 'key : empty startup language', content: new OA\JsonContent(
        properties: [],
        type: 'object'
    ))]
    public function dev()
    {
        header('Content-Type: application/json; charset=utf-8');
        header('US-Version: '.$this->config->item('us_version'));
        echo us_json_encode([]);
    }
    
    /**
     * Load all lang files
     * 
     * @param   string      $idiom      The language (english, etc.)
     */
    private function _json_all_lang_files(string $idiom)
    {
        $us_version = us_substr(str_replace('.', '', $this->config->item('us_version')), 0, 9);
        $cache_file = $this->_cache_path.$idiom.'_'.$us_version.'_lang.json';
        
        if (!file_exists($cache_file)) {
            $lang_files = $this->_get_all_lang_files($idiom);
            foreach ($lang_files as $lang_file) {
                $this->lang->load($lang_file, $idiom);
            }
            
            file_put_contents($cache_file, us_json_encode($this->lang->language));
        }
        
        
        header('Content-Type: application/json; charset=utf-8');
        header('US-Version: '.$this->config->item('us_version'));
        readfile($cache_file);
    }
    
    /**
     * Get all lang files
     * 
     * @param   string      $idiom      The language (english, etc.)
     * @return  array
     */
    private function _get_all_lang_files(string $idiom): array
    {
        return [
            'date',
            'calendar',
            'db',
            'email',
            'form_validation',
            'imglib',
            'number',
            'upload',
            'addressesandcontacts',
            'basic',
            'calculateaccordingexpenditure',
            'controlpanel',
            'correspondence',
            'datacollection',
            'drawings_inout',
            'drawings',
            'externalexpenses',
            'fe',
            'fee_areas',
            'fees',
            'files',
            'holidayadmin',
            'inoutbox',
            'masterdatamanagement',
            'meetingmanager',
            'opentasks',
            'pages',
            'personalapp',
            'projects',
            'reporting',
            'staffadmin',
            'target_hours',
            'timelogging',
            'useroptions',
            'whatiswhere',
            'work_input',
            'work_packages'
        ];
    }
    
}
