diff options
-rw-r--r-- | it_dbi.class | 27 | ||||
-rwxr-xr-x | tests/it_dbi.t | 29 |
2 files changed, 55 insertions, 1 deletions
diff --git a/it_dbi.class b/it_dbi.class index 413ca8d..b02960d 100644 --- a/it_dbi.class +++ b/it_dbi.class @@ -36,6 +36,7 @@ class it_dbi 'charset' => null, # client charset (requires MySQL 5.0.7 or later) 'classprefix' => "", 'getfieldinfo' => true, # do not read schema. only select() allowed + 'localized_defaultlanguage' => "de", # Localize fields with this suffix, e.g. copy title_de to title on read ); var $_key; # Key of currently loaded record or null (public readonly) @@ -418,7 +419,7 @@ function _write_pre_process(/* &$tags */) */ function clear($pp = true) { - foreach ((array)$this->_fields as $field => $dummy) + foreach ((array)$this->_fields + (array)$this->_localizedfields as $field => $dummy) unset($this->$field); unset($this->_data); unset($this->_key); @@ -592,6 +593,26 @@ function iterate() foreach ($this->_data as $field => $value) $this->$field = isset($value) && $this->_isint[$field] ? ($this->_data[$field] = intval($value)) : $value; + if ($localizedfields = $this->_localizedfields) + { + $lang = T_lang(); + foreach ($localizedfields as $field => $dummy) + { + $value = $this->{$field . "_" . $lang}; + + if (!isset($value)) + $value = $this->{$field . "_" . $this->_p['localized_defaultlanguage']}; + + if (isset($value)) + { + if (isset($this->$field)) + it::fatal("Field name clash: Overwriting {$this->_p['table']}.$field with {$field}_{$lang}, only use one of those fields"); + else + $this->$field = $value; + } + } + } + if (!empty($this->_p['keyfield'])) $this->_key = $this->_data[$this->_p['keyfield']]; } @@ -741,6 +762,10 @@ function _get_field_info() $this->_isint[$field['Field']] = preg_match('/^(tiny|small|medium|)int/', $field['Type']); } + # Consider all fields which have _{localized_defaultlanguage} suffix as localized + foreach (preg_grep('/_' . $this->_p['localized_defaultlanguage'] . '$/', array_keys($this->_fields)) as $field) + $this->_localizedfields[substr($field, 0, -1 - strlen($this->_p['localized_defaultlanguage']))] = true; + $state = it_dbi::_state_get($dbid); # State could have been modified by query above $state['fields'][$this->_p['table']] = $this->_fields; $state['isint'][$this->_p['table']] = $this->_isint; diff --git a/tests/it_dbi.t b/tests/it_dbi.t index 3ee2562..c885930 100755 --- a/tests/it_dbi.t +++ b/tests/it_dbi.t @@ -215,3 +215,32 @@ $count = 0; foreach ($record as $dummy_rec) $count++; is($count, 2, "Iterator reused"); + +# Test field localization feature + +$dbi->query('create temporary table it_dbi_testlocalized ( + ID int not null auto_increment, + foobar_de varchar(42), + foobar_fr varchar(42), + primary key(ID) +);'); + +$record = new it_dbi($db + array('table' => "it_dbi_testlocalized")); +$record->insert(array('foobar_de' => "deutsch", 'foobar_fr' => "franz")); + +T_set_language('de'); +$record->read(1); +is( + array($record->_key, $record->foobar), + array(1, "deutsch"), + "localized field foobar_de" +); + +T_set_language('fr'); +$record->read(1); +is( + array($record->_key, $record->foobar), + array(1, "franz"), + "localized field foobar_fr" +); + |