Example #1
0
 /**
  * Persist all the translations
  *
  * @param   array|null  $recordId Record id to just load that row
  * @param   string|null $language Language tag
  *
  * @return bool True on success
  */
 public function persistTranslations($recordId = null, $language = null)
 {
     if ($this->translate) {
         $commonData = array('contentType' => NenoContentElementTranslation::DB_STRING, 'contentId' => $this->getId(), 'content' => $this, 'state' => NenoContentElementTranslation::NOT_TRANSLATED_STATE, 'timeAdded' => new DateTime(), 'comment' => $this->comment);
         if ($language !== null) {
             $languageData = new stdClass();
             $languageData->lang_code = $language;
             $languages = array($languageData);
         } else {
             $languages = NenoHelper::getLanguages(false);
         }
         $defaultLanguage = NenoSettings::get('source_language');
         $this->translations = array();
         $strings = $this->getStrings($recordId);
         $primaryKeyData = $this->getTable()->getPrimaryKey();
         $translationmethods = NenoHelper::getTranslationMethodsByTableId($this->table->getId());
         if (!empty($strings)) {
             foreach ($strings as $string) {
                 $progressCounters = $this->getProgressCounters();
                 if (!NenoSettings::get('installation_completed')) {
                     NenoHelper::setSetupState(JText::sprintf('COM_NENO_INSTALLATION_MESSAGE_PARSING_GROUP_TABLE_FIELD_PROGRESS', $this->getTable()->getGroup()->getGroupName(), $this->getTable()->getTableName(), $this->getFieldName(), $progressCounters['processed'], $progressCounters['total']), 3);
                 }
                 if ($string['state'] == 1 || $string['state'] == 0 && NenoSettings::get('copy_unpublished', 1) || $string['state'] == -2 && NenoSettings::get('copy_trashed', 0)) {
                     foreach ($languages as $language) {
                         if ($defaultLanguage !== $language->lang_code) {
                             $commonData['language'] = $language->lang_code;
                             $commonData['string'] = $string['string'];
                             // If the string is empty or is a number, let's mark as translated.
                             if (empty($string['string']) || is_numeric($string['string'])) {
                                 $commonData['state'] = NenoContentElementTranslation::TRANSLATED_STATE;
                             } else {
                                 $commonData['state'] = NenoContentElementTranslation::NOT_TRANSLATED_STATE;
                             }
                             $translation = new NenoContentElementTranslation($commonData);
                             $sourceData = array();
                             $fieldBreakpoint = array();
                             foreach ($primaryKeyData as $primaryKey) {
                                 $field = self::getFieldByTableAndFieldName($this->getTable(), $primaryKey);
                                 $fieldData = array('field' => $field, 'value' => $string[$primaryKey]);
                                 $sourceData[] = $fieldData;
                                 $fieldBreakpoint[$primaryKey] = $string[$primaryKey];
                             }
                             // Save breakpoint into the database
                             NenoSettings::set('field_breakpoint', json_encode($fieldBreakpoint));
                             $translation->setSourceElementData($sourceData);
                             // If the translation does not exists already, let's add it
                             if ($translation->existsAlready()) {
                                 $translation = NenoContentElementTranslation::getTranslationBySourceElementData($sourceData, $language->lang_code, $this->getId());
                                 $translation->setElement($this);
                                 if ($translation->refresh()) {
                                     $translation->persist();
                                 }
                             }
                             $translationMethods = $translation->getTranslationMethods();
                             if (empty($translationMethods[$language->lang_code])) {
                                 if (!empty($translationmethods[$language->lang_code])) {
                                     $translationMethodsTr = $translationmethods[$language->lang_code];
                                     foreach ($translationMethodsTr as $translationMethodTr) {
                                         $translation->addTranslationMethod($translationMethodTr->translation_method_id);
                                     }
                                 }
                             }
                             $translation->persist();
                             $this->translations[] = $translation;
                         }
                     }
                 }
             }
             NenoSettings::set('field_breakpoint', null);
         }
     } else {
         for ($i = 0; $i < count($this->translations); $i++) {
             $translation = $this->translations[$i];
             /* @var $translation NenoContentElementTranslation */
             $translation->refresh();
             $this->translations[$i] = $translation;
         }
     }
     return true;
 }
