예제 #1
0
 /**
  * @param null $data
  * @param $locale
  * @param array $options
  * @return array
  */
 protected function _loadTranslationData($data, $locale, array $options = array())
 {
     $locale = (string) $locale;
     $tmpKeyParts = explode("\\", self::getBackend());
     $cacheKey = "Translate_" . array_pop($tmpKeyParts) . "_data_" . $locale;
     if (!($data = Cache::load($cacheKey))) {
         $data = array("__pimcore_dummy" => "only_a_dummy");
         $listClass = self::getBackend() . "\\Listing";
         $list = new $listClass();
         if ($list->isCacheable()) {
             $list->setCondition("language = ?", array($locale));
             $translations = $list->loadRaw();
             foreach ($translations as $translation) {
                 $data[mb_strtolower($translation["key"])] = Tool\Text::removeLineBreaks($translation["text"]);
             }
             Cache::save($data, $cacheKey, array("translator", "translator_website", "translate"), null, 999);
             $this->isCacheable = true;
         } else {
             $this->isCacheable = false;
         }
     }
     $this->_translate[$locale] = $data;
     return $this->_translate;
 }
예제 #2
0
 /**
  * @return string
  */
 public function getTitle()
 {
     return \Pimcore\Tool\Text::removeLineBreaks($this->title);
 }
예제 #3
0
 /**
  * IMPORTER
  */
 public function importUploadAction()
 {
     $data = file_get_contents($_FILES["Filedata"]["tmp_name"]);
     $data = Tool\Text::convertToUTF8($data);
     $importFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_" . $this->getParam("id");
     File::put($importFile, $data);
     $importFileOriginal = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_" . $this->getParam("id") . "_original";
     File::put($importFileOriginal, $data);
     $this->_helper->json(array("success" => true), false);
     // set content-type to text/html, otherwise (when application/json is sent) chrome will complain in
     // Ext.form.Action.Submit and mark the submission as failed
     $this->getResponse()->setHeader("Content-Type", "text/html");
 }
