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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
<?php
/**
* Translation class
*
* Automatic generation of inputs and textareas for i18n
*
* @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
* @author Peter Thienel <pthienel@data-quest.de>
* @author Elmar Ludwig <elmar.ludwig@uos.de>
* @author Florian Bieringer
* @license GPL2 or any later version
*/
class I18N
{
/**
* Creates a set of HTML input elements for this form element in text form.
* One element will be generated for each configured content language.
*
* @param string $name HTML name of the Inputfild
* @param I18NString $value (Needs to be an i18n input string)
* @param array $attributes Additional attributes of the input
* @return string Crafted input
*/
public static function input($name, $value, array $attributes = [])
{
return new static('i18n/input.php', $name, $value, $attributes);
}
/**
* Create a set of HTML textarea elements for this form element in text form.
* One element will be generated for each configured content language.
*
* @param string $name HTML name of the Textarea
* @param I18NString $value (Needs to be an i18n input string)
* @param array $attributes Additional attributes of the input
* @return string Crafted textarea
*/
public static function textarea($name, $value, array $attributes = [])
{
return new static('i18n/textarea.php', $name, $value, $attributes);
}
/**
* Returns whether the i18n functionality is enabeld i.e. more than the
* default language is configured in config_defaults.inc.php.
*
* @return bool True if i18n is enabled.
*/
public static function isEnabled()
{
return is_array($GLOBALS['CONTENT_LANGUAGES']) &&
count($GLOBALS['CONTENT_LANGUAGES']) > 1;
}
protected $template;
protected $name;
protected $value;
protected $attributes;
/**
* Protected constructor in order to always force a specific input type.
*
* @param string|Flexi_Template $template Template to use
* @param string $name Name of the element
* @param string|I18NString $value Value of the element
* @param array_merge $attributes Additional variables for the
* element
*/
protected function __construct($template, $name, $value, array $attributes)
{
$this->template = $GLOBALS['template_factory']->open($template);
$this->name = $name;
$this->value = $value instanceof I18NString
? $value
: new I18NString($value);
$this->attributes = $attributes;
}
/**
* Sets the readonly state of the element.
*
* @param bool $state State of the readonly attribute (default true)
* @return I18N object to allow chaining
*/
public function setReadonly($state = true)
{
$this->attributes['readonly'] = (bool) $state;
return $this;
}
/**
* MVV: Check user's permissions for an object and set the readonly state
* accordingly.
*
* @param ModuleManagementModel $object Object to check permissions for
* @param string $perm Permission to check (default create)
* @return I18N object to allow chaining
*/
public function checkMvvPerms(ModuleManagementModel $object, $perm = MvvPerm::PERM_WRITE)
{
$may_edit = MvvPerm::get($object)->haveFieldPerm($field ?: $this->name, $perm);
return $this->setReadOnly(!$may_edit);
}
/**
* Renders the element as html.
*
* @param array $attributes Additional attributes
* @return string
*/
public function render(array $attributes = [])
{
$template = $this->template;
if (self::isEnabled()) {
$template->set_layout('i18n/group.php');
}
// Merge initially set attributes with current attributes
$attributes = array_merge($this->attributes, $attributes);
return $template->render([
'languages' => $GLOBALS['CONTENT_LANGUAGES'],
'base_lang' => key($GLOBALS['CONTENT_LANGUAGES']),
'wysiwyg' => in_array('wysiwyg', words($attributes['class'])),
'name' => $this->name,
'value' => $this->value,
'attributes' => $attributes,
]);
}
/**
* Converts the object to a string by rendering it.
*
* @return string
* @see I18N::render()
*/
public function __toString()
{
return $this->render();
}
}
|