blocks.php 10.2 KB
Newer Older
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage core
6
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9
10
11
12
 *
 */

define('INTERNAL', 1);
13
14
15
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'view');
define('SECTION_PAGE', 'blocks');
Richard Mansfield's avatar
Richard Mansfield committed
16

17
require(dirname(dirname(__FILE__)) . '/init.php');
Richard Mansfield's avatar
Richard Mansfield committed
18
19
require_once(get_config('libroot') . 'view.php');
require_once(get_config('libroot') . 'group.php');
20

21
$id = param_integer('id', 0); // if 0, we're editing our profile.
Richard Mansfield's avatar
Richard Mansfield committed
22
$new = param_boolean('new', false);
23
$profile = param_boolean('profile');
24
$dashboard = param_boolean('dashboard');
25
26
27
28

if (empty($id)) {
    if (!empty($profile)) {
        try {
29
            $view = $USER->get_profile_view();
30
31
32
            $id = $view->get('id');
        }
        catch (ViewNotFoundException $_e) {
33
            throw new ParameterException("Missing parameter id and couldn't find default user profile view");
34
35
        }
    }
36
37
38
    else {
        throw new ParameterException("Missing parameter id");
    }
39
40
41
42
43
}
if (!empty($id) && empty($view)) {
    $view = new View($id);
}

Richard Mansfield's avatar
Richard Mansfield committed
44
45
46
if (!$USER->can_edit_view($view)) {
    throw new AccessDeniedException();
}
47

48
49
50
51
// If the view has been submitted, disallow editing
if ($view->is_submitted()) {
    $submittedto = $view->submitted_to();
    throw new AccessDeniedException(get_string('canteditsubmitted', 'view', $submittedto['name']));
52
53
}

Richard Mansfield's avatar
Richard Mansfield committed
54
55
$group = $view->get('group');
$institution = $view->get('institution');
56

57
58
59
60
if ($group && !group_within_edit_window($group)) {
    throw new AccessDeniedException();
}

61
62
63
64
// If a block was configured & submitted, build the form now so it can
// be processed without having to render the other blocks.
if ($blockid = param_integer('blockconfig', 0)) {
    // However, if removing a newly placed block, let it fall through to process_changes
65
    if (!isset($_POST['cancel_action_configureblockinstance_id_' . $blockid]) || !param_integer('removeoncancel', 0) || param_integer('pieform_jssubmission', 0)) {
66
67
        require_once(get_config('docroot') . 'blocktype/lib.php');
        $bi = new BlockInstance($blockid);
68
69
70
71
        // Check if the block_instance belongs to this view
        if ($bi->get('view') != $view->get('id')) {
            throw new AccessDeniedException(get_string('blocknotinview', 'view', $bi->get('id')));
        }
72
        $bi->build_configure_form($new);
73
74
75
    }
}

76
$view->set_edit_nav();
Naomi Guyer's avatar
Naomi Guyer committed
77
$state = get_string('editcontent', 'view');
Richard Mansfield's avatar
Richard Mansfield committed
78

79
80
if ($view->get('type') == 'profile') {
    $profile = true;
81
    $title = get_string('usersprofile', 'mahara', display_name($view->get('owner'), null, true));
Naomi Guyer's avatar
Naomi Guyer committed
82
    define('TITLE', $title);
83
}
84
85
86
else if ($view->get('type') == 'dashboard') {
    $dashboard = true;
    $title = get_string('usersdashboard', 'mahara', display_name($view->get('owner'), null, true));
Naomi Guyer's avatar
Naomi Guyer committed
87
    define('TITLE', $title );
88
}
89
90
else if ($view->get('type') == 'grouphomepage') {
    $title = get_string('grouphomepage', 'view');
91
    if ($view->get('template') != View::SITE_TEMPLATE) {
92
93
        $groupurl = group_homepage_url(get_record('group', 'id', $view->get('group')), false);
    }
Naomi Guyer's avatar
Naomi Guyer committed
94
    define('TITLE', $title);
95
}
96
else if ($new) {
Naomi Guyer's avatar
Naomi Guyer committed
97
    define('TITLE', get_string('notitle', 'view'));
Richard Mansfield's avatar
Richard Mansfield committed
98
99
}
else {
Naomi Guyer's avatar
Naomi Guyer committed
100
    define('TITLE', $view->get('title'));
101
    $editabletitle = true;
Richard Mansfield's avatar
Richard Mansfield committed
102
}
103
define('SUBSECTIONHEADING', TITLE);
104
// Make the default category the first tab if none is set
105
106
107
108
109
$category = '';
if (param_exists('c')) {
    $category = param_variable('c');
}
if (empty($category)) {
110
    $category = $view->get_default_category();
111
}
112
113
114