예제 #4
0
 /**
  * Imports translations from a csv file
  * The CSV file has to have the same format as an Pimcore translation-export-file
  *
  * @static
  * @param $file - path to the csv file
  * @param bool $replaceExistingTranslations
  * @throws \Exception
  */
 public static function importTranslationsFromFile($file, $replaceExistingTranslations = true, $languages = null)
 {
     $delta = [];
     if (is_readable($file)) {
         if (!$languages || empty($languages) || !is_array($languages)) {
             $languages = Tool::getValidLanguages();
         }
         //read import data
         $tmpData = file_get_contents($file);
         //replace magic excel bytes
         $tmpData = str_replace("", "", $tmpData);
         //convert to utf-8 if needed
         $tmpData = Tool\Text::convertToUTF8($tmpData);
         //store data for further usage
         $importFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_translations";
         File::put($importFile, $tmpData);
         $importFileOriginal = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_translations_original";
         File::put($importFileOriginal, $tmpData);
         // determine csv type
         $dialect = Tool\Admin::determineCsvDialect(PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_translations_original");
         //read data
         if (($handle = fopen(PIMCORE_SYSTEM_TEMP_DIRECTORY . "/import_translations", "r")) !== false) {
             while (($rowData = fgetcsv($handle, 0, $dialect->delimiter, $dialect->quotechar, $dialect->escapechar)) !== false) {
                 $data[] = $rowData;
             }
             fclose($handle);
         }
         //process translations
         if (is_array($data) and count($data) > 1) {
             $keys = $data[0];
             // remove wrong quotes in some export/import constellations
             $keys = array_map(function ($value) {
                 return trim($value, '""');
             }, $keys);
             $data = array_slice($data, 1);
             foreach ($data as $row) {
                 $keyValueArray = [];
                 for ($counter = 0; $counter < count($row); $counter++) {
                     $rd = str_replace("&quot;", '"', $row[$counter]);
                     $keyValueArray[$keys[$counter]] = $rd;
                 }
                 $textKey = $keyValueArray["key"];
                 if ($textKey) {
                     $t = static::getByKey($textKey, true);
                     $dirty = false;
                     foreach ($keyValueArray as $key => $value) {
                         if (in_array($key, $languages)) {
                             $currentTranslation = $t->getTranslation($key);
                             if ($replaceExistingTranslations) {
                                 $t->addTranslation($key, $value);
                                 if ($currentTranslation != $value) {
                                     $dirty = true;
                                 }
                             } else {
                                 if (!$t->getTranslation($key)) {
                                     $t->addTranslation($key, $value);
                                     if ($currentTranslation != $value) {
                                         $dirty = true;
                                     }
                                 } elseif ($t->getTranslation($key) != $value && $value) {
                                     $delta[] = ["lg" => $key, "key" => $textKey, "text" => $t->getTranslation($key), "csv" => $value];
                                 }
                             }
                         }
                     }
                     if ($dirty) {
                         if ($keyValueArray['creationDate']) {
                             $t->setCreationDate($keyValueArray['creationDate']);
                         }
                         $t->setModificationDate(time());
                         //ignore modificationDate from file
                         $t->save();
                     }
                 }
             }
             Model\Translation\AbstractTranslation::clearDependentCache();
         } else {
             throw new \Exception("less than 2 rows of data - nothing to import");
         }
     } else {
         throw new \Exception("{$file} is not readable");
     }
     return $delta;
 }
예제 #5
0
 /**
  * @param null $page
  * @param null $path
  * @return bool|string
  * @throws \Exception
  */
 public function getText($page = null, $path = null)
 {
     $path = $path ? $this->preparePath($path) : $this->path;
     if ($page || parent::isFileTypeSupported($path)) {
         // for per page extraction we have to convert the document to PDF and extract the text via ghostscript
         return parent::getText($page, $this->getPdf($path));
     } elseif (File::getFileExtension($path)) {
         // if we want to get the text of the whole document, we can use libreoffices text export feature
         $cmd = self::getLibreOfficeCli() . " --headless --nologo --nofirststartwizard --norestore --convert-to txt:Text --outdir " . escapeshellarg(PIMCORE_TEMPORARY_DIRECTORY) . " " . escapeshellarg($path);
         $out = Console::exec($cmd, null, 240);
         Logger::debug("LibreOffice Output was: " . $out);
         $tmpName = PIMCORE_TEMPORARY_DIRECTORY . "/" . preg_replace("/\\." . File::getFileExtension($path) . "\$/", ".txt", basename($path));
         if (file_exists($tmpName)) {
             $text = file_get_contents($tmpName);
             $text = \Pimcore\Tool\Text::convertToUTF8($text);
             unlink($tmpName);
             return $text;
         } else {
             $message = "Couldn't convert document to PDF: " . $path . " with the command: '" . $cmd . "' - now trying to get the text out of the PDF ...";
             Logger::error($message);
             return parent::getText(null, $this->getPdf($path));
         }
     }
     return "";
     // default empty string
 }
예제 #6
0
 /**
  * @param Object\Concrete $object
  * @return string
  */
 public function preGetData($object, $params = array())
 {
     $data = "";
     if ($object instanceof Object\Concrete) {
         $data = $object->{$this->getName()};
     } else {
         if ($object instanceof Object\Localizedfield || $object instanceof Object\Classificationstore) {
             $data = $params["data"];
         } else {
             if ($object instanceof Object\Fieldcollection\Data\AbstractData) {
                 $data = $object->{$this->getName()};
             } else {
                 if ($object instanceof Object\Objectbrick\Data\AbstractData) {
                     $data = $object->{$this->getName()};
                 }
             }
         }
     }
     return Text::wysiwygText($data);
 }
예제 #7
0
 /**
  * @param  Webservice\Data\Object\Concrete $apiElement
  * @return void
  */
 public function correctObjectRelations($apiElement, $idMapping)
 {
     if ($apiElement->elements) {
         foreach ($apiElement->elements as $el) {
             if ($el->type == "href" and $el->value["id"]) {
                 $el->value["id"] = $idMapping[$el->value["type"]][$el->value["id"]];
             } else {
                 if ($el->type == "image" and $el->value) {
                     $el->value = $idMapping["asset"][$el->value];
                 } else {
                     if ($el->type == "link" and $el->value["internal"]) {
                         $el->value["internal"] = $idMapping[$el->value["internalType"]][$el->value["internal"]];
                     } else {
                         if ($el->type == "multihref") {
                             if (is_array($el->value)) {
                                 for ($i = 0; $i < count($el->value); $i++) {
                                     $el->value[$i]["id"] = $idMapping[$el->value[$i]["type"]][$el->value[$i]["id"]];
                                 }
                             }
                         } else {
                             if ($el->type == "objects") {
                                 if (is_array($el->value)) {
                                     for ($i = 0; $i < count($el->value); $i++) {
                                         $el->value[$i]["id"] = $idMapping["object"][$el->value[$i]["id"]];
                                     }
                                 }
                             } else {
                                 if ($el->type == "wysiwyg") {
                                     $el->value = Tool\Text::replaceWysiwygTextRelationIds($idMapping, $el->value);
                                 } else {
                                     if ($el->type == "fieldcollections") {
                                         if ($el instanceof Webservice\Data\Object\Element and is_array($el->value)) {
                                             foreach ($el->value as $fieldCollectionEl) {
                                                 if (is_array($fieldCollectionEl->value)) {
                                                     foreach ($fieldCollectionEl->value as $collectionItem) {
                                                         if ($collectionItem->type == "image") {
                                                             $collectionItem->value = $idMapping["asset"][$collectionItem->value];
                                                         } else {
                                                             if ($collectionItem->type == "wysiwyg") {
                                                                 $collectionItem->value = Tool\Text::replaceWysiwygTextRelationIds($idMapping, $collectionItem->value);
                                                             } else {
                                                                 if ($collectionItem->type == "link" and $collectionItem->value["internalType"]) {
                                                                     $collectionItem->value["internal"] = $idMapping[$collectionItem->value["internalType"]][$collectionItem->value["internal"]];
                                                                 } else {
                                                                     if ($collectionItem->type == "href" and $collectionItem->value["id"]) {
                                                                         $collectionItem->value["id"] = $idMapping[$collectionItem->value["type"]][$collectionItem->value["id"]];
                                                                     } else {
                                                                         if (($collectionItem->type == "objects" or $collectionItem->type == "multihref") and is_array($collectionItem->value) and count($collectionItem->value) > 0) {
                                                                             for ($i = 0; $i < count($collectionItem->value); $i++) {
                                                                                 if ($collectionItem->value[$i]["id"]) {
                                                                                     $collectionItem->value[$i]["id"] = $idMapping[$collectionItem->value[$i]["type"]][$collectionItem->value[$i]["id"]];
                                                                                 }
                                                                             }
                                                                         }
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     } else {
                                         if ($el->type == "localizedfields") {
                                             if (is_array($el->value)) {
                                                 foreach ($el->value as $localizedDataEl) {
                                                     if ($localizedDataEl->type == "image") {
                                                         $localizedDataEl->value = $idMapping["asset"][$localizedDataEl->value];
                                                     } else {
                                                         if ($localizedDataEl->type == "wysiwyg") {
                                                             $localizedDataEl->value = Tool\Text::replaceWysiwygTextRelationIds($idMapping, $localizedDataEl->value);
                                                         } else {
                                                             if ($localizedDataEl->type == "link" and $localizedDataEl->value["internalType"]) {
                                                                 $localizedDataEl->value["internal"] = $idMapping[$localizedDataEl->value["internalType"]][$localizedDataEl->value["internal"]];
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
예제 #8
0
 public function exportAction()
 {
     $this->checkPermission("translations");
     $admin = $this->getParam("admin");
     // clear translation cache
     Translation\AbstractTranslation::clearDependentCache();
     if ($admin) {
         $list = new Translation\Admin\Listing();
     } else {
         $list = new Translation\Website\Listing();
     }
     $list->setOrder("asc");
     $list->setOrderKey("key");
     $condition = $this->getGridFilterCondition();
     if ($condition) {
         $list->setCondition($condition);
     }
     $list->load();
     $translations = array();
     $translationObjects = $list->getTranslations();
     // fill with one dummy translation if the store is empty
     if (empty($translationObjects)) {
         if ($admin) {
             $t = new Translation\Admin();
             $languages = Tool\Admin::getLanguages();
         } else {
             $t = new Translation\Website();
             $languages = Tool::getValidLanguages();
         }
         foreach ($languages as $language) {
             $t->addTranslation($language, "");
         }
         $translationObjects[] = $t;
     }
     foreach ($translationObjects as $t) {
         $translations[] = array_merge(array("key" => $t->getKey(), "creationDate" => $t->getCreationDate(), "modificationDate" => $t->getModificationDate()), $t->getTranslations());
     }
     //header column
     $columns = array_keys($translations[0]);
     if ($admin) {
         $languages = Tool\Admin::getLanguages();
     } else {
         $languages = Tool::getValidLanguages();
     }
     //add language columns which have no translations yet
     foreach ($languages as $l) {
         if (!in_array($l, $columns)) {
             $columns[] = $l;
         }
     }
     $headerRow = array();
     foreach ($columns as $key => $value) {
         $headerRow[] = '"' . $value . '"';
     }
     $csv = implode(";", $headerRow) . "\r\n";
     foreach ($translations as $t) {
         $tempRow = array();
         foreach ($columns as $key) {
             $value = $t[$key];
             //clean value of evil stuff such as " and linebreaks
             if (is_string($value)) {
                 $value = Tool\Text::removeLineBreaks($value);
                 $value = str_replace('"', '&quot;', $value);
                 $tempRow[$key] = '"' . $value . '"';
             } else {
                 $tempRow[$key] = $value;
             }
         }
         $csv .= implode(";", $tempRow) . "\r\n";
     }
     $suffix = $admin ? "admin" : "website";
     header('Content-type: text/csv; charset=UTF-8');
     header("Content-Disposition: attachment; filename=\"export_ " . $suffix . "_translations.csv\"");
     ini_set('display_errors', false);
     //to prevent warning messages in csv
     echo $csv;
     die;
 }
예제 #9
0
파일: Wysiwyg.php 프로젝트: pimcore/pimcore
 /**
  * @param $ownerDocument
  * @param array $blockedTags
  * @return array
  */
 public function getCacheTags($ownerDocument, $blockedTags = [])
 {
     return Text::getCacheTagsOfWysiwygText($this->text, $blockedTags);
 }
예제 #10
0
 public function exportAction()
 {
     $this->checkPermission("translations");
     $admin = $this->getParam("admin");
     if ($admin) {
         $class = "\\Pimcore\\Model\\Translation\\Admin";
     } else {
         $class = "\\Pimcore\\Model\\Translation\\Website";
     }
     $tableName = call_user_func($class . "\\Dao::getTableName");
     // clear translation cache
     Translation\AbstractTranslation::clearDependentCache();
     if ($admin) {
         $list = new Translation\Admin\Listing();
     } else {
         $list = new Translation\Website\Listing();
     }
     $joins = [];
     $list->setOrder("asc");
     $list->setOrderKey($tableName . ".key", false);
     $condition = $this->getGridFilterCondition($tableName);
     if ($condition) {
         $list->setCondition($condition);
     }
     $filters = $this->getGridFilterCondition($tableName, true);
     if ($filters) {
         $joins = array_merge($joins, $filters["joins"]);
     }
     $this->extendTranslationQuery($joins, $list, $tableName, $filters);
     $list->load();
     $translations = [];
     $translationObjects = $list->getTranslations();
     // fill with one dummy translation if the store is empty
     if (empty($translationObjects)) {
         if ($admin) {
             $t = new Translation\Admin();
             $languages = Tool\Admin::getLanguages();
         } else {
             $t = new Translation\Website();
             $languages = $this->getUser()->getAllowedLanguagesForViewingWebsiteTranslations();
         }
         foreach ($languages as $language) {
             $t->addTranslation($language, "");
         }
         $translationObjects[] = $t;
     }
     foreach ($translationObjects as $t) {
         $translations[] = array_merge(["key" => $t->getKey(), "creationDate" => $t->getCreationDate(), "modificationDate" => $t->getModificationDate()], $t->getTranslations());
     }
     //header column
     $columns = array_keys($translations[0]);
     if ($admin) {
         $languages = Tool\Admin::getLanguages();
     } else {
         $languages = $this->getUser()->getAllowedLanguagesForViewingWebsiteTranslations();
     }
     //add language columns which have no translations yet
     foreach ($languages as $l) {
         if (!in_array($l, $columns)) {
             $columns[] = $l;
         }
     }
     //remove invalid languages
     foreach ($columns as $key => $column) {
         if (strtolower(trim($column)) != 'key' && !in_array($column, $languages)) {
             unset($columns[$key]);
         }
     }
     $columns = array_values($columns);
     $headerRow = [];
     foreach ($columns as $key => $value) {
         $headerRow[] = '"' . $value . '"';
     }
     $csv = implode(";", $headerRow) . "\r\n";
     foreach ($translations as $t) {
         $tempRow = [];
         foreach ($columns as $key) {
             $value = $t[$key];
             //clean value of evil stuff such as " and linebreaks
             if (is_string($value)) {
                 $value = Tool\Text::removeLineBreaks($value);
                 $value = str_replace('"', '&quot;', $value);
                 $tempRow[$key] = '"' . $value . '"';
             } else {
                 $tempRow[$key] = $value;
             }
         }
         $csv .= implode(";", $tempRow) . "\r\n";
     }
     $suffix = $admin ? "admin" : "website";
     header('Content-Encoding: UTF-8');
     header('Content-type: text/csv; charset=UTF-8');
     header("Content-Disposition: attachment; filename=\"export_ " . $suffix . "_translations.csv\"");
     ini_set('display_errors', false);
     //to prevent warning messages in csv
     echo "";
     echo $csv;
     die;
 }