Test Setup Failed
Pull Request — master (#197)
by Gorrie
01:07
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 field to an ElementContent';
16
17
    public function run($request)
18
    {
19
        // TODO: needs rewriting for multiple elemental areas
20
        $pageTypes = ElementalArea::elemental_page_types();
21
        $count = 0;
22
        foreach ($pageTypes as $pageType) {
23
            $pages = $pageType::get()->filter('ElementalAreaID', 0);
24
            foreach ($pages as $page) {
25
                $content = $page->Content;
26
                $page->Content = '';
27
                // trigger area relations to be setup
28
                $page->write();
29
                $area = $page->ElementalArea();
30
                $element = new ElementContent();
31
                $element->Title = 'Auto migrated content';
32
                $element->HTML = $content;
0 ignored issues
show
Bug Best Practice introduced by John Milmine
The property HTML does not exist on DNADesign\Elemental\Models\ElementContent. Since you implemented __set, consider adding a @property annotation.
Loading history...
33
                $element->ParentID = $area->ID;
0 ignored issues
show
Bug Best Practice introduced by John Milmine
The property ParentID does not exist on DNADesign\Elemental\Models\ElementContent. Since you implemented __set, consider adding a @property annotation.
Loading history...
34
                $element->write();
35
            }
36
            $count += $pages->Count();
37
            echo 'Migrated ' . $pages->Count() . ' ' . $pageType . ' pages\' content<br>';
38
        }
39
        echo 'Finished migrating ' . $count . ' pages\' content<br>';
40
    }
41
}
42