$view->process_changes($category, $new);

115
116
$extraconfig = array(
    'sidebars'    => false,
117
118
);

119
// Set up theme
120
121
$viewtheme = $view->set_user_theme();

122
$allowedthemes = get_user_accessible_themes();
123
124

// Pull in cross-theme view stylesheet and file stylesheets
Naomi Guyer's avatar
Naomi Guyer committed
125
$stylesheets = array();
126
foreach (array_reverse($THEME->get_url('style/style.css', true, 'artefact/file')) as $sheet) {
127
    $stylesheets[] = '<link rel="stylesheet" type="text/css" href="' . append_version_number($sheet) . '">';
128
}
129
130
131
foreach (array_reverse($THEME->get_url('style/select2.css', true)) as $sheet) {
    $stylesheets[] = '<link rel="stylesheet" type="text/css" href="' . append_version_number($sheet) . '">';
}
Naomi Guyer's avatar
Naomi Guyer committed
132

133
$stylesheets = array_merge($stylesheets, $view->get_all_blocktype_css());
134
135
// Tell the user to change the view theme if the current one is no
// longer available to them.
136
if ($viewtheme && !isset($allowedthemes[$viewtheme])) {
137
    $smarty = smarty(array(), $stylesheets, false, $extraconfig);
138
    $smarty->assign('PAGEHEADING', get_string('choosetheme'));
139
140
141
142
143
144
145
146
147
    $smarty->assign('formurl', get_config('wwwroot') . 'view/blocks.php');
    $smarty->assign('view', $view->get('id'));
    $smarty->assign('viewtitle', $view->get('title'));
    $smarty->assign('viewtheme', $viewtheme);
    $smarty->assign('viewthemes', $allowedthemes);
    $smarty->display('view/changetheme.tpl');
    exit;
}

148
$javascript = array('views', 'tinymce', 'paginator', 'js/jquery/jquery-ui/js/jquery-ui.min.js',
149
                    'js/jquery/jquery-ui/js/jquery-ui.touch-punch.min.js', 'tablerenderer', 'artefact/file/js/filebrowser.js',
150
                    'lib/pieforms/static/core/pieforms.js','js/jquery/modernizr.custom.js', 'js/switchbox.js');
151
152
$blocktype_js = $view->get_all_blocktype_javascript();
$javascript = array_merge($javascript, $blocktype_js['jsfiles']);
153
if (is_plugin_active('externalvideo', 'blocktype')) {
154
155
    $javascript = array_merge($javascript, array((is_https() ? 'https:' : 'http:') . '//cdn.embedly.com/widgets/platform.js'));
}
156
$inlinejs = "addLoadEvent( function() {\n" . join("\n", $blocktype_js['initjs']) . "\n});";
157
158
require_once('pieforms/pieform/elements/select.php');
$inlinejs .= pieform_element_select_get_inlinejs();
159

160
161
162
163
164
// The form for adding blocks via the keyboard
$addform = pieform(array(
    'name' => 'addblock',
    'method' => 'post',
    'jsform' => true,
Naomi Guyer's avatar
Naomi Guyer committed
165
    'renderer' => 'div',
166
    'autofocus' => false,
167
    'class' => 'cell-radios',
168
169
170
    'elements' => array(
        'cellchooser' => array(
            'type' => 'radio',
171
            'class' => 'fullwidth',
172
173
174
175
176
177
178
179
180
181
182
            'title' => get_string('blockcell', 'view'),
            'rowsize' => 2,
            'options' => array('R1C1', 'R1C2', 'R2C1'),
        ),
        'position' => array(
            'type' => 'select',
            'title' => get_string('blockorder', 'view'),
            'options' => array('Top', 'After 1', 'After 2'),
        ),
        'submit' => array(
            'type' => 'submitcancel',
183
            'class' => 'btn-default',
184
            'value' => array(get_string('add'), get_string('cancel')),
185
186
187
188
        ),
    ),
));

