/
Snippet.php
154 lines (133 loc) · 4.62 KB
/
Snippet.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace Concrete\Core\Editor;
use Loader;
use Concrete\Core\Foundation\Object;
use Concrete\Core\Package\PackageList;
use Core;
use Package;
abstract class Snippet extends Object
{
/**
* Required for snippets to work.
*/
abstract public function replace();
public function getSystemContentEditorSnippetHandle()
{
return $this->scsHandle;
}
public function getSystemContentEditorSnippetName()
{
return $this->scsName;
}
public function isSystemContentEditorSnippetActive()
{
return $this->scsIsActive;
}
public function getPackageID()
{
return $this->pkgID;
}
public function getPackageHandle()
{
return PackageList::getHandle($this->pkgID);
}
public function getPackageObject()
{
return Package::getByID($this->pkgID);
}
public function findAndReplace($text)
{
$r = preg_replace_callback('/\<span[^>]*data-scsHandle="' . $this->scsHandle . '">[^<]*<\/span>/is', array($this, 'replace'), $text);
return $r;
}
public static function getByHandle($scsHandle)
{
$db = Loader::db();
$r = $db->GetRow('select scsHandle, scsIsActive, pkgID, scsName from SystemContentEditorSnippets where scsHandle = ?', array($scsHandle));
if (is_array($r) && $r['scsHandle']) {
$pkgHandle = false;
if ($r['pkgID']) {
$pkgHandle = PackageList::getHandle($r['pkgID']);
}
$class = overrideable_core_class(
'Core\\Editor\\' . camelcase($r['scsHandle']) . 'Snippet',
DIRNAME_CLASSES . '/Editor/' . camelcase($r['scsHandle']) . 'Snippet.php',
$pkgHandle
);
$sc = Core::make($class);
$sc->setPropertiesFromArray($r);
return $sc;
}
}
public static function add($scsHandle, $scsName, $pkg = false)
{
$pkgID = 0;
if (is_object($pkg)) {
$pkgID = $pkg->getPackageID();
}
$db = Loader::db();
$db->Execute('insert into SystemContentEditorSnippets (scsHandle, scsName, pkgID) values (?, ?, ?)', array($scsHandle, $scsName, $pkgID));
return static::getByHandle($scsHandle);
}
public function delete()
{
$db = Loader::db();
$db->Execute('delete from SystemContentEditorSnippets where scsHandle = ?', array($this->scsHandle));
}
public function activate()
{
$db = Loader::db();
$db->Execute('update SystemContentEditorSnippets set scsIsActive = 1 where scsHandle = ?', array($this->scsHandle));
}
public function deactivate()
{
$db = Loader::db();
$db->Execute('update SystemContentEditorSnippets set scsIsActive = 0 where scsHandle = ?', array($this->scsHandle));
}
public static function getList()
{
$db = Loader::db();
$scsHandles = $db->GetCol('select scsHandle from SystemContentEditorSnippets order by scsHandle asc');
$libraries = array();
foreach ($scsHandles as $scsHandle) {
$scs = static::getByHandle($scsHandle);
$libraries[] = $scs;
}
return $libraries;
}
public static function getActiveList()
{
$db = Loader::db();
$scsHandles = $db->GetCol('select scsHandle from SystemContentEditorSnippets where scsIsActive = 1 order by scsHandle asc');
$libraries = array();
foreach ($scsHandles as $scsHandle) {
$scs = static::getByHandle($scsHandle);
$libraries[] = $scs;
}
return $libraries;
}
public static function getListByPackage($pkg)
{
$db = Loader::db();
$scsHandles = $db->GetCol('select scsHandle from SystemContentEditorSnippets where pkgID = ? order by scsHandle asc', array($pkg->getPackageID()));
$libraries = array();
foreach ($scsHandles as $scsHandle) {
$scs = static::getByHandle($scsHandle);
$libraries[] = $scs;
}
return $libraries;
}
public static function exportList($xml)
{
$list = self::getList();
$nxml = $xml->addChild('systemcontenteditorsnippets');
foreach ($list as $sc) {
$activated = 0;
$type = $nxml->addChild('snippet');
$type->addAttribute('handle', $sc->getSystemContentEditorSnippetHandle());
$type->addAttribute('name', $sc->getSystemContentEditorSnippetName());
$type->addAttribute('package', $sc->getPackageHandle());
$type->addAttribute('activated', $sc->isSystemContentEditorSnippetActive());
}
}
}