/
Type.php
261 lines (225 loc) · 8.03 KB
/
Type.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
namespace Concrete\Core\Page\Type\PublishTarget\Type;
use Loader;
use Concrete\Core\Page\Type\Type as PageType;
use Concrete\Core\Foundation\ConcreteObject;
use Concrete\Core\Package\PackageList;
use Environment;
use Concrete\Core\Package\Package as Package;
use Core;
use Database;
use Symfony\Component\HttpFoundation\Request;
abstract class Type extends ConcreteObject
{
protected $ptPublishTargetTypeID;
protected $ptPublishTargetTypeHandle;
protected $ptPublishTargetTypeName;
protected $pkgID;
abstract public function configurePageTypePublishTarget(PageType $pt, $post);
abstract public function configurePageTypePublishTargetFromImport($txml);
public function getPageTypePublishTargetTypeName()
{
return $this->ptPublishTargetTypeName;
}
public function getPageTypePublishTargetTypeHandle()
{
return $this->ptPublishTargetTypeHandle;
}
public function getPageTypePublishTargetTypeID()
{
return $this->ptPublishTargetTypeID;
}
public function getPackageID()
{
return $this->pkgID;
}
public function getPackageHandle()
{
return PackageList::getHandle($this->pkgID);
}
public function getPackageObject()
{
return Package::getByID($this->pkgID);
}
/** Returns the display name for this instance (localized and escaped accordingly to $format)
* @param string $format = 'html' Escape the result in html format (if $format is 'html'). If $format is 'text' or any other value, the display name won't be escaped.
*
* @return string
*/
public function getPageTypePublishTargetTypeDisplayName($format = 'html')
{
$value = tc('PageTypePublishTargetTypeName', $this->ptPublishTargetTypeName);
switch ($format) {
case 'html':
return h($value);
case 'text':
default:
return $value;
}
}
public function validatePageTypeRequest(Request $request)
{
$e = Core::make('error');
return $e;
}
public static function getByID($ptPublishTargetTypeID)
{
$db = Database::connection();
$r = $db->fetchAssoc(
'select ptPublishTargetTypeID, ptPublishTargetTypeHandle, ptPublishTargetTypeName, pkgID
from PageTypePublishTargetTypes
where ptPublishTargetTypeID = ?',
array($ptPublishTargetTypeID)
);
if (is_array($r) && $r['ptPublishTargetTypeHandle']) {
$txt = Loader::helper('text');
$class = overrideable_core_class(
'Core\\Page\\Type\\PublishTarget\\Type\\'
. $txt->camelcase($r['ptPublishTargetTypeHandle']) . 'Type',
DIRNAME_CLASSES . '/Page/Type/PublishTarget/Type/'
. $txt->camelcase($r['ptPublishTargetTypeHandle']) . '.php',
PackageList::getHandle($r['pkgID'])
);
$sc = Core::make($class);
$sc->setPropertiesFromArray($r);
return $sc;
}
return null;
}
public static function getByHandle($ptPublishTargetTypeHandle)
{
$db = Database::Connection();
$r = $db->fetchAssoc(
'select ptPublishTargetTypeID, ptPublishTargetTypeHandle, ptPublishTargetTypeName, pkgID
from PageTypePublishTargetTypes
where ptPublishTargetTypeHandle = ?',
array($ptPublishTargetTypeHandle)
);
if (is_array($r) && isset($r['ptPublishTargetTypeHandle'])) {
$txt = Loader::helper('text');
$class = overrideable_core_class(
'Core\\Page\\Type\\PublishTarget\\Type\\'
. $txt->camelcase($r['ptPublishTargetTypeHandle']) . 'Type',
DIRNAME_CLASSES . '/Page/Type/PublishTarget/Type/'
. $txt->camelcase($r['ptPublishTargetTypeHandle']) . '.php',
PackageList::getHandle($r['pkgID'])
);
$sc = Core::make($class);
$sc->setPropertiesFromArray($r);
return $sc;
}
return null;
}
public static function importConfiguredPageTypePublishTarget($txml)
{
$type = static::getByHandle((string) $txml['handle']);
$target = $type->configurePageTypePublishTargetFromImport($txml);
return $target;
}
public static function add($ptPublishTargetTypeHandle, $ptPublishTargetTypeName, $pkg = false)
{
$pkgID = 0;
if (is_object($pkg)) {
$pkgID = $pkg->getPackageID();
}
$db = Database::connection();
$db->executeQuery(
'insert into PageTypePublishTargetTypes (ptPublishTargetTypeHandle, ptPublishTargetTypeName, pkgID)
values (?, ?, ?)',
array($ptPublishTargetTypeHandle, $ptPublishTargetTypeName, $pkgID)
);
return static::getByHandle($ptPublishTargetTypeHandle);
}
public function delete()
{
$db = Database::connection();
$db->executeQuery(
'delete from PageTypePublishTargetTypes where ptPublishTargetTypeID = ?',
array($this->ptPublishTargetTypeID)
);
}
public static function getList()
{
$db = Database::connection();
$ids = $db->fetchAll('select ptPublishTargetTypeID from PageTypePublishTargetTypes');
$types = array();
foreach ($ids as $id) {
$type = static::getByID($id['ptPublishTargetTypeID']);
if (is_object($type)) {
$types[] = $type;
}
}
usort(
$types,
function (Type $a, Type $b) {
return strcasecmp(
$a->getPageTypePublishTargetTypeDisplayName(),
$b->getPageTypePublishTargetTypeDisplayName()
);
}
);
return $types;
}
public static function getListByPackage($pkg)
{
$db = Database::connection();
$ids = $db->fetchAll(
'select ptPublishTargetTypeID from PageTypePublishTargetTypes where pkgID = ?',
array($pkg->getPackageID())
);
$types = array();
foreach ($ids as $id) {
$type = static::getByID($id['ptPublishTargetTypeID']);
if (is_object($type)) {
$types[] = $type;
}
}
usort(
$types,
function (Type $a, Type $b) {
return strcasecmp(
$a->getPageTypePublishTargetTypeDisplayName(),
$b->getPageTypePublishTargetTypeDisplayName()
);
}
);
return $types;
}
public function export($xml)
{
$type = $xml->addChild('type');
$type->addAttribute('handle', $this->getPageTypePublishTargetTypeHandle());
$type->addAttribute('name', $this->getPageTypePublishTargetTypeName());
$type->addAttribute('package', $this->getPackageHandle());
}
public static function exportList($xml)
{
$list = self::getList();
$nxml = $xml->addChild('pagetypepublishtargettypes');
foreach ($list as $sc) {
$sc->export($nxml);
}
}
public function hasOptionsForm()
{
$env = Environment::get();
$rec = $env->getRecord(
DIRNAME_ELEMENTS . '/' . DIRNAME_PAGE_TYPES . '/' . DIRNAME_ELEMENTS_PAGE_TYPES_PUBLISH_TARGET_TYPES . '/' . $this->getPageTypePublishTargetTypeHandle(
) . '.php',
$this->getPackageHandle()
);
return $rec->exists();
}
public function includeOptionsForm($pagetype = false, $siteType = false)
{
if (is_object($pagetype)) {
$siteType = $pagetype->getSiteTypeObject();
}
Loader::element(
DIRNAME_PAGE_TYPES . '/' . DIRNAME_ELEMENTS_PAGE_TYPES_PUBLISH_TARGET_TYPES . '/' . $this->getPageTypePublishTargetTypeHandle(
),
array('type' => $this, 'pagetype' => $pagetype, 'sitetype' => $siteType),
$this->getPackageHandle()
);
}
}