diff --git a/application/Espo/Classes/FieldValidators/WorkingTimeRange/Calendars/OnlyIfNoUsers.php b/application/Espo/Classes/FieldValidators/WorkingTimeRange/Calendars/OnlyIfNoUsers.php new file mode 100644 index 0000000000..746fa83a86 --- /dev/null +++ b/application/Espo/Classes/FieldValidators/WorkingTimeRange/Calendars/OnlyIfNoUsers.php @@ -0,0 +1,55 @@ +. + * + * 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\Classes\FieldValidators\WorkingTimeRange\Calendars; + +use Espo\Core\FieldValidation\Validator; +use Espo\Core\FieldValidation\Validator\Data; +use Espo\Core\FieldValidation\Validator\Failure; +use Espo\Entities\WorkingTimeRange; +use Espo\ORM\Entity; + +/** + * @implements Validator + */ +class OnlyIfNoUsers implements Validator +{ + public function validate(Entity $entity, string $field, Data $data): ?Failure + { + if ($entity->getCalendars()->getCount() === 0) { + return null; + } + + if ($entity->getUsers()->getCount() === 0) { + return null; + } + + return Failure::create(); + } +} diff --git a/application/Espo/Classes/FieldValidators/WorkingTimeRange/Calendars/RequiredIfNoUsers.php b/application/Espo/Classes/FieldValidators/WorkingTimeRange/Calendars/RequiredIfNoUsers.php new file mode 100644 index 0000000000..7730b09450 --- /dev/null +++ b/application/Espo/Classes/FieldValidators/WorkingTimeRange/Calendars/RequiredIfNoUsers.php @@ -0,0 +1,52 @@ +. + * + * 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\Classes\FieldValidators\WorkingTimeRange\Calendars; + +use Espo\Core\FieldValidation\Validator; +use Espo\Core\FieldValidation\Validator\Data; +use Espo\Core\FieldValidation\Validator\Failure; +use Espo\Entities\WorkingTimeRange; +use Espo\ORM\Entity; + +/** + * @implements Validator + */ +class RequiredIfNoUsers implements Validator +{ + + public function validate(Entity $entity, string $field, Data $data): ?Failure + { + if ($entity->getCalendars()->getCount() !== 0 || $entity->getUsers()->getCount() !== 0) { + return null; + } + + return Failure::create(); + } +} diff --git a/application/Espo/Classes/FieldValidators/WorkingTimeRange/Users/OnlyIfNoCalendars.php b/application/Espo/Classes/FieldValidators/WorkingTimeRange/Users/OnlyIfNoCalendars.php new file mode 100644 index 0000000000..24c82a5a3c --- /dev/null +++ b/application/Espo/Classes/FieldValidators/WorkingTimeRange/Users/OnlyIfNoCalendars.php @@ -0,0 +1,55 @@ +. + * + * 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\Classes\FieldValidators\WorkingTimeRange\Users; + +use Espo\Core\FieldValidation\Validator; +use Espo\Core\FieldValidation\Validator\Data; +use Espo\Core\FieldValidation\Validator\Failure; +use Espo\Entities\WorkingTimeRange; +use Espo\ORM\Entity; + +/** + * @implements Validator + */ +class OnlyIfNoCalendars implements Validator +{ + public function validate(Entity $entity, string $field, Data $data): ?Failure + { + if ($entity->getUsers()->getCount() === 0) { + return null; + } + + if ($entity->getCalendars()->getCount() === 0) { + return null; + } + + return Failure::create(); + } +} diff --git a/application/Espo/Entities/WorkingTimeRange.php b/application/Espo/Entities/WorkingTimeRange.php index 282842520c..08ba264e75 100644 --- a/application/Espo/Entities/WorkingTimeRange.php +++ b/application/Espo/Entities/WorkingTimeRange.php @@ -128,4 +128,13 @@ class WorkingTimeRange extends Entity /** @var LinkMultiple */ return $this->getValueObject('users'); } + + /** + * @since 9.3.1 + */ + public function getCalendars(): LinkMultiple + { + /** @var LinkMultiple */ + return $this->getValueObject('calendars'); + } } diff --git a/application/Espo/Resources/layouts/WorkingTimeRange/detail.json b/application/Espo/Resources/layouts/WorkingTimeRange/detail.json index ccfc2151eb..67a59492b4 100644 --- a/application/Espo/Resources/layouts/WorkingTimeRange/detail.json +++ b/application/Espo/Resources/layouts/WorkingTimeRange/detail.json @@ -13,9 +13,13 @@ {"name": "timeRanges"}, false ], + [ + {"name": "users"}, + false + ], [ {"name": "calendars"}, - {"name": "users"} + false ], [ {"name": "description"} diff --git a/application/Espo/Resources/layouts/WorkingTimeRange/detailSmall.json b/application/Espo/Resources/layouts/WorkingTimeRange/detailSmall.json index ccfc2151eb..67a59492b4 100644 --- a/application/Espo/Resources/layouts/WorkingTimeRange/detailSmall.json +++ b/application/Espo/Resources/layouts/WorkingTimeRange/detailSmall.json @@ -13,9 +13,13 @@ {"name": "timeRanges"}, false ], + [ + {"name": "users"}, + false + ], [ {"name": "calendars"}, - {"name": "users"} + false ], [ {"name": "description"} diff --git a/application/Espo/Resources/metadata/entityDefs/WorkingTimeRange.json b/application/Espo/Resources/metadata/entityDefs/WorkingTimeRange.json index 3a89ba0bf3..80d9090ffe 100644 --- a/application/Espo/Resources/metadata/entityDefs/WorkingTimeRange.json +++ b/application/Espo/Resources/metadata/entityDefs/WorkingTimeRange.json @@ -34,12 +34,20 @@ }, "calendars": { "type": "linkMultiple", - "tooltip": true + "tooltip": true, + "validatorClassNameList": [ + "Espo\\Classes\\FieldValidators\\WorkingTimeRange\\Calendars\\OnlyIfNoUsers", + "Espo\\Classes\\FieldValidators\\WorkingTimeRange\\Calendars\\RequiredIfNoUsers" + ], + "autocompleteOnEmpty": true }, "users": { "type": "linkMultiple", "view": "views/working-time-range/fields/users", - "tooltip": true + "tooltip": true, + "validatorClassNameList": [ + "Espo\\Classes\\FieldValidators\\WorkingTimeRange\\Users\\OnlyIfNoCalendars" + ] }, "createdAt": { "type": "datetime", diff --git a/application/Espo/Resources/metadata/logicDefs/WorkingTimeRange.json b/application/Espo/Resources/metadata/logicDefs/WorkingTimeRange.json index 605b9f7262..3684ab3489 100644 --- a/application/Espo/Resources/metadata/logicDefs/WorkingTimeRange.json +++ b/application/Espo/Resources/metadata/logicDefs/WorkingTimeRange.json @@ -11,7 +11,7 @@ ] } }, - "users": { + "calendars": { "visible": { "conditionGroup": [ { @@ -19,8 +19,36 @@ "value": [ { "type": "isNotEmpty", - "attribute": "id" + "attribute": "calendarsIds" }, + { + "type": "isEmpty", + "attribute": "usersIds" + } + ] + } + ] + } + }, + "users": { + "required": { + "conditionGroup": [ + { + "type": "or", + "value": [ + { + "type": "isEmpty", + "attribute": "calendarsIds" + } + ] + } + ] + }, + "visible": { + "conditionGroup": [ + { + "type": "or", + "value": [ { "type": "isNotEmpty", "attribute": "usersIds"