Index: trunk/core/units/general/helpers/multilanguage.php =================================================================== diff -u -N -r5514 -r6093 --- trunk/core/units/general/helpers/multilanguage.php (.../multilanguage.php) (revision 5514) +++ trunk/core/units/general/helpers/multilanguage.php (.../multilanguage.php) (revision 6093) @@ -5,7 +5,7 @@ * */ class kMultiLanguageHelper extends kHelper { - + var $languageCount = 0; /** @@ -14,34 +14,34 @@ * @var Array */ var $curStructure = Array(); - + /** * Field, to get structure information from * * @var string */ var $curSourceField = false; - + /** * Indexes used in table of 32 * * @var int */ var $curIndexCount = 0; - + /** * Fields from config, that are currently used * * @var Array */ var $curFields = Array(); - + function kMultiLanguageHelper() { parent::kHelper(); $this->languageCount = $this->getLanguageCount(); } - + /** * Returns language count in system (always is divisible by 5) * @@ -56,33 +56,33 @@ } return $languages_count + 5 - ( $languages_count % 5 ? ($languages_count % 5) : 5 ); } - - + + function scanTable($mask) { $i = 0; $fields_found = 0; $fields = array_keys($this->curStructure); - + foreach ($fields as $field_name) { if (preg_match($mask, $field_name)) { $fields_found++; } } return $fields_found; } - - function readTableStructure($table_name) + + function readTableStructure($table_name, $refresh=false) { static $structure_status = Array(); - - if (!getArrayValue($structure_status, $table_name)) { + + if ($refresh || !getArrayValue($structure_status, $table_name)) { $this->curStructure = $this->Conn->Query('DESCRIBE '.$table_name, 'Field'); $this->curIndexCount = count($this->Conn->Query('SHOW INDEXES FROM '.$table_name)); $structure_status[$table_name] = true; } } - + /** * Creates missing multilanguage fields in table by specified prefix * @@ -94,29 +94,34 @@ if ($refresh) { $this->Application->HandleEvent( new kEvent($prefix.':OnCreateCustomFields') ); } - + $table_name = $this->Application->getUnitOption($prefix, 'TableName'); $this->curFields = $this->Application->getUnitOption($prefix, 'Fields'); - + if (!($table_name && $this->curFields) ) { // invalid config found or prefix not found return true; } $sqls = Array(); + $this->readTableStructure($table_name, $refresh); + foreach($this->curFields as $field_name => $field_options) { if (getArrayValue($field_options, 'formatter') == 'kMultiLanguage') { - $this->readTableStructure($table_name); - + if (isset($field_options['master_field'])) { + unset($this->curFields[$field_name]); + continue; + } + $created_count = $this->getCreatedCount($field_name); $create_count = $this->languageCount - $created_count; if ($create_count > 0) { // `l77_Name` VARCHAR( 255 ) NULL DEFAULT '0'; $field_mask = Array(); $field_mask['name'] = 'l%s_'.$field_name; $field_mask['null'] = getArrayValue($field_options, 'not_null') ? 'NOT NULL' : 'NULL'; - + if ($this->curSourceField) { $default_value = $this->getFieldParam('Default') != 'NULL' ? $this->Conn->qstr($this->getFieldParam('Default')) : $this->getFieldParam('Default'); $field_mask['type'] = $this->getFieldParam('Type'); @@ -127,23 +132,23 @@ } $field_mask['default'] = 'DEFAULT '.$default_value; $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) ); } } } - + foreach ($sqls as $sql_query) { $this->Conn->Query($sql_query); } } - + function deleteField($prefix, $custom_id) { $table_name = $this->Application->getUnitOption($prefix, 'TableName'); $sql = 'DESCRIBE '.$table_name.' "l%_cust_'.$custom_id.'"'; $fields = $this->Conn->GetCol($sql); - + $sql = 'ALTER TABLE '.$table_name.' '; $sql_template = 'DROP COLUMN %s, '; foreach ($fields as $field_name) { @@ -152,7 +157,7 @@ $sql = preg_replace('/(.*), $/', '\\1', $sql); $this->Conn->Query($sql); } - + /** * Returns parameter requested of current source field * @@ -163,7 +168,7 @@ { return $this->curStructure[$this->curSourceField][$param_name]; } - + function getCreatedCount($field_name) { $ret = $this->scanTable('/^l[\d]+_'.preg_quote($field_name, '/').'/'); @@ -197,7 +202,7 @@ // if language count = 0, then assume it's multi language mode $single_lang = $this->Conn->GetOne($sql) == 1; } - + $ret = ' '; $i_count = $start_index + $create_count; while ($start_index < $i_count) { @@ -207,23 +212,24 @@ if (!$this->curSourceField) { // get field name before this one $fields = array_keys($this->curFields); +// $prev_field = key(end($this->curStructure)); $prev_field = $fields[array_search($prev_field, $fields) - 1]; if (getArrayValue($this->curFields[$prev_field], 'formatter') == 'kMultiLanguage') { $prev_field = 'l'.$this->languageCount.'_'.$prev_field; } } } - + $field_expression = sprintf($field_mask, $start_index); $ret .= 'ADD COLUMN '.$field_expression.' AFTER `'.$prev_field.'`, '; - + 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); + list($field_name, $field_params) = explode(' ', $field_expression, 2); $ret .= 'ADD INDEX (`'.$field_name.'` (5) ), '; $this->curIndexCount++; } - + $start_index++; } return preg_replace('/, $/',';',$ret);