function.translate_url.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
13 /*
14 * Smarty plugin
15 * -------------------------------------------------------------
16 * File: function.translate_url.php
17 * Type: function
18 * Name: translate_url
19 * Purpose: translate a relative url into an absolute one. The 'base' parameter
20 * defines the relative path from the executed script to the location
21 * from which the relative path given in the 'url' parameter is seen
22 * from. The result maybe printed directly or assigned to a smarty variable.
23 * If the parameter 'absolute' is set to true, the url will be given absolute
24 * (the default value is false which means the url is relative).
25 * Example: If an image is stored in the database under the location
26 * '../../media/image1.png' as seen from http://www.example.com/cms/application/main.php
27 * and the image should be displayed in a template that is displayed by the script
28 * http://www.example.com/index.php than the image url may be translated by using the
29 * following call:
30 * Usage: {translate_url url=$image->getFile() base="cms/application/"} or
31 * {translate_url url=$image->getFile() base="cms/application/" varname="imageFile" absolute=true}
32 * -------------------------------------------------------------
33 */
34 function smarty_function_translate_url($params, \Smarty_Internal_Template $template) {
35  $url = $params['url'];
36  $base = $params['base'];
37 
38  $urls = URIUtil::translate($url, $base);
39 
40  $result = $urls['relative'];
41  if (isset($params['absolute']) && $params['absolute']) {
42  $result = $urls['absolute'];
43  }
44  if (isset($params['varname'])) {
45  $template->assign($params['varname'], $result);
46  }
47  else {
48  echo $result;
49  }
50 }
51 ?>