Issues (3627)

CoreBundle/Templating/TemplateReference.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CoreBundle\Templating;
13
14
use Mautic\CoreBundle\Helper\PathsHelper;
15
use Mautic\CoreBundle\Helper\ThemeHelper;
16
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference as BaseTemplateReference;
17
18
class TemplateReference extends BaseTemplateReference
19
{
20
    /**
21
     * @var string
22
     */
23
    protected $themeOverride;
24
25
    /**
26
     * @var ThemeHelper
27
     */
28
    protected $themeHelper;
29
30
    /**
31
     * @var PathsHelper
32
     */
33
    protected $pathsHelper;
34
35
    public function setThemeHelper(ThemeHelper $themeHelper)
36
    {
37
        $this->themeHelper = $themeHelper;
38
    }
39
40
    public function setPathsHelper(PathsHelper $pathsHelper)
41
    {
42
        $this->pathsHelper = $pathsHelper;
43
    }
44
45
    /**
46
     * Set a template specific theme override.
47
     *
48
     * @param string $theme
49
     */
50
    public function setThemeOverride($theme)
51
    {
52
        $this->themeOverride = $theme;
53
    }
54
55
    public function getPath()
56
    {
57
        $controller = str_replace('\\', '/', $this->get('controller'));
58
59
        if (!empty($this->themeOverride)) {
60
            try {
61
                $theme    = $this->themeHelper->getTheme($this->themeOverride);
62
                $themeDir = $theme->getThemePath();
63
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
64
            }
65
        } else {
66
            $theme    = $this->themeHelper->getTheme();
67
            $themeDir = $theme->getThemePath();
68
        }
69
70
        $fileName = $this->get('name').'.'.$this->get('format').'.'.$this->get('engine');
71
        $path     = (empty($controller) ? '' : $controller.'/').$fileName;
72
73
        if (!empty($this->parameters['bundle'])) {
74
            $bundleRoot = $this->pathsHelper->getSystemPath('bundles', true);
75
            $pluginRoot = $this->pathsHelper->getSystemPath('plugins', true);
76
77
            // Check for a system-wide override
78
            $themePath      = $this->pathsHelper->getSystemPath('themes', true);
79
            $systemTemplate = $themePath.'/system/'.$this->parameters['bundle'].'/'.$path;
80
81
            if (file_exists($systemTemplate)) {
82
                $template = $systemTemplate;
83
            } else {
84
                //check for an override and load it if there is
85
                if (!empty($themeDir) && file_exists($themeDir.'/html/'.$this->parameters['bundle'].'/'.$path)) {
86
                    // Theme override
87
                    $template = $themeDir.'/html/'.$this->parameters['bundle'].'/'.$path;
88
                } else {
89
                    // We prefer /*Bundle/Views/something.html.php
90
                    preg_match('/Mautic(.*?)Bundle/', $this->parameters['bundle'], $match);
91
92
                    if (
93
                        (!empty($match[1]) && file_exists($bundleRoot.'/'.$match[1].'Bundle/Views/'.$path)) ||
94
                        file_exists($pluginRoot.'/'.$this->parameters['bundle'].'/Views/'.$path) || // Check plugin dir directly
95
                        file_exists($bundleRoot.'/'.$this->parameters['bundle'].'/Views/'.$path) // Bundles dir directly
96
                    ) {
97
                        // Mautic core template
98
                        $template = '@'.$this->get('bundle').'/Views/'.$path;
99
                    }
100
                }
101
            }
102
        } else {
103
            $themes = $this->themeHelper->getInstalledThemes();
104
            if (isset($themes[$controller])) {
105
                //this is a file in a specific Mautic theme folder
106
                $theme = $this->themeHelper->getTheme($controller);
107
108
                $template = $theme->getThemePath().'/html/'.$fileName;
109
            }
110
        }
111
112
        if (empty($template)) {
113
            // Try the parent
114
            return parent::getPath();
115
        }
116
117
        return $template;
118
    }
119
120
    public function getLogicalName()
121
    {
122
        $logicalName = parent::getLogicalName();
123
124
        if (!empty($this->themeOverride)) {
125
            $logicalName = $this->themeOverride.'|'.$logicalName;
126
        }
127
128
        return $logicalName;
129
    }
130
}
131