<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Lang_switcher_site extends MX_Controller
{

    public function index()
    {
        $this->load->library('db_lang_lib');

        $content = [];
        $langs = $this->db_lang_lib->get_lang_by_id();

        foreach ($langs as $lang) {
            $link = str_replace($this->uri->current_language . '/', '', $this->uri->uri_string());
            if (empty($link)) {
                $link = 'home';
            }
            $link = '/' . $lang->short_name . '/' . $link;
            if ($lang->short_name == $this->config->item('language_short')) {
                $link = str_replace(['/' . $lang->short_name . '/', 'home'], ['/', ''], $link);
            }
            $item = [
                'name'       => $lang->name,
                'short_name' => $lang->short_name,
                'link'       => $link,
                'active'     => ($this->uri->current_language == $lang->short_name) ? 'class="activ_lang"' : ''
            ];
            // Set current language first
            $key = ($this->uri->current_language == $lang->short_name) ? 0 : $lang->id_lan;

            $content['items'][$key] = $item;
        }

        ksort($content['items']);

        return $this->parser->parse('lang_switcher/template', $content, TRUE);
    }

}
