SuiteCRM-Core/include/portability/ApiBeanMapper/ApiBeanMapper.php
Dillon-Brown 75ea167930 Squashed 'public/legacy/' changes from ace35f6573..23d711b703
23d711b703 [Legacy] update install language label
58fe59c8da [Legacy] Dashlet Settings Modal Styling Fixes
b880c34877 [Legacy] User Profile Mobile Styling Fixes
ab33cfc7cf [Legacy] Add acls calculation on legacy list data calls
220bf5b8be [Legacy] Get list of visible modules from module name mapper
a7a0754a2d Fix valid module check
020a5dcb1b [Legacy] add widget acl metadata - update acl config for top widget
89e8f7c0f2 [Legacy] Subpanels Action Button Styling Fixes
6c287af813 remove photo widgets for contacts and leads
4ac8347d61 [Legacy] Admin Release Styling Fixes
3336b10790 Suite8 ChangeLog ModalPopup Styling Fixes
8df8ad992c [Legacy] Add close menu label
1dfdf6a005 [Legacy] Add not module selected label
b234b8a1fe [Legacy] Add parent type ApiBeanMapper
4def7cdfc6 [Legacy] Fix username display for unauthorized users
c9771bd330 [Legacy] DetailView Header Title Overlaps fixes
0a0dd3c2c5 [Legacy] Fix htaccess generation
687d5ea7bd Fix Date Filed for Targets
5f863f4b16 [Legacy] Remove Button Hover Fixes
78672e7a27 [Legacy] Reports footer button removed fixes
4b3a9778e8 [Legacy] Reports Styling Fixes
3ab39b6584 [Legacy] Maps Button Position Fixes
e50f76f3fc [Legacy] Add Tab Dashlet Sapcing Fixes
d2f6433a06 [Legacy] Dashlet Setting Modal Label Fix & Button Fixes
e5137262f7 [Legacy] Activity Stream Post Button Hover color fixes
71d44b7274 [Legacy] Bump version to 8.0.0-rc
abafbc280b [Legacy] Remove license from install app strings
43281fae73 Fix ignore system checks valitation
feab3f0abf [Legacy] Add install labels
b0fc8ac7a6 [Legacy] add new install labels
8fea1b7c7f [Legacy] add install validator class
e4f7648e50 [Legacy] Add Label for Subpanel Edit Line Action
b8ecc0a37e [Legacy] Dashboard Mobile Styling Fixes
7d427720f5 [Legacy] History Subpanel Button Styling Fixes
63d10c40d6 [Legacy] Workflow Stying Fixes
811ad79455 [Legacy] Convert Lead Styling Fixes
01764542a1 [Legacy] Roles Styling Fixes
7ecc573f0e [Legacy] Admin Modules label font fixes
9f13c91e22 [Legacy] HomePage Mobile Fixes
005d1ac949 update legacy handlers to apply new logic considering - base actions - action resolvers - fix url navigation issue in classic view
b3c7a14380 [Legacy] Module framework - move suite 8 module config to legacy
542198ac5c [Legacy] Add support for more metadata on listviewdefs
e3c82fc9ea [Legacy] Add Support for all modules with parent css classes
f2dd774903 [Legacy] Security Group Styling Fixes
b6e5591cc4 [Legacy] Password Management Mobile Fixes
1624fdcfad [Leagcy] Email Settings Fixes
e033259dac [Legacy] Products Modules Buttons fixes
eae8efde52 [Legacy] OAUTH2 Clients and tokens fixes
f90c5f5315 [Legacy] adapt legacy install scripts to work with Suite8 install
d099fac028 [Legacy] add labels for S8 Installer
8be5c78c4c [Legacy] Add widget not found label
eb4449777b [Legacy] Email Action DropDown Styling Fixes
06aff4ce92 SuiteCRM 7.12.0 Release
1cf315c98b Fix BasicSearchResults hit counter
c7458ea520 Update PDF samples for compatibility with new PDF Engine
a2a392f6d3 Update Reports PDF for compatibility with new PDF Engine
575561903b Update PDF Engines for backward compatibility
3ed5326ca1 Deprecate TCPDF
b6c4a4941f Fix TCPDF image scale
d6c47de2f5 Fix TCPDF filename
412fd0d006 Add optional malicious file scanner
f88149fcfe Feature: Allow configuring the Calendar name for the Google Sync via the config.
ae836af690 Fix SearchFormView visible options
5f3545193f Update workflow acceptance test
7c54f0fbc4 SuiteCRM 7.12.0-RC Release
0159e0cd86 Remove unused currency config entries
1c49d879ad Default new MySQL based installs to use utf8mb4
dee7ba0594 Clean up search setting
b9b6aeae3a Cleanup old CSS files
ed59f5f6d9 Add missing admin icons
732e84c6a3 Move Workflow to default as admin function
60dce30854 Update admin panel to improve grouping
6f7524b46a Fix filepath for mPDF class
cdab640886 Add default Engines
f79cc55782 Remove MPDFEngine Test
77d38a259d Add check for MPDF class
59261aac8d Remove PDF_Lib
49fb8525a0 Implement TCPDF

