<?php

class ModelLocalisationLanguage extends Model
{
    public function getLanguage($language_id)
    {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "language WHERE language_id = '" . (int)$language_id . "'");

        return $query->row;
    }

    public function getLanguages()
    {
        $language_data = $this->cache->get('catalog.language');

        if (!$language_data) {
            $language_data = array();
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "language WHERE status = '1' ORDER BY sort_order, name");
            foreach ($query->rows as $result) {
                $code = $result['code'];
                $language_data[] = array(
                    'language_id' => $result['language_id'],
                    'name' => $result['name'],
                    'code' => $result['code'],
                    'locale' => $result['locale'],
                    'image' => "catalog/language/{$code}/{$code}.png",
                    'sort_order' => $result['sort_order'],
                    'status' => $result['status']
                );
            }
            $this->cache->setExpire(2 * 3600)->set('catalog.language', $language_data);
        }
        return $language_data;
    }

    public function getAutoLanguages()
    {
        $language_data = [];
        $auto_langs = $this->config->get('config_auto_langs') ?? []; //自动翻译支持语种
        $support_langs = $this->config->get('support_langs');
        $i=1;
        foreach ($support_langs as $code => $result) {
            if (in_array($code, $auto_langs)) {
                $language_data[] = array(
                    'language_id' => $i,
                    'name' => $result['english'],
                    'code' => $code,
                    'locale' => $code,
                    'image' => 'image/langs/' . $code . '.png',
                    'sort_order' => 99,
                    'status' => 1
                );
                $i++;
            }
        }
        return $language_data;
    }
}