metadata = $metadata; $this->language = $language; $this->fileManager = $fileManager; $this->metadataUtils = new \Espo\Core\Utils\Metadata\Utils($this->metadata); } protected function getMetadata() { return $this->metadata; } protected function getLanguage() { return $this->language; } protected function getFileManager() { return $this->fileManager; } protected function getMetadataUtils() { return $this->metadataUtils; } public function create($name, $type, $params = array()) { if ($this->getMetadata()->get('scopes.' . $name)) { throw new Conflict('Entity ['.$name.'] already exists.'); } if (empty($name) || empty($type)) { throw new Error(); } $contents = "<" . "?" . "php\n". "namespace Espo\Custom\Entities;\n". "class {$name} extends \Espo\Core\Templates\Entities\\{$type}\n". "{\n". "}\n"; $filePath = "custom/Espo/Custom/Entities/{$name}.php"; $this->getFileManager()->putContents($filePath, $contents); $contents = "<" . "?" . "php\n". "namespace Espo\Custom\Controllers;\n". "class {$name} extends \Espo\Core\Templates\Controllers\\{$type}\n". "{\n". "}\n"; $filePath = "custom/Espo/Custom/Controllers/{$name}.php"; $this->getFileManager()->putContents($filePath, $contents); $contents = "<" . "?" . "php\n". "namespace Espo\Custom\Services;\n". "class {$name} extends \Espo\Core\Templates\Services\\{$type}\n". "{\n". "}\n"; $filePath = "custom/Espo/Custom/Services/{$name}.php"; $this->getFileManager()->putContents($filePath, $contents); $contents = "<" . "?" . "php\n". "namespace Espo\Custom\Repositories;\n". "class {$name} extends \Espo\Core\Templates\Repositories\\{$type}\n". "{\n". "}\n"; $filePath = "custom/Espo/Custom/Repositories/{$name}.php"; $this->getFileManager()->putContents($filePath, $contents); $stream = false; if (!empty($params['stream'])) { $stream = $params['stream']; } $labelSingular = $name; if (!empty($params['labelSingular'])) { $labelSingular = $params['labelSingular']; } $labelPlural = $name; if (!empty($params['labelPlural'])) { $labelPlural = $params['labelPlural']; } $labelCreate = $this->getLanguage()->translate('Create') . ' ' . $labelSingular; $scopeData = array( 'entity' => true, 'layouts' => true, 'tab' => true, 'acl' => true, 'module' => 'Custom', 'isCustom' => true, 'customizable' => true, 'importable' => true, 'type' => $type, 'stream' => $stream ); $this->getMetadata()->set($scopeData, 'scopes', $name); $filePath = "application/Espo/Core/Templates/Metadata/{$type}/entityDefs.json"; $entityDefsData = Json::decode($this->getFileManager()->getContents($filePath), true); $this->getMetadata()->set($entityDefsData, 'entityDefs', $name); $filePath = "application/Espo/Core/Templates/Metadata/{$type}/clientDefs.json"; $clientDefsData = Json::decode($this->getFileManager()->getContents($filePath), true); $this->getMetadata()->set($clientDefsData, 'clientDefs', $name); $this->getLanguage()->set($name, $labelSingular, 'scopeNames', 'Global'); $this->getLanguage()->set($name, $labelPlural, 'scopeNamesPlural', 'Global'); $this->getLanguage()->set('Create ' . $name, $labelCreate, 'labels', $name); $this->getLanguage()->save(); return true; } public function update($name, $fieldDef, $scope) { return true; } public function delete($name) { if (!$this->isCustom($name)) { throw new Forbidden; } $unsets = array( 'entityDefs', 'clientDefs', 'scopes' ); $res = $this->getMetadata()->delete($unsets, $this->metadataType, $name); $this->getFileManager()->removeFile("custom/Espo/Custom/Resources/metadata/entityDefs/{$name}.json"); $this->getFileManager()->removeFile("custom/Espo/Custom/Resources/metadata/clientDefs/{$name}.json"); $this->getFileManager()->removeFile("custom/Espo/Custom/Resources/metadata/scopes/{$name}.json"); $this->getFileManager()->removeFile("custom/Espo/Custom/Entities/{$name}.php"); $this->getFileManager()->removeFile("custom/Espo/Custom/Services/{$name}.php"); $this->getFileManager()->removeFile("custom/Espo/Custom/Controllers/{$name}.php"); $this->getFileManager()->removeFile("custom/Espo/Custom/Repositories/{$name}.php"); try { $this->getLanguage()->delete($name, 'scopeNames', 'Global'); $this->getLanguage()->delete($name, 'scopeNamesPlural', 'Global'); } catch (\Exception $e) {} $this->getLanguage()->save(); return true; } protected function isCustom($name) { return $this->getMetadata()->get('scopes.' . $name . '.isCustom'); } }