fileManager = $fileManager; $this->layout = $layout; } protected function sanitizeInput($name) { return preg_replace("([\.]{2,})", '', $name); } /** * Get layout in string format. */ public function get(string $scope, string $name) : ?string { return $this->layout->get($scope, $name); } /** * Set layout data. */ public function set($data, string $scope, string $name) { $scope = $this->sanitizeInput($scope); $name = $this->sanitizeInput($name); if (empty($scope) || empty($name)) { return false; } $this->changedData[$scope][$name] = $data; } public function resetToDefault(string $scope, string $name) { $scope = $this->sanitizeInput($scope); $name = $this->sanitizeInput($name); $filePath = 'custom/Espo/Custom/Resources/layouts/' . $scope . '/' . $name . '.json'; if ($this->fileManager->isFile($filePath)) { $this->fileManager->removeFile($filePath); } if (!empty($this->changedData[$scope]) && !empty($this->changedData[$scope][$name])) { unset($this->changedData[$scope][$name]); } return $this->get($scope, $name); } /** * Save changes. */ public function save() { $result = true; if (!empty($this->changedData)) { foreach ($this->changedData as $scope => $rowData) { foreach ($rowData as $layoutName => $layoutData) { if (empty($scope) || empty($layoutName)) { continue; } $layoutPath = $this->getDirPath($scope, true); $data = Json::encode($layoutData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); $result &= $this->fileManager->putContents(array($layoutPath, $layoutName.'.json'), $data); } } } if ($result == true) { $this->clearChanges(); } return (bool) $result; } /** * Clear unsaved changes. */ public function clearChanges() { $this->changedData = []; } /** * Merge layout data. * Ex. $scope= Account, $name= detail then will be created a file layoutFolder/Account/detail.json */ public function merge($data, string $scope, string $name) { $scope = $this->sanitizeInput($scope); $name = $this->sanitizeInput($name); $prevData = $this->get($scope, $name); $prevDataArray = Json::getArrayData($prevData); $dataArray = Json::getArrayData($data); $data = Util::merge($prevDataArray, $dataArray); $data = Json::encode($data); return $this->set($data, $scope, $name); } protected function getDirPath(string $entityType, bool $isCustom = false) : string { return $this->layout->getDirPath($entityType, $isCustom); } }