blocks.php 10.4 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 (!param_exists('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();
77
$state = get_string('editcontent1', '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
        $groupurl = group_homepage_url(get_group_by_id($view->get('group'), true), false);
93
    }
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
}
Naomi Guyer's avatar
Naomi Guyer committed
129

130
$stylesheets = array_merge($stylesheets, $view->get_all_blocktype_css());
131
132
// Tell the user to change the view theme if the current one is no
// longer available to them.
133
if ($viewtheme && !isset($allowedthemes[$viewtheme])) {
134
    $smarty = smarty(array(), $stylesheets, false, $extraconfig);
135
    $smarty->assign('PAGEHEADING', get_string('choosetheme'));
136
137
138
139
140
141
142
143
144
    $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;
}

145
$javascript = array('views', 'tinymce', 'paginator', 'js/jquery/jquery-ui/js/jquery-ui.min.js',
146
                    'js/jquery/jquery-ui/js/jquery-ui.touch-punch.min.js', 'tablerenderer', 'artefact/file/js/filebrowser.js',
147
                    'lib/pieforms/static/core/pieforms.js','js/jquery/modernizr.custom.js', 'js/switchbox.js');
148
149
$blocktype_js = $view->get_all_blocktype_javascript();
$javascript = array_merge($javascript, $blocktype_js['jsfiles']);
150
if (is_plugin_active('externalvideo', 'blocktype')) {
151
152
    $javascript = array_merge($javascript, array((is_https() ? 'https:' : 'http:') . '//cdn.embedly.com/widgets/platform.js'));
}
153
$inlinejs = "jQuery( function() {\n" . join("\n", $blocktype_js['initjs']) . "\n});";
154
155
require_once('pieforms/pieform/elements/select.php');
$inlinejs .= pieform_element_select_get_inlinejs();
156
$inlinejs .= "jQuery(window).on('pageupdated', {}, function() { dock.init(jQuery(document)); });";
157
158
159
160
161
// The form for adding blocks via the keyboard
$addform = pieform(array(
    'name' => 'addblock',
    'method' => 'post',
    'jsform' => true,
Naomi Guyer's avatar
Naomi Guyer committed
162
    'renderer' => 'div',
163
    'autofocus' => false,
164
    'class' => 'cell-radios',
165
    'elements' => array(
166
167
168
169
170
171
172
173
174
175
176
177
178
179
        'blocklegend' => array(
            'type' => 'fieldset',
            'elements' => array(
                'celltitle' => array(
                    'type' => 'markup',
                    'value'=> '<legend>' . get_string('celltitle', 'view') . '</legend>',
                ),
                'cellchooser' => array(
                    'type' => 'radio',
                    'class' => 'fullwidth',
                    'rowsize' => 2,
                    'options' => array('R1C1', 'R1C2', 'R2C1'),
                ),
            ),
180
181
182
183
184
185
186
187
        ),
        'position' => array(
            'type' => 'select',
            'title' => get_string('blockorder', 'view'),
            'options' => array('Top', 'After 1', 'After 2'),
        ),
        'submit' => array(
            'type' => 'submitcancel',
188
            'class' => 'btn-default',
189
            'value' => array(get_string('add'), get_string('cancel')),
190
191
192
193
        ),
    ),
));

194
195
196
197
198
199
200
201
202
$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);
    }
}

203
204
205
206
$smarty = smarty($javascript, $stylesheets, array(
    'view' => array(
        'addblock',
        'cellposition',
207
        'blockordertopcell',
208
209
210
211
212
213
        'blockorderafter',
        'moveblock',
    ),
), $extraconfig);

$smarty->assign('addform', $addform);
214
215

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

218
219
220
// The list of shortcut blocks
$smarty->assign('shortcut_list', $view->build_blocktype_list('shortcut'));

221
// The list of blocktypes for the default category
222
$smarty->assign('blocktype_list', $view->build_blocktype_list($category));
223
224
225
226

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

Aaron Wells's avatar
Aaron Wells committed
227
228
// 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
229
230
231
232
// 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
233
            // When configuring a blockinstance and the search tab is open,
234
235
236
237
238
239
240
241
242
243
244
            // 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;
    }
}

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

268
$smarty->assign('viewtype', $viewtype);
269
$smarty->assign('view', $view->get('id'));
270
$smarty->assign('groupid', $group);
271
272
273
if (isset($groupurl)) {
    $smarty->assign('groupurl', $groupurl);
}
274
$smarty->assign('institution', $institution);
275

276
if (get_config('userscanchooseviewthemes') && $view->is_themeable()) {
277
278
279
    $smarty->assign('viewtheme', $viewtheme);
    $smarty->assign('viewthemes', $allowedthemes);
}
280
281

$smarty->assign('viewid', $view->get('id'));
282
283
284
285
286
$collectionid = false;
if ($collection = $view->get('collection')) {
    $collectionid = $collection->get('id');
}
$smarty->assign('collectionid', $collectionid);
287

288
289
290
291
292
293
294
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
295
    $columns = $viewcontent;
296
297
    $smarty->assign('columns', $columns);
}
298
$smarty->assign('issiteview', isset($institution) && ($institution == 'mahara'));
299

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