template.php 6.69 KB
Newer Older
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
<?php
/**
 * This program is part of Mahara
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    mahara
 * @subpackage core
 * @author     Penny Leach <penny@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

defined('INTERNAL') || die();
28
29
define('TEMPLATE_RENDER_READONLY', 1);
define('TEMPLATE_RENDER_EDITMODE', 2);
30
31
32
33
34

function template_parse($templatename) {

    $t = array();
    
35
36
37
    $template = template_locate($templatename, false);
    
    $fragment = file_get_contents($template['fragment']);
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

    preg_match_all('/(.*?)\{\{(.*?)\}\}/xms', $fragment, $matches, PREG_SET_ORDER);
    
    $strlen = 0;

    foreach ($matches as $m) {
        $temp = array('type'    => 'html',
                      'content' => $m[1],
                      );
        $t[] = $temp;
        $temp = array('type'    => 'block', 
                      'data'    => template_parse_block($m[2]),
                      );
        $t[] = $temp;

        $strlen += strlen($m[0]);
           
    }
   
    $temp = array('type'    => 'html',
                  'content' => substr($fragment, $strlen),
                  );

    $t[] = $temp;

63
64
    $template['parseddata'] = $t;
    return $template;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
}

function template_parse_block($blockstr) {
    $data = array();
    $bits = explode(' ', $blockstr);

    // the first bit should be 'block'
    if ($bits[0] != 'block') {
        throw new InvalidArgumentException("Invalid block section $blockstr");
    }

    array_shift($bits);
    foreach ($bits as $b) {
        $keyvalue = explode('=', $b);
        $data[$keyvalue[0]] = substr($keyvalue[1], 1, -1);
    }
    
    if (!isset($data['id']) || empty($data['id'])) {
        throw new InvalidArgumentException("Invalid block $blockstr. Must have id");
    }
    // everything else can theoretically be optional....

87
88
89
    template_validate_block($data, '');
    template_validate_block($data, 'default');

90
91
92
93
    return $data;
        
}

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
function template_validate_block($data, $name='') {
    
    $type = isset($data[$name . 'type']) ? $data[$name . 'type'] : '';
    $format = isset($data[$name . 'format']) ? $data[$name . 'format'] : '';
    
    if (!empty($type)) {
        if (empty($format)) {
            throw new InvalidArgumentException("Cannot specify type without format");
        }
    }
    
    if (!empty($format) && $format != 'label') {
        if (empty($type)) {
            throw new InvalidArgumentException("Cannot specify format without type");
        }
    }
        
    if ((empty($format) && empty($type)) || $format == 'label') { // labels are special cases
        return true;
    }
        
    // figure out what plugin handles this type and validate the class exists.
    if (!$plugin = get_field('artefact_installed_type', 'plugin', 'name', $type)) {
        throw new InvalidArgumentException("Type $type is not installed");
    }
    
120
    require_once('artefact.php');
121
    safe_require('artefact', $plugin);
122

123
124
125
126
127
128
129
    if (!artefact_can_render_to($type, $format)) {
        throw new InvalidArgumentException("Type $type can't render to format $format");
    }
    
    // @todo validate resizing stuff
}

130
function template_locate($templatename, $fetchdb=true) {
131
132

    // check dataroot first for custom templates
133
134
135
    $templatedir = 'templates/' . $templatename . '/';
    $fragment = $templatedir . 'fragment.template';
    $css = $templatedir . 'fragment.css';
136
137
138

    $template = array();

Penny Leach's avatar
Penny Leach committed
139
140
141
142
    $thumbnails = array('jpg'  => 'image/jpeg',
                        'jpeg' => 'image/jpeg',
                        'png'  => 'image/png',
                        'gif'  => 'image/gif');
143

144
    if ($path = realpath(get_config('dataroot') . $fragment)) {
145
        $template['fragment'] = $path;
146
        if (is_readable(get_config('dataroot') . $css)) {
147
148
            $template['css'] = get_config('dataroot') . $css;
        }
Penny Leach's avatar
Penny Leach committed
149
        foreach ($thumbnails as $t => $contenttype) {
150
            if (is_readable(get_config('dataroot') . $templatedir . 'thumbnail.' . $t)) {
Penny Leach's avatar
Penny Leach committed
151
                $template['thumbnailcontenttype'] = $contenttype;
152
153
154
                $template['thumbnail'] = get_config('dataroot') . $templatedir . 'thumbnail.' . $t;
                break;
            }
155
        }
Penny Leach's avatar
Penny Leach committed
156
157
158
159
        if ($dbstuff = get_record('template', 'name', $templatename)) {
            $template['cacheddata'] = unserialize($dbstuff->cacheddata);
            $template['category'] = $dbstuff->category;
        }
160
        $template['location'] = get_config('datarootroot') . 'templates/' . $templatename . '/';
161
162
163
164
        return $template;
    }

    if ($path = realpath(get_config('libroot') . $fragment)) {
165
        $template['fragment'] = $path;
166
        if (is_readable(get_config('libroot') . $css)) {
167
168
            $template['css'] = get_config('libroot') . $css;
        }
Penny Leach's avatar
Penny Leach committed
169
        foreach ($thumbnails as $t => $contenttype) {
170
            if (is_readable(get_config('libroot') . $templatedir . 'thumbnail.' . $t)) {
Penny Leach's avatar
Penny Leach committed
171
                $template['thumbnailcontenttype'] = $contenttype;
172
173
174
                $template['thumbnail'] = get_config('libroot') . $templatedir . 'thumbnail.' . $t;
                break;
            }
175
        }
Penny Leach's avatar
Penny Leach committed
176
177
178
179
        if ($dbstuff = get_record('template', 'name', $templatename)) {
            $template['cacheddata'] = unserialize($dbstuff->cacheddata);
            $template['category'] = $dbstuff->category;
        }
180
        $template['location'] = get_config('libroot') . 'templates/' . $templatename . '/';
181
182
183
184
185
186
        return $template;
    }

    throw new InvalidArgumentException("Invalid template name $templatename, couldn't find");
}

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
 * renders a template in either edit mode or read only mode
 *
 * @param array $template a parsed template see {@link template_parse}
 * @param mode either TEMPLATE_RENDER_READONLY or TEMPLATE_RENDER_EDITMODE
 */
function template_render($template, $mode) {
    $td = $template['parseddata'];
    foreach ($td as $t) {
        if ($t['type'] == 'html') {
            echo $t['content'];
        }
        else {
            // @todo call something depending on mode
            
        }
    }
}
205
206

?>