aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/forms/I18n_textareaInput.php
blob: a8f9dba3ff4a58f5dfa5c781cd389d96dedadeb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php

namespace Studip\Forms;

class I18n_textareaInput extends Input
{
    public function render()
    {
        if (!isset($this->attributes['id'])) {
            $id = md5(uniqid());
            $this->attributes['id'] = $id;
        } else {
            $id = $this->attributes['id'];
        }
        $value = $this->getValue();
        if (is_object($value)) {
            $value_arr = [\I18NString::getDefaultLanguage() => $value->original() ?? ''];
            $value = array_merge($value_arr, $value->toArray());
            $value = array_map(function ($item) {
                return $item ?? '';
            }, $value);
            $value = json_encode($value);
        }
        $template = $GLOBALS['template_factory']->open('forms/i18n_textarea_input');
        $template->title = $this->title;
        $template->name = $this->name;
        $template->value = $value;
        $template->id = $id;
        $template->required = $this->required;
        $template->attributes = $this->attributes;
        return $template->render();
    }

    public function getAllInputNames()
    {
        $all_names = [$this->getName()];
        if (is_object($this->value)) {
            foreach (\Config::get()->CONTENT_LANGUAGES as $lang_id => $language) {
                if (\I18NString::getDefaultLanguage() !== $lang_id) {
                    $all_names[] = $this->getName() . '_i18n[' . $lang_id . ']';
                }
            }
        }
        return $all_names;
    }

    public function getRequestValue()
    {
        return \Request::i18n($this->name);
    }
}