editlayout.php 24.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php

/*
 * Created by De Chiara Antonella
 * Eticeo Santé (http://eticeo.fr)
 */

define('INTERNAL', 1);
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'view');
define('SECTION_PAGE', 'editlayout');

require_once(dirname(dirname(__FILE__)) . '/init.php');
14
require_once('skin.php');
15
16
17
18
19
20
require_once('view.php');
require_once(get_config('libroot') . 'group.php');
require_once(get_config('libroot') . 'layoutpreviewimage.php');
require_once(get_config('docroot') . 'blocktype/lib.php');
require_once(get_config('libroot') . 'view.php');

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
$id = param_integer('id', false);
$new = param_boolean('new', false);

if ($new && $id === false) {
    // Use the site default portfolio page to create a new page
    // cribbed from createview_submit()
    $sitedefaultviewid = get_field('view', 'id', 'institution', 'mahara', 'template', View::SITE_TEMPLATE, 'type', 'portfolio');
    if (!empty($sitedefaultviewid)) {
        $artefactcopies = array();
        $values = array();
        $groupid = param_integer('group', 0);
        $institutionname = param_alphanum('institution', false);
        if (!empty($groupid)) {
            $values['group'] = $groupid;
        }
        else if (!empty($institutionname)) {
            $values['institution'] = $institutionname;
        }
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
        list($view, $template, $copystatus) = View::create_from_template($values, $sitedefaultviewid, null, true, false, $artefactcopies);
        if (isset($copystatus['quotaexceeded'])) {
            $SESSION->add_error_msg(get_string('viewcreatewouldexceedquota', 'view'));
            redirect(get_config('wwwroot') . 'view/index.php');
        }
    }
    else {
         throw new ConfigSanityException(get_string('viewtemplatenotfound', 'error'));
    }

    $goto = get_config('wwwroot') . 'view/editlayout.php?new=1&id=' . $view->get('id');
    if (!empty($values)) {
        $goto .= '&' . http_build_query($values);
    }
    redirect($goto);
}

$view = new View($id);
58
59
60
61
62
63
64
65
66
67
68
69

if ($new) {
    define('TITLE', get_string('notitle', 'view'));
}
else {
    define('TITLE', $view->get('title'));
}
define('SUBSECTIONHEADING', TITLE);

if (!$USER->can_edit_view($view)) {
    throw new AccessDeniedException();
}
70

71
72
73
74
$issiteview = $view->get('institution') == 'mahara';
$issitetemplate = ($view->get('template') == View::SITE_TEMPLATE ? true : false);
$canedittitle = $view->can_edit_title();
$canuseskins = !$issitetemplate && can_use_skins(null, false, $issiteview);
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
// 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']));
}

$group = $view->get('group');
$institution = $view->get('institution');
$view->set_edit_nav();
$view->set_user_theme();

// Clean urls are only available for portfolio views owned by groups or users who already
// have their own clean profiles or group homepages.
if ($urlallowed = get_config('cleanurls') && $view->get('type') == 'portfolio' && !$institution) {
    if ($group) {
        $groupdata = get_record('group', 'id', $group);
        if ($urlallowed = !is_null($groupdata->urlid) && strlen($groupdata->urlid)) {
            $cleanurlbase = group_homepage_url($groupdata) . '/';
        }
    }
    else {
        $userurlid = $USER->get('urlid');
        if ($urlallowed = !is_null($userurlid) && strlen($userurlid)) {
            $cleanurlbase = profile_url($USER) . '/';
        }
    }
}

104
105
if ($group && !group_within_edit_window($group)) {
    throw new AccessDeniedException();
106
107
}

108
109
110
111
112
$state = get_string('settings', 'view');

$pieformname = 'settings';
list($form, $inlinejavascript) = create_settings_pieform();

113
$javascript = array('jquery','js/jquery/jquery-ui/js/jquery-ui.min.js');
114
115
116
117
118
119
120
121
122
$stylesheets[] = '<link rel="stylesheet" type="text/css" href="' . append_version_number(get_config('wwwroot') . 'js/jquery/jquery-ui/css/smoothness/jquery-ui.min.css') . '">';

$smarty = smarty($javascript, $stylesheets, array('view' => array('Row', 'removethisrow', 'rownr', 'nrrows')), array('sidebars' => false));

