2020-02-10 00:52:25 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Api;
|
|
|
|
|
|
|
|
use Api\Parameter\Parameter;
|
|
|
|
use Api\Parameter\StringType;
|
2020-04-02 00:02:51 +02:00
|
|
|
use Driver\SQL\Condition\CondOr;
|
|
|
|
use Driver\SQL\Condition\Compare;
|
2020-04-03 15:56:04 +02:00
|
|
|
use Objects\Language;
|
2020-02-10 00:52:25 +01:00
|
|
|
|
|
|
|
class SetLanguage extends Request {
|
|
|
|
|
2020-04-03 15:56:04 +02:00
|
|
|
private Language $language;
|
2020-02-10 00:52:25 +01:00
|
|
|
|
2020-04-03 15:56:04 +02:00
|
|
|
public function __construct($user, $externalCall = false) {
|
|
|
|
parent::__construct($user, $externalCall, array(
|
2020-02-10 00:52:25 +01:00
|
|
|
'langId' => new Parameter('langId', Parameter::TYPE_INT, true, NULL),
|
|
|
|
'langCode' => new StringType('langCode', 5, true, NULL),
|
|
|
|
));
|
2020-06-14 19:39:52 +02:00
|
|
|
$this->csrfTokenRequired = true;
|
2020-02-10 00:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function checkLanguage() {
|
|
|
|
$langId = $this->getParam("langId");
|
|
|
|
$langCode = $this->getParam("langCode");
|
|
|
|
|
|
|
|
if(is_null($langId) && is_null($langCode)) {
|
|
|
|
return $this->createError(L("Either langId or langCode must be given"));
|
|
|
|
}
|
|
|
|
|
2020-04-02 00:02:51 +02:00
|
|
|
$res = $this->user->getSQL()
|
|
|
|
->select("uid", "code", "name")
|
|
|
|
->from("Language")
|
|
|
|
->where(new CondOr(new Compare("uid", $langId), new Compare("code", $langCode)))
|
|
|
|
->execute();
|
2020-02-10 00:52:25 +01:00
|
|
|
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->success = ($res !== FALSE);
|
|
|
|
$this->lastError = $this->user->getSQL()->getLastError();
|
|
|
|
|
|
|
|
if ($this->success) {
|
|
|
|
if(count($res) == 0) {
|
2020-02-10 00:52:25 +01:00
|
|
|
return $this->createError(L("This Language does not exist"));
|
|
|
|
} else {
|
2020-04-02 00:02:51 +02:00
|
|
|
$row = $res[0];
|
2020-04-03 15:56:04 +02:00
|
|
|
$this->language = Language::newInstance($row['uid'], $row['code'], $row['name']);
|
2020-02-10 00:52:25 +01:00
|
|
|
if(!$this->language) {
|
|
|
|
return $this->createError(L("Error while loading language"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function updateLanguage() {
|
|
|
|
$languageId = $this->language->getId();
|
|
|
|
$userId = $this->user->getId();
|
2020-04-02 00:02:51 +02:00
|
|
|
$sql = $this->user->getSQL();
|
2020-02-10 00:52:25 +01:00
|
|
|
|
2020-04-02 00:02:51 +02:00
|
|
|
$this->success = $sql->update("User")
|
|
|
|
->set("language_id", $languageId)
|
|
|
|
->where(new Compare("uid", $userId))
|
|
|
|
->execute();
|
|
|
|
$this->lastError = $sql->getLastError();
|
2020-02-10 00:52:25 +01:00
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute($values = array()) {
|
|
|
|
if(!parent::execute($values)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$this->checkLanguage())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if($this->user->isLoggedIn()) {
|
|
|
|
$this->updateLanguage();
|
|
|
|
}
|
|
|
|
|
2020-04-03 15:56:04 +02:00
|
|
|
$this->user->setLanguage($this->language);
|
2020-02-10 00:52:25 +01:00
|
|
|
return $this->success;
|
|
|
|
}
|
2020-04-03 15:56:04 +02:00
|
|
|
}
|