/**
  * @param $varValue
  * @param string $strForceLanguage
  * @return string
  */
 public function translateValue($varValue, $strForceLanguage = '')
 {
     // Return value if it is already translated
     if (!is_numeric($varValue)) {
         return $varValue;
     }
     $arrLanguages = array();
     // If force language is set than add it as first language param
     if (strlen($strForceLanguage)) {
         $arrLanguages[] = $strForceLanguage;
     }
     // Add current langauge and default language to languages array
     $arrLanguages[] = $GLOBALS['TL_LANGUAGE'];
     $arrLanguages[] = 'en';
     // Get translation by current language and if it doesn't exist use the english translation
     foreach ($arrLanguages as $strLanguage) {
         $objTranslation = TranslationFieldsModel::findOneByFidAndLanguage($varValue, $strLanguage);
         if ($objTranslation !== null) {
             return $objTranslation->content;
         }
     }
     // Get any translation
     $objTranslation = TranslationFieldsModel::findOneByFid($varValue);
     if ($objTranslation !== null) {
         return $objTranslation->content;
     }
     return '';
 }
 /**
  * @param $table
  * @param $field
  */
 public static function convertTranslationField($table, $field)
 {
     $backup = $field . '_backup';
     $objDatabase = Database::getInstance();
     /* @var $objLanguages Languages */
     $objLanguages = \System::getContainer()->get('craffft.translation_fields.service.languages');
     // Backup the original column and then change the column type
     if (!$objDatabase->fieldExists($backup, $table, true)) {
         $objDatabase->query("ALTER TABLE `{$table}` ADD `{$backup}` text NULL");
         $objDatabase->query("UPDATE `{$table}` SET `{$backup}`=`{$field}`");
         $objDatabase->query("ALTER TABLE `{$table}` CHANGE `{$field}` `{$field}` int(10) unsigned NOT NULL default '0'");
         $objDatabase->query("UPDATE `{$table}` SET `{$field}`='0'");
     }
     $objRow = $objDatabase->query("SELECT id, {$backup} FROM {$table} WHERE {$backup}!=''");
     while ($objRow->next()) {
         if (is_numeric($objRow->{$backup})) {
             $intFid = $objRow->{$backup};
         } else {
             if (strlen($objRow->{$backup}) > 0) {
                 $intFid = TranslationFieldsModel::saveValuesAndReturnFid($objLanguages->getLanguagesWithValue($objRow->{$backup}));
             } else {
                 $intFid = 0;
             }
         }
         $objDatabase->prepare("UPDATE {$table} SET {$field}=? WHERE id=?")->execute($intFid, $objRow->id);
     }
 }
