1 | <?php |
||
2 | |||
3 | namespace SilverStripe\Subsites\Tasks; |
||
4 | |||
5 | use InvalidArgumentException; |
||
6 | use SilverStripe\CMS\Model\SiteTree; |
||
7 | use SilverStripe\Dev\BuildTask; |
||
8 | use SilverStripe\ORM\DataObject; |
||
9 | use SilverStripe\Subsites\Model\Subsite; |
||
10 | use SilverStripe\Subsites\Pages\SubsitesVirtualPage; |
||
11 | use SilverStripe\Versioned\Versioned; |
||
12 | |||
13 | /** |
||
14 | * Handy alternative to copying pages when creating a subsite through the UI. |
||
15 | * |
||
16 | * Can be used to batch-add new pages after subsite creation, or simply to |
||
17 | * process a large site outside of the UI. |
||
18 | * |
||
19 | * Example: sake dev/tasks/SubsiteCopyPagesTask from=<subsite-source> to=<subsite-target> |
||
20 | * |
||
21 | * @package subsites |
||
22 | */ |
||
23 | class SubsiteCopyPagesTask extends BuildTask |
||
24 | { |
||
25 | protected $title = 'Copy pages to different subsite'; |
||
26 | protected $description = ''; |
||
27 | |||
28 | private static $segment = 'SubsiteCopyPagesTask'; |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
29 | |||
30 | public function run($request) |
||
31 | { |
||
32 | $subsiteFromId = $request->getVar('from'); |
||
33 | if (!is_numeric($subsiteFromId)) { |
||
34 | throw new InvalidArgumentException('Missing "from" parameter'); |
||
35 | } |
||
36 | $subsiteFrom = DataObject::get_by_id(Subsite::class, $subsiteFromId); |
||
37 | if (!$subsiteFrom) { |
||
0 ignored issues
–
show
|
|||
38 | throw new InvalidArgumentException('Subsite not found'); |
||
39 | } |
||
40 | |||
41 | $subsiteToId = $request->getVar('to'); |
||
42 | if (!is_numeric($subsiteToId)) { |
||
43 | throw new InvalidArgumentException('Missing "to" parameter'); |
||
44 | } |
||
45 | $subsiteTo = DataObject::get_by_id(Subsite::class, $subsiteToId); |
||
46 | if (!$subsiteTo) { |
||
0 ignored issues
–
show
|
|||
47 | throw new InvalidArgumentException('Subsite not found'); |
||
48 | } |
||
49 | |||
50 | $useVirtualPages = (bool)$request->getVar('virtual'); |
||
51 | |||
52 | Subsite::changeSubsite($subsiteFrom); |
||
53 | |||
54 | // Copy data from this template to the given subsite. Does this using an iterative depth-first search. |
||
55 | // This will make sure that the new parents on the new subsite are correct, and there are no funny |
||
56 | // issues with having to check whether or not the new parents have been added to the site tree |
||
57 | // when a page, etc, is duplicated |
||
58 | $stack = [[0, 0]]; |
||
59 | while (count($stack) > 0) { |
||
60 | list($sourceParentID, $destParentID) = array_pop($stack); |
||
61 | |||
62 | $children = Versioned::get_by_stage(SiteTree::class, 'Live', "\"ParentID\" = $sourceParentID", ''); |
||
63 | |||
64 | if ($children) { |
||
65 | foreach ($children as $child) { |
||
66 | if ($useVirtualPages) { |
||
67 | $childClone = new SubsitesVirtualPage(); |
||
68 | $childClone->writeToStage('Stage'); |
||
69 | $childClone->CopyContentFromID = $child->ID; |
||
70 | $childClone->SubsiteID = $subsiteTo->ID; |
||
71 | } else { |
||
72 | $childClone = $child->duplicateToSubsite($subsiteTo->ID, true); |
||
73 | } |
||
74 | |||
75 | $childClone->ParentID = $destParentID; |
||
76 | $childClone->writeToStage('Stage'); |
||
77 | $childClone->copyVersionToStage('Stage', 'Live'); |
||
78 | array_push($stack, [$child->ID, $childClone->ID]); |
||
0 ignored issues
–
show
The property
ID does not exist on SilverStripe\Subsites\Pages\SubsitesVirtualPage . Since you implemented __get , consider adding a @property annotation.
Loading history...
|
|||
79 | |||
80 | $this->log(sprintf('Copied "%s" (#%d, %s)', $child->Title, $child->ID, $child->Link())); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | unset($children); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | public function log($msg) |
||
89 | { |
||
90 | echo $msg . "\n"; |
||
91 | } |
||
92 | } |
||
93 |