Index: branches/RC/core/units/general/helpers/multilanguage.php =================================================================== diff -u -r8929 -r9334 --- branches/RC/core/units/general/helpers/multilanguage.php (.../multilanguage.php) (revision 8929) +++ branches/RC/core/units/general/helpers/multilanguage.php (.../multilanguage.php) (revision 9334) @@ -6,9 +6,21 @@ */ class kMultiLanguageHelper extends kHelper { + /** + * Maximal language id + * + * @var int + */ var $languageCount = 0; /** + * Languages created in system + * + * @var Array + */ + var $languagesIDs = Array (); + + /** * Structure of table, that is currently processed * * @var Array @@ -43,18 +55,30 @@ } /** + * Checks if language with specified id is created + * + * @param int $language_id + * @return bool + */ + function LanguageFound($language_id) + { + return in_array($language_id, $this->languagesIDs) || $language_id <= 5; + } + + /** * Returns language count in system (always is divisible by 5) * */ function getLanguageCount() { + $id_field = $this->Application->getUnitOption('lang', 'IDField'); $table_name = $this->Application->getUnitOption('lang', 'TableName'); - $languages_count = $this->Conn->GetOne('SELECT COUNT(*) FROM '.$table_name); - if (!$languages_count) { - // during installation we have not languages, but we need to created custom field columns - $languages_count = 1; - } - return $languages_count + 5 - ( $languages_count % 5 ? ($languages_count % 5) : 5 ); + + $this->languagesIDs = $this->Conn->GetCol('SELECT '.$id_field.' FROM '.$table_name); + + $languages_count = $this->Conn->GetOne('SELECT MAX('.$id_field.') FROM '.$table_name); + + return max($languages_count, 5); } @@ -112,10 +136,9 @@ unset($this->curFields[$field_name]); continue; } - - $created_count = $this->getCreatedCount($field_name); - $create_count = $this->languageCount - $created_count; - if ($create_count > 0) { + + $this->setSourceField($field_name); + if ($this->languageCount > 0) { // `l77_Name` VARCHAR( 255 ) NULL DEFAULT '0'; $field_mask = Array(); $field_mask['name'] = 'l%s_'.$field_name; @@ -130,16 +153,19 @@ $field_mask['type'] = $field_options['db_type']; } $field_mask['default'] = 'DEFAULT '.$default_value; - + if (strtoupper($field_mask['type']) == 'TEXT') { // text fields in mysql doesn't have default value $field_mask = $field_mask['name'].' '.$field_mask['type'].' '.$field_mask['null']; } else { $field_mask = $field_mask['name'].' '.$field_mask['type'].' '.$field_mask['null'].' '.$field_mask['default']; } - - $sqls[] = 'ALTER TABLE '.$table_name.( $this->generateAlterSQL($field_mask, $created_count + 1, $create_count) ); + + $alter_sqls = $this->generateAlterSQL($field_mask, 1, $this->languageCount); + if ($alter_sqls) { + $sqls[] = 'ALTER TABLE '.$table_name.' '.$alter_sqls; + } } } } @@ -175,7 +201,12 @@ return $this->curStructure[$this->curSourceField][$param_name]; } - function getCreatedCount($field_name) + /** + * Detects field name to create other fields from + * + * @param string $field_name + */ + function setSourceField($field_name) { $ret = $this->scanTable('/^l[\d]+_'.preg_quote($field_name, '/').'$/'); if (!$ret) { @@ -186,8 +217,8 @@ else { $this->curSourceField = 'l1_'.$field_name; } - return $ret; } + /** * Returns ALTER statement part for adding required fields to table * @@ -209,12 +240,22 @@ $single_lang = $this->Conn->GetOne($sql) == 1; } - $ret = ' '; - $ml_field = preg_replace('/l(.*)_(.*?) (.*)/', '\\2', $field_mask); - + $ret = ''; + $ml_field = preg_replace('/l(.*?)_(.*?) (.*)/', '\\2', $field_mask); + $i_count = $start_index + $create_count; while ($start_index < $i_count) { - list($prev_field,$type) = explode(' ', sprintf($field_mask, $start_index - 1) ); + + if (isset($this->curStructure['l'.$start_index.'_'.$ml_field]) || (!$this->LanguageFound($start_index)) ) { + $start_index++; + continue; + } + + $prev_index = $start_index - 1; + do { + list($prev_field,$type) = explode(' ', sprintf($field_mask, $prev_index) ); + } while ($prev_index > 0 && !$this->LanguageFound($prev_index--)); + if (substr($prev_field, 0, 3) == 'l0_') { $prev_field = substr($prev_field, 3, strlen($prev_field)); if (!$this->curSourceField) { @@ -234,16 +275,16 @@ if ($this->curIndexCount < 32 && ($start_index == $this->Application->GetDefaultLanguageId() || !$single_lang)) { // create index for primary language column + for all others (if multiple languages installed) list($field_name, $field_params) = explode(' ', $field_expression, 2); - + $index_type = isset($this->curFields[$ml_field]['index_type']) ? $this->curFields[$prev_field]['index_type'] : 'string'; - + $ret .= $index_type == 'string' ? 'ADD INDEX (`'.$field_name.'` (5) ), ' : 'ADD INDEX (`'.$field_name.'`), '; $this->curIndexCount++; } $start_index++; } - return preg_replace('/, $/',';',$ret); + return preg_replace('/, $/', ';', $ret); } }