public static function getInstance() { if (self::$Instance === null) { self::$Instance = new MetaDataConverter(); } return self::$Instance; }
/** * Static entry point, will instantiate an object of itself to run the process. * Will convert $defs to legacy format $viewtype if there is a converter for * it, otherwise will return the defs as-is with no modification. * * @static * @param string $viewtype One of list|edit|detail * @param array $defs The defs to convert * @return array Converted defs if there is a converter, else the passed in defs */ public static function toLegacy($viewtype, $defs) { if (null === self::$converter) { self::$converter = new self(); } $method = 'toLegacy' . ucfirst(strtolower($viewtype)); if (method_exists(self::$converter, $method)) { return self::$converter->{$method}($defs); } return $defs; }
/** * Special copy operation for virtual files. * * @param IFile $file The source file to copy data from. * @param bool $keepOGP Set to TRUE to force keeping the Owner-Group-Permissions of the current file (target), * FALSE to overwrite them with the ones from the source file ($file) * @param bool $overwrite Set to TRUE to overwrite the current file (target) */ protected function copyFromAndKeepOGP(IFile $file, $keepOGP = false, $overwrite = true) { if ($this->realFile === null) { throw new EyeUnsupportedOperationException(__METHOD__ . ' on ' . $this->path); } //if the destination ($this) is a a directory AND: the source is not a directory OR is a directory with a different name if ($this->isDirectory() && (!$file->isDirectory() || $this->getName() != $file->getName())) { if ($this->getName() != '/' || $file->getName() != '/') { //we redirect the copy operation to a file located within the destination directory ($this) return $this->getChildFile($file->getName())->copyFromAndKeepOGP($file, $keepOGP, $overwrite); } } if ($file instanceof ISecurableFile) { $file->checkReadPermission(); } // $this file exists => check write permissions if ($this->realFile->exists()) { $this->checkWritePermission(); if (!$overwrite) { throw new EyeIOException('Unable to copy: File ' . $this->path . ' exists but overwrite option is not enabled.'); } } else { $this->getParentFile()->checkWritePermission(); } //are we trying to copy a file to itself? if ($this->equals($file)) { throw new EyeIOException('Unable to copy ' . $file->getPath() . ': source and target are the same.'); } //checks ok, we can start the copy process $success = true; try { //use the "real" file instead of the virtual one for next steps if ($file instanceof IVirtualFile) { $realFile = $file->getRealFile(); } else { $realFile = $file; } //file to file //@todo Find which of these functions creates metadata if ($file->isFile()) { $fileCreated = false; if (!$this->exists()) { $this->createNewFile(); $fileCreated = true; } if ($this->realFile->copyFrom($realFile, $overwrite)) { // using metadata converter $newMetaData = MetaDataConverter::getInstance()->convertThis($file, $this); $this->setMeta($newMetaData); //process metadata /* $meta = null; if ($file instanceof IMetaAssociable) { // ---------------------------------------------------- // FIXME: We need Metadata Converter $sourceFile = $file->realFile; $destFile = FSI::getFile($this->path); if ($sourceFile instanceof LocalFile && $destFile instanceof EyeWorkgroupFile) { $meta = $file->getMeta(); $meta->set('activity', null); $meta->set('id', null); // END OF FIXME // ---------------------------------------------------- } else { //Assign owner metadata to current User $meta = $file->getMeta(); if ($meta->get('owner')) { $meta->set('owner', ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosUser()->getName()); $meta->set('group', ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosGroup()->getName()); $meta->set('permissions', null); } $meta = MetaManager::getInstance()->getNewMetaDataInstance($this, $meta); } } if ($meta !== null) { if (!$keepOGP && !$fileCreated) { $currentUser = ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosUser(); $group = ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosGroup(); $meta->set(self::METADATA_KEY_OWNER, $currentUser->getName()); $meta->set(self::METADATA_KEY_GROUP, $group->getName()); $meta->set(self::METADATA_KEY_PERMISSIONS, AdvancedPathLib::permsToUnix(self::PERMISSIONS_MASK_FILE & ~$this->getUMask())); } $this->setMeta($meta); } */ } else { $success = false; } } else { if ($file->isDirectory()) { //create destination directory if needed if (!$this->exists()) { $this->mkdirs(); } //copy subfiles one by one (needed for metadata) foreach ($file->listFiles() as $subFile) { //create child path $urlParts = $this->getURLComponents(); $urlParts['path'] .= '/' . $subFile->getName(); $params = array('realFile' => $this->realFile->getChildFile($subFile->getName())); //instantiate new file object $thisClass = get_class($this); $thisSubFile = new $thisClass(AdvancedPathLib::buildURL($urlParts), $params); if ($thisSubFile instanceof EyeosAbstractVirtualFile) { $success = $success && $thisSubFile->copyFromAndKeepOGP($subFile, $keepOGP, $overwrite); } else { $success = $success && $thisSubFile->copyFrom($subFile, $overwrite); } } //process metadata $meta = null; if ($file instanceof IMetaAssociable) { $meta = MetaManager::getInstance()->getNewMetaDataInstance($this, $file->getMeta()); } else { $meta = MetaManager::getInstance()->getNewMetaDataInstance($this); } if ($meta !== null) { /* if (!$keepOGP) { $currentUser = ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosUser(); $group = ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosGroup(); $meta->set(self::METADATA_KEY_OWNER, $currentUser->getName()); $meta->set(self::METADATA_KEY_GROUP, $group->getName()); $meta->set(self::METADATA_KEY_PERMISSIONS, AdvancedPathLib::permsToUnix(self::PERMISSIONS_MASK_DIR & ~$this->getUMask())); } */ //$this->setMeta($meta); } } else { if ($file->isLink()) { $linkTarget = $file->getLinkTarget(); if ($linkTarget === null) { throw new EyeNullPointerException('Unable to get link\'s target for ' . $file->getPath() . '.'); } $this->createNewFile($overwrite); /* $meta = $file->getMeta(); if (!$keepOGP) { $currentUser = ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosUser(); $group = ProcManager::getInstance()->getCurrentProcess()->getLoginContext()->getEyeosGroup(); $meta->set(self::METADATA_KEY_OWNER, $currentUser->getName()); $meta->set(self::METADATA_KEY_GROUP, $group->getName()); $meta->set(self::METADATA_KEY_PERMISSIONS, AdvancedPathLib::permsToUnix(self::PERMISSIONS_MASK_LINK & ~$this->getUMask())); $meta->set(self::METADATA_KEY_LINKTARGET, $linkTarget); } */ //$this->setMeta($meta); } } } } catch (EyeErrorException $e) { throw new EyeIOException('Unable to copy ' . $file->getPath() . ' to ' . $this->path . '.', 0, $e); } catch (EyeException $e) { throw new EyeIOException('Unable to copy ' . $file->getPath() . ' to ' . $this->path . '.', 0, $e); } if ($success) { return true; } throw new EyeUnknownErrorException('Unable to (entirely) copy ' . $file->getPath() . ' to ' . $this->path . '.'); }
/** * Load the layout def file and associate the definition with a variable in the file. */ function open_layout_defs($reload = false, $layout_def_key = '', $original_only = false) { require_once 'include/MetaDataManager/MetaDataManager.php'; $mm = MetaDataManager::getManager(); $layout_defs[$this->_focus->module_dir] = array(); $layout_defs[$layout_def_key] = array(); $def_path = array(); if (empty($this->layout_defs) || $reload || !empty($layout_def_key) && !isset($layout_defs[$layout_def_key])) { if (!$original_only) { if (isModuleBWC($this->_focus->module_dir)) { $def_path = array('modules/' . $this->_focus->module_dir . '/metadata/' . ($this->platform == 'mobile' ? 'wireless.' : '') . 'subpaneldefs.php'); $def_path[] = SugarAutoLoader::loadExtension($this->platform == 'mobile' ? 'wireless_subpanels' : 'layoutdefs', $this->_focus->module_dir); foreach (SugarAutoLoader::existing($def_path) as $file) { require $file; } } else { $viewdefs = $mm->getModuleLayouts($this->_focus->module_dir); $viewdefs = !empty($viewdefs['subpanels']['meta']['components']) ? $viewdefs['subpanels']['meta']['components'] : array(); } } $layoutDefsKey = !empty($layout_def_key) ? $layout_def_key : $this->_focus->module_dir; // convert sidecar subpanels to the array the SubpanelDefinitions are looking for if ($this->_focus instanceof SugarBean && !isModuleBWC($this->_focus->module_dir) && isset($viewdefs)) { require_once 'include/MetaDataManager/MetaDataConverter.php'; $metaDataConverter = new MetaDataConverter(); $layout_defs[$layoutDefsKey] = $metaDataConverter->toLegacySubpanelLayoutDefs($viewdefs, $this->_focus); } $this->layout_defs = $layout_defs[$layoutDefsKey]; } }
/** * @param $moduleName, the module name * @param $relationshipName the relationship name * @param array $definition the definitions to be saved * @param string $basepath - the path to save the file to * @param string $client base|mobile|portal * @return string filename the layout definition is saved to */ public function saveSidecarSubpanelDefinitions($basepath, $installDefPrefix, $relationshipName, $subpanelDefinitions, $client = 'base') { $basepath = str_replace('relationships', "clients/{$client}/layouts", $basepath); $installDefs = array(); foreach ($subpanelDefinitions as $moduleName => $definitions) { if (!$this->hasSidecarLayoutDefs($moduleName, $client)) { continue; } if (strstr($basepath, 'modulebuilder')) { $layoutPath = "{$basepath}/subpanels"; } else { $layoutPath = "custom/Extension/modules/{$moduleName}/Ext/clients/{$client}/layouts/subpanels"; } if (!is_dir($layoutPath)) { sugar_mkdir($layoutPath, null, true); } foreach ($definitions as $definition) { $override_array = array(); // we currently do not support collections in sidecar if (!empty($definition['collection_list'])) { continue; } if ($definition['subpanel_name'] === 'Default') { // There is a typo somewhere in the bowels of the Activities relationship code $definition['subpanel_name'] = 'default'; } if ($definition['subpanel_name'] !== 'default') { require_once 'include/MetaDataManager/MetaDataConverter.php'; $mc = new MetaDataConverter(); $override_array = array('override_subpanel_list_view' => array('link' => $definition['get_subpanel_data'], 'view' => $mc->fromLegacySubpanelName($definition['subpanel_name']))); } $fileName = "{$layoutPath}/{$relationshipName}_{$moduleName}.php"; $varName = "viewdefs['{$moduleName}']['{$client}']['layout']['subpanels']['components'][]"; $layoutDefs = array('layout' => "subpanel", 'label' => $definition['title_key'], 'context' => array('link' => $definition['get_subpanel_data'])); $previous_contents = ''; if (file_exists($fileName)) { $previous_contents = file_get_contents($fileName) . "\n\n"; } write_array_to_file($varName, $layoutDefs, $fileName, 'w', $previous_contents); if (!empty($override_array)) { write_array_to_file("viewdefs['{$moduleName}']['{$client}']['layout']['subpanels']['components'][]", $override_array, "{$layoutPath}/_overridesubpanel-for-{$relationshipName}.php"); } } // In cases of package publishing, we need to fetch the right path. // That path will come from installDefPrefix being set to something // with /SugarModules as a path. Otherwise, use the path that the defs // are being saved to in this request. $fromPath = $fileName; if (strpos($installDefPrefix, '/SugarModules') !== false) { $fromPath = "{$installDefPrefix}/clients/{$client}/layouts/subpanels/{$relationshipName}_{$moduleName}.php"; } $installDefs[$moduleName] = array('from' => $fromPath, 'to_module' => $moduleName); } return $installDefs; }
/** * Copies metadata from $objectFrom to $objectTo. * * @param mixed $objectFrom * @param mixed $objectTo * @return boolean * @throws EyeException */ public function copyMeta($objectFrom, $objectTo) { //$Logger = Logger::getLogger('metaManager.copyMeta'); self::$Logger->debug("copyMeta from " . get_class($objectFrom) . " => " . get_class($objectTo)); // ask to MetaDataConverte to return the new metadata $newMetadata = MetaDataConverter::getInstance()->convertThis($objectFrom, $objectTo); if ($newMetadata === null) { throw new EyeMetaDataNotFoundException('Unable to retrieve metadata for ' . get_class($objectFrom)); } self::$Logger->debug("MetaDataConverter returns:"); self::$Logger->debug($newMetadata); //self::storeMeta($objectTo, $newMetadata); //$handlerTo = $this->getHandlerInfo($objectTo); //$handlerTo[self::OBJECT]->storeMeta($objectTo, $newMetadata, $handlerTo[self::METADATACLASS]); try { $handlerFrom = $this->getHandlerInfo($objectFrom); $meta = $handlerFrom[self::OBJECT]->retrieveMeta($objectFrom, $handlerFrom[self::METADATACLASS]); if ($meta === null) { throw new EyeMetaDataNotFoundException('Unable to retrieve metadata for ' . get_class($objectFrom)); } $handlerTo = $this->getHandlerInfo($objectTo); $handlerFrom[self::OBJECT]->storeMeta($objectTo, $newMetadata, $handlerTo[self::METADATACLASS]); } catch (EyeMetaDataException $e) { throw new EyeMetaDataException('An error occured while coping metadatas from the $objectFrom to the $objectTo.', 0, $e); } }