git-subtree-dir: public/legacy
git-subtree-split: 23d711b70345ccf90d7508ce6aa5ddc8fd3dc4dd
2021-11-05 15:19:00 +00:00

501 lines
15 KiB
PHP

<?php
/**
* SuiteCRM is a customer relationship management program developed by SalesAgility Ltd.
* Copyright (C) 2021 SalesAgility Ltd.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by the
* Free Software Foundation with the addition of the following permission added
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
* IN WHICH THE COPYRIGHT IS OWNED BY SALESAGILITY, SALESAGILITY DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* 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 http://www.gnu.org/licenses.
*
* 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
* "Supercharged by SuiteCRM" logo. If the display of the logos is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Supercharged by SuiteCRM".
*/
require_once __DIR__ . '/FieldMappers/AssignedUserMapper.php';
require_once __DIR__ . '/LinkMappers/LinkMapperInterface.php';
require_once __DIR__ . '/LinkMappers/EmailAddressLinkMapper.php';
require_once __DIR__ . '/TypeMappers/FullNameMapper.php';
require_once __DIR__ . '/TypeMappers/ParentMapper.php';
require_once __DIR__ . '/TypeMappers/DateMapper.php';
require_once __DIR__ . '/TypeMappers/DateTimeMapper.php';
require_once __DIR__ . '/TypeMappers/DateTimeComboMapper.php';
require_once __DIR__ . '/TypeMappers/MultiEnumMapper.php';
require_once __DIR__ . '/TypeMappers/BooleanMapper.php';
require_once __DIR__ . '/ApiBeanModuleMappers.php';
require_once __DIR__ . '/ModuleMappers/SavedSearch/SavedSearchMappers.php';
require_once __DIR__ . '/ModuleMappers/AOP_Case_Updates/CaseUpdatesMappers.php';
class ApiBeanMapper
{
/**
* @var FieldMapperInterface[]
*/
protected $fieldMappers = [];
/**
* @var TypeMapperInterface[]
*/
protected $typeMappers = [];
/**
* @var LinkMapperInterface[][]
*/
protected $linkMappers = [];
/**
* @var ApiBeanModuleMappers[]
*/
protected $moduleMappers = [];
public function __construct()
{
$this->fieldMappers[AssignedUserMapper::getField()] = new AssignedUserMapper();
$this->typeMappers[FullNameMapper::getType()] = new FullNameMapper();
$this->typeMappers[ParentMapper::getType()] = new ParentMapper();
$this->typeMappers[DateMapper::getType()] = new DateMapper();
$this->typeMappers[DateTimeMapper::getType()] = new DateTimeMapper();
$this->typeMappers[MultiEnumMapper::getType()] = new MultiEnumMapper();
$this->typeMappers[BooleanMapper::getType()] = new BooleanMapper();
$this->typeMappers['boolean'] = $this->typeMappers[BooleanMapper::getType()];
$this->moduleMappers[SavedSearchMappers::getModule()] = new SavedSearchMappers();
$this->typeMappers[DateTimeComboMapper::getType()] = new DateTimeMapper();
$this->linkMappers[EmailAddressLinkMapper::getRelateModule()] = [];
$this->linkMappers[EmailAddressLinkMapper::getRelateModule()]['all'] = new EmailAddressLinkMapper();
$this->moduleMappers[CaseUpdatesMappers::getModule()] = new CaseUpdatesMappers();
}
/**
* @param SugarBean $bean
* @return array
*/
public function toApi(SugarBean $bean): array
{
$arr = [];
$arr['module_name'] = $bean->module_name ?? '';
$arr['object_name'] = $bean->object_name ?? '';
foreach ($bean->field_defs as $field => $definition) {
if ($this->isSensitiveField($definition)) {
continue;
}
if (!$this->checkFieldAccess($bean, $definition)) {
continue;
}
if ($this->isLinkField($definition)) {
if (!$this->hasLinkMapper($bean->module_name, $definition)) {
continue;
}
$this->mapLinkFieldToApi($bean, $arr, $definition);
continue;
}
if ($this->isRelateField($definition)) {
$this->addRelateFieldToArray($bean, $definition, $arr, $field);
continue;
}
$this->setValue($bean, $field, $arr, $definition);
}
return $arr;
}
/**
* @param SugarBean $bean
* @param array $values
* @return void
*/
public function toBean(SugarBean $bean, array $values): void
{
require_once __DIR__ . '/../../../include/SugarFields/SugarFieldHandler.php';
foreach ($bean->field_defs as $field => $properties) {
if (!isset($values[$field])) {
continue;
}
$this->toBeanMap($bean, $values, $properties, $field);
if ($this->isLinkField($properties)) {
if (!$this->hasLinkMapper($bean->module_name, $properties)) {
continue;
}
$this->mapLinkFieldToBean($bean, $values, $properties);
}
$bean->$field = $values[$field];
}
foreach ($bean->relationship_fields as $field => $link) {
if (!empty($values[$field])) {
$bean->$field = $values[$field];
}
}
}
/**
* @param SugarBean $bean
* @param array $values
* @return void
*/
public function toBeanAttributes(SugarBean $bean, array &$values): void
{
require_once __DIR__ . '/../../../include/SugarFields/SugarFieldHandler.php';
foreach ($bean->field_defs as $field => $properties) {
if (!isset($values[$field])) {
continue;
}
$this->toBeanMap($bean, $values, $properties, $field);
}
}
/**
* @param $definition
* @return bool
*/
protected function isRelateField($definition): bool
{
return isset($definition['type']) && $definition['type'] === 'relate';
}
/**
* @param $definition
* @return bool
*/
protected function isLinkField($definition): bool
{
return isset($definition['type']) && $definition['type'] === 'link';
}
/**
* @param $fieldDefinition
* @return bool
*/
protected function isSensitiveField($fieldDefinition): bool
{
return $fieldDefinition['sensitive'] ?? false;
}
/**
* @param $fieldDefinition
* @return bool
*/
protected function isAdminOnlyField($fieldDefinition): bool
{
return $fieldDefinition['admin-only'] ?? false;
}
/**
* @param $fieldDefinition
* @return bool
*/
protected function isOwnerOnlyField($fieldDefinition): bool
{
return $fieldDefinition['owner-only'] ?? false;
}
/**
* @param $fieldDefinition
* @return bool
*/
protected function checkAdminOnlyField($fieldDefinition): bool
{
global $current_user;
$isAdminOnlyField = $this->isAdminOnlyField($fieldDefinition);
if (!$isAdminOnlyField) {
return true;
}
return $isAdminOnlyField && $current_user->isAdmin();
}
/**
* @param $fieldDefinition
* @param SugarBean $bean
* @return bool
*/
protected function checkOwnerOnlyField($fieldDefinition, SugarBean $bean): bool
{
global $current_user;
$assignedUserId = $bean->assigned_user_id ?? '';
$isOwnerOnlyField = $this->isOwnerOnlyField($fieldDefinition);
if (!$isOwnerOnlyField) {
return true;
}
return $isOwnerOnlyField && $current_user->id === $assignedUserId;
}
/**
* @param SugarBean $bean
* @param $definition
* @return bool
*/
protected function checkFieldAccess(SugarBean $bean, $definition): bool
{
if (!$this->isAdminOnlyField($definition) && !$this->isOwnerOnlyField($definition)) {
return true;
}
return !$this->checkAdminOnlyField($definition) && !$this->checkOwnerOnlyField($definition, $bean);
}
/**
* @param SugarBean $bean
* @param $definition
* @param array $arr
* @param $field
*/
protected function addRelateFieldToArray(SugarBean $bean, $definition, array &$arr, $field): void
{
$fieldRName = $definition['rname'] ?? 'name';
$idName = $definition['id_name'] ?? '';
$source = $definition['source'] ?? '';
$idDefinition = $definition[$idName] ?? [];
$groupingField = $field;
if ($source !== 'non-db') {
$this->setValue($bean, $field, $arr, $definition);
return;
}
if ($idName === $field) {
$this->setValue($bean, $field, $arr, $definition);
return;
}
$arr[$groupingField] = $arr[$groupingField] ?? [];
$this->setValue($bean, $field, $arr[$groupingField], $definition, $fieldRName);
if (isset($bean->$idName)) {
$idFieldRName = $idDefinition['rname'] ?? 'id';
$this->setValue($bean, $idName, $arr[$groupingField], $definition, $idFieldRName);
}
}
/**
* @param SugarBean $bean
* @param $field
* @param array $arr
* @param array $definition
* @param string $alternativeName
*/
protected function setValue(
SugarBean $bean,
$field,
array &$arr,
array $definition,
string $alternativeName = ''
): void {
$name = $field;
if (!empty($alternativeName)) {
$name = $alternativeName;
}
$fieldMapper = $this->getFieldMapper($bean->module_name, $field);
if (null !== $fieldMapper) {
$fieldMapper->toApi($bean, $arr, $name);
return;
}
$type = $definition['type'] ?? '';
$typeMapper = $this->getTypeMappers($bean->module_name, $type);
if (null !== $typeMapper) {
$typeMapper->toApi($bean, $arr, $field, $name);
return;
}
$arr[$name] = html_entity_decode($bean->$field ?? '', ENT_QUOTES);
}
/**
* @param SugarBean $bean
* @param array $container
* @param array $definition
*/
protected function mapLinkFieldToApi(SugarBean $bean, array &$container, array $definition): void
{
$module = $bean->module_name ?? '';
$relateModule = $definition['module'] ?? '';
$name = $definition['name'] ?? '';
$linkMapper = $this->getLinkMapper($module, $relateModule, $name);
if ($linkMapper === null) {
return;
}
$linkMapper->toApi($bean, $container, $name);
}
/**
* @param SugarBean $bean
* @param array $container
* @param array $definition
*/
protected function mapLinkFieldToBean(SugarBean $bean, array &$container, array $definition): void
{
$module = $bean->module_name ?? '';
$relateModule = $definition['module'] ?? '';
$name = $definition['name'] ?? '';
$linkMapper = $this->getLinkMapper($module, $relateModule, $name);
if ($linkMapper === null) {
return;
}
$linkMapper->toBean($bean, $container, $name);
}
/**
* @param string $module
* @param string $field
* @return FieldMapperInterface
*/
protected function getFieldMapper(string $module, string $field): ?FieldMapperInterface
{
$moduleMappers = $this->moduleMappers[$module] ?? null;
if ($moduleMappers !== null && $moduleMappers->hasFieldMapper($field)) {
return $moduleMappers->getFieldMappers()[$field];
}
return $this->fieldMappers[$field] ?? null;
}
/**
* @param string $module
* @param string $relateModule
* @param string $field
* @return LinkMapperInterface
*/
protected function getLinkMapper(string $module, string $relateModule, string $field): ?LinkMapperInterface
{
if ($module === '' || $relateModule === '' || $field === '') {
return null;
}
$moduleMappers = $this->moduleMappers[$module] ?? null;
if ($moduleMappers !== null && $moduleMappers->hasLinkMapper($relateModule, $field)) {
return $moduleMappers->getLinkMapper($relateModule, $field);
}
$moduleLinkMappers = $this->linkMappers[$relateModule] ?? [];
return $moduleLinkMappers[$field] ?? $moduleLinkMappers['all'] ?? null;
}
/**
* @param $definition
* @return bool
*/
protected function hasLinkMapper($module, $definition): bool
{
$relateModule = $definition['module'] ?? '';
$name = $definition['name'] ?? '';
if ($relateModule === '' || $name === '') {
return false;
}
return $this->getLinkMapper($module, $relateModule, $name) !== null;
}
/**
* @param string $module
* @param string $type
* @return TypeMapperInterface
*/
protected function getTypeMappers(string $module, string $type): ?TypeMapperInterface
{
$moduleMappers = $this->moduleMappers[$module] ?? null;
if ($moduleMappers !== null && $moduleMappers->hasTypeMapper($type)) {
return $moduleMappers->getTypeMappers()[$type];
}
return $this->typeMappers[$type] ?? null;
}
/**
* @param SugarBean $bean
* @param array $values
* @param $properties
* @param $field
*/
protected function toBeanMap(SugarBean $bean, array &$values, $properties, $field): void
{
$type = $properties['type'] ?? '';
if ($type === 'relate' && isset($bean->field_defs[$field])) {
$idName = $bean->field_defs[$field]['id_name'] ?? '';
if ($idName !== $field) {
$idValue = $values[$field]['id'] ?? '';
if (empty($values[$idName]) && !empty($idValue)) {
$values[$idName] = $idValue;
}
$rName = $bean->field_defs[$field]['rname'] ?? '';
$value = $values[$field][$rName] ?? '';
$values[$field] = $value;
}
}
if (!empty($properties['isMultiSelect']) || $type === 'multienum') {
$multiSelectValue = $values[$field];
if (!is_array($values[$field])) {
$multiSelectValue = [];
}
$values[$field] = encodeMultienumValue($multiSelectValue);
}
$fieldMapper = $this->getFieldMapper($bean->module_name, $field);
if (null !== $fieldMapper) {
$fieldMapper->toBean($bean, $values, $field);
}
$typeMapper = $this->getTypeMappers($bean->module_name, $type);
if (null !== $typeMapper) {
$typeMapper->toBean($bean, $values, $field, $field);
}
}
}