-
Notifications
You must be signed in to change notification settings - Fork 0
/
TinyCMS.php
92 lines (87 loc) · 2.84 KB
/
TinyCMS.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
<?php
namespace tiny_cms;
use atomar\core\Auth;
/**
* This is the internal api class that can be used by third party extensions
*/
class TinyCMS {
/**
* Render the text given the text id
*
*/
public static function render_stub($stub_slug, $template_name = null) {
if(!is_string($template_name)) {
$template_name = null;
}
$stub = \R::findOne('cmslightstub', 'slug=? LIMIT 1', array($stub_slug));
if ($stub->id) {
// update the stub location in case we moved it.
if ($template_name != null) {
if (stripos($stub->location, $template_name) === false) {
if ($stub->location == '') {
$stub->location = $template_name;
} else {
$stub->location .= ', ' . $template_name;
}
}
}
store($stub);
} else {
// create the stub for the backend and return nothing
$stub = \R::dispense('cmslightstub');
$stub->slug = $stub_slug;
$stub->name = machine_to_human($stub_slug);
$stub->value = ' ';
if ($template_name != null) {
$stub->location = $template_name;
}
store($stub);
}
// display editing shortcuts if viewed by administrator.
if (Auth::has_authentication('administer_cms_light')) {
$value = $stub->value;
if (trim($value) == '') {
$value = '[CMS Light: ' . $stub->name . ']';
$muted = 'text-muted';
}
$name = $stub->name;
$id = $stub->id;
$response = <<<HTML
<div id="cms-light-container-$id" class="cms-light-editor">
<a data-lightbox="/admin/cms_light/stub/$id/edit/" class="cms-light-controls pull-right">Edit '$name' <span class="glyphicon glyphicon-cog"></span></a>
<span class="cms-light-value muted">$value</span>
</div>
HTML;
return $response;
} else {
return $stub->value;
}
}
/**
* Define a new text stub or replace an existing one.
*
*/
public static function define_stub($name, $value) {
$slug = human_to_machine($name);
$stub = \R::findOne('cmslightstub', 'name=? OR slug=? LIMIT 1', array($name, $slug));
if ($stub->id) {
$stub->name = $name;
$stub->slug = $slug;
$stub->value = $value;
} else {
unset($stub);
$stub = \R::dispense('cmslightstub');
$stub->name = $name;
$stub->slug = $slug;
$stub->value = $value;
}
return store($stub);
}
/**
* Get all the stubs
*
*/
public static function get_all_stubs() {
return \R::findAll('cmslightstub');
}
}