template.php 4.57 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
28
29
30
31
32
<?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();

function template_parse($templatename) {

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

    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;

61
62
    $template['parseddata'] = $t;
    return $template;
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
}

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....

    return $data;
        
}

89
function template_locate($templatename, $fetchdb=true) {
90
91

    // check dataroot first for custom templates
92
93
94
    $templatedir = 'templates/' . $templatename . '/';
    $fragment = $templatedir . 'fragment.template';
    $css = $templatedir . 'fragment.css';
95
96
97

    $template = array();

98
99
    $thumbnails = array('jpg', 'jpeg', 'png', 'gif');

100
    if ($path = realpath(get_config('dataroot') . $fragment)) {
101
        $template['fragment'] = $path;
102
        if (is_readable(get_config('dataroot') . $css)) {
103
104
105
106
107
108
109
            $template['css'] = get_config('dataroot') . $css;
        }
        foreach ($thumbnails as $t) {
            if (is_readable(get_config('dataroot') . $templatedir . 'thumbnail.' . $t)) {
                $template['thumbnail'] = get_config('dataroot') . $templatedir . 'thumbnail.' . $t;
                break;
            }
110
        }
Penny Leach's avatar
Penny Leach committed
111
112
113
114
        if ($dbstuff = get_record('template', 'name', $templatename)) {
            $template['cacheddata'] = unserialize($dbstuff->cacheddata);
            $template['category'] = $dbstuff->category;
        }
115
        $template['location'] = get_config('datarootroot') . 'templates/' . $templatename . '/';
116
117
118
119
        return $template;
    }

    if ($path = realpath(get_config('libroot') . $fragment)) {
120
        $template['fragment'] = $path;
121
        if (is_readable(get_config('libroot') . $css)) {
122
123
124
125
126
127
128
            $template['css'] = get_config('libroot') . $css;
        }
        foreach ($thumbnails as $t) {
            if (is_readable(get_config('libroot') . $templatedir . 'thumbnail.' . $t)) {
                $template['thumbnail'] = get_config('libroot') . $templatedir . 'thumbnail.' . $t;
                break;
            }
129
        }
Penny Leach's avatar
Penny Leach committed
130
131
132
133
        if ($dbstuff = get_record('template', 'name', $templatename)) {
            $template['cacheddata'] = unserialize($dbstuff->cacheddata);
            $template['category'] = $dbstuff->category;
        }
134
        $template['location'] = get_config('libroot') . 'templates/' . $templatename . '/';
135
136
137
138
139
140
141
142
        return $template;
    }

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


?>