Initial commit

This commit is contained in:
root
2026-01-19 17:44:46 +01:00
commit 823af8b11d
8721 changed files with 1130846 additions and 0 deletions

View File

@@ -0,0 +1,172 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Layout;
use Espo\Core\DataManager;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Conflict;
use Espo\Core\Exceptions\Error;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Utils\File\Manager as FileManager;
use Espo\Core\Utils\Language;
use Espo\Core\Utils\Metadata;
use RuntimeException;
class CustomLayoutService
{
private const TYPE_LIST = 'list';
public function __construct(
private Metadata $metadata,
private FileManager $fileManager,
private Language $baseLanguage,
private LayoutProvider $layoutProvider,
private DataManager $dataManager
) {}
/**
* @throws Conflict
* @throws BadRequest
* @throws Forbidden
*/
public function create(LayoutDefs $defs): void
{
$type = $defs->getType();
$name = $defs->getName();
$scope = $defs->getScope();
$label = $defs->getLabel();
$this->checkName($name);
if ($type !== self::TYPE_LIST) {
throw new BadRequest("Not supported type.");
}
if (!$this->metadata->get(['scopes', $scope, 'entity'])) {
throw new Forbidden("Bad scope.");
}
if (
$this->metadata->get(['clientDefs', $scope, 'additionalLayouts', $name]) ||
$this->fileManager->exists("application/Espo/Resources/defaults/layouts/$name") ||
$this->layoutProvider->get($scope, $name)
) {
throw Conflict::createWithBody(
"Layout $name already exists.",
Error\Body::create()
->withMessageTranslation('alreadyExists', 'LayoutManager', ['name' => $name])
->encode()
);
}
$this->writeDefaultListLayout($scope, $name);
$this->metadata->set('clientDefs', $scope, [
'additionalLayouts' => [
$name => [
'type' => $type,
'isCustom' => true,
]
]
]);
$this->baseLanguage->set($scope, 'layouts', $name, $label);
$this->metadata->save();
$this->baseLanguage->save();
$this->clearCache();
}
private function clearCache(): void
{
try {
$this->dataManager->clearCache();
} catch (Error $e) {
throw new RuntimeException($e->getMessage());
}
}
private function writeDefaultListLayout(string $scope, string $name): void
{
$file = $this->composerFilePath($scope, $name);
$listLayout = [
(object) [
'name' => 'name',
'link' => true,
],
];
$this->fileManager->putJsonContents($file, $listLayout);
}
/**
* @throws BadRequest
* @throws Forbidden
*/
public function delete(string $scope, string $name): void
{
if (!$this->metadata->get(['scopes', $scope, 'entity'])) {
throw new Forbidden("Bad scope.");
}
$this->checkName($name);
$file = $this->composerFilePath($scope, $name);
$this->metadata->delete('clientDefs', $scope, "additionalLayouts.$name");
$this->baseLanguage->delete($scope, 'layouts', $name);
$this->metadata->save();
$this->baseLanguage->save();
$this->fileManager->remove($file);
$this->clearCache();
}
private function composerFilePath(string $scope, string $name): string
{
return "custom/Espo/Custom/Resources/layouts/$scope/$name.json";
}
/**
* @throws BadRequest
*/
private function checkName(string $name): void
{
if (
lcfirst($name[0]) !== $name[0] ||
preg_match('/[^a-zA-Z\d]/', $name)
) {
throw new BadRequest("Bad name.");
}
}
}

View File

@@ -0,0 +1,78 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Layout;
class LayoutDefs
{
/**
* @param non-empty-string $scope
* @param non-empty-string $name
* @param non-empty-string $type
* @param non-empty-string $label
*/
public function __construct(
private string $scope,
private string $name,
private string $type,
private string $label
) {}
/**
* @return non-empty-string
*/
public function getScope(): string
{
return $this->scope;
}
/**
* @return non-empty-string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return non-empty-string
*/
public function getType(): string
{
return $this->type;
}
/**
* @return non-empty-string
*/
public function getLabel(): string
{
return $this->label;
}
}

