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