Skip to content

Commit

Permalink
#49 Add files URL template filter
Browse files Browse the repository at this point in the history
  • Loading branch information
amazeika committed Dec 5, 2017
1 parent 899dd8f commit 3371499
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions template/filter/url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Joomlatools Framework - https://www.joomlatools.com/developer/framework/
*
* @copyright Copyright (C) 2007 Johan Janssens and Timble CVBA. (http://www.timble.net)
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
* @link https://github.com/joomlatools/joomlatools-framework for the canonical source repository
*/

/**
* Url Template Filter
*
* Filter allows to create url schemes that are replaced on compile and render.
*
* @author Arunas Mazeika <https://github.com/amazeika>
* @package Koowa\Component\Koowa\Template\Filter
*/
class ComFilesTemplateFilterUrl extends KTemplateFilterAbstract
{
protected function _initialize(KObjectConfig $config)
{
$config->append(array('base_path' => $this->getObject('request')->getBasePath()));

parent::_initialize($config);
}

public function filter(&$text)
{
$pattern = '~files://(.+)/([^\s"\']+?)~isU';

$matches = array();

preg_match_all($pattern, $text, $matches, PREG_SET_ORDER);

foreach ($matches as $match)
{
$container = $this->getObject('com:files.model.containers')->slug($match[1])->fetch();

if (!$container->isNew())
{
if ($base = $this->_getBasePath($container))
{
$url = sprintf('/%s/%s', $base, $match[2]);

$text = str_replace($match[0], $this->_getCleanBasePath($url), $text);
}
}
}
}

protected function _getBasePath($container)
{
return $this->_getCleanBasePath(trim(sprintf('%s/%s', $this->getConfig()->base_path, $container->path), '/'));
}

protected function _getCleanBasePath($path)

This comment has been minimized.

Copy link
@johanjanssens

johanjanssens Dec 18, 2017

Member

@amazeika This method name can probably be chosen a bit better. It's not really a clean base path you are getting here?

This comment has been minimized.

Copy link
@amazeika

amazeika Dec 19, 2017

Author Member

@johanjanssens Yes, agreed. This was some copy/paste from FILEman content plugin. I'll choose a proper name for this.

{
$folders = explode('/', $path);

foreach ($folders as &$folder) {
$folder = rawurlencode($folder);
}

return implode('/', $folders);
}
}

0 comments on commit 3371499

Please sign in to comment.