Exemplo n.º 1
0
 public static function addById($intId, $intParentId = 0)
 {
     global $_CONF;
     $objDoc = Structure::getXmlDoc($intId);
     $arrUserIds = array();
     $arrGroupIds = array();
     $arrStorageIds = array();
     $arrFeedIds = array();
     $arrLanguageIds[0] = 0;
     $arrTemplateIds[0] = 0;
     $arrTemplateFieldIds[0] = 0;
     $arrLinkFieldIds = array();
     $arrElementIds[0] = 0;
     $arrElementFieldIds = array();
     $arrElementFieldIds["link"][0] = 0;
     $arrElementFieldIds["largeText"][0] = 0;
     $intTemplateParentId = 0;
     $intElementParentId = 0;
     //*** Get structure fields from selects.
     if (Structure::hasSelect($intId)) {
         $objSelects = Structure::getSelectsById($intId);
         foreach ($objSelects as $objSelect) {
             switch ($objSelect->getType()) {
                 case "language":
                     $intId = Request::get("frm_select_{$objSelect->getId()}");
                     $arrLanguageIds[$objSelect->getLogicId()] = $intId;
                     break;
                 case "element":
                     $intId = Request::get("frm_select_{$objSelect->getId()}");
                     if ($objSelect->getLogicId() == "PARENT") {
                         $intElementParentId = $intId;
                     } else {
                         $arrElementIds[$objSelect->getLogicId()] = $intId;
                     }
                     break;
             }
         }
     }
     foreach ($objDoc->childNodes as $rootNode) {
         if ($rootNode->nodeName == "structure") {
             //*** Valid structure XML.
             switch ($rootNode->getAttribute("type")) {
                 case "template":
                     $intTemplateParentId = $intParentId;
                     break;
                 case "element":
                     $intElementParentId = $intParentId;
                     break;
             }
             foreach ($rootNode->childNodes as $logicNode) {
                 if ($logicNode->nodeName == "logic") {
                     foreach ($logicNode->childNodes as $childNode) {
                         switch ($childNode->nodeName) {
                             case "languages":
                                 //*** Add languages to the account.
                                 foreach ($childNode->childNodes as $languageNode) {
                                     $objLanguage = new ContentLanguage();
                                     $objLanguage->setAccountId($_CONF['app']['account']->getId());
                                     $objLanguage->setName($languageNode->getAttribute("name"));
                                     $objLanguage->setAbbr($languageNode->getAttribute("abbr"));
                                     $objLanguage->default = $languageNode->getAttribute("default");
                                     $objLanguage->setActive($languageNode->getAttribute("active"));
                                     $objLanguage->setSort($languageNode->getAttribute("sort"));
                                     $objLanguage->setUsername($languageNode->getAttribute("username"));
                                     $objLanguage->save();
                                     $arrLanguageIds[$languageNode->getAttribute("id")] = $objLanguage->getId();
                                     if ($languageNode->getAttribute("default") == 1) {
                                         $intDefaultLanguage = $objLanguage->getId();
                                     }
                                 }
                                 break;
                             case "templates":
                                 //*** Add templates to the account.
                                 ImpEx::importTemplates($childNode, $_CONF['app']['account']->getId(), $arrTemplateIds, $arrTemplateFieldIds, $arrLinkFieldIds, $intTemplateParentId);
                                 break;
                             case "elements":
                                 //*** Add elements to the account.
                                 ImpEx::importElements($childNode, $_CONF['app']['account']->getId(), $arrTemplateIds, $arrTemplateFieldIds, $arrElementIds, $arrElementFieldIds, $arrLinkFieldIds, $arrLanguageIds, $arrUserIds, $arrGroupIds, $arrStorageIds, $arrFeedIds, $intElementParentId);
                                 break;
                             case "aliases":
                                 //*** Add aliases to the account.
                                 foreach ($childNode->childNodes as $aliasNode) {
                                     $objAlias = new Alias();
                                     $objAlias->setAccountId($_CONF['app']['account']->getId());
                                     $objAlias->setAlias($aliasNode->getAttribute("alias"));
                                     if (array_key_exists($aliasNode->getAttribute("url"), $arrElementIds)) {
                                         $objAlias->setUrl($arrElementIds[$aliasNode->getAttribute("url")]);
                                     } else {
                                         $objAlias->setUrl(0);
                                     }
                                     if (array_key_exists($aliasNode->getAttribute("language"), $arrLanguageIds)) {
                                         $objAlias->setLanguageId($arrLanguageIds[$aliasNode->getAttribute("language")]);
                                     } else {
                                         $objAlias->setLanguageId(0);
                                     }
                                     $objAlias->setCascade($aliasNode->getAttribute("cascade"));
                                     $objAlias->setActive($aliasNode->getAttribute("active"));
                                     $objAlias->setSort($aliasNode->getAttribute("sort"));
                                     $objAlias->setCreated($aliasNode->getAttribute("created"));
                                     $objAlias->setModified($aliasNode->getAttribute("modified"));
                                     $objAlias->save();
                                 }
                                 break;
                         }
                         //*** Adjust the links for deeplink fields.
                         ImpEx::adjustDeeplinks($arrElementFieldIds["link"], $arrElementIds, $arrLanguageIds);
                         //*** Adjust the links in large text fields.
                         ImpEx::adjustTextlinks($arrElementFieldIds["largeText"], $arrElementIds, $arrLanguageIds, array(0));
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
 public static function importIn($strXml, $intElementId, $intTemplateId, $intAccountId, $importTemplates = true, $importElements = true, $validate = true)
 {
     global $objLiveAdmin, $intDefaultLanguage, $_CONF;
     $objReturn = NULL;
     $objSettings = NULL;
     $blnZip = false;
     $validTemplateStructure = false;
     //*** Init DOM object.
     $objDoc = new DOMDocument("1.0", "UTF-8");
     $objDoc->formatOutput = false;
     $objDoc->preserveWhiteSpace = true;
     if (is_file($strXml)) {
         $objZip = new dUnzip2($strXml);
         if (is_object($objZip)) {
             //*** Zip file.
             $strXml = $objZip->unzip('data.xml');
             if ($strXml !== false) {
                 //*** Fix a unicode bug. Replace forbidden characters (The first 8).
                 for ($intCount = 1; $intCount < 9; $intCount++) {
                     $strHex = str_pad(dechex($intCount), 4, "0", STR_PAD_LEFT);
                     $strXml = preg_replace('/\\x{' . $strHex . '}/u', "", $strXml);
                 }
                 $strXml = preg_replace('/\\x{001f}/u', "", $strXml);
                 $objDoc->loadXML($strXml);
                 $blnZip = true;
             }
         } else {
             //*** XML file.
             $objDoc->load($strXml);
         }
     } else {
         $objDoc->loadXML($strXml);
     }
     $arrUserIds = array();
     $arrGroupIds = array();
     $arrStorageIds = array();
     $arrFeedIds = array();
     $arrLanguageIds[0] = 0;
     $arrTemplateIds[0] = 0;
     $arrTemplateFieldIds[0] = 0;
     $arrLinkFieldIds = array();
     $arrElementIds[0] = 0;
     $arrElementFieldIds = array();
     $arrElementFieldIds["link"][0] = 0;
     $arrElementFieldIds["largeText"][0] = 0;
     if ($validate) {
         //*** validate template structure
         foreach ($objDoc->childNodes as $rootNode) {
             foreach ($rootNode->childNodes as $logicNode) {
                 if ($logicNode->nodeName == "logic") {
                     foreach ($logicNode->childNodes as $childNode) {
                         switch ($childNode->nodeName) {
                             case "templates":
                                 $validTemplateStructure = ImpEx::validateImportTemplates($childNode, $intAccountId, $arrTemplateIds, $arrTemplateFieldIds, $arrLinkFieldIds, $intTemplateId);
                                 break;
                         }
                     }
                 }
             }
         }
     }
     if ($validTemplateStructure || !$validate) {
         //*** Import elements
         foreach ($objDoc->childNodes as $rootNode) {
             foreach ($rootNode->childNodes as $logicNode) {
                 if ($logicNode->nodeName == "logic") {
                     foreach ($logicNode->childNodes as $childNode) {
                         switch ($childNode->nodeName) {
                             case "languages":
                                 // Get all languages
                                 $arrCurrentLangs = array();
                                 $objContentLangs = ContentLanguage::select();
                                 $objDefaultLang = NULL;
                                 foreach ($objContentLangs as $objContentLang) {
                                     $arrCurrentLangs[$objContentLang->getAbbr()] = $objContentLang->getId();
                                     if ($objContentLang->default == 1) {
                                         $objDefaultLang = $objContentLang;
                                     }
                                 }
                                 $arrLanguageIds[0] = 0;
                                 // loop trough languages from export
                                 foreach ($childNode->childNodes as $languageNode) {
                                     // if abbreviations match, use that language ID
                                     if (array_key_exists($languageNode->getAttribute('abbr'), $arrCurrentLangs)) {
                                         $arrLanguageIds[$languageNode->getAttribute("id")] = $arrCurrentLangs[$languageNode->getAttribute('abbr')];
                                     } else {
                                         // if no match, use default current language
                                         $arrLanguageIds[$languageNode->getAttribute("id")] = $arrCurrentLangs[$objDefaultLang->getAbbr()];
                                     }
                                 }
                                 break;
                             case "templates":
                                 if ($importTemplates) {
                                     //*** Add templates to the account.
                                     self::importTemplates($childNode, $_CONF['app']['account']->getId(), $arrTemplateIds, $arrTemplateFieldIds, $arrLinkFieldIds, $intTemplateId);
                                 }
                                 break;
                             case "elements":
                                 if ($importElements) {
                                     if ($intElementId == NULL) {
                                         $strSql = sprintf("SELECT * FROM pcms_element WHERE templateId = '%s' AND accountId = '%s' ORDER BY sort LIMIT 1", $intTemplateId, $intAccountId);
                                         $objElements = Element::select($strSql);
                                         foreach ($objElements as $objElement) {
                                             self::importElements($childNode, $intAccountId, $arrTemplateIds, $arrTemplateFieldIds, $arrElementIds, $arrElementFieldIds, $arrLinkFieldIds, $arrLanguageIds, $arrUserIds, $arrGroupIds, $arrStorageIds, $arrFeedIds, $objElement->getId());
                                         }
                                     } else {
                                         self::importElements($childNode, $intAccountId, $arrTemplateIds, $arrTemplateFieldIds, $arrElementIds, $arrElementFieldIds, $arrLinkFieldIds, $arrLanguageIds, $arrUserIds, $arrGroupIds, $arrStorageIds, $arrFeedIds, $intElementId);
                                     }
                                 }
                                 break;
                             case "aliases":
                                 if ($importElements) {
                                     //*** Add aliases to the account.
                                     foreach ($childNode->childNodes as $aliasNode) {
                                         if (in_array($aliasNode->getAttribute("url"), $arrElementIds)) {
                                             $objAlias = new Alias();
                                             $objAlias->setAccountId($_CONF['app']['account']->getId());
                                             $objAlias->setAlias($aliasNode->getAttribute("alias"));
                                             $objAlias->setUrl($arrElementIds[$aliasNode->getAttribute("url")]);
                                             $objAlias->setLanguageId($arrLanguageIds[$aliasNode->getAttribute("language")]);
                                             // check for existence of alias
                                             $objTmpAliases = Alias::selectByAlias($aliasNode->getAttribute("alias"));
                                             $objTmpAlias = $objTmpAliases->current();
                                             if (!is_object($objTmpAlias)) {
                                                 $objAlias->setCascade($aliasNode->getAttribute("cascade"));
                                                 $objAlias->setActive($aliasNode->getAttribute("active"));
                                                 $objAlias->setSort($aliasNode->getAttribute("sort"));
                                                 $objAlias->setCreated($aliasNode->getAttribute("created"));
                                                 $objAlias->setModified($aliasNode->getAttribute("modified"));
                                                 $objAlias->save();
                                             }
                                         }
                                     }
                                 }
                                 break;
                         }
                         if ($importElements) {
                             //*** Adjust the links for deeplink fields.
                             self::adjustDeeplinks($arrElementFieldIds["link"], $arrElementIds, $arrLanguageIds);
                             //*** Adjust the links in large text fields.
                             self::adjustTextlinks($arrElementFieldIds["largeText"], $arrElementIds, $arrLanguageIds, array(0));
                         }
                     }
                 }
             }
         }
         //*** Import files
         $objAccount = Account::selectByPK($_CONF['app']['account']->getId());
         if ($blnZip && is_object($objAccount) && $importElements) {
             self::importFiles($objZip, $objAccount);
             //*** Move files to remote server.
             self::moveImportedFiles($objAccount);
         }
         return true;
     }
     return false;
 }