i18n_object.inc 5.4 KB

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