-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.cb_template_provider.php
68 lines (62 loc) · 2.31 KB
/
class.cb_template_provider.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
<?php
/* This file is part of cbapi.
* Copyright © 2011-2012 stiftung kulturserver.de ggmbh <[email protected]>
*
* cbapi is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cbapi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with cbapi. If not, see <http://www.gnu.org/licenses/>.
*/
Cb::import('CbAbstractProvider');
/**
* Template providers return dumb templates to be filled from JS. No
* authentication is necessary and the templates are expected as an array of
* name => absolute_file_name.
*/
class CbTemplateProvider extends CbAbstractProvider {
protected $default_template; ///< Resource to be used if none is specified.
/**
* Create a template provider.
* @param array $templates Mapping of name => path for templates.
* @param string $default_template Template to be used if none is given.
*/
public function __construct(array $templates = array(), $default_template = null)
{
parent::__construct(array());
$this->default_template = $default_template;
$this->templates = $templates;
}
private function resolveTemplate($request)
{
return isset($request['template']) ? $this->templates[$request['template']] : $this->default_template;
}
protected function execHandler($method, $request)
{
if ($method !== 'get') {
throw new CbApiException(403, 'You cannot modify templates.');
}
return new CbContentAdapter($this->resolveTemplate($request));
}
protected function getMetadata($method, $request)
{
if ($method !== 'get') {
throw new CbApiException(403, 'You cannot modify templates.');
}
return array(
'last_modified' => filemtime($this->resolveTemplate($request)),
'privacy' => 'public',
'name' => $request['template'],
'formats' => array(
'text/html' => 'CbHtmlFileFormatter'
)
);
}
}