Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 52 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| CssHelper | |
0.00% |
0 / 52 |
|
0.00% |
0 / 6 |
506 | |
0.00% |
0 / 1 |
| _buildWarning | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| saveCssFile | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| downloadCssFile | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| cssToArray | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
72 | |||
| arrayToCss | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |||
| loadCssFile | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2013-2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoQtiItem\helpers; |
| 22 | |
| 23 | use core_kernel_classes_Resource as RdfResource; |
| 24 | |
| 25 | class CssHelper |
| 26 | { |
| 27 | // phpcs:disable PSR2.Methods.MethodDeclaration |
| 28 | private static function _buildWarning(): string |
| 29 | { |
| 30 | return " /* Do not edit */"; |
| 31 | } |
| 32 | // phpcs:enable PSR2.Methods.MethodDeclaration |
| 33 | |
| 34 | /** |
| 35 | * Stores an css array in the file |
| 36 | */ |
| 37 | public static function saveCssFile(RdfResource $item, string $lang, string $styleSheetPath, array $cssArr): bool |
| 38 | { |
| 39 | $directory = \taoItems_models_classes_ItemsService::singleton()->getItemDirectory($item, $lang); |
| 40 | |
| 41 | $file = $directory->getFile($styleSheetPath); |
| 42 | |
| 43 | if (empty($cssArr) && $file->exists()) { |
| 44 | return $file->delete(); |
| 45 | } |
| 46 | |
| 47 | $css = self::_buildWarning() . self::arrayToCss($cssArr); |
| 48 | return $file->put($css); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Download existing CSS file |
| 53 | * |
| 54 | * @return string with css classes on null if file not exist |
| 55 | */ |
| 56 | public static function downloadCssFile(RdfResource $item, string $lang, string $styleSheetPath): ?string |
| 57 | { |
| 58 | $directory = \taoItems_models_classes_ItemsService::singleton()->getItemDirectory($item, $lang); |
| 59 | $file = $directory->getFile($styleSheetPath); |
| 60 | if ($file->exists()) { |
| 61 | return $file->read(); |
| 62 | } |
| 63 | return null; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Convert incoming CSS to CSS array |
| 68 | * This CSS must have the format generated by the online editor |
| 69 | */ |
| 70 | public static function cssToArray(string $css): array |
| 71 | { |
| 72 | if (!$css) { |
| 73 | return []; |
| 74 | } |
| 75 | $css = str_replace(self::_buildWarning(), '', $css); |
| 76 | $oldCssArr = explode("\n", $css); |
| 77 | $newCssArr = []; |
| 78 | foreach ($oldCssArr as $line) { |
| 79 | if (false === strpos($line, '{')) { |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | preg_match('~(?P<selector>[^{]+)(\{)(?P<rules>[^}]+)\}~', $line, $matches); |
| 84 | |
| 85 | foreach ($matches as $key => &$match) { |
| 86 | if (is_numeric($key)) { |
| 87 | continue; |
| 88 | } |
| 89 | $match = trim($match); |
| 90 | if ($key === 'rules') { |
| 91 | $ruleSet = array_filter(array_map('trim', explode(';', $match))); |
| 92 | $match = []; |
| 93 | foreach ($ruleSet as $rule) { |
| 94 | $rule = array_map('trim', explode(':', $rule)); |
| 95 | $match[$rule[0]] = $rule[1]; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | $newCssArr[$matches['selector']] = $matches['rules']; |
| 101 | } |
| 102 | return $newCssArr; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Convert incoming CSS array to proper CSS |
| 107 | */ |
| 108 | public static function arrayToCss(array $array): string |
| 109 | { |
| 110 | $css = ''; |
| 111 | |
| 112 | // rebuild CSS |
| 113 | foreach ($array as $key1 => $value1) { |
| 114 | $css .= $key1 . '{'; |
| 115 | |
| 116 | foreach ($value1 as $key2 => $value2) { |
| 117 | // in the case that the code is embedded in a media query |
| 118 | if (is_array($value2)) { |
| 119 | foreach ($value2 as $value3) { |
| 120 | $css .= $key2 . '{'; |
| 121 | foreach ($value3 as $mProp) { |
| 122 | $css .= $mProp . ':' . $value3 . ';'; |
| 123 | } |
| 124 | $css .= '}'; |
| 125 | } |
| 126 | } else { // regular selectors |
| 127 | $css .= $key2 . ':' . $value2 . ';'; |
| 128 | } |
| 129 | } |
| 130 | $css .= "}\n"; |
| 131 | } |
| 132 | return $css; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Loads the content of a css file into a css array |
| 137 | * Returns an empty stylesheet if it does not yet exist |
| 138 | */ |
| 139 | public static function loadCssFile(RdfResource $item, string $lang, string $styleSheet): array |
| 140 | { |
| 141 | $directory = \taoItems_models_classes_ItemsService::singleton()->getItemDirectory($item, $lang); |
| 142 | |
| 143 | // no user style sheet has been created yet |
| 144 | $file = $directory->getFile($styleSheet); |
| 145 | if (!$file->exists()) { |
| 146 | \common_Logger::d('Stylesheet ' . $styleSheet . ' does not exist yet, returning empty array'); |
| 147 | return []; |
| 148 | } |
| 149 | |
| 150 | return self::cssToArray($file->read()); |
| 151 | } |
| 152 | } |