Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Template | |
0.00% |
0 / 31 |
|
0.00% |
0 / 7 |
380 | |
0.00% |
0 / 1 |
| img | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| css | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| js | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| inc | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
42 | |||
| getTemplate | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| getAssetService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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) 2014 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\tao\helpers; |
| 24 | |
| 25 | use oat\tao\model\theme\Theme; |
| 26 | use oat\oatbox\service\ServiceManager; |
| 27 | use oat\tao\model\asset\AssetService; |
| 28 | |
| 29 | class Template |
| 30 | { |
| 31 | /** |
| 32 | * Expects a relative url to the image as path |
| 33 | * if extension name is omitted the current extension is used |
| 34 | * |
| 35 | * @param string $path |
| 36 | * @param string $extensionId |
| 37 | * @return string |
| 38 | */ |
| 39 | public static function img($path, $extensionId = null) |
| 40 | { |
| 41 | if (is_null($extensionId)) { |
| 42 | $extensionId = \Context::getInstance()->getExtensionName(); |
| 43 | } |
| 44 | |
| 45 | return self::getAssetService()->getAsset('img/' . $path, $extensionId); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Expects a relative url to the css as path |
| 50 | * if extension name is omitted the current extension is used |
| 51 | * |
| 52 | * @param string $path |
| 53 | * @param string $extensionId |
| 54 | * @return string |
| 55 | */ |
| 56 | public static function css($path, $extensionId = null) |
| 57 | { |
| 58 | if (is_null($extensionId)) { |
| 59 | $extensionId = \Context::getInstance()->getExtensionName(); |
| 60 | } |
| 61 | return self::getAssetService()->getAsset('css/' . $path, $extensionId); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Expects a relative url to the java script as path |
| 66 | * if extension name is omitted the current extension is used |
| 67 | * |
| 68 | * @param string $path |
| 69 | * @param string $extensionId |
| 70 | * @return string |
| 71 | */ |
| 72 | public static function js($path, $extensionId = null) |
| 73 | { |
| 74 | if (is_null($extensionId)) { |
| 75 | $extensionId = \Context::getInstance()->getExtensionName(); |
| 76 | } |
| 77 | return self::getAssetService()->getAsset('js/' . $path, $extensionId); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Expects a relative url to the template that is to be included as path |
| 82 | * if extension name is omitted the current extension is used |
| 83 | * |
| 84 | * @param string $path |
| 85 | * @param string $extensionId |
| 86 | * @param array $data bind additional data to the context |
| 87 | */ |
| 88 | public static function inc($path, $extensionId = null, $data = []) |
| 89 | { |
| 90 | $context = \Context::getInstance(); |
| 91 | |
| 92 | if ($extensionId !== null && $extensionId !== $context->getExtensionName()) { |
| 93 | // template is within different extension, change context |
| 94 | $formerContext = $context->getExtensionName(); |
| 95 | $context->setExtensionName($extensionId); |
| 96 | } |
| 97 | |
| 98 | if (!empty($data)) { |
| 99 | \RenderContext::pushContext($data); |
| 100 | } |
| 101 | |
| 102 | $absPath = Layout::getThemeTemplate(Theme::CONTEXT_BACKOFFICE, $path) ?? self::getTemplate($path, $extensionId); |
| 103 | |
| 104 | if (file_exists($absPath)) { |
| 105 | include($absPath); |
| 106 | } else { |
| 107 | \common_Logger::w('Failed to include "' . $absPath . '" in template'); |
| 108 | } |
| 109 | // restore context |
| 110 | if (isset($formerContext)) { |
| 111 | $context->setExtensionName($formerContext); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @param $path |
| 117 | * @param null $extensionId |
| 118 | * @return string |
| 119 | */ |
| 120 | public static function getTemplate($path, $extensionId = null) |
| 121 | { |
| 122 | $extensionId = is_null($extensionId) ? \Context::getInstance()->getExtensionName() : $extensionId; |
| 123 | $ext = \common_ext_ExtensionsManager::singleton()->getExtensionById($extensionId); |
| 124 | return $ext->getConstant('DIR_VIEWS') . 'templates' . DIRECTORY_SEPARATOR . $path; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @FIXME get_data and has_data should be used exclusively inside templates (not namespaced) |
| 129 | * @return array|bool |
| 130 | */ |
| 131 | public static function getMessages() |
| 132 | { |
| 133 | $messages = []; |
| 134 | if (has_data('errorMessage')) { |
| 135 | $messages['error'] = get_data('errorMessage'); |
| 136 | } |
| 137 | if (has_data('message')) { |
| 138 | $messages['info'] = get_data('message'); |
| 139 | } |
| 140 | return !!count($messages) ? $messages : false; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @return AssetService |
| 145 | * @throws \common_Exception |
| 146 | * @throws \oat\oatbox\service\ServiceNotFoundException |
| 147 | */ |
| 148 | private static function getAssetService() |
| 149 | { |
| 150 | return ServiceManager::getServiceManager()->get(AssetService::SERVICE_ID); |
| 151 | } |
| 152 | } |