|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ViewTemplatePageExtension is the admin model class to manage view templates. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @package viewtemplate |
|
9
|
|
|
*/ |
|
10
|
|
|
class ViewTemplatePageExtension extends DataExtension |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Page setting field for module status. |
|
14
|
|
|
* |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
private static $db = [ |
|
18
|
|
|
'EnableViewTemplate' => 'Boolean', |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
4 |
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
4 |
|
parent::__construct(); |
|
24
|
|
|
|
|
25
|
|
|
// This may get called before the schema is created. |
|
26
|
4 |
|
if (!DB::get_schema()->isSchemaUpdating()) { |
|
27
|
3 |
|
$htmlEditorNames = Config::inst()->get('ViewTemplate', 'htmleditor_names'); |
|
28
|
3 |
|
if (is_array($htmlEditorNames)) { |
|
29
|
3 |
|
foreach ($htmlEditorNames as $htmlEditorName) { |
|
30
|
3 |
|
HtmlEditorConfig::get($htmlEditorName)->setOption('viewtemplate', ViewTemplate::get()->map('Title')->toArray()); |
|
31
|
3 |
|
} |
|
32
|
3 |
|
} |
|
33
|
3 |
|
} |
|
34
|
4 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Override the Content property from the page object. |
|
38
|
|
|
* |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function Content() |
|
42
|
|
|
{ |
|
43
|
|
|
// Page content |
|
44
|
2 |
|
$content = $this->owner->Content; |
|
45
|
|
|
|
|
46
|
|
|
// Return original content |
|
47
|
2 |
|
if (!$this->owner->EnableViewTemplate) { |
|
48
|
1 |
|
return $content; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// Replace placeholders |
|
52
|
2 |
|
return $this->replacePlaceholders($content); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Replaces placeholders within the $content property their actual value. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $content |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function replacePlaceholders($content) |
|
63
|
|
|
{ |
|
64
|
|
|
// Find placeholders |
|
65
|
2 |
|
preg_match_all('/{{(.*?)}}/', $this->owner->Content, $matches); |
|
66
|
|
|
|
|
67
|
|
|
// If placeholders found |
|
68
|
2 |
|
if (!empty($matches[1])) { |
|
69
|
|
|
// Fetch view templates |
|
70
|
|
|
/** @var DataList $templates */ |
|
71
|
2 |
|
$templates = DataObject::get('ViewTemplate')->filter([ |
|
72
|
2 |
|
'Title' => $matches[1], |
|
73
|
2 |
|
]); |
|
74
|
|
|
|
|
75
|
|
|
// Replace placeholders with their templates |
|
76
|
2 |
|
foreach ($templates as $template) { |
|
77
|
|
|
// Get template placeholder |
|
78
|
2 |
|
$templateHolder = $template->getPlaceHolder(); |
|
79
|
|
|
// Remove <p> if TinyMCE added them |
|
80
|
2 |
|
$content = str_replace('<p>' . $templateHolder . '</p>', $templateHolder, $content); |
|
81
|
|
|
// Process the template |
|
82
|
2 |
|
$templateContent = SSViewer::fromString($template->ViewTemplate)->process($this->owner); |
|
83
|
|
|
// Replace the placeholder with its template |
|
84
|
2 |
|
$content = str_replace($templateHolder, $templateContent, $content); |
|
85
|
2 |
|
} |
|
86
|
2 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
// Set and return content |
|
89
|
2 |
|
$this->owner->Content = $content; |
|
90
|
|
|
|
|
91
|
2 |
|
return $content; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Add an element to the page settings tab to set the status of this module (enabled or not enabled). |
|
96
|
|
|
* |
|
97
|
|
|
* @param \FieldList $fields |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function updateSettingsFields(\FieldList $fields) |
|
100
|
|
|
{ |
|
101
|
1 |
|
$field = new FieldGroup( |
|
102
|
1 |
|
new CheckboxField('EnableViewTemplate', _t('ViewTemplate.YES', 'Yes'), 0) |
|
103
|
1 |
|
); |
|
104
|
1 |
|
$field->setTitle(_t('ViewTemplate.ViewTemplate', 'Enable view template')); |
|
105
|
1 |
|
$fields->addFieldToTab('Root.Settings', $field); |
|
106
|
1 |
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|