public final function moveAllTables($from, $to) { $this->fromLangTag = $from; $this->toLangTag = $to; if (!$this->fromLangTag) { $this->sendOutput(JText::_('SOURCE_LANGUAGE_IS_NOT_SPECIFIED_OR_NOT_SUPPORTED')); return false; } if (!$this->toLangTag) { $this->sendOutput(JText::_('DESTINATION_LANGUAGE_IS_NOT_SPECIFIED_OR_NOT_SUPPORTED')); return false; } JalangHelper::createLanguageContent($this->toLangTag); $adapters = JalangHelperContent::getListAdapters(); $db = JFactory::getDbo(); foreach ($adapters as $adapter) { $component = $adapter['title']; $adapter = JalangHelperContent::getInstance($adapter['name']); if ($adapter->table_type == 'native') { if (!$adapter->language_field) { continue; } $this->sendOutput('<h3>' . JText::sprintf('START_TO_MOVE_ITEM_FROM_THE_COMPONENT', $component) . '</h3>'); //Only support native table now $query = $db->getQuery(true); $query->update('#__' . $adapter->table); if ($adapter->language_mode == 'id') { $query->set($db->quoteName($adapter->language_field) . '=' . $db->quote(JalangHelper::getLanguageIdFromCode($this->toLangTag))); $query->where($db->quoteName($adapter->language_field) . '=' . $db->quote(JalangHelper::getLanguageIdFromCode($this->fromLangTag))); } else { $query->set($db->quoteName($adapter->language_field) . '=' . $db->quote($this->toLangTag)); $query->where($db->quoteName($adapter->language_field) . '=' . $db->quote($this->fromLangTag)); } $db->setQuery($query); $db->execute(); $num_items = $db->getAffectedRows(); $this->sendOutput(JText::sprintf('NUM_ITEMS_ARE_MOVED', $num_items) . '<br />'); } } }
public final function translateTable($itemtype, $from, $to, $adapter = null, &$count = 0) { $defaultLanguage = JalangHelper::getDefaultLanguage(); $params = JComponentHelper::getParams('com_jalang'); $firstRun = !$adapter ? 1 : 0; if ($firstRun) { $this->sendOutput('[' . JText::sprintf('STARTED_TRANSLATE_THE_TABLE_VAR', $itemtype) . ']'); } if (!$from) { $from = '*'; } $this->convertLangTag = null; if ($this->fromLangTag !== $from) { JalangHelper::createLanguageContent($from); $this->from = $this->getLangCode($from); $this->fromLangTag = $from; } if ($this->toLangTag !== $to) { JalangHelper::createLanguageContent($to); $this->to = $this->getLangCode($to); $this->toLangTag = $to; } if (!$this->from) { $this->sendOutput(JText::_('SOURCE_LANGUAGE_IS_NOT_SPECIFIED_OR_NOT_SUPPORTED')); return false; } if (!$this->to || $this->to == '*') { $this->sendOutput(JText::_('DESTINATION_LANGUAGE_IS_NOT_SPECIFIED_OR_NOT_SUPPORTED')); return false; } if ($this->fromLangTag == $this->toLangTag) { $this->sendOutput(JText::_('SOURCE_LANGUAGE_AND_DESTINATION_LANGUAGE_MUST_DIFFERENT')); return false; } if (!is_object($adapter)) { $adapter = JalangHelperContent::getInstance($itemtype); if (!$adapter) { $this->sendOutput(JText::sprintf('CONTENT_TYPE_VAR_IS_NOT_SUPPORTED', $itemtype)); return false; } } if (!count($adapter->translate_fields)) { $this->sendOutput(JText::sprintf('LIST_OF_FIELDS_FOR_TRANSLATING_IS_EMPTY_PLEASE_CHECK_CONFIGURATION', $itemtype)); return false; } if ($firstRun) { //load association data if (count($adapter->reference_tables) || count($adapter->reference_fields)) { $reference_tables = @array_merge($adapter->reference_tables, array_values($adapter->reference_fields)); $reference_tables = array_unique($reference_tables); foreach ($reference_tables as $table) { $adapter2 = JalangHelperContent::getInstance($table); if ($adapter2) { $this->loadAssociate($adapter2->table, $adapter2->primarykey, $adapter2->associate_context); } } } $this->loadAssociate($adapter->table, $adapter->primarykey, $adapter->associate_context); $adapter->beforeTranslate($this); } //TRANSLATE ITEMS $from_table = $to_table = $table = '#__' . $adapter->table; if ($adapter->table_type == 'table') { $from_table = $this->getLangTable($table, $this->fromLangTag); $to_table = $this->getLangTable($table, $this->toLangTag); } $fields = $adapter->translate_fields; $context = $adapter->associate_context; $alias = $adapter->alias_field; $langField = $adapter->language_field; $db = JFactory::getDbo(); $query = $db->getQuery(true); $query->select('*'); $query->from($from_table); if ($adapter->nested_field) { $query->where($db->quoteName($adapter->nested_field) . '=' . $db->quote($adapter->nested_value)); } if (!empty($adapter->translate_filters)) { $query->where($adapter->translate_filters); } if ($this->ignoreTranslated && !$adapter->nested_field) { //always check all items of nested table to get a new sub-items $translatedItems = $this->getTranslatedItems($adapter->table); if (count($translatedItems)) { $query->where($db->quoteName($adapter->primarykey) . ' NOT IN (' . implode(',', $translatedItems) . ')'); } } if ($adapter->table_type == 'alias') { if ($this->fromLangTag != '*') { if ($this->fromLangTag == $defaultLanguage) { $query->where('(' . $db->quoteName($adapter->alias_field) . ' LIKE ' . $db->quote('%-' . $this->from) . ' OR ' . $db->quoteName($adapter->alias_field) . ' NOT REGEXP ' . $db->quote('-[a-z]{2}$') . ')'); } else { $query->where($db->quoteName($adapter->alias_field) . ' LIKE ' . $db->quote('%-' . $this->from)); } } else { $query->where($db->quoteName($adapter->alias_field) . ' NOT REGEXP ' . $db->quote('-[a-z]{2}$')); } } elseif ($adapter->table_type == 'table') { } else { //native + table_ml if ($adapter->language_mode == 'id') { $query->where($langField . ' = ' . $db->quote(JalangHelper::getLanguageIdFromCode($this->fromLangTag))); } else { if ($this->fromLangTag == $defaultLanguage) { $query->where('(' . $langField . '=' . $db->quote($this->fromLangTag) . ' OR ' . $langField . '=' . $db->quote('*') . ' OR ' . $langField . '=' . $db->quote('') . ')'); } else { $query->where($langField . '=' . $db->quote($this->fromLangTag)); } } } $db->setQuery($query); $rows = $db->loadAssocList(); if (count($rows)) { foreach ($rows as $row) { $sourceid = $row[$adapter->primarykey]; //check associated item? if ($adapter->table_type == 'alias') { $newalias = $adapter->getNewAlias($row[$alias], $this->fromLangTag, $this->toLangTag, $row, 'alias', false); $queryAlias = $db->getQuery(true); $queryAlias->select($adapter->primarykey)->from($from_table)->where($db->quoteName($alias) . '=' . $db->quote($newalias)); $db->setQuery($queryAlias); $newid = $db->loadResult(); } else { $newid = $this->getAssociatedItem($adapter->table, $sourceid, null); } if (!$newid) { $count++; $title = isset($row[$adapter->title_field]) ? $row[$adapter->title_field] : '#' . $sourceid; $this->sendOutput(str_pad('', 5, '-') . ' ' . JText::sprintf('START_TRANSLATING_VAR_VAR', $adapter->table, $title)); $iFields = array(); $sentences = array(); foreach ($fields as $f) { $sentences[] = $row[$f]; } if ($this->from != $this->to) { $translated = $this->translateArray($sentences, array_keys($fields)); } else { //clone content if two language has the same language code (E.g: en-GB, en-US or en-AU) $translated = $sentences; } if (!is_array($translated) || !count($translated)) { $this->sendOutput('<span class="failed">' . JText::_('FAILED') . '</span>: ' . JText::sprintf('FAILED_TRANSLATE_VAR', $this->getError())); } else { if (isset($row[$langField])) { //language field if ($adapter->language_mode == 'id') { $iFields[$langField] = JalangHelper::getLanguageIdFromCode($this->toLangTag); } else { // update the translated item to default (in this case is from) language. Ignore if the current item is default menu item /*if($row['language'] == '*') { $query = "UPDATE {$table} SET `language` = ".$db->quote($this->fromLangTag)." WHERE `{$adapter->primarykey}` = ".$sourceid; }*/ $iFields[$langField] = $this->toLangTag; } } // item id for table translate if ($adapter->table_type == 'table' || $adapter->table_type == 'table_ml') { $newid = $row[$adapter->primarykey]; //in case of database structure for multilingual is designed by using multiple tables for each language //then associated items in each tables will have the same id $iFields[$adapter->primarykey] = $newid; } //nested field if ($adapter->nested_field) { $iFields[$adapter->nested_field] = $this->getAssociatedItem($adapter->table, $row[$adapter->nested_field], $row[$adapter->nested_field]); } if (is_array($adapter->reference_fields)) { foreach ($adapter->reference_fields as $rel_field => $rel_table) { $iFields[$rel_field] = $this->getAssociatedItem($rel_table, $row[$rel_field], $row[$rel_field]); } } //translated fields $i = 0; foreach ($fields as $f) { $iFields[$f] = $translated[$i++]; } //clone other fields foreach ($row as $of => $ov) { if ($of != $adapter->primarykey && $of != $adapter->alias_field && !isset($iFields[$of])) { $iFields[$of] = $ov; } } //alias if (!empty($alias)) { $iFields[$alias] = $adapter->getNewAlias($row[$alias], $this->fromLangTag, $this->toLangTag, $iFields, '', true); } //prepare data $adapter->beforeSave($this, $sourceid, $iFields); $queryInsert = $db->getQuery(true); $queryInsert->insert($to_table); //columns $colums = array_keys($iFields); foreach ($colums as &$val) { $val = $db->quoteName($val); } $queryInsert->columns($colums); //values $values = array_values($iFields); foreach ($values as &$val) { $val = $db->quote($val); } $queryInsert->values(implode(',', $values)); $db->setQuery($queryInsert); $result = $db->execute(); if (!$result) { $this->sendOutput('<span class="failed">' . JText::_('FAILED') . '</span>' . $db->getErrorMsg()); continue; } if ($adapter->table_type != 'table') { $newid = $db->insertid(); $iFields[$adapter->primarykey] = $newid; } if ($adapter->table_type != 'table' && $adapter->table_type != 'table_ml') { $this->addAssociate($adapter->table, $context, $row[$adapter->primarykey], $newid); } if ($newid) { $adapter->afterSave($this, $sourceid, $iFields); } $this->sendOutput('<span class="success">' . JText::_('SUCCESS') . '</span>'); } } else { /** * @todo update existing items? */ } if ($adapter->nested_field) { //backup data before call recursive $nested_value = $adapter->nested_value; $adapter->nested_value = $row[$adapter->primarykey]; $this->translateTable($adapter->table, $this->fromLangTag, $this->toLangTag, $adapter, $count); $adapter->nested_value = $nested_value; } } //only fire afterTranslate event if has new items if ($firstRun && $count) { $this->sendOutput(JText::sprintf('RUN_REGISTERED_TASK_AFTER_TRANSLATE_TABLE_VAR', $adapter->table)); $adapter->afterTranslate($this); } } }