function assetTreeSwitchDataDefinitionForBlock(aohs\AssetOperationHandlerService $service, p\Child $child, $params = NULL, &$results = NULL) { global $common_assets_site_name; $type = $child->getType(); if ($type != a\DataBlock::TYPE) { return; } if (!isset($params["cascade"])) { throw new \Exception("The Cascade object is not supplied"); } else { $cascade = $params["cascade"]; } $block = $child->getAsset($service); $b_name = $block->getName(); if ($b_name == "_footer-contact") { $dd_name = "Footer Contact"; } elseif ($b_name == "_left-column") { $dd_name = "Left Column"; } elseif ($b_name == "_right-column") { $dd_name = "Right Column"; } elseif ($b_name == "_site-info") { $dd_name = "Site Info"; } else { return; } $dd = $cascade->getAsset(a\DataDefinition::TYPE, $dd_name, $common_assets_site_name); $st = new p\StructuredData($dd->getStructuredData(), $service, $dd->getId()); $identifiers = $block->getIdentifiers(); foreach ($identifiers as $identifier) { // create the correct number of instances for multiple field if (u\StringUtility::endsWith($identifier, ";0") && $st->hasIdentifier($identifier)) { $st->createNInstancesForMultipleField($block->getNumberOfSiblings($identifier), $identifier); } // map the data if ($st->hasNode($identifier) && $block->isTextNode($identifier)) { $st->setText($identifier, $block->getText($identifier)); } elseif ($st->hasNode($identifier) && $block->isAssetNode($identifier)) { $asset_node_type = $block->getAssetNodeType($identifier); if ($asset_node_type == "page" && $block->getPageId($identifier) != null) { $st->setPage($identifier, $cascade->getPage($block->getPageId($identifier))); } elseif ($asset_node_type == "file" && $block->getFileId($identifier) != null) { $st->setFile($identifier, $cascade->getFile($block->getFileId($identifier))); } elseif ($asset_node_type == "block" && $block->getBlockId($identifier) != null) { $st->setBlock($identifier, a\Block::getBlock($service, $block->getBlockId($identifier))); } elseif ($asset_node_type == "page,file,symlink" && $block->getLinkableId($identifier) != null) { $st->setLinkable($identifier, a\Linkable::getLinkable($service, $block->getLinkableId($identifier))); } } } $block->setStructuredData($st); }
public static function assetTreeUpdateDataDefinitionBlock(aohs\AssetOperationHandlerService $service, p\Child $child, $params = NULL, &$results = NULL) { if (isset($params['source-cascade'])) { $source_cascade = $params['source-cascade']; } else { echo c\M::SOURCE_CASCADE_NOT_SET . BR; return; } if (isset($params['target-cascade'])) { $target_cascade = $params['target-cascade']; } else { echo c\M::TARGET_CASCADE_NOT_SET . BR; return; } if (isset($params['source-site'])) { $source_site = $params['source-site']; } else { echo c\M::SOURCE_SITE_NOT_SET . BR; return; } if (isset($params['target-site'])) { $target_site = $params['target-site']; } else { echo c\M::TARGET_SITE_NOT_SET . BR; return; } if (isset($params['exception-thrown'])) { $exception_thrown = $params['exception-thrown']; } else { echo c\M::EXCEPTION_THROWN_NOT_SET . BR; return; } if (isset($params['update-data'])) { $update_data = $params['update-data']; } else { $update_data = true; } if (isset($params['update-metadata'])) { $update_metadata = $params['update-metadata']; } else { $update_metadata = true; } if (self::DEBUG && self::DUMP) { u\DebugUtility::out("Retrieving block"); u\DebugUtility::dump($child->toStdClass()); } try { $source_block = $child->getAsset($service); $source_block_path = u\StringUtility::removeSiteNameFromPath($source_block->getPath()); } catch (\Exception $e) { throw new e\CascadeInstancesErrorException($e . BR . S_SPAN . "Path: " . $child->getPathPath() . E_SPAN); } $target_dd = NULL; // it will fail if there is any irregularity in the block if ($source_block->hasStructuredData()) { $source_block_dd = $source_block->getDataDefinition(); $source_block_dd_path = u\StringUtility::removeSiteNameFromPath($source_block_dd->getPath()); $source_block_dd_site = $source_block_dd->getSiteName(); $target_block_dd_site = $source_block_dd_site; // compare the two data definitions $source_dd = Asset::getAsset($service, DataDefinition::TYPE, $source_block_dd_path, $source_block_dd_site); // the data definition must be there $target_dd = $target_cascade->getAsset(DataDefinition::TYPE, $source_block_dd_path, $target_block_dd_site); $source_xml = new \SimpleXMLElement($source_dd->getXml()); $target_xml = new \SimpleXMLElement($target_dd->getXml()); if (!u\XMLUtility::isXmlIdentical($source_xml, $target_xml)) { throw new e\CascadeInstancesErrorException(S_SPAN . c\M::DIFFERENT_DATA_DEFINITIONS . E_SPAN); } $source_structured_data_std = $source_block->getStructuredData()->toStdClass(); $target_dd_id = $target_dd->getId(); $target_structured_data_std = $source_structured_data_std; $target_structured_data_std->definitionId = $target_dd_id; } else { $source_content = $source_block->getXhtml(); } $source_block_path_array = u\StringUtility::getExplodedStringArray("/", $source_block_path); $source_block_path = $source_block_path_array[count($source_block_path_array) - 1]; $source_block_parent_path = $source_block->getParentContainerPath(); // create block $target_site_name = $target_site->getName(); // parent must be there $target_parent = $target_cascade->getAsset(Folder::TYPE, $source_block_parent_path, $target_site_name); $target_block = $target_cascade->createXhtmlDataDefinitionBlock($target_parent, $source_block_path, $target_dd, $source_content); // update data if ($update_data) { if ($target_block->hasStructuredData()) { if (self::DEBUG && self::DUMP) { u\DebugUtility::dump($target_structured_data_std); } try { $target_service = $target_cascade->getService(); $identifiers = $source_block->getIdentifiers(); $identifier_asset_map = array(); foreach ($identifiers as $identifier) { if ($source_block->isAssetNode($identifier)) { $block_id = $source_block->getBlockId($identifier); if (isset($block_id)) { $resource_id = $block_id; $source_resource_type = Block::getBlockType($service, $resource_id); } $file_id = $source_block->getFileId($identifier); if (isset($file_id)) { $resource_id = $file_id; $source_resource_type = File::TYPE; } $page_id = $source_block->getPageId($identifier); if (isset($page_id)) { $resource_id = $page_id; $source_resource_type = Page::TYPE; } $symlink_id = $source_block->getSymlinkId($identifier); if (isset($symlink_id)) { $resource_id = $symlink_id; $source_resource_type = Symlink::TYPE; } if (isset($resource_id)) { $source_resource = $service->retrieve($service->createId($source_resource_type, $resource_id)); if ($service->isSuccessful()) { $source_resource_site = $source_resource->siteName; $source_resource_path = $source_resource->path; $target_resource_site = $source_resource_site; try { $asset = $target_cascade->getAsset($source_resource_type, $source_resource_path, $source_resource_site); $identifier_asset_map[$identifier] = $asset; } catch (\Exception $e) { if ($exception_thrown) { throw new e\CascadeInstancesErrorException($e . BR . S_SPAN . "Block: " . $source_block->getPath() . BR . "Resource: " . $source_resource_path . E_SPAN); } else { u\DebugUtility::out($e->getMessage() . BR . "<span style='color:red;font-weight:bold;'>" . "Block: " . $source_block->getPath() . BR . "Resource: " . $source_resource_path . "</span>"); } } } else { echo "Failed to retrieve resource." . BR; } // reinitialized for the next round $resource_id = NULL; } } } $identifiers = array_keys($identifier_asset_map); $count = count($identifiers); $structured_data = new p\StructuredData($target_structured_data_std, $target_service); if ($count > 0) { foreach ($identifiers as $identifier) { $asset = $identifier_asset_map[$identifier]; $type = $asset->getType(); switch ($type) { case 'file': $method = 'setFile'; break; case 'page': $method = 'setPage'; break; case 'symlink': $method = 'setSymlink'; break; default: $method = 'setBlock'; } if ($structured_data->hasNode($identifier)) { $structured_data->{$method}($identifier, $asset); } } } $target_block->setStructuredData($structured_data); } catch (\Exception $e) { if (self::DEBUG) { u\DebugUtility::out($child->getPathPath()); } } } } if ($update_metadata) { self::setMetadataSet($target_cascade, $source_site, $target_site, $source_block, $target_block, $exception_thrown); } }
private static function copyData($source, StructuredData $target, $id) { if (!$source instanceof StructuredData && !$source instanceof StructuredDataPhantom) { throw new \Exception("Wrong source type"); } if ($source->isTextNode($id) || $source->isWYSIWYG($id)) { $target->setText($id, $source->getText($id)); if ($target->getText($id) == NULL) { $target->setText($id, ""); } } elseif ($source->isAssetNode($id)) { $asset_type = $source->getAssetNodeType($id); try { switch ($asset_type) { case c\T::PAGE: $page_id = $source->getPageId($id); if (isset($page_id)) { $target->setPage($id, $source->getService()->getAsset($source->getService()->createId(a\Page, $page_id))); } break; case c\T::FILE: $file_id = $source->getFileId($id); if (isset($file_id)) { $target->setFile($id, $source->getService()->getAsset($source->getService()->createId(a\File, $file_id))); } break; case c\T::BLOCK: $block_id = $source->getBlockId($id); if (isset($block_id)) { $target->setBlock($id, a\Block::getBlock($target->getService(), $block_id)); } break; case c\T::SYMLINK: $symlink_id = $source->getSymlinkId($id); if (isset($symlink_id)) { $target->setSymlink($id, $source->getService()->getAsset($source->getService()->createId(a\Symlink, $symlink_id))); } break; case c\T::PFS: $linkable_id = $source->getLinkableId($id); if (isset($linkable_id)) { $target->setLinkable($id, a\Linkable::getLinkable($source->getService(), $linkable_id)); } break; } } catch (e\NoSuchTypeException $e) { // do nothing to skip deleted blocks } } }