web-base/Core/API/LanguageAPI.class.php

116 lines
3.1 KiB
PHP
Raw Normal View History

2020-06-20 20:13:51 +02:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\API {
2020-06-20 20:13:51 +02:00
2022-11-18 18:06:46 +01:00
use Core\Objects\Context;
2020-06-20 20:13:51 +02:00
2022-06-20 19:52:31 +02:00
abstract class LanguageAPI extends Request {
public function __construct(Context $context, bool $externalCall = false, array $params = array()) {
parent::__construct($context, $externalCall, $params);
}
2020-06-20 20:13:51 +02:00
}
}
2022-11-18 18:06:46 +01:00
namespace Core\API\Language {
2020-06-20 20:13:51 +02:00
2022-11-18 18:06:46 +01:00
use Core\API\LanguageAPI;
use Core\API\Parameter\Parameter;
use Core\API\Parameter\StringType;
use Core\Driver\SQL\Condition\Compare;
use Core\Driver\SQL\Condition\CondOr;
use Core\Objects\Context;
use Core\Objects\DatabaseEntity\Language;
2020-06-20 20:13:51 +02:00
class Get extends LanguageAPI {
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, $externalCall = false) {
parent::__construct($context, $externalCall, array());
2020-06-20 20:13:51 +02:00
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
$languages = Language::findAll($sql);
$this->success = ($languages !== null);
2020-06-20 20:13:51 +02:00
$this->lastError = $sql->getLastError();
2022-06-20 19:52:31 +02:00
if ($this->success) {
$this->result['languages'] = [];
if (count($languages) === 0) {
2020-06-20 20:13:51 +02:00
$this->lastError = L("No languages found");
} else {
2022-06-20 19:52:31 +02:00
foreach ($languages as $language) {
$this->result['languages'][$language->getId()] = $language->jsonSerialize();
2020-06-20 20:13:51 +02:00
}
}
}
return $this->success;
}
}
class Set extends LanguageAPI {
private Language $language;
2022-06-20 19:52:31 +02:00
public function __construct(Context $context, $externalCall = false) {
parent::__construct($context, $externalCall, array(
2020-06-20 20:13:51 +02:00
'langId' => new Parameter('langId', Parameter::TYPE_INT, true, NULL),
'langCode' => new StringType('langCode', 5, true, NULL),
));
2020-06-20 20:13:51 +02:00
}
2022-06-20 19:52:31 +02:00
private function checkLanguage(): bool {
2020-06-20 20:13:51 +02:00
$langId = $this->getParam("langId");
$langCode = $this->getParam("langCode");
2022-06-20 19:52:31 +02:00
if (is_null($langId) && is_null($langCode)) {
2020-06-20 20:13:51 +02:00
return $this->createError(L("Either langId or langCode must be given"));
}
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
$languages = Language::findAll($sql,
new CondOr(new Compare("id", $langId), new Compare("code", $langCode))
);
2020-06-20 20:13:51 +02:00
2022-06-20 19:52:31 +02:00
$this->success = ($languages !== null);
$this->lastError = $sql->getLastError();
2020-06-20 20:13:51 +02:00
if ($this->success) {
2022-06-20 19:52:31 +02:00
if (count($languages) === 0) {
2020-06-20 20:13:51 +02:00
return $this->createError(L("This Language does not exist"));
} else {
2022-06-20 19:52:31 +02:00
$this->language = array_shift($languages);
2020-06-20 20:13:51 +02:00
}
}
return $this->success;
}
2022-06-20 19:52:31 +02:00
private function updateLanguage(): bool {
2020-06-20 20:13:51 +02:00
$languageId = $this->language->getId();
2022-06-20 19:52:31 +02:00
$userId = $this->context->getUser()->getId();
$sql = $this->context->getSQL();
2020-06-20 20:13:51 +02:00
$this->success = $sql->update("User")
->set("language_id", $languageId)
2022-06-20 19:52:31 +02:00
->where(new Compare("id", $userId))
2020-06-20 20:13:51 +02:00
->execute();
2022-06-20 19:52:31 +02:00
2020-06-20 20:13:51 +02:00
$this->lastError = $sql->getLastError();
return $this->success;
}
2022-02-21 13:01:03 +01:00
public function _execute(): bool {
2022-06-20 19:52:31 +02:00
if (!$this->checkLanguage())
2020-06-20 20:13:51 +02:00
return false;
2022-06-20 19:52:31 +02:00
if ($this->context->getSession()) {
2020-06-20 20:13:51 +02:00
$this->updateLanguage();
}
2022-06-20 19:52:31 +02:00
$this->context->setLanguage($this->language);
2020-06-20 20:13:51 +02:00
return $this->success;
}
}
}