1 <?php
2
3 /**
4 * Class file.
5 *
6 * @author Tobias Munk <schmunk@usrbin.de>
7 * @link http://www.phundament.com/
8 * @copyright Copyright © 2005-2011 diemeisterei GmbH
9 * @license http://www.phundament.com/license/
10 */
11
12 /**
13 * Provides a translation for attributes in the specified relation
14 *
15 * <pre>
16 * 'Translation' => array(
17 * 'class' => 'P3TranslationBehavior',
18 * 'relation' => 'daContentTranslations',
19 * 'fallbackLanguage' => 'de_de'
20 * )
21 * </pre>
22 *
23 * @author Tobias Munk <schmunk@usrbin.de>
24 * @package p3extensions.behaviors
25 * @since 3.0.3
26 */
27 class P3TranslationBehavior extends CActiveRecordBehavior {
28
29 /**
30 * Name of the relation identifier in the model which should contain
31 * extended translation attributes
32 * @var string
33 */
34 public $relation;
35
36 /**
37 * Language to use if preferred language is not found
38 * @var type
39 */
40 public $fallbackLanguage;
41
42 /**
43 * Value to use if fallback value from language is not found
44 * @var type
45 */
46 public $fallbackIndicator = null;
47
48 /**
49 * Value to use if fallback value from language is not found
50 * @var type
51 */
52 public $fallbackValue = "not yet translated**";
53
54
55 /**
56 * Attributes which should not be translated
57 * @var type
58 */
59 public $attributesBlacklist = array();
60
61 #public $attributesWhitelist = array();
62
63 /**
64 * Tranlates attribute
65 * @param type $name attribute to translate
66 * @param type $language preferred language
67 * @param type $fallback fallback language
68 * @return mixed
69 */
70 public function t($name, $language = null, $fallback = false) {
71 if ($language === null) {
72 $language = Yii::app()->language;
73 }
74
75 if ($this->hasTranslationAttribute($name)) {
76 return $this->resolveTranslation($language, $name, $fallback);
77 } else {
78 throw new CException("Translation property '{$name}' is not defined. ");
79 }
80 }
81
82 public function getTranslationModel($language = null) {
83 if ($language === null) {
84 $language = Yii::app()->language;
85 }
86 // parse models into array
87 $models = array();
88 foreach ($this->owner->getRelated($this->relation) AS $translationModel) {
89 $models[$translationModel->language] = $translationModel;
90 }
91
92 if (isset($models[$language])) {
93 // desired model
94 return $models[$language];
95 } else {
96 return null;
97 }
98 }
99
100 private function resolveTranslation($language, $attr, $fallback) {
101 // parse models into array
102 $models = array();
103 foreach ($this->owner->getRelated($this->relation) AS $translationModel) {
104 $models[$translationModel->language] = $translationModel;
105 }
106
107 if (isset($models[$language])) {
108 // desired model
109 return $models[$language]->$attr;
110 } else if ($fallback === true && isset($models[$this->fallbackLanguage])) {
111 // fallback model
112 return $models[$this->fallbackLanguage]->$attr.$this->fallbackIndicator[$attr];
113 } else if ($fallback === true && !in_array($attr, $this->attributesBlacklist)) {
114 // return string if there's no value, but fallback in on
115 return $this->fallbackValue;
116 } else {
117 return null;
118 }
119 }
120
121 public function hasTranslationAttribute($attr) {
122 $relations = $this->owner->relations();
123 $model = new $relations[$this->relation][1];
124
125 // check relations
126 $modelRelations = $model->relations();
127 if (isset($modelRelations[$attr])) {
128 return true;
129 }
130
131 return ($model->hasAttribute($attr));
132 }
133
134 }
135
136 ?>