189
190
191
192
193
194
195
196
197
$blockid = $view->get_blockinstance_currently_being_configured();
if (!$blockid) {
    $blockid = param_integer('block', 0);
    if (!$blockid) {
        // Build content before initialising smarty in case pieform elements define headers.
        $viewcontent = $view->build_rows(true);
    }
}

198
199
200
201
$smarty = smarty($javascript, $stylesheets, array(
    'view' => array(
        'addblock',
        'cellposition',
202
        'blockordertopcell',
203
204
205
206
207
208
        'blockorderafter',
        'moveblock',
    ),
), $extraconfig);

$smarty->assign('addform', $addform);
209
210

// The list of categories for the tabbed interface
211
$smarty->assign('category_list', $view->build_category_list($category, $new));
212

213
214
215
// The list of shortcut blocks
$smarty->assign('shortcut_list', $view->build_blocktype_list('shortcut'));

216
// The list of blocktypes for the default category
217
$smarty->assign('blocktype_list', $view->build_blocktype_list($category));
218
219
220
221

// Tell smarty we're editing rather than just rendering
$smarty->assign('editing', true);

Aaron Wells's avatar
Aaron Wells committed
222
223
// Work out what action is being performed. This is used to put a hidden submit
// button right at the very start of the form, so that hitting enter in any
224
225
226
227
// form fields will cause the correct action to be performed
foreach (array_keys($_POST + $_GET) as $key) {
    if (substr($key, 0, 7) == 'action_') {
        if (param_boolean('s')) {
Aaron Wells's avatar
Aaron Wells committed
228
            // When configuring a blockinstance and the search tab is open,
229
230
231
232
233
234
235
236
237
238
239
            // pressing enter should search
            $key = str_replace('configureblockinstance', 'acsearch', $key);
            if (substr($key, -2) == '_x') {
                $key = substr($key, 0, -2);
            }
        }
        $smarty->assign('action_name', $key);
        break;
    }
}

240
$viewid = $view->get('id');
241
242
243
244
$displaylink = $view->get_url();
if ($new) {
    $displaylink .= (strpos($displaylink, '?') === false ? '?' : '&') . 'new=1';
}
245
$smarty->assign('edittitle', $view->can_edit_title());
246
$smarty->assign('displaylink', $displaylink);
247
$smarty->assign('formurl', get_config('wwwroot') . 'view/blocks.php');
248
$smarty->assign('category', $category);
249
$smarty->assign('new', $new);
250
$smarty->assign('profile', $profile);
251
$smarty->assign('dashboard', $dashboard);
252
if (get_config('blockeditormaxwidth')) {
253
    $inlinejs .= 'config.blockeditormaxwidth = true;';
254
}
255
$smarty->assign('INLINEJAVASCRIPT', $inlinejs);
256
257
258
$viewtype = $view->get('type');
$viewtitle = $view->get('title');
$owner = $view->get('owner');
259
260
261
262
if ($owner &&  $viewtype == 'profile') {
    $viewtitle = get_string('usersprofile', 'mahara', display_name($view->get('owner'), null, true));
}

263
$smarty->assign('viewtype', $viewtype);
264
$smarty->assign('view', $view->get('id'));
265
$smarty->assign('groupid', $group);
266
267
268
if (isset($groupurl)) {
    $smarty->assign('groupurl', $groupurl);
}
269
$smarty->assign('institution', $institution);
270

271
if (get_config('userscanchooseviewthemes') && $view->is_themeable()) {
272
273
274
    $smarty->assign('viewtheme', $viewtheme);
    $smarty->assign('viewthemes', $allowedthemes);
}
275
276

$smarty->assign('viewid', $view->get('id'));
277
278
279
280
281
$collectionid = false;
if ($collection = $view->get('collection')) {
    $collectionid = $collection->get('id');
}
$smarty->assign('collectionid', $collectionid);
282

283
284
285
286
287
288
289
if ($blockid) {
    // Configuring a single block
    $bi = new BlockInstance($blockid);
    $smarty->assign('block', $bi->render_editing(true));
}
else {
    // The HTML for the columns in the view
290
    $columns = $viewcontent;
291
292
    $smarty->assign('columns', $columns);
}
293
$smarty->assign('issiteview', isset($institution) && ($institution == 'mahara'));
294

295
$smarty->assign('issitetemplate', $view->is_site_template());
Naomi Guyer's avatar
Naomi Guyer committed
296
$smarty->assign('PAGEHEADING', $state);
297
$smarty->display('view/blocks.tpl');