edit.php 6.7 KB
Newer Older
Martyn Smith's avatar
Martyn Smith committed
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
<?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     Nigel McNie <nigel@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

define('INTERNAL', 1);
define('MENUITEM', 'view');
require(dirname(dirname(__FILE__)) . '/init.php');
Martyn Smith's avatar
Martyn Smith committed
30
define('TITLE', get_string('editview', 'view'));
Martyn Smith's avatar
Martyn Smith committed
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
require_once('template.php');

$view_id = param_integer('viewid');
$artefacts = param_variable('template', array());

$view_data = get_record( 'view', 'id', $view_id, 'owner', $USER->get('id'));

if(!$view_data) {
    $SESSION->add_error_msg(get_string('canteditdontown', 'view'));
    redirect(get_config('wwwroot') . 'view/');
}

$data = array(
    'template'    => $view_data->template,
    'title'       => $view_data->title,
    'description' => $view_data->description,
    'ownerformat' => $view_data->ownerformat,
    'artefacts'   => array(),
);

$view_content = get_records_array('view_content', 'view', $view_id);
if ($view_content) {
    foreach ($view_content as &$label) {
        $data['artefacts'][$label->block] = array(
            'value' => $label->content,
        );
    }
}

$view_artefact = get_records_array('view_artefact', 'view', $view_id);
if ($view_artefact) {
    foreach ($view_artefact as &$artefact) {
        if (isset($data['artefacts'][$artefact->block])) {
            if (!is_array($data['artefacts'][$artefact->block]['id'])) {
                $data['artefacts'][$artefact->block]['id'] = array($data['artefacts'][$artefact->block]['id']);
            }
            $data['artefacts'][$artefact->block]['id'][] = $artefact->artefact;
        }
        else {
            $data['artefacts'][$artefact->block] = array(
                'id'     => $artefact->artefact,
                'format' => $artefact->format,
            );
        }
    }
}

// @todo load artefacts

$parsed_template = template_locate($data['template']);

function validate_artefacts(&$artefacts) {
    global $parsed_template;

    if (isset($parsed_template['parseddata'])) {
        $template_data = $parsed_template['parseddata'];
    }
    else {
        $template_data = $parsed_template['cacheddata'];
    }

    $template_fields = array();

    foreach ($template_data as $block) {
        if ($block['type'] == 'block') {
            $template_fields[$block['data']['id']] = $block['data'];
        }
    }

    foreach ($artefacts as $block => &$data) {
        if (!isset($template_fields[$block])) {
            unset($artefacts[$block]);
            next;
        }
        // @todo martyn more validation ;)

        $data['type'] = $template_fields[$block]['type'];
    }
}

if (param_boolean('submit')) {
    validate_artefacts($artefacts);

    $data['artefacts'] = $artefacts;

    db_begin();

    delete_records('view_content', 'view', $view_id);
    delete_records('view_artefact', 'view', $view_id);

    $time = db_format_timestamp(time());

    foreach ($data['artefacts'] as $block => $blockdata) {
        if ($blockdata['type'] == 'label') {
            $viewcontent          = new StdClass;
            $viewcontent->view    = $view_id;
            $viewcontent->content = $blockdata['value'];
            $viewcontent->block   = $block;
            $viewcontent->ctime   = $time;
            insert_record('view_content', $viewcontent);
        }
        else if ($blockdata['type'] == 'artefact') {
            $blockdata['id'] = (array)$blockdata['id'];
            foreach ($blockdata['id'] as $id) {
                $viewartefact           = new StdClass;
                $viewartefact->view     = $view_id;
                $viewartefact->artefact = $id;
                $viewartefact->block    = $block;
                $viewartefact->ctime    = $time;
                $viewartefact->format   = $blockdata['format'];
                insert_record('view_artefact', $viewartefact);
            }
        }
        else {
            throw new MaharaException('Unknown block data type, this simply should _not_ happen. Perhaps someone changed step3 and forgot to change this?');
        }
    }

    db_commit();

    $SESSION->add_ok_msg(get_string('viewinformationsaved', 'view'));
    redirect(get_config('wwwroot') . 'view/');
}

if (param_boolean('cancel')) {
    redirect(get_config('wwwroot') . 'view/');
}

// Get the list of root things for the tree
$rootinfo = "var data = [";
foreach (plugins_installed('artefact') as $artefacttype) {
    safe_require('artefact', $artefacttype->name);
    if ($artefacttype->active) {
        foreach (call_static_method('PluginArtefact' . ucfirst($artefacttype->name), 'get_toplevel_artefact_types') as $type) {
            $rootinfo .= json_encode(array(
                'id'         => $artefacttype->name,
                'isartefact' => false,
                'container'  => true,
                'text'       => get_string($type, "artefact.{$artefacttype->name}"),
                'pluginname' => $artefacttype->name
            )) . ',';
        }
    }
}
$rootinfo = substr($rootinfo, 0, -1) . '];';

$template = template_render($parsed_template, TEMPLATE_RENDER_EDITMODE, array_merge($data, $data['artefacts']));

$headers = array();
if (isset($parsed_template['css'])) {
    $headers[] = '<link rel="stylesheet" type="text/css" href="' . get_config('wwwroot') . 'view/template.css.php?template=' . $data['template'] . '">';
}

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
$smarty = smarty(
    array('collapsabletree', 'move', 'tablerenderer'),
    $headers,
    array(
        'view' => array(
            'chooseformat',
            'format.listself',
            'format.listchildren',
            'format.renderfull',
            'format.rendermetadata',
            'empty_block',
            'empty_label',
        ),
    )
);
Martyn Smith's avatar
Martyn Smith committed
199
$smarty->assign('rootinfo', $rootinfo);
200
201
$smarty->assign('plusicon', theme_get_image_url('plus.png'));
$smarty->assign('minusicon', theme_get_image_url('minus.png'));
Martyn Smith's avatar
Martyn Smith committed
202
203
204
205
206
207
208
209

$smarty->assign('template', $template);

$smarty->assign('EDITMODE', true);
$smarty->assign('viewid', $view_id);
$smarty->display('view/create3.tpl');

?>