示例#1
0
 /**
  * Build an SQL query to load the list data.
  *
  * @return  JDatabaseQuery
  * @since   1.6
  */
 public function getListQuery2($model)
 {
     if (!$this->table) {
         $this->errorExtendMethod(__METHOD__);
         return false;
     }
     // Create a new query object.
     $db = JFactory::getDbo();
     $query = $db->getQuery(true);
     $query->select('*');
     $query->from('#__' . $this->table . ' AS a');
     // Filter on the language.
     $defaultLanguage = JalangHelper::getDefaultLanguage();
     $language = $model->getState('mainlanguage');
     $filter_search = $model->getState('filter.search');
     if ($language) {
         if ($this->table_type == 'native' || $this->table_type == 'table_ml') {
             $langField = 'a.' . $db->quoteName($this->language_field);
             if ($this->language_mode == 'id') {
                 $query->where($langField . ' = ' . $db->quote(JalangHelper::getLanguageIdFromCode($language)));
             } else {
                 if ($language == $defaultLanguage) {
                     $query->where('(' . $langField . ' = ' . $db->quote($language) . ' OR ' . $langField . ' = ' . $db->quote('*') . ' OR ' . $langField . ' = ' . $db->quote('') . ')');
                 } else {
                     $query->where($langField . ' = ' . $db->quote($language));
                 }
             }
         } elseif ($this->table_type == 'table') {
             if ($language == '*') {
                 $language = $defaultLanguage;
             }
             $params = JComponentHelper::getParams('com_jalang');
             $translator = JalangHelperTranslator::getInstance($params->get('translator_api_active', 'bing'));
             $table = $translator->getLangTable($this->table, $language);
             $query->clear('from');
             $query->from('#__' . $table);
         } elseif ($this->table_type == 'alias') {
             if ($language != '*') {
                 @(list($shortlang, $country) = explode('-', $language));
                 if ($language == $defaultLanguage) {
                     $query->where('(' . $db->quoteName($this->alias_field) . ' LIKE ' . $db->quote('%-' . $shortlang) . ' OR ' . $db->quoteName($this->alias_field) . ' NOT REGEXP ' . $db->quote('\\-[a-z]{2}$') . ')');
                 } else {
                     $query->where($db->quoteName($this->alias_field) . ' LIKE ' . $db->quote('%-' . $shortlang));
                 }
             } else {
                 $query->where($db->quoteName($this->alias_field) . ' NOT REGEXP ' . $db->quote('\\-[a-z]{2}$'));
             }
         }
     }
     if (count($this->translate_filters)) {
         $query->where($this->translate_filters);
     }
     if ($filter_search) {
         $fields = $this->getDisplayFields();
         if (is_array($fields)) {
             $filterKeyword = array();
             foreach ($fields as $field => $ftitle) {
                 $filterKeyword[] = $db->quoteName($field) . ' LIKE ' . $db->quote('%' . $filter_search . '%');
             }
             $query->where('(' . implode(' OR ', $filterKeyword) . ')');
         } else {
             $query->where($db->quoteName($this->title_field) . ' LIKE ' . $db->quote('%' . $filter_search . '%'));
         }
     }
     $ordering = $model->getState('list.ordering', 'a.ordering');
     if (in_array($ordering, $this->getSortFields())) {
         $query->order($ordering . ' ' . $model->getState('list.direction', 'ASC'));
     }
     return $query;
 }
示例#2
0
 public final function removeLanguage($languageTag)
 {
     if (!$languageTag || $languageTag == '*') {
         $this->sendOutput(JText::_('SOURCE_LANGUAGE_IS_NOT_SPECIFIED_OR_NOT_SUPPORTED'));
         return false;
     }
     if ($languageTag == JalangHelper::getDefaultLanguage()) {
         $this->sendOutput(JText::_('ALERT_CANNOT_REMOVE_DEFAULT_LANGUAGE'));
         return false;
     }
     $langId = JalangHelper::getLanguageIdFromCode($languageTag);
     $parts = explode('-', $languageTag);
     $langCode = strtolower(trim($parts[0]));
     $adapters = JalangHelperContent::getListAdapters();
     $db = JFactory::getDbo();
     foreach ($adapters as $adapter) {
         $component = $adapter['title'];
         $adapter = JalangHelperContent::getInstance($adapter['name']);
         $table = '#__' . $adapter->table;
         $this->sendOutput('<h3>' . JText::sprintf('START_TO_REMOVE_ITEM_FROM_THE_COMPONENT', $component) . '</h3>');
         if ($adapter->table_type == 'native' || $adapter->table_type == 'table_ml') {
             if (!$adapter->language_field) {
                 continue;
             }
             if ($adapter->language_mode == 'id') {
                 $where = $db->quoteName($adapter->language_field) . '=' . $db->quote($langId);
             } else {
                 $where = $db->quoteName($adapter->language_field) . '=' . $db->quote($languageTag);
             }
             if ($adapter->table_type == 'native') {
                 //delete association data
                 $query = "DELETE FROM #__associations\r\n\t\t\t\t\t\tWHERE id IN (\r\n\t\t\t\t\t\t\tSELECT " . $db->quote($adapter->primarykey) . "\r\n\t\t\t\t\t\t\tFROM " . $table . "\r\n\t\t\t\t\t\t\tWHERE `context` = " . $db->quote($adapter->associate_context) . "\r\n\t\t\t\t\t\t\tAND " . $where . "\r\n\t\t\t\t\t\t)";
                 $db->setQuery($query);
                 $db->execute();
             }
             //delete items
             $query = $db->getQuery(true);
             $query->delete($table);
             $query->where($where);
             $db->setQuery($query);
             $db->execute();
             $num_items = $db->getAffectedRows();
             $this->sendOutput(JText::sprintf('NUM_ITEMS_ARE_REMOVED', $num_items) . '<br />');
         } elseif ($adapter->table_type == 'alias') {
             $query = $db->getQuery(true);
             $query->delete($table);
             $query->where($db->quoteName($adapter->alias_field) . ' LIKE ' . $db->quote('%-' . $langCode));
             $db->setQuery($query);
             $db->execute();
             $num_items = $db->getAffectedRows();
             $this->sendOutput(JText::sprintf('NUM_ITEMS_ARE_REMOVED', $num_items) . '<br />');
         } elseif ($adapter->table_type == 'table') {
             $tableml = $this->getLangTable($table, $languageTag);
             $query = "DROP TABLE " . $db->quoteName($tableml);
             $db->setQuery($query);
             $db->execute();
             $this->sendOutput(JText::sprintf('DROP_THE_LANGUAGE_TABLE', $tableml) . '<br />');
         }
     }
     //remove content language?
 }
示例#3
0
 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);
         }
     }
 }