1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
/**
|
3 |
|
|
* @file
|
4 |
|
|
* i18n Object Class
|
5 |
|
|
*/
|
6 |
|
|
|
7 |
|
|
/**
|
8 |
|
|
* Object wrapper
|
9 |
|
|
*/
|
10 |
|
|
class i18n_object_wrapper {
|
11 |
|
|
protected $type;
|
12 |
|
|
protected $key;
|
13 |
|
|
protected $object;
|
14 |
|
|
// Object translations, static cache.
|
15 |
|
|
protected $translations;
|
16 |
|
|
|
17 |
|
|
/**
|
18 |
|
|
* Class constructor
|
19 |
|
|
*/
|
20 |
|
|
public function __construct($type, $key, $object) {
|
21 |
|
|
$this->type = $type;
|
22 |
|
|
$this->key = $key;
|
23 |
|
|
$this->object = $object ? $object : $this->load_object($key);
|
24 |
|
|
}
|
25 |
|
|
|
26 |
|
|
/**
|
27 |
|
|
* Get edit path for object
|
28 |
|
|
*/
|
29 |
|
|
function get_edit_path() {
|
30 |
|
|
return $this->path_replace($this->get_info('edit path'));
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
/**
|
34 |
|
|
* Get field value from object/array
|
35 |
|
|
*/
|
36 |
|
|
function get_field($field, $default = NULL) {
|
37 |
|
|
return i18n_object_field($this->object, $field, $default);
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
/**
|
41 |
|
|
* Set field value to object/array
|
42 |
|
|
*/
|
43 |
|
|
function set_field($field, $value) {
|
44 |
|
|
if (is_object($this->object)) {
|
45 |
|
|
$this->object->$field = $value;
|
46 |
|
|
}
|
47 |
|
|
elseif (is_array($this->object)) {
|
48 |
|
|
$this->object[$field] = $value;
|
49 |
|
|
}
|
50 |
|
|
return $this;
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
/**
|
54 |
|
|
* Get string numeric key for indexing.
|
55 |
|
|
*/
|
56 |
|
|
function get_index() {
|
57 |
|
|
$key = $this->get_key();
|
58 |
|
|
return is_array($key) ? implode(':', $key) : $key;
|
59 |
|
|
}
|
60 |
|
|
|
61 |
|
|
/**
|
62 |
|
|
* Get key value from object/array
|
63 |
|
|
*/
|
64 |
|
|
function get_key($default = NULL) {
|
65 |
|
|
if ($field = $this->get_info('key')) {
|
66 |
|
|
return $this->get_field($field, $default);
|
67 |
|
|
}
|
68 |
|
|
else {
|
69 |
|
|
return $default;
|
70 |
|
|
}
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
/**
|
74 |
|
|
* Get language code
|
75 |
|
|
*/
|
76 |
|
|
public function get_langcode() {
|
77 |
|
|
return i18n_object_langcode($this->object);
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
/**
|
81 |
|
|
* Get real object or array.
|
82 |
|
|
*/
|
83 |
|
|
public function get_object() {
|
84 |
|
|
return $this->object;
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
/**
|
88 |
|
|
* Load real object or array.
|
89 |
|
|
*
|
90 |
|
|
* @param $object
|
91 |
|
|
*/
|
92 |
|
|
function load_object($object) {
|
93 |
|
|
if ($callback = $this->get_info('load callback', NULL)) {
|
94 |
|
|
$this->object = call_user_func($callback, $object);
|
95 |
|
|
}
|
96 |
|
|
elseif ($entity_type = $this->get_info('entity', NULL)) {
|
97 |
|
|
$entity = entity_load($entity_type, array($object));
|
98 |
|
|
$this->object = $entity ? reset($entity) : FALSE;
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
return $this->get_object();
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
/**
|
105 |
|
|
* Get menu placehoders for object
|
106 |
|
|
*/
|
107 |
|
|
protected function get_placeholders() {
|
108 |
|
|
$placeholders = $this->get_info('placeholders', array());
|
109 |
|
|
foreach ($placeholders as $name => $field) {
|
110 |
|
|
$placeholders[$name] = $this->get_field($field);
|
111 |
|
|
}
|
112 |
|
|
return $placeholders;
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
/**
|
116 |
|
|
* Get link for item
|
117 |
|
|
*/
|
118 |
|
|
public function get_path() {
|
119 |
|
|
if ($uri = entity_uri($this->type, $this->object)) {
|
120 |
|
|
return $uri['path'];
|
121 |
|
|
}
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
/**
|
125 |
|
|
* Get title from item
|
126 |
|
|
*/
|
127 |
|
|
public function get_title() {
|
128 |
|
|
return entity_label($this->type, $this->object);
|
129 |
|
|
}
|
130 |
|
|
/**
|
131 |
|
|
* Get object type
|
132 |
|
|
*/
|
133 |
|
|
public function get_type() {
|
134 |
|
|
return $this->type;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
/**
|
138 |
|
|
* Menu access callback for mixed translation tab
|
139 |
|
|
*/
|
140 |
|
|
function get_translate_access() {
|
141 |
|
|
switch ($this->get_translate_mode()) {
|
142 |
|
|
case I18N_MODE_TRANSLATE:
|
143 |
|
|
return $this->translate_access();
|
144 |
|
|
case I18N_MODE_LOCALIZE:
|
145 |
|
|
return $this->localize_access();
|
146 |
|
|
default:
|
147 |
|
|
return FALSE;
|
148 |
|
|
}
|
149 |
|
|
}
|
150 |
|
|
|
151 |
|
|
/**
|
152 |
|
|
* Get translate or localize mode for object
|
153 |
|
|
*/
|
154 |
|
|
function get_translate_mode() {
|
155 |
|
|
return I18N_MODE_NONE;
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
/**
|
159 |
|
|
* Get translation set id if any
|
160 |
|
|
*/
|
161 |
|
|
function get_tsid() {
|
162 |
|
|
return $this->get_field($this->get_translation_info('field', 'i18n_tsid'));
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
/**
|
166 |
|
|
* Set translation set id
|
167 |
|
|
*/
|
168 |
|
|
function set_tsid($tsid) {
|
169 |
|
|
return $this->set_field($this->get_translation_info('field', 'i18n_tsid'), $tsid);
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
/**
|
173 |
|
|
* Localize object if localizable.
|
174 |
|
|
*/
|
175 |
|
|
function localize($langcode, $options = array()) {
|
176 |
|
|
if ($this->get_translate_mode() == I18N_MODE_LOCALIZE) {
|
177 |
|
|
return $this->translate($langcode, $options);
|
178 |
|
|
}
|
179 |
|
|
else {
|
180 |
|
|
return $this->object;
|
181 |
|
|
}
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
/**
|
185 |
|
|
* Translate object if translatable.
|
186 |
|
|
*/
|
187 |
|
|
function translate($langcode, $options = array()) {
|
188 |
|
|
if (isset($this->translations[$langcode])) {
|
189 |
|
|
return $this->translations[$langcode];
|
190 |
|
|
}
|
191 |
|
|
else {
|
192 |
|
|
return $this->object;
|
193 |
|
|
}
|
194 |
|
|
}
|
195 |
|
|
|
196 |
|
|
/**
|
197 |
|
|
* Translate access (translation sets)
|
198 |
|
|
*/
|
199 |
|
|
protected function translate_access() {
|
200 |
|
|
return FALSE;
|
201 |
|
|
}
|
202 |
|
|
|
203 |
|
|
/**
|
204 |
|
|
* Translate access (localize strings)
|
205 |
|
|
*/
|
206 |
|
|
protected function localize_access() {
|
207 |
|
|
return FALSE;
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
/**
|
211 |
|
|
* Replace path with placeholders
|
212 |
|
|
*
|
213 |
|
|
* @param $path
|
214 |
|
|
* Path to replace
|
215 |
|
|
* @param $replacements
|
216 |
|
|
* Replacement variables to override or add to placeholders
|
217 |
|
|
*/
|
218 |
|
|
protected function path_replace($path, $replacements = array()) {
|
219 |
|
|
if ($path) {
|
220 |
|
|
$path = strtr($path, $replacements + $this->get_placeholders());
|
221 |
|
|
// Clean up duplicated and final '/' (empty placeholders)
|
222 |
|
|
$path = strtr($path, array('//' => '/'));
|
223 |
|
|
return trim($path, '/');
|
224 |
|
|
}
|
225 |
|
|
else {
|
226 |
|
|
return '';
|
227 |
|
|
}
|
228 |
|
|
}
|
229 |
|
|
/**
|
230 |
|
|
* Get object info
|
231 |
|
|
*/
|
232 |
|
|
public function get_info($property, $default = NULL) {
|
233 |
|
|
return i18n_object_info($this->type, $property, $default);
|
234 |
|
|
}
|
235 |
|
|
/**
|
236 |
|
|
* Get object translation set info
|
237 |
|
|
*/
|
238 |
|
|
public function get_translation_info($property, $default = NULL) {
|
239 |
|
|
return function_exists('i18n_translation_set_info') ? i18n_translation_set_info($this->type, $property, $default) : $default;
|
240 |
|
|
}
|
241 |
|
|
/**
|
242 |
|
|
* Get object string translation info
|
243 |
|
|
*/
|
244 |
|
|
public function get_string_info($property, $default = NULL) {
|
245 |
|
|
$info = $this->get_info('string translation');
|
246 |
|
|
return $info && isset($info[$property]) ? $info[$property] : $default;
|
247 |
|
|
}
|
248 |
|
|
} |