|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file plugins/generic/thoth/classes/hooks/PublicationFormatFormHandler.php |
| 5 | + * |
| 6 | + * Copyright (c) 2024-2026 Lepidus Tecnologia |
| 7 | + * Copyright (c) 2024-2026 Thoth |
| 8 | + * Distributed under the GNU GPL v3. For full terms see the file docs/COPYING. |
| 9 | + * |
| 10 | + * @class PublicationFormatFormHandler |
| 11 | + * |
| 12 | + * @ingroup plugins_generic_thoth |
| 13 | + * |
| 14 | + * @brief Handles Thoth accessibility metadata in OMP publication format forms |
| 15 | + */ |
| 16 | + |
| 17 | +namespace APP\plugins\generic\thoth\classes\hooks; |
| 18 | + |
| 19 | +use APP\plugins\generic\thoth\classes\templateFilters\PublicationFormatTemplateFilter; |
| 20 | +use APP\template\TemplateManager; |
| 21 | +use PKP\plugins\GenericPlugin; |
| 22 | + |
| 23 | +class PublicationFormatFormHandler |
| 24 | +{ |
| 25 | + public const ACCESSIBILITY_FIELDS = [ |
| 26 | + 'accessibilityStandard', |
| 27 | + 'accessibilityAdditionalStandard', |
| 28 | + 'accessibilityException', |
| 29 | + 'accessibilityReportUrl', |
| 30 | + ]; |
| 31 | + |
| 32 | + private GenericPlugin $plugin; |
| 33 | + |
| 34 | + public function __construct(GenericPlugin $plugin) |
| 35 | + { |
| 36 | + $this->plugin = $plugin; |
| 37 | + } |
| 38 | + |
| 39 | + public function addAccessibilityFields($hookName, $args): bool |
| 40 | + { |
| 41 | + $form = $args[0]; |
| 42 | + $templateMgr = TemplateManager::getManager(); |
| 43 | + $publicationFormat = $form->getPublicationFormat(); |
| 44 | + |
| 45 | + foreach (self::ACCESSIBILITY_FIELDS as $fieldName) { |
| 46 | + if ($publicationFormat && $form->getData($fieldName) === null) { |
| 47 | + $form->setData($fieldName, $publicationFormat->getData($fieldName)); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + $templateMgr->assign([ |
| 52 | + 'thothAccessibilityStandardOptions' => $this->getAccessibilityStandardOptions(), |
| 53 | + 'thothAccessibilityExceptionOptions' => $this->getAccessibilityExceptionOptions(), |
| 54 | + ]); |
| 55 | + |
| 56 | + (new PublicationFormatTemplateFilter($this->plugin))->register($templateMgr); |
| 57 | + |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + public function addAccessibilityFieldNames($hookName, $dao, &$fieldNames): bool |
| 62 | + { |
| 63 | + $fieldNames = array_values(array_unique(array_merge($fieldNames, self::ACCESSIBILITY_FIELDS))); |
| 64 | + |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + public function addAccessibilityUserVars($hookName, $args): bool |
| 69 | + { |
| 70 | + $vars = & $args[1]; |
| 71 | + $vars = array_unique(array_merge($vars, self::ACCESSIBILITY_FIELDS)); |
| 72 | + |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + public function validateAccessibilityFields($hookName, $args): bool |
| 77 | + { |
| 78 | + $form = $args[0]; |
| 79 | + $reportUrl = trim((string) $form->getData('accessibilityReportUrl')); |
| 80 | + |
| 81 | + if ($reportUrl !== '' && filter_var($reportUrl, FILTER_VALIDATE_URL) === false) { |
| 82 | + $form->addError( |
| 83 | + 'accessibilityReportUrl', |
| 84 | + __('plugins.generic.thoth.publicationFormat.accessibilityReportUrl.invalid') |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + public function saveAccessibilityFields($hookName, $args): bool |
| 92 | + { |
| 93 | + $form = $args[0]; |
| 94 | + $publicationFormat = $form->getPublicationFormat(); |
| 95 | + |
| 96 | + if (!$publicationFormat) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + foreach (self::ACCESSIBILITY_FIELDS as $fieldName) { |
| 101 | + $publicationFormat->setData($fieldName, $this->normalizeOptionalValue($form->getData($fieldName))); |
| 102 | + } |
| 103 | + |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + private function normalizeOptionalValue($value): ?string |
| 108 | + { |
| 109 | + $value = trim((string) $value); |
| 110 | + return $value === '' ? null : $value; |
| 111 | + } |
| 112 | + |
| 113 | + private function getAccessibilityStandardOptions(): array |
| 114 | + { |
| 115 | + return [ |
| 116 | + '' => 'common.none', |
| 117 | + 'WCAG21AA' => 'plugins.generic.thoth.publicationFormat.accessibilityStandard.wcag21aa', |
| 118 | + 'WCAG21AAA' => 'plugins.generic.thoth.publicationFormat.accessibilityStandard.wcag21aaa', |
| 119 | + 'WCAG22AA' => 'plugins.generic.thoth.publicationFormat.accessibilityStandard.wcag22aa', |
| 120 | + 'WCAG22AAA' => 'plugins.generic.thoth.publicationFormat.accessibilityStandard.wcag22aaa', |
| 121 | + 'EPUB_A11Y10AA' => 'plugins.generic.thoth.publicationFormat.accessibilityStandard.epubA11y10aa', |
| 122 | + 'EPUB_A11Y10AAA' => 'plugins.generic.thoth.publicationFormat.accessibilityStandard.epubA11y10aaa', |
| 123 | + 'EPUB_A11Y11AA' => 'plugins.generic.thoth.publicationFormat.accessibilityStandard.epubA11y11aa', |
| 124 | + 'EPUB_A11Y11AAA' => 'plugins.generic.thoth.publicationFormat.accessibilityStandard.epubA11y11aaa', |
| 125 | + 'PDF_UA1' => 'plugins.generic.thoth.publicationFormat.accessibilityStandard.pdfUa1', |
| 126 | + 'PDF_UA2' => 'plugins.generic.thoth.publicationFormat.accessibilityStandard.pdfUa2', |
| 127 | + ]; |
| 128 | + } |
| 129 | + |
| 130 | + private function getAccessibilityExceptionOptions(): array |
| 131 | + { |
| 132 | + return [ |
| 133 | + '' => 'common.none', |
| 134 | + 'MICRO_ENTERPRISES' => 'plugins.generic.thoth.publicationFormat.accessibilityException.microEnterprises', |
| 135 | + 'DISPROPORTIONATE_BURDEN' => 'plugins.generic.thoth.publicationFormat.accessibilityException.disproportionateBurden', |
| 136 | + 'FUNDAMENTAL_ALTERATION' => 'plugins.generic.thoth.publicationFormat.accessibilityException.fundamentalAlteration', |
| 137 | + ]; |
| 138 | + } |
| 139 | +} |
0 commit comments