Beispiel #3
0
 /**
  * @param $strField
  */
 protected function addTranslationContentToField($strField)
 {
     if ($this->Config->get($strField) === null) {
         $arrValues = array();
         System::loadLanguageFile('tl_email', 'de', true);
         $arrValues['de'] = $GLOBALS['TL_LANG']['tl_email']['defaultContents'][$strField];
         System::loadLanguageFile('tl_email', 'en', true);
         $arrValues['en'] = $GLOBALS['TL_LANG']['tl_email']['defaultContents'][$strField];
         // Load translation file by current language
         System::loadLanguageFile('tl_email', null, true);
         $this->Config->persist($strField, TranslationFieldsModel::saveValuesAndReturnFid($arrValues));
     }
 }
 /**
  * @return string
  */
 public function generate()
 {
     $type = $this->hideInput ? 'password' : 'text';
     // Get post array
     $arrPost = \Input::post($this->strName);
     // Get languages array with values
     $this->varValue = TranslationFieldsModel::getTranslationsByFid($this->varValue);
     /* @var $objLanguages Languages */
     $objLanguages = \System::getContainer()->get('craffft.translation_fields.service.languages');
     $arrLngInputs = $objLanguages->getLanguageKeys();
     $arrFields = array();
     foreach ($arrLngInputs as $i => $strLanguage) {
         $arrFields[] = sprintf('<div class="tf_field_wrap tf_field_wrap_%s%s"><input type="%s" name="%s[%s]" id="ctrl_%s" class="tf_field tl_text" value="%s"%s onfocus="Backend.getScrollOffset()"></div>', $strLanguage, $i > 0 ? ' hide' : '', $type, $this->strName, $strLanguage, $this->strId . '_' . $strLanguage, specialchars(isset($arrPost[$strLanguage]) && $arrPost[$strLanguage] !== null ? $arrPost[$strLanguage] : @$this->varValue[$strLanguage]), $i > 0 ? WidgetUtil::getCleanedAttributes($this->getAttributes()) : $this->getAttributes());
     }
     // Get language button
     $strLngButton = WidgetUtil::getCurrentTranslationLanguageButton();
     // Get language list
     $strLngList = WidgetUtil::getTranslationLanguagesList(is_array($this->varValue) ? $this->varValue : array());
     return sprintf('<div id="ctrl_%s" class="tf_wrap tf_text_wrap%s">%s%s%s</div>%s', $this->strId, $this->strClass != '' ? ' ' . $this->strClass : '', implode(' ', $arrFields), $strLngList, $strLngButton, $this->wizard);
 }
 /**
  * @param $dc
  */
 public static function deleteDataRecord($dc)
 {
     // If this is not the backend than return
     if (TL_MODE != 'BE') {
         return;
     }
     // Check if there is an active record
     if ($dc instanceof DataContainer && $dc->activeRecord) {
         $intId = $dc->activeRecord->id;
         $strTable = $dc->table;
         $strModel = '\\' . Model::getClassFromTable($strTable);
         // Return if the class does not exist (#9 thanks to tsarma)
         if (!class_exists($strModel)) {
             return;
         }
         // Get object from model
         $objModel = $strModel::findByPk($intId);
         if ($objModel !== null) {
             $arrData = $objModel->row();
             if (is_array($arrData) && count($arrData) > 0) {
                 // Load current data container
                 Controller::loadDataContainer($strTable);
                 // Get tl_undo data
                 $objUndo = Database::getInstance()->prepare("SELECT * FROM tl_undo WHERE fromTable=? ORDER BY id DESC")->limit(1)->execute($dc->table);
                 $arrSet = $objUndo->row();
                 // Deserialize tl_undo data
                 $arrSet['data'] = deserialize($arrSet['data']);
                 foreach ($arrData as $strField => $varValue) {
                     $strInputType = $GLOBALS['TL_DCA'][$strTable]['fields'][$strField]['inputType'];
                     switch ($strInputType) {
                         case 'TranslationInputUnit':
                         case 'TranslationTextArea':
                         case 'TranslationTextField':
                             $intFid = $varValue;
                             if ($strInputType == 'TranslationInputUnit') {
                                 $arrDeserialized = deserialize($varValue);
                                 $intFid = $arrDeserialized['value'];
                             }
                             // Get translation values
                             $objTranslation = TranslationFieldsModel::findByFid($intFid);
                             if ($objTranslation !== null) {
                                 while ($objTranslation->next()) {
                                     $t = TranslationFieldsModel::getTable();
                                     // Add cross table record to undo data
                                     $arrSet['data'][$t][] = $objTranslation->row();
                                     // Delete translation
                                     $objTranslation->delete();
                                 }
                             }
                             break;
                     }
                 }
                 // Serialize tl_undo data
                 $arrSet['data'] = serialize($arrSet['data']);
                 // Update tl_undo
                 Database::getInstance()->prepare("UPDATE tl_undo %s WHERE id=?")->set($arrSet)->execute($objUndo->id);
             }
         }
     }
 }
 /**
  * @return string
  */
 public function generate()
 {
     $arrUnits = array();
     foreach ($this->arrUnits as $arrUnit) {
         $arrUnits[] = sprintf('<option value="%s"%s>%s</option>', specialchars($arrUnit['value']), $this->isSelected($arrUnit), $arrUnit['label']);
     }
     if (!is_array($this->varValue)) {
         $this->varValue = array('value' => $this->varValue);
     }
     // Get languages array with values
     $this->varValue['value'] = TranslationFieldsModel::getTranslationsByFid($this->varValue['value']);
     /* @var $objLanguages Languages */
     $objLanguages = \System::getContainer()->get('craffft.translation_fields.service.languages');
     $arrLngInputs = $objLanguages->getLanguageKeys();
     $arrFields = array();
     foreach ($arrLngInputs as $i => $strLanguage) {
         $arrFields[] = sprintf('<div class="tf_field_wrap tf_field_wrap_%s%s"><input type="text" name="%s[value][%s]" id="ctrl_%s" class="tf_field tl_text_unit" value="%s"%s onfocus="Backend.getScrollOffset()"></div>', $strLanguage, $i > 0 ? ' hide' : '', $this->strName, $strLanguage, $this->strId . '_' . $strLanguage, specialchars(@$this->varValue['value'][$strLanguage]), $i > 0 ? WidgetUtil::getCleanedAttributes($this->getAttributes()) : $this->getAttributes());
     }
     $strUnit = sprintf('<select name="%s[unit]" class="tl_select_unit" onfocus="Backend.getScrollOffset()">%s</select>', $this->strName, implode('', $arrUnits));
     // Get language button
     $strLngButton = WidgetUtil::getCurrentTranslationLanguageButton();
     // Get language list
     $strLngList = WidgetUtil::getTranslationLanguagesList(isset($this->varValue['value']) && is_array($this->varValue['value']) ? $this->varValue['value'] : array());
     return sprintf('<div id="ctrl_%s" class="tf_wrap tf_text_unit_wrap%s">%s%s%s</div> %s%s', $this->strId, $this->strClass != '' ? ' ' . $this->strClass : '', implode(' ', $arrFields), $strLngList, $strLngButton, $strUnit, $this->wizard);
 }
 /**
  * @return string
  * @throws \Exception
  */
 public function generate()
 {
     // Get post array
     $arrPost = \Input::post($this->strName);
     // Get languages array with values
     $this->varValue = TranslationFieldsModel::getTranslationsByFid($this->varValue);
     /* @var $objLanguages Languages */
     $objLanguages = \System::getContainer()->get('craffft.translation_fields.service.languages');
     $arrLngInputs = $objLanguages->getLanguageKeys();
     $arrFields = array();
     foreach ($arrLngInputs as $i => $strLanguage) {
         $strRte = $GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['eval']['rte'];
         $key = 'ctrl_' . $this->strId . '_' . $strLanguage;
         $strScript = $this->getRteScriptByTranslatedField($strRte, $key);
         $arrFields[] = sprintf('<div class="tf_field_wrap tf_field_wrap_%s%s"><textarea name="%s[%s]" id="%s" class="tf_field tl_textarea" rows="%s" cols="%s"%s onfocus="Backend.getScrollOffset()">%s</textarea>%s</div>', $strLanguage, $i > 0 ? ' hide' : '', $this->strName, $strLanguage, $key, $this->intRows, $this->intCols, $i > 0 ? WidgetUtil::getCleanedAttributes($this->getAttributes()) : $this->getAttributes(), specialchars(isset($arrPost[$strLanguage]) && $arrPost[$strLanguage] !== null ? $arrPost[$strLanguage] : @$this->varValue[$strLanguage]), $strScript);
     }
     // Get language button
     $strLngButton = WidgetUtil::getCurrentTranslationLanguageButton();
     // Get language list
     $strLngList = WidgetUtil::getTranslationLanguagesList(is_array($this->varValue) ? $this->varValue : array());
     return sprintf('<div id="ctrl_%s" class="tf_wrap tf_textarea_wrap%s%s">%s%s%s</div>%s', $this->strId, $this->strClass != '' ? ' ' . $this->strClass : '', !empty($this->rte) ? ' rte' : '', implode(' ', $arrFields), $strLngList, $strLngButton, $this->wizard);
 }