-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.php
116 lines (106 loc) · 3.21 KB
/
utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
$GLOBALS['midgard_filters'] = array
(
'h' => 'htmlentities',
'u' => 'rawurlencode',
'f' => 'nl2br',
);
/**
* Register PHP function as string formatter to the Midgard formatting engine.
* @see http://www.midgard-project.org/documentation/reference-other-mgd_register_filter/
*/
function mgd_register_filter($name, $function)
{
$GLOBALS['midgard_filters']["x{$name}"] = $function;
}
/**
* Return a string as formatted by a Midgard formatter
* @see http://www.midgard-project.org/documentation/reference-other-mgd_format/
*/
function mgd_format($content, $name)
{
if (!isset($GLOBALS['midgard_filters'][$name]))
{
return $content;
}
ob_start();
call_user_func($GLOBALS['midgard_filters'][$name], $content);
return ob_get_clean();
}
/**
* Invalidate Midgard's element cache
*
* @todo Caching the elements found by mgd_element() might be a good idea
*/
function mgd_cache_invalidate()
{
}
/**
* Include an element
*/
function mgd_element($name)
{
static $style = null;
$element = $name[1];
// Sensible fallback if we don't have a style or ROOT element
$root_fallback = '<html><head><?php $_MIDCOM->print_head_elements(); ?><title><?php echo $_MIDCOM->get_context_data(MIDCOM_CONTEXT_PAGETITLE); ?></title></head><body class="<?php echo $_MIDCOM->metadata->get_page_class(); ?>"><?php $_MIDCOM->content(); $_MIDCOM->uimessages->show(); $_MIDCOM->toolbars->show(); $_MIDCOM->finish(); ?></body></html>';
switch ($element)
{
case 'title':
case 'content':
return midgardmvc_core::get_instance()->context->page->$element;
break;
default:
// TODO: Element inheritance, page elements
if (!isset(midgardmvc_core::get_instance()->context->style))
{
if ($element == 'ROOT')
{
return $root_fallback;
}
return '';
}
$style = midgardmvc_core::get_instance()->context->style;
$qb = new midgard_query_builder('midgard_element');
$qb->add_constraint('name', '=', $element);
$qb->add_constraint('style', '=', $style->id);
$elements = $qb->execute();
if (count($elements) == 0)
{
if ($element == 'ROOT')
{
return $root_fallback;
}
return '';
}
else
{
$value = $elements[0]->value;
}
return preg_replace_callback("/<\\(([a-zA-Z0-9 _-]+)\\)>/", 'mgd_element', $value);
}
}
/**
* Show a variable
*/
function mgd_variable($variable)
{
//echo "<br />\nxxX{$variable[1]}Xxx";
$variable_parts = explode(':', $variable[1]);
// TODO: Formatter support
return "<?php echo \${$variable_parts[0]}; ?>";
}
/**
* Preparse a string to handle element inclusion and variable
*
* @see mgd_preparse
*/
function mgd_preparse($code)
{
// Get style elements
$code = preg_replace_callback("/<\\(([a-zA-Z0-9 _-]+)\\)>/", 'mgd_element', $code);
// Echo variables
$code = preg_replace_callback("%&\(([^)]*)\);%i", 'mgd_variable', $code);
return $code;
}
?>