public function addLanguage($languageToAdd) { // Check if language is not already added if (in_array($languageToAdd, Tool::getValidLanguages())) { $result = false; } else { // Read all the documents from the first language $availableLanguages = Tool::getValidLanguages(); $firstLanguageDocument = \Pimcore\Model\Document::getByPath('/' . reset($availableLanguages)); \Zend_Registry::set('SEI18N_add', 1); // Add the language main folder $document = Page::create(1, array('key' => $languageToAdd, "userOwner" => 1, "userModification" => 1, "published" => true, "controller" => 'default', "action" => 'go-to-first-child')); $document->setProperty('language', 'text', $languageToAdd); // Set the language to this folder and let it inherit to the child pages $document->setProperty('isLanguageRoot', 'text', 1, false, false); // Set as language root document $document->save(); // Add Link to other languages $t = new Keys(); $t->insert(array("document_id" => $document->getId(), "language" => $languageToAdd, "sourcePath" => $firstLanguageDocument->getFullPath())); // Lets add all the docs $this->addDocuments($firstLanguageDocument->getId(), $languageToAdd); \Zend_Registry::set('SEI18N_add', 0); $oldConfig = Config::getSystemConfig(); $settings = $oldConfig->toArray(); $languages = explode(',', $settings['general']['validLanguages']); $languages[] = $languageToAdd; $settings['general']['validLanguages'] = implode(',', $languages); $config = new \Zend_Config($settings, true); $writer = new \Zend_Config_Writer_Xml(array("config" => $config, "filename" => PIMCORE_CONFIGURATION_SYSTEM)); $writer->write(); $result = true; } return $result; }
public function addAction() { $success = false; $errorMessage = ""; // check for permission $parentDocument = Document::getById(intval($this->getParam("parentId"))); if ($parentDocument->isAllowed("create")) { $intendedPath = $parentDocument->getFullPath() . "/" . $this->getParam("key"); if (!Document\Service::pathExists($intendedPath)) { $createValues = array("userOwner" => $this->getUser()->getId(), "userModification" => $this->getUser()->getId(), "published" => false); $createValues["key"] = $this->getParam("key"); // check for a docType $docType = Document\DocType::getById(intval($this->getParam("docTypeId"))); if ($docType) { $createValues["template"] = $docType->getTemplate(); $createValues["controller"] = $docType->getController(); $createValues["action"] = $docType->getAction(); $createValues["module"] = $docType->getModule(); } else { if ($this->getParam("type") == "page" || $this->getParam("type") == "snippet" || $this->getParam("type") == "email") { $createValues["controller"] = Config::getSystemConfig()->documents->default_controller; $createValues["action"] = Config::getSystemConfig()->documents->default_action; } } switch ($this->getParam("type")) { case "page": $document = Document\Page::create($this->getParam("parentId"), $createValues, false); $document->setTitle($this->getParam('title', null)); $document->setProperty("navigation_name", "text", $this->getParam('name', null), false); $document->save(); $success = true; break; case "snippet": $document = Document\Snippet::create($this->getParam("parentId"), $createValues); $success = true; break; case "email": //ckogler $document = Document\Email::create($this->getParam("parentId"), $createValues); $success = true; break; case "link": $document = Document\Link::create($this->getParam("parentId"), $createValues); $success = true; break; case "hardlink": $document = Document\Hardlink::create($this->getParam("parentId"), $createValues); $success = true; break; case "folder": $document = Document\Folder::create($this->getParam("parentId"), $createValues); $document->setPublished(true); try { $document->save(); $success = true; } catch (\Exception $e) { $this->_helper->json(array("success" => false, "message" => $e->getMessage())); } break; default: $classname = "\\Pimcore\\Model\\Document\\" . ucfirst($this->getParam("type")); // this is the fallback for custom document types using prefixes // so we need to check if the class exists first if (!\Pimcore\Tool::classExists($classname)) { $oldStyleClass = "\\Document_" . ucfirst($this->getParam("type")); if (\Pimcore\Tool::classExists($oldStyleClass)) { $classname = $oldStyleClass; } } if (Tool::classExists($classname)) { $document = $classname::create($this->getParam("parentId"), $createValues); try { $document->save(); $success = true; } catch (\Exception $e) { $this->_helper->json(array("success" => false, "message" => $e->getMessage())); } break; } else { \Logger::debug("Unknown document type, can't add [ " . $this->getParam("type") . " ] "); } break; } } else { $errorMessage = "prevented adding a document because document with same path+key [ {$intendedPath} ] already exists"; \Logger::debug($errorMessage); } } else { $errorMessage = "prevented adding a document because of missing permissions"; \Logger::debug($errorMessage); } if ($success) { $this->_helper->json(array("success" => $success, "id" => $document->getId(), "type" => $document->getType())); } else { $this->_helper->json(array("success" => $success, "message" => $errorMessage)); } }
/** * @return bool */ public static function createTables() { $result = true; if (self::checkTables()) { return true; } try { $db = PluginLib\AbstractPlugin::getDb(); // Create table $db->query("CREATE TABLE `" . self::$_tableName . "` (\n\t\t\t\t\t\t\t\t `document_id` BIGINT(20) NOT NULL,\n\t\t\t\t\t\t\t\t `language` VARCHAR(2) DEFAULT NULL,\n\t\t\t\t\t\t\t\t `sourcePath` VARCHAR(255) DEFAULT NULL,\n\t\t\t\t\t\t\t\t PRIMARY KEY (`document_id`)\n\t\t\t\t\t\t\t\t) ENGINE=MyISAM DEFAULT CHARSET=utf8;\n\t\t\t\t\t\t\t"); $languages = Tool::getValidLanguages(); // Get list of sites $siteList = new \Pimcore\Model\Site\Listing(); $sites = $siteList->load(); if ($languages && count($languages) >= 1) { $primaryLanguage = $languages[0]; foreach ($languages as $language) { $rootIds = array(); if ($sites) { // Multisite website, add language folders for all subsites /** * @var $site Site */ foreach ($sites as $site) { $rootIds[] = $site->getRootId(); } } else { // No sites; just use the primary root $rootIds[] = 1; } foreach ($rootIds as $rootId) { $rootDocument = Document::getById($rootId); if ($rootDocument->getKey() == '') { $sourcePath = $rootDocument->getRealFullPath() . $primaryLanguage; } else { $sourcePath = $rootDocument->getRealFullPath() . '/' . $primaryLanguage; } if ($rootDocument->getKey() == '') { $rootPath = $rootDocument->getRealFullPath() . $language; } else { $rootPath = $rootDocument->getRealFullPath() . '/' . $language; } $folder = Document\Page::getByPath($rootPath); if (!$folder) { $folder = Document\Page::create($rootId, array('key' => $language, "userOwner" => 1, "userModification" => 1, "published" => true, "module" => "Multilingual", "controller" => 'default', "action" => 'go-to-first-child')); } // Also set the language property for the document $folder->setProperty('language', 'text', $language); $folder->setProperty('isLanguageRoot', 'text', 1, false, false); $folder->save(); // Create enty in plugin table, so basic link is provided $sql = "INSERT INTO " . self::$_tableName . "(document_id,language,sourcePath) VALUES(\n\t\t\t\t\t'" . $folder->getId() . "',\n\t\t\t\t\t'" . $language . "',\n\t\t\t\t\t'" . $sourcePath . "')"; $db->query($sql); } } } } catch (Exception $e) { $result = false; Logger::error("Failed to create tables: " . $e->getMessage()); } return $result; }