public static isValidLanguage ( $language ) : boolean | ||
$language | ||
Résultat | boolean |
/** * Get user preferred locale. * * @return string|null */ protected function getPreferredLanguage() { if (!isset($_COOKIE[self::LANG_PARAM_NAME])) { return null; } $lang = $_COOKIE[self::LANG_PARAM_NAME]; if (Tool::isValidLanguage($lang)) { return $lang; } return null; }
/** * @return string * @throws \Exception * @throws \Zend_Exception */ protected function getTableName() { if (empty($this->tableName)) { // default $this->tableName = "object_" . $this->model->getClassId(); if (!$this->model->getIgnoreLocalizedFields()) { $language = null; // check for a localized field and if they should be used for this list if (property_exists("\\Pimcore\\Model\\Object\\" . ucfirst($this->model->getClassName()), "localizedfields")) { if ($this->model->getLocale()) { if (Tool::isValidLanguage((string) $this->model->getLocale())) { $language = (string) $this->model->getLocale(); } } if (!$language && \Zend_Registry::isRegistered("Zend_Locale")) { $locale = \Zend_Registry::get("Zend_Locale"); if (Tool::isValidLanguage((string) $locale)) { $language = (string) $locale; } } if (!$language) { $language = Tool::getDefaultLanguage(); } if (!$language) { throw new \Exception("No valid language/locale set. Use \$list->setLocale() to add a language to the listing, or register a global locale"); } $this->tableName = "object_localized_" . $this->model->getClassId() . "_" . $language; } } } return $this->tableName; }
/** * @throws \Exception * @param null $language * @return string */ public function getLanguage($language = null) { if ($language) { return (string) $language; } // try to get the language from the registry try { $locale = \Zend_Registry::get("Zend_Locale"); if (Tool::isValidLanguage((string) $locale)) { return (string) $locale; } throw new \Exception("Not supported language"); } catch (\Exception $e) { return Tool::getDefaultLanguage(); } }
/** * @return null|\Pimcore\Translate|\Pimcore\Translate\Website * @throws \Zend_Exception */ public function initTranslation() { $translate = null; if (\Zend_Registry::isRegistered("Zend_Translate")) { $t = \Zend_Registry::get("Zend_Translate"); // this check is necessary for the case that a document is rendered within an admin request // example: send test newsletter if ($t instanceof \Pimcore\Translate) { $translate = $t; } } if (!$translate) { // setup \Zend_Translate try { $locale = \Zend_Registry::get("Zend_Locale"); $translate = new \Pimcore\Translate\Website($locale); if (Tool::isValidLanguage($locale)) { $translate->setLocale($locale); } else { \Logger::error("You want to use an invalid language which is not defined in the system settings: " . $locale); // fall back to the first (default) language defined $languages = Tool::getValidLanguages(); if ($languages[0]) { \Logger::error("Using '" . $languages[0] . "' as a fallback, because the language '" . $locale . "' is not defined in system settings"); $translate = new \Pimcore\Translate\Website($languages[0]); // reinit with new locale $translate->setLocale($languages[0]); } else { throw new \Exception("You have not defined a language in the system settings (Website -> Frontend-Languages), please add at least one language."); } } // register the translator in \Zend_Registry with the key "\Zend_Translate" to use the translate helper for \Zend_View \Zend_Registry::set("Zend_Translate", $translate); } catch (\Exception $e) { \Logger::error("initialization of Pimcore_Translate failed"); \Logger::error($e); } } return $translate; }
public function gridGetColumnConfigAction() { if ($this->getParam("id")) { $class = Object\ClassDefinition::getById($this->getParam("id")); } else { if ($this->getParam("name")) { $class = Object\ClassDefinition::getByName($this->getParam("name")); } } $gridType = "search"; if ($this->getParam("gridtype")) { $gridType = $this->getParam("gridtype"); } $objectId = $this->getParam("objectId"); if ($objectId) { $fields = Object\Service::getCustomGridFieldDefinitions($class->getId(), $objectId); } if (!$fields) { $fields = $class->getFieldDefinitions(); } $types = array(); if ($this->getParam("types")) { $types = explode(",", $this->getParam("types")); } // grid config $gridConfig = array(); if ($objectId) { $configFiles["configFileClassUser"] = PIMCORE_CONFIGURATION_DIRECTORY . "/object/grid/" . $this->getParam("objectId") . "_" . $class->getId() . "-user_" . $this->getUser()->getId() . ".psf"; $configFiles["configFileUser"] = PIMCORE_CONFIGURATION_DIRECTORY . "/object/grid/" . $this->getParam("objectId") . "-user_" . $this->getUser()->getId() . ".psf"; foreach ($configFiles as $configFile) { if (is_file($configFile)) { $gridConfig = Tool\Serialize::unserialize(file_get_contents($configFile)); if (is_array($gridConfig) && array_key_exists("classId", $gridConfig)) { if ($gridConfig["classId"] == $class->getId()) { break; } else { $gridConfig = array(); } } else { break; } } } } $localizedFields = array(); $objectbrickFields = array(); foreach ($fields as $key => $field) { if ($field instanceof Object\ClassDefinition\Data\Localizedfields) { $localizedFields[] = $field; } else { if ($field instanceof Object\ClassDefinition\Data\Objectbricks) { $objectbrickFields[] = $field; } } } $availableFields = array(); $systemColumns = array("id", "fullpath", "published", "creationDate", "modificationDate", "filename", "classname"); if (empty($gridConfig)) { $count = 0; if (!$this->getParam("no_system_columns")) { $vis = $class->getPropertyVisibility(); foreach ($systemColumns as $sc) { $key = $sc; if ($key == "fullpath") { $key = "path"; } if (empty($types) && ($vis[$gridType][$key] || $gridType == "all")) { $availableFields[] = array("key" => $sc, "type" => "system", "label" => $sc, "position" => $count); $count++; } } } $includeBricks = !$this->getParam("no_brick_columns"); foreach ($fields as $key => $field) { if ($field instanceof Object\ClassDefinition\Data\Localizedfields) { foreach ($field->getFieldDefinitions() as $fd) { if (empty($types) || in_array($fd->getFieldType(), $types)) { $fieldConfig = $this->getFieldGridConfig($fd, $gridType, $count); if (!empty($fieldConfig)) { $availableFields[] = $fieldConfig; $count++; } } } } else { if ($field instanceof Object\ClassDefinition\Data\Objectbricks && $includeBricks) { if (in_array($field->getFieldType(), $types)) { $fieldConfig = $this->getFieldGridConfig($field, $gridType, $count); if (!empty($fieldConfig)) { $availableFields[] = $fieldConfig; $count++; } } else { $allowedTypes = $field->getAllowedTypes(); if (!empty($allowedTypes)) { foreach ($allowedTypes as $t) { $brickClass = Object\Objectbrick\Definition::getByKey($t); $brickFields = $brickClass->getFieldDefinitions(); if (!empty($brickFields)) { foreach ($brickFields as $bf) { $fieldConfig = $this->getFieldGridConfig($bf, $gridType, $count, false, $t . "~"); if (!empty($fieldConfig)) { $availableFields[] = $fieldConfig; $count++; } } } } } } } else { if (empty($types) || in_array($field->getFieldType(), $types)) { $fieldConfig = $this->getFieldGridConfig($field, $gridType, $count, !empty($types)); if (!empty($fieldConfig)) { $availableFields[] = $fieldConfig; $count++; } } } } } } else { $savedColumns = $gridConfig['columns']; foreach ($savedColumns as $key => $sc) { if (!$sc['hidden']) { if (in_array($key, $systemColumns)) { $colConfig = array("key" => $key, "type" => "system", "label" => $key, "position" => $sc['position']); if (isset($sc['width'])) { $colConfig['width'] = $sc['width']; } $availableFields[] = $colConfig; } else { $keyParts = explode("~", $key); if (substr($key, 0, 1) == "~") { // not needed for now // $type = $keyParts[1]; // $field = $keyParts[2]; // $keyid = $keyParts[3]; } else { if (count($keyParts) > 1) { $brick = $keyParts[0]; $key = $keyParts[1]; $brickClass = Object\Objectbrick\Definition::getByKey($brick); $fd = $brickClass->getFieldDefinition($key); if (!empty($fd)) { $fieldConfig = $this->getFieldGridConfig($fd, $gridType, $sc['position'], true, $brick . "~"); if (!empty($fieldConfig)) { if (isset($sc['width'])) { $fieldConfig['width'] = $sc['width']; } $availableFields[] = $fieldConfig; } } } else { $fd = $class->getFieldDefinition($key); //if not found, look for localized fields if (empty($fd)) { foreach ($localizedFields as $lf) { $fd = $lf->getFieldDefinition($key); if (!empty($fd)) { break; } } } if (!empty($fd)) { $fieldConfig = $this->getFieldGridConfig($fd, $gridType, $sc['position'], true); if (!empty($fieldConfig)) { if (isset($sc['width'])) { $fieldConfig['width'] = $sc['width']; } $availableFields[] = $fieldConfig; } } } } } } } } usort($availableFields, function ($a, $b) { if ($a["position"] == $b["position"]) { return 0; } return $a["position"] < $b["position"] ? -1 : 1; }); $config = \Pimcore\Config::getSystemConfig(); $frontendLanguages = Tool\Admin::reorderWebsiteLanguages(\Pimcore\Tool\Admin::getCurrentUser(), $config->general->validLanguages); if ($frontendLanguages) { $language = explode(',', $frontendLanguages)[0]; } else { $language = $this->getLanguage(); } if (!Tool::isValidLanguage($language)) { $validLanguages = Tool::getValidLanguages(); $language = $validLanguages[0]; } if (!empty($gridConfig) && !empty($gridConfig['language'])) { $language = $gridConfig['language']; } $this->_helper->json(array("sortinfo" => $gridConfig['sortinfo'], "language" => $language, "availableFields" => $availableFields, "onlyDirectChildren" => $gridConfig['onlyDirectChildren'], "pageSize" => $gridConfig['pageSize'])); }
/** * @param $language * @param bool $useFrontendLanguages * @return $this * @throws \Zend_Exception */ public function setLanguage($language, $useFrontendLanguages = false) { if (\Zend_Locale::isLocale($language, true)) { $locale = new \Zend_Locale($language); } else { $locale = new \Zend_Locale("en"); } if ($useFrontendLanguages) { // check if given language is a valid language if (!Tool::isValidLanguage($locale)) { return; } \Zend_Registry::set("Zend_Locale", $locale); } else { // check if given language is installed if not => skip if (!in_array((string) $locale->getLanguage(), AdminTool::getLanguages())) { return; } \Zend_Registry::set("Zend_Locale", $locale); if (\Zend_Registry::isRegistered("Zend_Translate")) { $t = \Zend_Registry::get("Zend_Translate"); if ((string) $locale != (string) $t->getLocale()) { $languageFile = AdminTool::getLanguageFile($locale); $t->addTranslation($languageFile, $locale); $t->setLocale($locale); } } } $this->language = (string) $locale; $this->view->language = $this->getLanguage(); return $this; }
/** * @param $locale * @param $messageId * @return mixed */ protected function createEmptyTranslation($locale, $messageId) { $messageIdOriginal = $messageId; $messageId = mb_strtolower($messageId); // don't create translation if it's just empty if (array_key_exists($messageId, $this->_translate[$locale])) { return; } $class = self::getBackend(); // no translation found create key if (Tool::isValidLanguage($locale)) { try { $t = $class::getByKey($messageId); $t->addTranslation($locale, ""); } catch (\Exception $e) { $t = new $class(); $t->setKey($messageId); // add all available languages $availableLanguages = (array) Tool::getValidLanguages(); foreach ($availableLanguages as $language) { $t->addTranslation($language, ""); } } $t->save(); } // put it into the store, otherwise when there are more calls to the same key during one process // the key would be inserted/updated several times, what would be redundant $this->_translate[$locale][$messageId] = $messageIdOriginal; }
public function wordExportAction() { //error_reporting(E_ERROR); //ini_set("display_errors", "off"); $id = $this->getParam("id"); $data = \Zend_Json::decode($this->getParam("data")); $source = $this->getParam("source"); $exportFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/" . $id . ".html"; if (!is_file($exportFile)) { /*file_put_contents($exportFile, '<!DOCTYPE html>' . "\n" . '<html> <head> <style type="text/css">' . file_get_contents(PIMCORE_PATH . "/static/css/word-export.css") . '</style> </head> <body> ');*/ File::put($exportFile, '<style type="text/css">' . file_get_contents(PIMCORE_PATH . "/static6/css/word-export.css") . '</style>'); } foreach ($data as $el) { try { $element = Element\Service::getElementById($el["type"], $el["id"]); $output = ""; // check supported types (subtypes) if (!in_array($element->getType(), array("page", "snippet", "email", "object"))) { continue; } if ($element instanceof Element\ElementInterface) { $output .= '<h1 class="element-headline">' . ucfirst($element->getType()) . " - " . $element->getFullPath() . ' (ID: ' . $element->getId() . ')</h1>'; } if ($element instanceof Document\PageSnippet) { if ($element instanceof Document\Page) { $structuredDataEmpty = true; $structuredData = ' <table border="1" cellspacing="0" cellpadding="5"> <tr> <td colspan="2"><span style="color:#cc2929;font-weight: bold;">Structured Data</span></td> </tr> '; if ($element->getTitle()) { $structuredData .= '<tr> <td><span style="color:#cc2929;">Title</span></td> <td>' . $element->getTitle() . ' </td> </tr>'; $structuredDataEmpty = false; } if ($element->getDescription()) { $structuredData .= '<tr> <td><span style="color:#cc2929;">Description</span></td> <td>' . $element->getDescription() . ' </td> </tr>'; $structuredDataEmpty = false; } if ($element->getKeywords()) { $structuredData .= '<tr> <td><span style="color:#cc2929;">Keywords</span></td> <td>' . $element->getKeywords() . ' </td> </tr>'; $structuredDataEmpty = false; } if ($element->getProperty("navigation_name")) { $structuredData .= '<tr> <td><span style="color:#cc2929;">Navigation</span></td> <td>' . $element->getProperty("navigation_name") . ' </td> </tr>'; $structuredDataEmpty = false; } $structuredData .= '</table>'; if (!$structuredDataEmpty) { $output .= $structuredData; } } // we need to set the parameter "pimcore_admin" here to be able to render unpublished documents $reqBak = $_REQUEST; $_REQUEST["pimcore_admin"] = true; $html = Document\Service::render($element, array(), false); $_REQUEST = $reqBak; // set the request back to original $html = preg_replace("@</?(img|meta|div|section|aside|article|body|bdi|bdo|canvas|embed|footer|head|header|html)([^>]+)?>@", "", $html); $html = preg_replace('/<!--(.*)-->/Uis', '', $html); include_once "simple_html_dom.php"; $dom = str_get_html($html); if ($dom) { // remove containers including their contents $elements = $dom->find("form,script,style,noframes,noscript,object,area,mapm,video,audio,iframe,textarea,input,select,button,"); if ($elements) { foreach ($elements as $el) { $el->outertext = ""; } } $clearText = function ($string) { $string = str_replace("\r\n", "", $string); $string = str_replace("\n", "", $string); $string = str_replace("\r", "", $string); $string = str_replace("\t", "", $string); $string = preg_replace('/&[a-zA-Z0-9]+;/', '', $string); // remove html entities $string = preg_replace('#[ ]+#', '', $string); return $string; }; // remove empty tags (where it matters) $elements = $dom->find("a, li"); if ($elements) { foreach ($elements as $el) { $string = $clearText($el->plaintext); if (empty($string)) { $el->outertext = ""; } } } // replace links => links get [Linktext] $elements = $dom->find("a"); if ($elements) { foreach ($elements as $el) { $string = $clearText($el->plaintext); if (!empty($string)) { $el->outertext = "[" . $el->plaintext . "]"; } else { $el->outertext = ""; } } } $html = $dom->save(); $dom->clear(); unset($dom); // force closing tags (simple_html_dom doesn't seem to support this anymore) $doc = new \DOMDocument(); libxml_use_internal_errors(true); $doc->loadHTML('<?xml encoding="UTF-8"><article>' . $html . "</article>"); libxml_clear_errors(); $html = $doc->saveHTML(); $bodyStart = strpos($html, "<body>") + 6; $bodyEnd = strpos($html, "</body>"); if ($bodyStart && $bodyEnd) { $html = substr($html, $bodyStart, $bodyEnd - $bodyStart); } $output .= $html; } } else { if ($element instanceof Object\Concrete) { $hasContent = false; if ($fd = $element->getClass()->getFieldDefinition("localizedfields")) { $definitions = $fd->getFielddefinitions(); $locale = new \Zend_Locale(str_replace("-", "_", $source)); if (Tool::isValidLanguage((string) $locale)) { $locale = (string) $locale; } else { $locale = $locale->getLanguage(); } $output .= ' <table border="1" cellspacing="0" cellpadding="2"> <tr> <td colspan="2"><span style="color:#cc2929;font-weight: bold;">Localized Data</span></td> </tr> '; foreach ($definitions as $definition) { // check allowed datatypes if (!in_array($definition->getFieldtype(), array("input", "textarea", "wysiwyg"))) { continue; } $content = $element->{"get" . ucfirst($definition->getName())}($locale); if (!empty($content)) { $output .= ' <tr> <td><span style="color:#cc2929;">' . $definition->getTitle() . ' (' . $definition->getName() . ')<span></td> <td>' . $content . ' </td> </tr> '; $hasContent = true; } } $output .= '</table>'; } if (!$hasContent) { $output = ""; // there's no content in the object, so reset all contents and do not inclide it in the export } } } // append contents if (!empty($output)) { $f = fopen($exportFile, "a+"); fwrite($f, $output); fclose($f); } } catch (\Exception $e) { \Logger::error("Word Export: " . $e->getMessage()); \Logger::error($e); } } $this->_helper->json(array("success" => true)); }