templatemigration.php 26.8 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
<?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();
require_once('view.php');
29
safe_require('blocktype', 'textbox'); // need this for labels, always
30
31
32
safe_require('artefact', 'internal'); // needed for blocktype creation
safe_require('artefact', 'resume');   // ditto

33

34
35
36
37
/**
 * Performs the template migration, from the old view template style of 0.8 to 
 * the new views format for 0.9
 */
38
function upgrade_template_migration() {
39
    log_debug('upgrade_template_migration()');
40

41
42
    if (!$views = get_records_array('view', '', '', 'id')) {
        log_debug('no views to migrate');
43
44
        return;
    }
45
46
    log_debug('we have ' . count($views) . ' views to migrate...');

47

48
49
50
    //
    // Static data needed by various templates when they are migrated
    //
51

52
53
54
55
56
    // blogreflection
    $blogreflection_numbers = array(1 => 'One', 2 => 'One', 3 => 'Two', 4 => 'Two', 5 => 'Three', 6 => 'Three');

    // ppae
    $ppae_text = array(
57
58
59
60
61
62
63
64
        '<h4>Group Name</h4>', 
        '<h4>Student Names</h4>', 
        '<h4>Mission and Vision Statement (Concept and Concept Outline)</h4>', 
        '<h4>Physical Design of the space</h4><p>Explain, using theorists, how you created ambeince and support of holistic learning for each child</p>', 
        '<h4>Schedule or timetable of tasks and events prior to opening</h4>', 
        '<h4>Curriculum Matrix for first six weeks, specific activities for beginners, intermediates, end-game</h4><p>provide NZCF AO/level cross-reference</p>', 
        '<h4>Yearlong Teaching and Learning Topics plan for beginners, intermediates and end-game.</h4><p>Justify your choices by citing educationalists, psychologists and other theorists</p>', 
        '<h4>Priorised table of equipment, materials and supplies</h4>'
65
66
    );

67
68
69

    // The main view migration loop
    $count = 0;
70
    foreach ($views as $view) {
71
72
73
74
75
76
77
78
79
80
81
82
        $count++;
        log_debug("Migrating view $view->id ($count)");
        if ($view->template == 'test') {
            // We are NOT migrating test templates. There's not much point. The 
            // only real data loss from this is the labels, which are only one 
            // line text inputs. If this annoys someone they could always have 
            // a go at writing a migration. There's no technical reason why one 
            // couldn't be done.
            log_debug("View $view->id is a test view. Deleting instead");
            $view = new View($view->id);
            delete_records('view_content', 'view', $view->get('id'));
            $view->delete();
83
84
85
            continue;
        }

86
87
88
89
90
91
92
93
94
95
96
97
98
99
        //
        // $viewcolumns is a datastructure representing the new structure of a 
        // view. It's an array of columns, which are arrays of label titles etc.
        // All the values are null.
        //
        // We are trying to turn the nulls into blockinstances
        //
        if (!$viewcolumns = upgrade_template_get_structure($view->template)) {
            log_warn("Unsupported custom template $view->template! Deleting!");
            $view = new View($view->id);
            delete_records('view_content', 'view', $view->get('id'));
            $view->delete();
            continue;
        }
100
101
        $numcolumns = count($viewcolumns);

102
103
104
105
        // Handle all label blocks, by converting them to WYSIWYG blocks
        if ($lblocks = get_records_array('view_content', 'view', $view->id)) {
            foreach ($lblocks as $block) {
                upgrade_template_insert_block($viewcolumns, $block->block, upgrade_template_create_wysiwyg($block->content, $view->id));
106
            }
107
108
109
110
111
112
113
114
115
116
117
        }

        // Get all artefact blocks in the view
        if ($ablocks = get_records_sql_array('
            SELECT va.*, a.artefacttype, a.title
            FROM {view_artefact} va
            INNER JOIN {artefact} a ON (va.artefact = a.id)
            WHERE "view" = ?', array($view->id))) {
            foreach ($ablocks as $block) {
                if (!upgrade_template_block_exists($viewcolumns, $block->oldblock)) {
                    // There's no block here. We can make one and insert it
118
                    $bi = upgrade_template_convert_block_to_blockinstance($block, $view);
119
120
121
122
123
124
125
                    // note: the location to insert the block is known as 
                    // 'oldblock', as the column as been renamed in the database 
                    // in preparation for its removal
                    upgrade_template_insert_block($viewcolumns, $block->oldblock, $bi);
                }
                else {
                    // We're dealing with multiple artefacts in the same block... just need to append
126
                    upgrade_template_update_block($viewcolumns, $block, $view);
127
                }
128
129
130
            }
        }

131
132
133
134
        //
        // Special case code for each template, to make sure the labels and 
        // hard coded text are migrated nicely
        //
135
        if ($view->template == 'blogreflection') {
136
137
            // There are six labels in this view - the three reflection titles 
            // and three 'last date available' fields
138
            for ($i = 1; $i <= 6; $i++) {
139
140
                // The labels were badly numbered, 2, 4 and 5 are the right 
                // hand side (and thus the 'last date available' ones)
141
                if (in_array($i, array(2, 4, 5))) {
142
143
                    $column = 1;
                    $text = '<h4>Reflection ' . $blogreflection_numbers[$i] . ': Last Date Available</h4>';
144
145
                }
                else {
146
147
                    $column = 0;
                    $text = '<h3>Reflection ' . $blogreflection_numbers[$i] . ' Title</h3>'; 
148
149
                }

150
151
152
                // The label migration would have put WYSIWYG blocks in where 
                // there was content. If there is none, let's ignore this block
                if (empty($viewcolumns[$column]['tpl_label' . $i])) {
153
154
155
                    continue;
                }

156
                $labelkey = 'tpl_label' . $i;
157
158
159
160
161
                $replace = $text . '<p>' . upgrade_template_get_wysiwyg_content($viewcolumns, $column, 'tpl_label' . $i) . '</p>';

                // Update the WYSIWYG blocks to have the titles they need
                if (in_array($i, array(2, 4, 5))) {
                    upgrade_template_update_wysiwyg($viewcolumns, $column, $labelkey, null, $replace);
162
163
                }
                else {
164
                    upgrade_template_update_wysiwyg($viewcolumns, $column, 'tpl_label' . $i, null, $replace);
165
166
167
168
169
                }
            }
        }
        else if ($view->template == 'PPAE') {
            if (!empty($viewcolumns[0]['tpl_label1'])) {
170
                upgrade_template_update_wysiwyg($viewcolumns, 0, 'tpl_label1', null, '<h4>' . $ppae_text[0] . '</h4>' . upgrade_template_get_wysiwyg_content($viewcolumns, 0, 'tpl_label1'));
171
172
            }
            if (!empty($viewcolumns[0]['tpl_label2']) || !empty($viewcolumns[0]['tpl_label3']) || !empty($viewcolumns[0]['tpl_label4']) || !empty($viewcolumns[0]['tpl_label5'])) {
173
                // mash it all into the first one and unset the rest 
174
175
                $label2_text = '<h4>' . $ppae_text[1] . '</h4>'
                    . upgrade_template_get_wysiwyg_content($viewcolumns, 0, 'tpl_label2') . '<br>' 
176
177
                    . upgrade_template_get_wysiwyg_content($viewcolumns, 0, 'tpl_label3') . '<br>' 
                    . upgrade_template_get_wysiwyg_content($viewcolumns, 0, 'tpl_label4') . '<br>'
178
179
180
181
182
183
184
185
186
187
                    . upgrade_template_get_wysiwyg_content($viewcolumns, 0, 'tpl_label5');

                $label2_text = preg_replace('#(<br>)+$#', '', $label2_text);

                if (upgrade_template_block_exists($viewcolumns, 'tpl_label2')) {
                    upgrade_template_update_wysiwyg($viewcolumns, 0, 'tpl_label2', null, $label2_text);
                }
                else {
                    upgrate_template_insert_block($viewcolumns, 'tpl_label2', upgrade_template_create_wysiwyg($label2_text, $view->id));
                }
188
189
190
191
192
                unset($viewcolumns[0]['tpl_label3']);
                unset($viewcolumns[0]['tpl_label4']);
                unset($viewcolumns[0]['tpl_label5']);
            }
            if (!empty($viewcolumns[0]['tpl_blog1'])) {
193
                $viewcolumns[0]['tpl_converted1'] = upgrade_template_create_wysiwyg($ppae_text[2], $view->id);
194
195
            }
            if (!empty($viewcolumns[0]['tpl_files1']) || !empty($viewcolumns[0]['tpl_blog2'])) {
196
                $viewcolumns[0]['tpl_converted2'] = upgrade_template_create_wysiwyg($ppae_text[3], $view->id);
197
198
            }
            if (!empty($viewcolumns[0]['tpl_files2']) || !empty($viewcolumns[0]['tpl_blog3'])) {
199
                $viewcolumns[0]['tpl_converted3'] = upgrade_template_create_wysiwyg($ppae_text[4], $view->id);
200
201
            }
            if (!empty($viewcolumns[0]['tpl_files3']) || !empty($viewcolumns[0]['tpl_blog4'])) {
202
                $viewcolumns[0]['tpl_converted4'] = upgrade_template_create_wysiwyg($ppae_text[5], $view->id);
203
204
            }
            if (!empty($viewcolumns[0]['tpl_files4']) || !empty($viewcolumns[0]['tpl_blog5'])) {
205
                $viewcolumns[0]['tpl_converted5'] = upgrade_template_create_wysiwyg($ppae_text[6], $view->id);
206
207
            }
            if (!empty($viewcolumns[0]['tpl_files5']) || !empty($viewcolumns[0]['tpl_blog6'])) {
208
                $viewcolumns[0]['tpl_converted6'] = upgrade_template_create_wysiwyg($ppae_text[7], $view->id);
209
210
211
            }
        }
        
212
213
214
215
216
217
218
219
220
221
222
223
224
        // Clean up empty columns, although the gallery should always have its 
        // five columns
        if ($view->template != 'gallery') {
            foreach ($viewcolumns as $c => $col) {
                $empty = true;
                foreach ($col as $key => $guff) {
                    if (!empty($guff)) {
                        $empty = false;
                    }
                }
                if ($empty) {
                    $numcolumns--;
                    unset($viewcolumns[$c]);
225
226
227
                }
            }
        }
228
229

        // Make all the block instances have the correct column and order 
230
        foreach ($viewcolumns as $c => $col) {
231
232
            $order = 1;
            foreach ($col as $key => $block) {
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
                if (is_array($block)) {
                    foreach ($block as $bi) {
                        $bi->set('column', ($c + 1));
                        $bi->set('order', $order);
                        $bi->commit();
                        $order++;
                    }
                }
                else {
                    if ($block instanceof BlockInstance) {
                        $block->set('column', ($c + 1));
                        $block->set('order', $order);
                        $block->commit();
                        $order++;
                    }
248
                }
249
250
251
            }
        }

252
253
254
255
        // Work out what layout to set the view to
        $layout = upgrade_template_get_view_layout($view->template);

        // Commit the view!
256
257
        $view = new View($view->id);
        $view->set('numcolumns', $numcolumns);
258
259
260
        if ($layout) {
            $view->set('layout', $layout);
        }
261
        $view->commit();
262
    } // End of the view loop
263
264
}

265
/**
266
* Puts blockinstances into the appropriate place in the deeply nested array
267
268
*
* @param array (reference) $columns column structure
269
270
* @param string            $key     key to insert blockinstance at
* @param BlockInstance     $bi      blockinstance to insert
271
*/
272
function upgrade_template_insert_block(&$columns, $key, BlockInstance $bi) {
273
274
275
276
277
278
279
    foreach ($columns as &$c) {
        if (array_key_exists($key, $c)) {
            if (!empty($c[$key])) {
                $e = new TemplateBlockExistsAlreadyException();
                $e->set_block_data($c[$key]);
                throw $e;
            }
280
            $c[$key] = $bi;
281
282
283
284
285
            return;
        }
    }
}

286
287
288
289
290
291
292
293
294
/**
 * Takes a block that, according to its oldblock setting, wants to be inserted 
 * somewhere where there is an existing blockinstance.  With this information, 
 * establishes how to change the existing blockinstance, or what to replace it 
 * with, so that both artefacts are in the same blockinstance.
 *
 * @param array (reference) $columns column structure
 * @param stdClass          $block   the new block data
 */
295
function upgrade_template_update_block(&$columns, $block, $view) {
296
297
298
    // $block->oldblock is where the existing blockinstance is
    // Then we need to establish what to put in $columns, or in the blockinstance
    $bi = null;
299
    $column = null;
300
301
    foreach ($columns as &$c) {
        if (array_key_exists($block->oldblock, $c)) {
302
            $column =& $c;
303
304
            if (!empty($c[$block->oldblock])) {
                $bi = $c[$block->oldblock];
305
306
307
308
309
                // Will be an array if there's more than one blockinstance here. 
                // See the 'else' clause below
                if (is_array($bi)) {
                    $bi = $bi[0];
                }
310
            }
311
            break;
312
313
314
315
316
317
318
319
320
321
322
323
        }
    }

    if (empty($bi)) {
        log_debug("WTF: tried to update a block when there was nothing there to update");
        return;
    }

    // If the blockinstance is a filedownload block and we have a file or image 
    // to add, add it directly to the blockinstance
    if ($bi->get('blocktype') == 'filedownload') {
        if ($block->artefacttype == 'file' || $block->artefacttype == 'image') {
324
325
            upgrade_template_add_artefact_to_blockinstance($bi, $block->artefact);
        }
326
327
        // More than one artefact in this filedownload block, so remove the title
        $bi->set('title', '');
328
329
330
331
332
333
334
335
336
337
    }
    // If the blockinstance is profileinfo, we can keep adding profile fields to it
    else if ($bi->get('blocktype') == 'profileinfo') {
        if (in_array($block->artefacttype, PluginArtefactInternal::get_artefact_types())) {
            if ($block->artefacttype != 'profileicon') {
                upgrade_template_add_artefact_to_blockinstance($bi, $block->artefact);
            }
            else {
                // The profileicon is stored in a dedicated field, not 'artefactids'
                $configdata = $bi->get('configdata');
338
                $configdata['profileicon'] = $block->artefact;
339
340
                $bi->set('configdata', $configdata);
            }
341
342
        }
    }
343
344
345
346
347
    else if ($bi->get('blocktype') == 'recentposts') {
        if ($block->artefacttype == 'blog') {
            upgrade_template_add_artefact_to_blockinstance($bi, $block->artefact);
        }
    }
348
349
350
351
352
353
354
355
    else {
        // Make a new blockinstance and insert it below
        $bi = upgrade_template_convert_block_to_blockinstance($block, $view);
        if (!is_array($column[$block->oldblock])) {
            $column[$block->oldblock] = array($column[$block->oldblock]);
        }
        $column[$block->oldblock] = array_merge(array($bi), $column[$block->oldblock]);
    }
356
}
357

358
359
360
361
362
363
364
365
366
/**
 * Given a blockinstance that is assumed to have an 'artefactids' config field 
 * and an artefact, ensures that the blockinstance config includes the given 
 * artefact ID
 */
function upgrade_template_add_artefact_to_blockinstance(BlockInstance $bi, $artefact) {
    $configdata = $bi->get('configdata');
    $configdata['artefactids'][] = $artefact;
    $bi->set('configdata', $configdata);
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
}

/**
 * Determines whether a block already exists at the given location
 *
 * @param array (reference) $columns column structure
 * @param string            $key     key to check for existance
 */
function upgrade_template_block_exists(&$columns, $key) {
    foreach ($columns as &$c) {
        if (array_key_exists($key, $c)) {
            if (!empty($c[$key])) {
                return true;
            }
        }
    }
    return false;
}

/**
 * Creates a WYSIWYG blockinstance
 *
 * @param string $content The content for the wysiwyg block
 * @param int $view The view the new block will be in
 */
392
393
function upgrade_template_create_wysiwyg($content, $view) {
    $b = new BlockInstance(0, array(
394
395
        'title'      => '',
        'blocktype'  => 'textbox',
396
        'configdata' => serialize(array('text' => $content)),
397
        'view'       => $view,
398
399
400
401
402
    ));

    return $b;
}

403
404
405
/**
 * Given a location with a WYSIWYG blockinstance, either appends or replaces its content
 */
406
function upgrade_template_update_wysiwyg(&$columns, $column, $key, $appendcontent=null, $replacecontent=null) {
407
    $block = $columns[$column][$key];
408
409
410
411
412
413
414
415
416
417
    $data = $block->get('configdata');
    if (!empty($appendcontent)) {
        $data['text'] .= $appendcontent;
    }
    else {
        $data['text'] = $replacecontent;
    }
    $block->set('configdata', $data);
}

418
/**
419
420
 * Gets content of an existing WYSIWYG blockinstance, or an empty string if the 
 * block is empty
421
 */
422
423
function upgrade_template_get_wysiwyg_content($columns, $column, $key) {
    $block = $columns[$column][$key];
424
425
426
    if (empty($block)) {
        return '';
    }
427
428
    $data = $block->get('configdata');
    return $data['text'];
429
}
430

431
432
433
434
435
436
/**
 * Get the new view structure for the given template.
 *
 * @param string $template The template to get the structure for
 * @return array
 */
437
function upgrade_template_get_structure($template) {
438
439
440
441
442
443

    static $columnstructure;
    if (empty($columnstructure)) {

        $columnstructure = array(
            'blogandprofile' => array(
444
                // First column
445
                array(
446
                    // Each thing in the column, from top to bottom
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
                    'tpl_blogslabel' => null,
                    'tpl_blog1'      => null,
                    'tpl_blog2'      => null
                ),
                array(
                    'tpl_profilelabel' => null,
                    'tpl_profile'      => null,
                ),
            ),
            'blogreflection' => array( 
                array(
                    'tpl_label1' => null,
                    'tpl_blog1'  => null,
                    'tpl_label3' => null, 
                    'tpl_blog2'  => null,
                    'tpl_label6' => null,
463
                    'tpl_blog3'  => null,
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
497
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
                ), 
                array(
                    'tpl_label2' => null,
                    'tpl_label4' => null,
                    'tpl_label5' => null,
                ), // I know it looks like 5 and 6 are backwards, it's in the template html
            ),
            'filelist' => array(
                array(
                    'tpl_fileslabel1' => null,
                    'tpl_files1'      => null,
                    'tpl_fileslabel2' => null,
                    'tpl_files2'      => null,
                    'tpl_fileslabel3' => null,
                    'tpl_files3'      => null,
                ),
                array(
                    'tpl_freelabel' => null
                ),
            ),
            'gallery' => array(
                array(
                    'tpl_image1' => null,
                    'tpl_label1' => null,
                    'tpl_image6' => null,
                    'tpl_label6' => null,
                ),
                array(
                    'tpl_image2' => null,
                    'tpl_label2' => null, 
                    'tpl_image7' => null,
                    'tpl_label7' => null,
                ),
                array(
                    'tpl_image3' => null,
                    'tpl_label3' => null,
                    'tpl_image8' => null,
                    'tpl_label8' => null,
                ),
                array(
                    'tpl_image4' => null,
                    'tpl_label4' => null,
                    'tpl_image9' => null,
                    'tpl_label9' => null,
                ),
                array(
                    'tpl_image5'  => null,
                    'tpl_label5'  => null,
                    'tpl_image10' => null,
                    'tpl_label10' => null,
                ),
            ), 
            'generaltemplate' => array(
                array(
                    'tpl_label'    => null,
                    'tpl_generic1' => null,
                    'tpl_generic2' => null,
                    'tpl_generic3' => null,
                    'tpl_generic4' => null,
                ),
            ),
            'professionalprofile' => array( 
                array(
                    'tpl_firstname' => null,
                    'tpl_generic4'  => null,
                    'tpl_generic1'  => null,
                    'tpl_label1'    => null,
                    'tpl_generic2'  => null,
                ),
                array(
                    'tpl_lastname' => null,
                    'tpl_image'    => null,
                    'tpl_label2'   => null,
                    'tpl_generic3' => null,
                ),
            ),
            'PPAE' => array( 
                array(
                    // Free text here (Group Name)
                    'tpl_label1' => null,
                    // Free text here (Student Names)
                    'tpl_label2' => null,
                    'tpl_label3' => null,
                    'tpl_label4' => null, 
                    'tpl_label5' => null,
                    // Free text here (Mission.. )
                    'tpl_converted1' => null,
                    'tpl_blog1'  => null,
                    // Free text here (Physical Design.. )
                    'tpl_converted2' => null,
                    'tpl_files1' => null,
                    'tpl_blog2'  => null, 
                    // Free text here (Schedule or timetable.. )
                    'tpl_converted3' => null,
                    'tpl_files2' => null,
                    'tpl_blog3'  => null, 
                    // Free text here (Curriculum Matrix.. )
                    'tpl_converted4' => null,
                    'tpl_files3' => null,
                    'tpl_blog4'  => null,
                    // Free text here (Yearlong teaching.. )
                    'tpl_converted5' => null,
                    'tpl_files4' => null,
                    'tpl_blog5'  => null,
                    // Free text here (Prioritised table)
                    'tpl_converted6' => null,
                    'tpl_files5' => null,
                    'tpl_blog6'  => null
                ),
            ),
        );
    }

    if (!array_key_exists($template, $columnstructure)) {
        return false;
    }
    return $columnstructure[$template];
}

583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/**
 * Select what view layout the template should be migrated to
 */
function upgrade_template_get_view_layout($template) {
    if ($template == 'blogreflection') {
        return 2; // 67/33
    }
    return null;
}

/**
 * Given a record from the view_artefact table (otherwise known as a "block"), 
 * try and establish a blockinstance that it could be under the new system and 
 * return it
 */
function upgrade_template_convert_block_to_blockinstance($block, $view) {
    if ($block->artefacttype == 'blogpost') {
        $bi = new BlockInstance(0, array(
            'title' => $block->title,
            'blocktype' => 'blogpost',
            'configdata' => serialize(array('artefactid' => $block->artefact)),
604
            'view' => $view->id,
605
606
607
608
        ));
        return $bi;
    }
    else if ($block->artefacttype == 'blog') {
609
610
611
612
613
614
615
616
617
        if ($block->format == 'listself') {
            $blocktype = 'recentposts';
            $configdata = array('artefactids' => array($block->artefact));
        }
        else {
            $blocktype = 'blog';
            $configdata = array('artefactid' => $block->artefact);
        }

618
        $bi = new BlockInstance(0, array(
619
620
621
            'title' => '',
            'blocktype' => $blocktype,
            'configdata' => serialize($configdata),
622
            'view' => $view->id,
623
624
625
626
        ));
        return $bi;
    }
    else if ($block->artefacttype == 'image') {
627
628
629
630
631
632
        if ($block->format == 'listself') {
            $blocktype = 'filedownload';
            $configdata = array('artefactids' => array($block->artefact));
        }
        else {
            $blocktype = 'image';
633
            $configdata = array('artefactid' => $block->artefact, 'width' => ($view->template == 'gallery' ? 175 : 350));
634
        }
635
        $bi = new BlockInstance(0, array(
636
            'title' => '',
637
638
            'blocktype' => $blocktype,
            'configdata' => serialize($configdata),
639
            'view' => $view->id,
640
641
642
643
644
645
646
647
        ));
        return $bi;
    }
    else if ($block->artefacttype == 'file') {
        $bi = new BlockInstance(0, array(
            'title' => $block->title,
            'blocktype' => 'filedownload',
            'configdata' => serialize(array('artefactids' => array($block->artefact))),
648
            'view' => $view->id,
649
650
651
        ));
        return $bi;
    }
652
653
654
655
656
    else if (in_array($block->artefacttype, PluginArtefactResume::get_artefact_types())) {
        $bi = new BlockInstance(0, array(
            'title' => $block->title,
            'blocktype' => 'resumefield',
            'configdata' => serialize(array('artefactid' => $block->artefact)),
657
            'view' => $view->id,
658
659
660
        ));
        return $bi;
    }
661
662
663
664
665
666
667
668
669
670
    else if (in_array($block->artefacttype, PluginArtefactInternal::get_artefact_types())) {
        // This happens in the blogandprofile template, there's a 'listself' internal artefact thing
        $bi = new BlockInstance(0, array(
            'title' => '',
            'blocktype' => 'profileinfo',
            'configdata' => serialize(array('artefactids' => array($block->artefact))),
            'view' => $view->id,
        ));
        return $bi;
    }
671
672
    else if ($block->artefacttype == 'folder') {
        $bi = new BlockInstance(0, array(
673
            'title' => $block->title,
674
675
676
677
678
679
            'blocktype' => 'folder',
            'configdata' => serialize(array('artefactid' => $block->artefact)),
            'view' => $view->id,
        ));
        return $bi;
    }
680

681
682
    log_info("Created a placeholder blockinstance for:");
    log_info($block);
683
684
685
686
    $bi = new BlockInstance(0, array(
        'title' => 'TODO - correct blocktype',
        'blocktype' => 'textbox',
        'configdata' => serialize(array('text' => 'TODO - correct blocktype')),
687
        'view' => $view->id,
688
689
690
691
    ));
    return $bi;
}

692
693
694
695
696
697
698
699
700
701
702
703
class TemplateBlockExistsAlreadyException extends MaharaException {
    
    private $blockdata;

    public function set_block_data($data) {
        $this->blockdata = $data;
    }

    public function get_block_data() {
        return $this->blockdata;
    }
}
704

705
?>