Example #2
0
 /**
  * Persist all the translations
  *
  * @param   array|null  $recordId Record id to just load that row
  * @param   string|null $language Language tag
  *
  * @return bool True on success
  */
 public function persistTranslations($recordId = null, $language = null)
 {
     if ($this->translate) {
         $commonData = array('contentType' => NenoContentElementTranslation::DB_STRING, 'contentId' => $this->getId(), 'content' => $this, 'state' => NenoContentElementTranslation::NOT_TRANSLATED_STATE, 'timeAdded' => new DateTime(), 'comment' => $this->comment);
         if ($language != null) {
             $languageData = new stdClass();
             $languageData->lang_code = $language;
             $languages = array($languageData);
         } else {
             $languages = NenoHelper::getLanguages();
         }
         $defaultLanguage = NenoSettings::get('source_language');
         $this->translations = array();
         $strings = $this->getStrings($recordId);
         $primaryKeyData = $this->getTable()->getPrimaryKey();
         /* @var $db NenoDatabaseDriverMysqlx */
         $db = JFactory::getDbo();
         $query = $db->getQuery(true);
         $query->select(array('gtm.lang', 'gtm.translation_method_id'))->from('#__neno_content_element_tables AS t')->innerJoin('#__neno_content_element_groups AS g ON t.group_id = g.id')->innerJoin('#__neno_content_element_groups_x_translation_methods AS gtm ON gtm.group_id = g.id')->where('t.id = ' . $this->table->getId());
         $db->setQuery($query);
         $translationmethods = $db->loadObjectListMultiIndex('lang');
         if (!empty($strings)) {
             foreach ($languages as $language) {
                 if ($defaultLanguage !== $language->lang_code) {
                     $commonData['language'] = $language->lang_code;
                     foreach ($strings as $string) {
                         if ($string['state'] == 1 || $string['state'] == 0 && NenoSettings::get('copy_unpublished', 1) || $string['state'] == -2 && NenoSettings::get('copy_trashed', 0)) {
                             $commonData['string'] = $string['string'];
                             // If the string is empty or is a number, let's mark as translated.
                             if (empty($string['string']) || is_numeric($string['string'])) {
                                 $commonData['state'] = NenoContentElementTranslation::TRANSLATED_STATE;
                             } else {
                                 $commonData['state'] = NenoContentElementTranslation::NOT_TRANSLATED_STATE;
                             }
                             $translation = new NenoContentElementTranslation($commonData);
                             $sourceData = array();
                             foreach ($primaryKeyData as $primaryKey) {
                                 $field = self::getFieldByTableAndFieldName($this->getTable(), $primaryKey);
                                 $fieldData = array('field' => $field, 'value' => $string[$primaryKey]);
                                 $sourceData[] = $fieldData;
                             }
                             $translation->setSourceElementData($sourceData);
                             // If the translation does not exists already, let's add it
                             if ($translation->existsAlready()) {
                                 $translation = NenoContentElementTranslation::getTranslationBySourceElementData($sourceData, $language->lang_code, $this->getId());
                                 $translation->setElement($this);
                                 if ($translation->refresh()) {
                                     $translation->persist();
                                 }
                             }
                             $translationMethods = $translation->getTranslationMethods();
                             if (empty($translationMethods[$language->lang_code])) {
                                 $translationMethodsTr = $translationmethods[$language->lang_code];
                                 if (!empty($translationMethodsTr)) {
                                     foreach ($translationMethodsTr as $translationMethodTr) {
                                         $translation->addTranslationMethod($translationMethodTr->translation_method_id);
                                     }
                                 }
                             }
                             $translation->persist();
                             $this->translations[] = $translation;
                         }
                     }
                 }
             }
         }
     } else {
         for ($i = 0; $i < count($this->translations); $i++) {
             $translation = $this->translations[$i];
             /* @var $translation NenoContentElementTranslation */
             $translation->setState(NenoContentElementTranslation::SOURCE_CHANGED_STATE);
             $this->translations[$i] = $translation;
         }
     }
     return true;
 }