View File

@@ -0,0 +1,138 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Layout;
use Espo\Core\InjectableFactory;
use Espo\Core\Utils\File\Manager as FileManager;
use Espo\Core\Utils\Json;
use Espo\Core\Utils\Metadata;
use Espo\Core\Utils\Resource\FileReader;
use Espo\Core\Utils\Resource\FileReader\Params as FileReaderParams;
use RuntimeException;
class LayoutProvider
{
private string $defaultPath = 'application/Espo/Resources/defaults/layouts';
/** @internal Used by the portal layout util. */
protected FileReader $fileReader;
public function __construct(
private FileManager $fileManager,
private InjectableFactory $injectableFactory,
private Metadata $metadata,
FileReader $fileReader
) {
$this->fileReader = $fileReader;
}
public function get(string $scope, string $name): ?string
{
if (
$this->sanitizeInput($scope) !== $scope ||
$this->sanitizeInput($name) !== $name
) {
throw new RuntimeException("Bad parameters.");
}
$path = "layouts/$scope/$name.json";
$params = FileReaderParams::create()->withScope($scope);
$module = $this->getLayoutLocationModule($scope, $name);
if ($module) {
$params = $params
->withScope(null)
->withModuleName($module);
}
if ($this->fileReader->exists($path, $params)) {
return $this->fileReader->read($path, $params);
}
$default = $this->getDefault($scope, $name);
if ($default) {
return $default;
}
foreach (array_reverse($this->metadata->getModuleList()) as $module) {
$params = FileReaderParams::create()->withModuleName($module);
if ($this->fileReader->exists($path, $params)) {
return $this->fileReader->read($path, $params);
}
}
return null;
}
private function getLayoutLocationModule(string $scope, string $name): ?string
{
return $this->metadata->get("app.layouts.$scope.$name.module");
}
private function getDefault(string $scope, string $name): ?string
{
$defaultImplClassName = 'Espo\\Custom\\Classes\\DefaultLayouts\\' . ucfirst($name) . 'Type';
if (!class_exists($defaultImplClassName)) {
$defaultImplClassName = 'Espo\\Classes\\DefaultLayouts\\' . ucfirst($name) . 'Type';
}
if (class_exists($defaultImplClassName)) {
// @todo Use factory and interface.
$defaultImpl = $this->injectableFactory->create($defaultImplClassName);
if (!method_exists($defaultImpl, 'get')) {
throw new RuntimeException("No 'get' method in '$defaultImplClassName'.");
}
$data = $defaultImpl->get($scope);
return Json::encode($data);
}
$filePath = "$this->defaultPath/$name.json";
if (!$this->fileManager->isFile($filePath)) {
return null;
}
return $this->fileManager->getContents($filePath);
}
protected function sanitizeInput(string $name): string
{
/** @var string */
return preg_replace("([.]{2,})", '', $name);
}
}

View File

@@ -0,0 +1,57 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Layout;
use Espo\Core\Utils\Resource\FileReader\Params as FileReaderParams;
use RuntimeException;
class PortalLayoutProvider extends LayoutProvider
{
public function get(string $scope, string $name): ?string
{
if (
$this->sanitizeInput($scope) !== $scope ||
$this->sanitizeInput($name) !== $name
) {
throw new RuntimeException("Bad parameters.");
}
$path = 'layouts/' . $scope . '/portal/' . $name . '.json';
$params = FileReaderParams::create()
->withScope($scope);
if ($this->fileReader->exists($path, $params)) {
return $this->fileReader->read($path, $params);
}
return parent::get($scope, $name);
}
}

View File

