Passed
Push — master ( a1de92...1a465b )
by Robbie
03:52
created

src/Tasks/MigrateContentToElement.php (2 issues)

1
<?php
2
3
namespace DNADesign\Elemental\Tasks;
4
5
use DNADesign\Elemental\Models\ElementalArea;
6
use DNADesign\Elemental\Models\ElementContent;
7
8
use SilverStripe\Dev\BuildTask;
9
10
class MigrateContentToElement extends BuildTask
11
{
12
13
    protected $title = 'MigrateContentToElement';
14
15
    protected $description = 'When installing Elemental this task converts content in the $Content '
16
        . 'field to an ElementContent';
17
18
    public function run($request)
19
    {
20
        // TODO: needs rewriting for multiple elemental areas
21
        $pageTypes = singleton(ElementalArea::class)->supportedPageTypes();
22
        $count = 0;
23
        foreach ($pageTypes as $pageType) {
24
            $pages = $pageType::get()->filter('ElementalAreaID', 0);
25
            foreach ($pages as $page) {
26
                $content = $page->Content;
27
                $page->Content = '';
28
                // trigger area relations to be setup
29
                $page->write();
30
                $area = $page->ElementalArea();
31
                $element = new ElementContent();
32
                $element->Title = 'Auto migrated content';
33
                $element->HTML = $content;
0 ignored issues
show
Bug Best Practice introduced by
The property HTML does not exist on DNADesign\Elemental\Models\ElementContent. Since you implemented __set, consider adding a @property annotation.
Loading history...
34
                $element->ParentID = $area->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property ParentID does not exist on DNADesign\Elemental\Models\ElementContent. Since you implemented __set, consider adding a @property annotation.
Loading history...
35
                $element->write();
36
            }
37
            $count += $pages->Count();
38
            echo 'Migrated ' . $pages->Count() . ' ' . $pageType . ' pages\' content<br>';
39
        }
40
        echo 'Finished migrating ' . $count . ' pages\' content<br>';
41
    }
42
}
43