Index: units/helpers/multilanguage_helper.php =================================================================== --- units/helpers/multilanguage_helper.php (revision 14103) +++ units/helpers/multilanguage_helper.php (working copy) @@ -193,6 +193,35 @@ } } + /** + * Creates missing multilanguage fields in table by specified prefix + * + * @param string $prefix + * @param int $src_language + * @param int $dst_language + */ + function copyMissingData($prefix, $src_language, $dst_language) + { + $table_name = $this->Application->getUnitOption($prefix, 'TableName'); + $this->curFields = $this->Application->getUnitOption($prefix, 'Fields'); + + if (!($table_name && $this->curFields) || ($table_name && !$this->Conn->TableFound($table_name))) { + // invalid config found or prefix not found + return true; + } + + foreach ($this->curFields as $field_name => $field_options) { + $formatter = isset($field_options['formatter']) ? $field_options['formatter'] : ''; + + if ( ($formatter == 'kMultiLanguage') && !isset($field_options['master_field']) ) { + $sql = 'UPDATE ' . $table_name . ' + SET l' . $dst_language . '_' . $field_name . ' = l' . $src_language . '_' . $field_name . ' + WHERE l' . $dst_language . '_' . $field_name . ' = "" OR l' . $dst_language . '_' . $field_name . ' IS NULL'; + $this->Conn->Query($sql); + } + } + } + function deleteField($prefix, $custom_id) { $table_name = $this->Application->getUnitOption($prefix, 'TableName'); Index: units/languages/languages_config.php =================================================================== --- units/languages/languages_config.php (revision 14103) +++ units/languages/languages_config.php (working copy) @@ -28,16 +28,6 @@ 'Conditional' => false, 'HookToPrefix' => 'lang', 'HookToSpecial' => '*', - 'HookToEvent' => Array ('OnSave'), - 'DoPrefix' => '', - 'DoSpecial' => '', - 'DoEvent' => 'OnUpdatePrimary', - ), - Array ( - 'Mode' => hAFTER, - 'Conditional' => false, - 'HookToPrefix' => 'lang', - 'HookToSpecial' => '*', 'HookToEvent' => Array('OnSave', 'OnMassDelete'), 'DoPrefix' => '', 'DoSpecial' => '', Index: units/languages/languages_event_handler.php =================================================================== --- units/languages/languages_event_handler.php (revision 14103) +++ units/languages/languages_event_handler.php (working copy) @@ -104,7 +104,7 @@ /* @var $object LanguagesItem */ $object->Load($id); - $object->setPrimary(); + $object->copyMissingData( $object->setPrimary() ); } } @@ -129,7 +129,7 @@ foreach ($ids as $id) { $object->Load($id); if ($object->GetDBField('PrimaryLang')) { - $object->setPrimary(true, false); + $object->copyMissingData( $object->setPrimary(true, false) ); } if ($object->GetDBField('AdminInterfaceLang')) { @@ -310,6 +310,7 @@ $this->Application->RemoveVar($var_name); $event->CallSubEvent('OnReflectMultiLingualFields'); + $event->CallSubEvent('OnUpdatePrimary'); } /** Index: units/languages/languages_item.php =================================================================== --- units/languages/languages_item.php (revision 14103) +++ units/languages/languages_item.php (working copy) @@ -22,10 +22,24 @@ return $this->Conn->GetOne($sql) + 1; } + /** + * Set's current language as new primary and return previous primary language + * + * @param bool $reset_primary + * @param bool $admin_language + * @return int + */ function setPrimary($reset_primary = true, $admin_language = false) { + $prev_primary = false; $primary_field = $admin_language ? 'AdminInterfaceLang' : 'PrimaryLang'; + if ($reset_primary) { + $sql = 'SELECT ' . $this->IDField . ' + FROM ' . $this->TableName . ' + WHERE ' . $primary_field . ' = 1'; + $prev_primary = $this->Conn->GetOne($sql); + $sql = 'UPDATE '.$this->TableName.' SET '.$primary_field.' = 0'; $this->Conn->Query($sql); @@ -39,9 +53,34 @@ // in case, when Update method is called for this langauge object $this->SetDBField($primary_field, 1); $this->SetDBField('Enabled', 1); + + return $prev_primary; } /** + * Copies missing data on current language from given language + * + * @param int $from_language + */ + function copyMissingData($from_language) + { + if ( !is_numeric($from_language) || ($from_language == $this->GetID()) ) { + // invalid or same language + return ; + } + + $ml_helper =& $this->Application->recallObject('kMultiLanguageHelper'); + /* @var $ml_helper kMultiLanguageHelper */ + + $to_language = $this->GetID(); + $this->Application->UnitConfigReader->ReReadConfigs(); + + foreach ($this->Application->UnitConfigReader->configData as $prefix => $config_data) { + $ml_helper->copyMissingData($prefix, $from_language, $to_language); + } + } + + /** * Allows to format number according to regional settings * * @param float $number