|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2017 [email protected] |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace FeatherBB\Core; |
| 19 | + |
| 20 | +class RunBBTwig extends \Twig_Extension |
| 21 | +{ |
| 22 | + |
| 23 | + public function getName() |
| 24 | + { |
| 25 | + return 'runBB_Twig'; |
| 26 | + } |
| 27 | + |
| 28 | + public function getFunctions() |
| 29 | + { |
| 30 | + return [ |
| 31 | + /** |
| 32 | + * fire RunBB hook with or without arguments |
| 33 | + */ |
| 34 | + new \Twig_SimpleFunction('fireHook', function ($name) { |
| 35 | + if (is_array($name)) { |
| 36 | + call_user_func_array([Container::get('hooks'), 'fire'], $name); |
| 37 | + } else { |
| 38 | + Container::get('hooks')->fire($name); |
| 39 | + } |
| 40 | + }, ['is_safe' => ['html']]), |
| 41 | + |
| 42 | + /** |
| 43 | + * return RunBB settings value |
| 44 | + */ |
| 45 | + new \Twig_SimpleFunction('settings', function ($name) { |
| 46 | + return ForumSettings::get($name); |
| 47 | + }, ['is_safe' => ['html']]), |
| 48 | + |
| 49 | + /** |
| 50 | + * Return the translation of a string with or without arguments |
| 51 | + */ |
| 52 | + new \Twig_SimpleFunction('trans', function ($str) { |
| 53 | + if (is_array($str)) { |
| 54 | + var_dump($str); |
| 55 | + return call_user_func_array('__', $str); |
| 56 | + } else { |
| 57 | + return __($str); |
| 58 | + } |
| 59 | + }, ['is_safe' => ['html']]), |
| 60 | + |
| 61 | + /** |
| 62 | + * Return the translation of a string in a specific domain |
| 63 | + * where first argument in array must be domain name |
| 64 | + */ |
| 65 | + new \Twig_SimpleFunction('transd', function ($str) { |
| 66 | + if (is_array($str)) { |
| 67 | + return call_user_func_array('d__', $str); |
| 68 | + } else { |
| 69 | + return $str; |
| 70 | + } |
| 71 | + }, ['is_safe' => ['html']]), |
| 72 | + |
| 73 | + /** |
| 74 | + * return Url::baseStatic() value |
| 75 | + */ |
| 76 | + new \Twig_SimpleFunction('baseStatic', function () { |
| 77 | + return Url::baseStatic(); |
| 78 | + }, ['is_safe' => ['html']]), |
| 79 | + |
| 80 | + /** |
| 81 | + * return Url::base() value |
| 82 | + */ |
| 83 | + new \Twig_SimpleFunction('urlBase', function () { |
| 84 | + return Url::base(); |
| 85 | + }, ['is_safe' => ['html']]), |
| 86 | + |
| 87 | + /** |
| 88 | + * return Url::slug() value |
| 89 | + */ |
| 90 | + new \Twig_SimpleFunction('slug', function ($url) { |
| 91 | + return Url::slug($url); |
| 92 | + }, ['is_safe' => ['html']]), |
| 93 | + |
| 94 | + /** |
| 95 | + * return Router::pathFor() value |
| 96 | + */ |
| 97 | + new \Twig_SimpleFunction('pathFor', function ($name, array $data = [], array $queryParams = []) { |
| 98 | + return Router::pathFor($name, $data, $queryParams); |
| 99 | + }, ['is_safe' => ['html']]), |
| 100 | + |
| 101 | + /** |
| 102 | + * return User::get()->value |
| 103 | + */ |
| 104 | + new \Twig_SimpleFunction('userGet', function ($val) { |
| 105 | + if (User::get()) { |
| 106 | + return User::get()->$val; |
| 107 | + } else { |
| 108 | + return $val; |
| 109 | + } |
| 110 | + }, ['is_safe' => ['html']]), |
| 111 | + |
| 112 | + /** |
| 113 | + * return token FIXME ??? |
| 114 | + */ |
| 115 | + new \Twig_SimpleFunction('getToken', function () { |
| 116 | + return Random::hash(User::get()->id.Random::hash(Utils::getIp())); |
| 117 | + }, ['is_safe' => ['html']]), |
| 118 | + |
| 119 | + /** |
| 120 | + * return given type hash |
| 121 | + */ |
| 122 | + new \Twig_SimpleFunction('getHash', function ($type, $var) { |
| 123 | + if ($type === 'md5') { |
| 124 | + return md5($var); |
| 125 | + }// TODO add types |
| 126 | + }, ['is_safe' => ['html']]), |
| 127 | + |
| 128 | + /** |
| 129 | + * return Container::get('utils')->timeFormat($var) result |
| 130 | + * Container::get('utils')->timeFormat( |
| 131 | + * $timestamp, $date_only, $date_format, $time_format, $time_only, $no_text |
| 132 | + * ) |
| 133 | + */ |
| 134 | + new \Twig_SimpleFunction('formatTime', function ( |
| 135 | + $timestamp, |
| 136 | + $date_only = false, |
| 137 | + $date_format = null, |
| 138 | + $time_format = null, |
| 139 | + $time_only = false, |
| 140 | + $no_text = false |
| 141 | + ) { |
| 142 | + return Container::get('utils')->formatTime( |
| 143 | + $timestamp, |
| 144 | + $date_only, |
| 145 | + $date_format, |
| 146 | + $time_format, |
| 147 | + $time_only, |
| 148 | + $no_text |
| 149 | + ); |
| 150 | + }, ['is_safe' => ['html']]), |
| 151 | + |
| 152 | + /** |
| 153 | + * return Utils::numberFormat($var) result |
| 154 | + */ |
| 155 | + new \Twig_SimpleFunction('formatNumber', function ($var) { |
| 156 | + return Utils::forumNumberFormat($var); |
| 157 | + }, ['is_safe' => ['html']]), |
| 158 | + |
| 159 | + /** |
| 160 | + * return Input::post() result |
| 161 | + * from Request::getParsedBodyParam |
| 162 | + */ |
| 163 | + new \Twig_SimpleFunction('inputPost', function ($var) { |
| 164 | + return Input::post($var); |
| 165 | + }, ['is_safe' => ['html']]), |
| 166 | + |
| 167 | + /** |
| 168 | + * Format user title |
| 169 | + * return Utils::getTitle($title, $name, $groupTitle, $gid) result |
| 170 | + */ |
| 171 | + new \Twig_SimpleFunction('formatTitle', function ($title, $name = '', $groupTitle = '', $gid = '') { |
| 172 | + return Utils::getTitleTwig($title, $name, $groupTitle, $gid); |
| 173 | + }, ['is_safe' => ['html']]), |
| 174 | + |
| 175 | + /** |
| 176 | + * Get forum environment var |
| 177 | + * TODO merge with settings??? |
| 178 | + * return ForumEnv::get($var) result |
| 179 | + */ |
| 180 | + new \Twig_SimpleFunction('getEnv', function ($var) { |
| 181 | + return ForumEnv::get($var); |
| 182 | + }, ['is_safe' => ['html']]), |
| 183 | + |
| 184 | + /** |
| 185 | + * Generate breadcrumbs from an array of name and URLs |
| 186 | + * return AdminUtils::breadcrumbsAdmin($links) result |
| 187 | + */ |
| 188 | + new \Twig_SimpleFunction('breadcrumbsAdmin', function (array $links) { |
| 189 | + return AdminUtils::breadcrumbsAdmin($links); |
| 190 | + }, ['is_safe' => ['html']]), |
| 191 | + |
| 192 | + /** |
| 193 | + * Generate increment |
| 194 | + * return incremented index |
| 195 | + */ |
| 196 | + new \Twig_SimpleFunction('getIndex', function () { |
| 197 | + static $index = 0; |
| 198 | + return ++$index; |
| 199 | + }, ['is_safe' => ['html']]), |
| 200 | + |
| 201 | + /** |
| 202 | + * Unserializer |
| 203 | + * return unserialized array |
| 204 | + */ |
| 205 | + new \Twig_SimpleFunction('unSerialize', function ($var) { |
| 206 | + return unserialize($var); |
| 207 | + }, ['is_safe' => ['html']]), |
| 208 | + ]; |
| 209 | + } |
| 210 | +} |
0 commit comments