@@ -0,0 +1,392 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Tools\Layout;
use Espo\Core\Acl;
use Espo\Core\Acl\Exceptions\NotImplemented;
use Espo\Core\DataManager;
use Espo\Core\Exceptions\Error;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\Exceptions\NotFound;
use Espo\Core\ORM\EntityManager;
use Espo\Core\Utils\Json;
use Espo\Core\Utils\Metadata;
use Espo\Entities\LayoutSet;
use Espo\Entities\Portal;
use Espo\Entities\Team;
use Espo\Entities\User;
use Espo\Entities\LayoutRecord;
use Espo\ORM\Defs\Params\RelationParam;
use Espo\ORM\Name\Attribute;
use Espo\Tools\LayoutManager\LayoutManager;
use stdClass;
class Service
{
public function __construct(
private Acl $acl,
private LayoutProvider $layoutProvider,
private LayoutManager $layoutManager,
private EntityManager $entityManager,
private Metadata $metadata,
private DataManager $dataManager,
private User $user
) {}
/**
* @return array<int, mixed>|stdClass|null
* @throws NotFound
* @throws Error
*/
public function getOriginal(string $scope, string $name, ?string $setId = null): mixed
{
$result = null;
if ($setId) {
$layout = $this->getRecordFromSet($scope, $name, $setId, true);
if ($layout && $layout->getData() !== null) {
/** @var string $data */
$data = $layout->getData();
$result = Json::decode($data);
}
}
if (!$result) {
$data = $this->layoutProvider->get($scope, $name) ?? 'false';
$result = Json::decode($data);
}
if ($result === false && $name === 'bottomPanelsDetail') {
return $this->getOriginalBottomPanelsDetail($scope);
}
return $result;
}
/**
* @throws Forbidden
* @throws NotFound
* @throws Error
*/
public function getForFrontend(string $scope, string $name): mixed
{
try {
if (!$this->acl->checkScope($scope)) {
throw new Forbidden("No access to scope $scope.");
}
} catch (NotImplemented) {}
$data = null;
$layoutSetId = $this->getUserLayoutSetId();
if ($layoutSetId) {
$nameReal = $name;
if (
$this->user->isPortal() &&
str_ends_with($name, 'Portal')
) {
$nameReal = substr($name, 0, -6);
}
$layout = $this->getRecordFromSet($scope, $nameReal, $layoutSetId, true);
$data = $layout?->getData();
}
if (!$data) {
$dataString = $this->layoutProvider->get($scope, $name) ?? 'null';
$data = Json::decode($dataString);
}
if (is_null($data)) {
throw new NotFound("Layout $scope:$name is not found.");
}
if (
!$this->user->isAdmin() &&
$name === 'relationships' &&
is_array($data)
) {
foreach ($data as $i => $item) {
$link = $item;
if (is_object($item)) {
/** @var stdClass $item */
$link = $item->name ?? null;
}
$foreignEntityType = $this->metadata
->get(['entityDefs', $scope, 'links', $link, RelationParam::ENTITY]);
if (
$foreignEntityType &&
!$this->acl->tryCheck($foreignEntityType)
) {
unset($data[$i]);
}
}
$data = array_values($data);
}
if ($data === false && $name === 'bottomPanelsDetail') {
return $this->getForFrontendBottomPanelsDetail($scope);
}
return $data;
}
/**
* @throws NotFound
*/
private function getRecordFromSet(
string $scope,
string $name,
string $setId,
bool $skipCheck = false
): ?LayoutRecord {
$layoutSet = $this->entityManager
->getRDBRepositoryByClass(LayoutSet::class)
->getById($setId);
if (!$layoutSet) {
throw new NotFound("LayoutSet $setId not found.");
}
$fullName = $scope . '.' . $name;
if (!in_array($fullName, $layoutSet->getLayoutList())) {
if ($skipCheck) {
return null;
}
throw new NotFound("Layout $fullName is no allowed in set.");
}
return $this->entityManager
->getRDBRepositoryByClass(LayoutRecord::class)
->where([
'layoutSetId' => $setId,
'name' => $fullName,
])
->findOne();
}
/**
* @throws NotFound
* @throws Error
* @throws Forbidden
*/
public function update(string $scope, string $name, ?string $setId, mixed $data): mixed
{
if (!$this->isCustomizable($scope)) {
throw new Forbidden("$scope is not customizable.");
}
if ($setId) {
$layout = $this->getRecordFromSet($scope, $name, $setId);
if (!$layout) {
$layout = $this->entityManager->getNewEntity(LayoutRecord::ENTITY_TYPE);
$layout->set([
'layoutSetId' => $setId,
'name' => $scope . '.' . $name,
]);
}
$layout->set('data', Json::encode($data));
$this->entityManager->saveEntity($layout);
return Json::decode(
$layout->getData()
);
}
$layoutManager = $this->layoutManager;
$layoutManager->set($data, $scope, $name);
$layoutManager->save();
$this->dataManager->updateCacheTimestamp();
return $layoutManager->get($scope, $name);
}
/**
* @return array<int, mixed>|stdClass|null
* @throws NotFound
* @throws Error
*/
public function resetToDefault(string $scope, string $name, ?string $setId = null): mixed
{
$this->dataManager->updateCacheTimestamp();
if ($setId) {
$layout = $this->getRecordFromSet($scope, $name, $setId);
if ($layout) {
$em = $this->entityManager;
$em->removeEntity($layout);
}
return $this->getOriginal($scope, $name);
}
$this->layoutManager->resetToDefault($scope, $name);
if ($name === 'bottomPanelsDetail') {
$this->resetToDefaultBottomPanelsDetail($scope);
}
return $this->getOriginal($scope, $name);
}
/**
* @throws Error
* @throws NotFound
*/
private function getOriginalBottomPanelsDetail(string $scope): stdClass
{
$relationships = $this->getOriginal($scope, 'relationships') ?? [];
if (!is_array($relationships)) {
throw new Error("Bad 'relationships' layout.");
}
$result = (object) [];
foreach ($relationships as $i => $item) {
if (is_string($item)) {
$item = (object) [
'name' => $item,
];
}
if (!is_object($item)) {
continue;
}
/** @var stdClass $item */
$item = clone $item;
$item->index = 5 + 0.001 * $i;
if (!isset($item->name)) {
continue;
}
$result->{$item->name} = $item;
}
return $result;
}
/**
* @throws NotFound
* @throws Error
*/
private function getForFrontendBottomPanelsDetail(string $scope): stdClass
{
return $this->getOriginalBottomPanelsDetail($scope);
}
private function resetToDefaultBottomPanelsDetail(string $scope): void
{
$this->layoutManager->resetToDefault($scope, 'relationships');
}
private function getUserLayoutSetId(): ?string
{
if ($this->user->isPortal()) {
$portalId = $this->user->getPortalId();
if (!$portalId) {
return null;
}
$portal = $this->entityManager
->getRDBRepositoryByClass(Portal::class)
->select(['layoutSetId'])
->where([Attribute::ID => $portalId])
->findOne();
if (!$portal) {
return null;
}
return $portal->getLayoutSet()?->getId();
}
if ($this->user->getLayoutSet()) {
return $this->user->getLayoutSet()->getId();
}
$teamId = $this->user->getDefaultTeam()?->getId();
if (!$teamId) {
return null;
}
$team = $this->entityManager
->getRDBRepositoryByClass(Team::class)
->select(['layoutSetId'])
->where([Attribute::ID => $teamId])
->findOne();
if (!$team) {
return null;
}
return $team->getLayoutSet()?->getId();
}
private function isCustomizable(string $scope): bool
{
if (!$this->metadata->get("scopes.$scope.customizable")) {
return false;
}
if ($this->metadata->get("scopes.$scope.entityManager.layouts") === false) {
return false;
}
return true;
}
}