$smarty->assign('INLINEJAVASCRIPT', $inlinejavascript);
$smarty->assign('form', $form);
$smarty->assign('viewid', $view->get('id'));
$smarty->assign('viewtype', $view->get('type'));
$smarty->assign('viewtitle', $view->get('title'));
123
124
$smarty->assign('edittitle', $canedittitle);
$smarty->assign('canuseskins', $canuseskins);
125
126
$smarty->assign('displaylink', $view->get_url());
$smarty->assign('new', $new);
127
128
$smarty->assign('issiteview', $issiteview);
$smarty->assign('issitetemplate', $issitetemplate);
129
130
$smarty->assign('PAGEHEADING', $state);
$smarty->display('view/editlayout.tpl');
131

132
function create_settings_pieform(){
133
134
    global $view, $new, $pieformname, $issiteview, $issitetemplate,
    $canedittitle, $canuseskins;
135
136
137
138
139
    $inlinejavascript = '';

    //get elements for each section of the form
    if ($canedittitle) {
        $basicelements = get_basic_elements();
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
    list($layoutelements, $hiddenlayoutelements, $inlinejs) = get_layout_elements();
    $inlinejavascript .= $inlinejs;

    if ($canuseskins) {
        list($skinelements, $hiddenskinelements, $inlinejs) = get_skin_elements();
        $inlinejavascript .= $inlinejs;
        $layoutclasslast = '';
    }
    else {
        $layoutclasslast = 'last';
    }

    //visible elements of the sections

    $formelements = array();

    if ($canedittitle) {
        $formelements['basic'] = array(
            'type'        => 'fieldset',
            'collapsible' => true,
            'collapsed'   => false,
            'legend'      => get_string('basics', 'view'),
            'elements'    => $basicelements
        );
    }

    $formelements['layout'] = array(
        'type'        => 'fieldset',
        'collapsible' => true,
        'collapsed'   => $canedittitle || $canuseskins,
        'class'       => 'advancedlayoutselect ' . $layoutclasslast,
        'legend'      => get_string('layout', 'view'),
        'elements'    => $layoutelements
    );

    if ($canuseskins) {
        $formelements['skin'] = array(
            'type' => 'fieldset',
            'class' => 'last',
            'collapsible' => true,
            'collapsed' => true,
            'legend' => get_string('skin', 'view'),
            'elements' => $skinelements,
        );
    }

    $formelements['submit'] = array(
            'type' => 'submit',
            'class' => 'btn-primary',
            'value' => get_string('save'),
    );

    //hidden elements of the sections
    $hiddenelements = array(
        // hidden 4 all sections
        'id' => array(
            'type'  => 'hidden',
            'value' => $view->get('id'),
        ),
        'new' => array(
            'type' => 'hidden',
            'value' => $new,
        ),
    );

    $hiddenelements = array_merge($hiddenelements, $hiddenlayoutelements);
    if ($canuseskins) {
        $hiddenelements = array_merge($hiddenelements, $hiddenskinelements);
    }

    $elements = array_merge($formelements, $hiddenelements);

    //main form
    $settingsform = array(
        'name'      => $pieformname,
        'renderer'   => 'div',
        'plugintype' => 'core',
        'pluginname' => 'admin',
        'elements'   => $elements,
        //'validatecallback' => 'layout_validate',
    );

    return array(pieform($settingsform), $inlinejavascript);
225
226
}

227
function get_basic_elements() {
228
    global $view, $new, $urlallowed, $group, $institution, $USER, $cleanurlbase;
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314

    $formatstring = '%s (%s)';
    $ownerformatoptions = array(
        FORMAT_NAME_FIRSTNAME => sprintf($formatstring, get_string('firstname'), $USER->get('firstname')),
        FORMAT_NAME_LASTNAME => sprintf($formatstring, get_string('lastname'), $USER->get('lastname')),
        FORMAT_NAME_FIRSTNAMELASTNAME => sprintf($formatstring, get_string('fullname'), full_name())
    );

    $displayname = display_name($USER);
    if ($displayname !== '') {
        $ownerformatoptions[FORMAT_NAME_DISPLAYNAME] = sprintf($formatstring, get_string('preferredname'), $displayname);
    }
    $studentid = (string)get_field('artefact', 'title', 'owner', $USER->get('id'), 'artefacttype', 'studentid');
    if ($studentid !== '') {
        $ownerformatoptions[FORMAT_NAME_STUDENTID] = sprintf($formatstring, get_string('studentid'), $studentid);
    }

    $elements = array(
        'title'       => array(
            'type'         => 'text',
            'title'        => get_string('title','view'),
            'defaultvalue' => $view->get('title'),
            'rules'        => array( 'required' => true ),
        ),
        'urlid'       => array(
            'type'         => 'text',
            'title'        => get_string('viewurl', 'view'),
            'prehtml'      => '<span class="description">' . (isset($cleanurlbase) ? $cleanurlbase : '') . '</span> ',
            'description'  => get_string('viewurldescription', 'view') . ' ' . get_string('cleanurlallowedcharacters'),
            'defaultvalue' => $new ? null : $view->get('urlid'),
            'rules'        => array('maxlength' => 100, 'regex' => get_config('cleanurlvalidate')),
            'ignore'       => !$urlallowed || $new,
        ),
        'description' => array(
            'type'         => 'wysiwyg',
            'title'        => get_string('description','view'),
            'rows'         => 5,
            'cols'         => 70,
            'class'        => 'view-description',
            'defaultvalue' => $view->get('description'),
            'rules'        => array('maxlength' => 65536),
        ),
        'tags'        => array(
            'type'         => 'tags',
            'title'        => get_string('tags'),
            'description'  => get_string('tagsdescprofile'),
            'defaultvalue' => $view->get('tags'),
            'help'         => true,
        )
    );
    if ($group) {
        $grouproles = $USER->get('grouproles');
        if ($grouproles[$group] == 'admin') {
            $elements['locked'] = array(
                'type'         => 'switchbox',
                'title'        => get_string('Locked', 'view'),
                'description'  => get_string('lockedgroupviewdesc', 'view'),
                'defaultvalue' => $view->get('locked'),
                'disabled'     => $view->get('type') == 'grouphomepage', // This page unreachable for grouphomepage anyway
            );
        }
    }
    if (!($group || $institution)) {
        $default = $view->get('ownerformat');
        if (!$default) {
            $default = FORMAT_NAME_DISPLAYNAME;
        }
        $elements['ownerformat'] = array(
            'type'         => 'select',
            'title'        => get_string('ownerformat','view'),
            'description'  => get_string('ownerformatdescription','view'),
            'options'      => $ownerformatoptions,
            'defaultvalue' => $default,
            'rules'        => array('required' => true),
        );
    }

    if (get_config('allowanonymouspages')) {
        $elements['anonymise'] = array(
            'type'         => 'switchbox',
            'title'        => get_string('anonymise','view'),
            'description'  => get_string('anonymisedescription','view'),
            'defaultvalue' => $view->get('anonymise'),
        );
    }
    return $elements;
315
316
}

317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
function get_layout_elements() {
    global $pieformname, $view, $USER;

    // Layout
    $numrows = $view->get('numrows');
    $layoutcolumns = View::$layoutcolumns; // static, all possible column width combinations
    $layoutrows = $view->get_layoutrows();
    $maxlayoutrows = View::$maxlayoutrows; // static, max possible rows for custom layouts
    $basicoptionids = array_keys(
            get_records_select_assoc(
                    'view_layout',
                    'layoutmenuorder > 0 AND iscustom = 0',
                    array(),
                    'layoutmenuorder',
                    'id, id'
            )
    );
    $currentlayout = $view->get('layout');
    // if not set, use equal width layout for that number of columns
    if (!$currentlayout) {
        // if columns have been dynamically added or removed from a multi-row layout,
        // there may be no valid layout id, in which case none of the layout options will be selected
        $currentlayout = $view->get_layout()->id;
    }
    if (!in_array($currentlayout, $basicoptionids)) {
        $basicoptionids[] = $currentlayout;
    }
344

345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
    $layoutoptions = array();
    $basiclayoutoptions = array();
    $maxrows = 3;
    foreach ($layoutrows as $key => $layout) {
        $maxrows = (count($layout) > $maxrows)? count($layout) : $maxrows;
        $layoutoptions[$key]['rows'] = count($layout);

        $structure = array();
        $rowtext = array();
        for ($r = 1; $r <= count($layout); $r++) {
            $widths = $layoutcolumns[$layout[$r]]->widths;
            $structure['layout']['row' . $r] = $widths;
            $rowtext[] = str_replace(',', '-', $widths);
        }
        $structure['text'] = implode(' / ', $rowtext);
        $l = new LayoutPreviewImage($structure);
        $layoutoptions[$key]['layout'] = $l->create_preview();
        $layoutoptions[$key]['columns'] = $structure['text'];
    }
364

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
    foreach ($basicoptionids as $id) {
        if (array_key_exists($id, $layoutoptions)) {
            $basiclayoutoptions[$id] = $layoutoptions[$id];
        }
    }

    $clnumcolumnsoptions = array();
    for ($i=1; $i<6; $i++) {
        $clnumcolumnsoptions[$i] = $i;
    }

    $columnlayoutoptions = array();
    $columnlayouts = get_records_assoc('view_layout_columns');
    foreach ($columnlayouts as $layout => $percents) {
        $percentswidths = str_replace(',', ' - ', $percents->widths);
        $columnlayoutoptions[$layout] = $percentswidths;
    }

    // provide a simple default to build custom layouts with
    $defaultcustomlayout = View::default_columnsperrow();
    $defaultlayout = get_record('view_layout_columns', 'columns', $defaultcustomlayout[1]->columns, 'widths', $defaultcustomlayout[1]->widths);
    $clnumcolumnsdefault = $defaultlayout->columns;
    $clwidths = $defaultlayout->widths;

    // Ready custom layout preview.
    $defaultlayoutpreviewdata['layout']['row1'] = $defaultcustomlayout[1]->widths;
    $defaultlayoutpreviewdata['text'] = get_string($defaultcustomlayout[1]->widths, 'view');
    $defaultlayoutpreview = new LayoutPreviewImage($defaultlayoutpreviewdata);

    $inlinejavascript = <<<JAVASCRIPT
395
396
397
398
399

function get_max_custom_rows() {
    return {$maxlayoutrows};
}

400
jQuery(function () {
401
    formchangemanager.add("{$pieformname}");
402
403
404
405
});

JAVASCRIPT;

406
407
408
409
410
411
412
413
414
415
416
417
    $elements = array();

    $elements['layoutselect'] =  array(
            'type'          => 'layout',
            'maxrows'       => $maxrows,
            'options' => $layoutoptions,
            'currentlayout' => $currentlayout,
            'clnumcolumnsoptions' => $clnumcolumnsoptions,
            'clnumcolumnsdefault' => $clnumcolumnsdefault,
            'columnlayoutoptions' =>$columnlayoutoptions,
            'customlayoutid' => $defaultlayout->id,
            'customlayout' => $defaultlayoutpreview->create_preview(),
418
419
    );

420
421
422
423
424
425
426
427
428
429
430
431
432
433
    $hiddenelements = array(
        'customlayoutnumrows' => array(
                'type'  => 'hidden',
                'value' => 1,
        ),
        'currentlayoutselect' => array(//lo que era 'layoutselect'
            'type'  => 'hidden',
            'value' => $currentlayout,
            'sesskey' =>  $USER->get('sesskey'),
        ),
        'layoutfallback' => array(
            'type'  => 'hidden',
            'value' => $defaultlayout->id,
        ),
434
    );
435
436
437

    return array($elements, $hiddenelements, $inlinejavascript);

438
439
}

440
441
442
function get_skin_elements() {
    global $view, $USER, $new, $pieformname;
    $issiteview = $view->get('institution') == 'mahara';
443

444
445
446
    if (!can_use_skins(null, false, $issiteview)) {
        throw new FeatureNotEnabledException();
    }
447

448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
    // Is page skin already saved/set for current page?
    $skin = param_integer('skin', null);
    $saved = false;
    if (!isset($skin)) {
        $skin = $view->get('skin');
        $saved = true;
    }
    if (!$skin || !($currentskin = get_record('skin', 'id', $skin))) {
        $currentskin = new stdClass();
        $currentskin->id = 0;
        $currentskin->title = get_string('skinnotselected', 'skin');
    }
    $incompatible = (isset($THEME->skins) && $THEME->skins === false && $currentskin->id != 0);
    if ($incompatible) {
        $incompatible = ($view->get('theme')) ? 'notcompatiblewithpagetheme' : 'notcompatiblewiththeme';
        $incompatible = get_string($incompatible, 'skin', $THEME->displayname);
    }
    $metadata = array();
    if (!empty($currentskin->id)) {
        $owner = new User();
        $owner->find_by_id($currentskin->owner);
        $currentskin->metadata = array(
            'displayname' => '<a href="' . get_config('wwwroot') . 'user/view.php?id=' . $currentskin->owner . '">' . display_name($owner) . '</a>',
            'description' => nl2br($currentskin->description),
            'ctime' => format_date(strtotime($currentskin->ctime)),
            'mtime' => format_date(strtotime($currentskin->mtime)),
         );
    }

    $userskins   = Skin::get_user_skins();
    $favorskins  = Skin::get_favorite_skins();
    $siteskins   = Skin::get_site_skins();
    $defaultskin = Skin::get_default_skin();

    if (!$USER->can_edit_view($view)) {
        throw new AccessDeniedException();
    }
    $displaylink = $view->get_url();
    if ($new) {
        $displaylink .= (strpos($displaylink, '?') === false ? '?' : '&') . 'new=1';
    }

    $snippet = smarty_core();
    $snippet->assign('saved', $saved);
    $snippet->assign('incompatible', $incompatible);
    $snippet->assign('currentskin', $currentskin->id);
    $snippet->assign('currenttitle', $currentskin->title);
    $snippet->assign('currentmetadata', (!empty($currentskin->metadata)) ? $currentskin->metadata : null);
    $snippet->assign('userskins', $userskins);
497
    $snippet->assign('favorskins',$favorskins);
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
    $snippet->assign('siteskins', $siteskins);
    $snippet->assign('defaultskin', $defaultskin);
    $snippet->assign('viewid', $view->get('id'));
    $snippet->assign('viewtype', $view->get('type'));
    $snippet->assign('edittitle', $view->can_edit_title());
    $snippet->assign('new', $new);
    $snippet->assign('issiteview', $issiteview);
    $skinform = array(
        'skins_html' => array(
            'type' => 'html',
            'value' => $snippet->fetch('view/skin.tpl'),
        )
    );

    $hiddenelements = array(
        'skinid' => array(
            'type' => 'hidden',
            'value' =>  $currentskin->id,
            'sesskey' =>  $USER->get('sesskey'),
        ),
    );

    $inlinejs = <<<JAVASCRIPT

function change_skin(view, skin) {
  var pd   = {
       'id': view,
       'skin': skin,
       'pieformname': "{$pieformname}"
       }
  sendjsonrequest(config['wwwroot'] + 'view/skins.json.php', pd, 'POST', function(data) {
      $('#settings_skins_html_container').html(data.html);
      $('#settings_skinid').val(data.skin);
      formchangemanager.setFormState($('#' + data.pieformname), FORM_CHANGED);
  });
};

JAVASCRIPT;

    return array($skinform, $hiddenelements, $inlinejs);
}
539

540
function settings_validate(Pieform $form, $values) {
541
    global $view, $issiteview, $issitetemplate, $canuseskins;
542
543
544

    $layoutrows = $view->get_layoutrows();
    if (!isset($layoutrows[$values['currentlayoutselect']]) ) {
545
546
547
548
549
550
551
552
553
554
555
556
557
558
        $form->set_error(null, get_string('invalidlayoutselection', 'error'));
    }

    if (isset($values['urlid']) && $values['urlid'] != $view->get('urlid')) {
        if (strlen($values['urlid']) < 3) {
            $form->set_error('urlid', get_string('rule.minlength.minlength', 'pieforms', 3));
        }
        else if ($group = $view->get('group') and record_exists('view', 'group', $group, 'urlid', $values['urlid'])) {
            $form->set_error('urlid', get_string('groupviewurltaken', 'view'));
        }
        else if ($owner = $view->get('owner') and record_exists('view', 'owner', $owner, 'urlid', $values['urlid'])) {
            $form->set_error('urlid', get_string('userviewurltaken', 'view'));
        }
    }
559
560
561
562
563
564
565

    if ($canuseskins && isset($values['skinid']) && $values['skinid']) {
        $skin = new Skin($values['skinid']);
        if (!$skin->can_use()) {
            throw new AcessDeniedException();
        }
    }
566
567
}

568
function settings_submit(Pieform $form, $values) {
569
    global $view, $SESSION, $new, $issiteview, $issitetemplate, $canedittitle, $canuseskins;
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587

    if ($canedittitle) {
        set_view_title_and_description($form, $values);
    }

    set_view_layout($form, $values);

    if ($canuseskins && isset($values['skinid'])) {
        $view->set('skin', $values['skinid']);
    }

    $view->commit();
    $SESSION->add_ok_msg(get_string('viewsavedsuccessfully', 'view'));
    redirect('/view/blocks.php?id=' . $view->get('id') . ($new ? '&new=1' : ''));
  }

function set_view_layout(Pieform $form, $values){
    global $view, $SESSION, $new;
588
589
590

    $oldrows = $view->get('numrows');
    $oldlayout = $view->get_layout();
591
592
593
    $newlayout = $values['currentlayoutselect'];
    $layoutrows = $view->get_layoutrows();
    $layoutcolumns = View::$layoutcolumns; // static, all possible column width combinations
594
    if (!isset($layoutrows[$newlayout])) {
595
        throw new ParamOutOfRangeException(get_string('invalidlayoutselection', 'error'));
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
    }
    else {
        $newrows = count($layoutrows[$newlayout]);
    }

    db_begin();

    // for each existing row which will still exist after the update, check whether to add or remove columns
    for ($i = 0; $i < min(array($oldrows, $newrows)); $i++) {
        // compare oldlayout column structure with newlayout
        $oldcolumns = $oldlayout->rows[$i+1]['columns'];
        $newcolumnindex = $layoutrows[$newlayout][$i+1];
        $newcolumns = $layoutcolumns[$newcolumnindex]->columns;

        // Specify row when adding or removing columns
        if ($oldcolumns > $newcolumns) {
            for ($j = $oldcolumns; $j > $newcolumns; $j--) {
                $view->removecolumn(array('row' => $i+1, 'column' => $j));
            }
        }
        else if ($oldcolumns < $newcolumns) {
            for ($j = $oldcolumns; $j < $newcolumns; $j++) {
                $view->addcolumn(array('row' => $i+1, 'before' => $j+1, 'returndata' => false));
            }
        }

        $dbcolumns = get_field('view_rows_columns', 'columns', 'view', $view->get('id'), 'row', $i+1);

        if ($dbcolumns != $newcolumns) {
            db_rollback();
            $SESSION->add_error_msg(get_string('changecolumnlayoutfailed', 'view'));
            redirect(get_config('wwwroot') . 'view/blocks.php?id=' . $view->get('id') . ($new ? '&new=1' : ''));
        }
    }
    // add or remove rows and move content accordingly if required
    if ($oldrows > $newrows) {
        for ($i = $oldrows; $i > $newrows; $i--) {
            $view->removerow(array('row' => $i, 'layout' => $oldlayout));
        }
    }
    else if ($oldrows < $newrows) {
        for ($i = $oldrows; $i < $newrows; $i++) {
            $view->addrow(array('before' => $i + 1, 'newlayout' => $newlayout, 'returndata' => false));
        }
    }

    if ($view->get('numrows') != $newrows) {
        db_rollback();
        $SESSION->add_error_msg(get_string('changerowlayoutfailed', 'view'));
645
        redirect(get_config('wwwroot') . 'view/editlayout.php?id=' . $view->get('id') . ($new ? '&new=1' : ''));
646
647
648
649
    }

    db_commit();

650
651
652
653
654
655
    $view->set('layout', $newlayout); //layout
}

function set_view_title_and_description(Pieform $form, $values){
    global $view, $new, $urlallowed;

656
657
658
659
660
661
662
663
664
665
    $view->set('title', $values['title']);
    if (trim($values['description']) !== '') {
        // Add or update embedded images in the view description
        require_once('embeddedimage.php');
        $view->set('description', EmbeddedImage::prepare_embedded_images($values['description'], 'description', $view->get('id')));
    }
    else {
        // deleting description
        $view->set('description', '');
    }
666
667
    $tags = $values['tags'] ? $values['tags'] : array();
    $view->set('tags', $tags);
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
    if (isset($values['locked'])) {
        $view->set('locked', (int)$values['locked']);
    }
    if (isset($values['ownerformat']) && $view->get('owner')) {
        $view->set('ownerformat', $values['ownerformat']);
    }
    if (isset($values['anonymise'])) {
        $view->set('anonymise', (int)$values['anonymise']);
    }
    if (isset($values['urlid'])) {
        $view->set('urlid', strlen($values['urlid']) == 0 ? null : $values['urlid']);
    }
    else if ($new && $urlallowed) {
        // Generate one automatically based on the title
        $desired = generate_urlid($values['title'], get_config('cleanurlviewdefault'), 3, 100);
        $ownerinfo = (object) array('owner' => $view->get('owner'), 'group' => $view->get('group'));
        $view->set('urlid', View::new_urlid($desired, $ownerinfo));
    }
686
}