/
arrange_blocks.php
151 lines (136 loc) · 6.74 KB
/
arrange_blocks.php
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace Concrete\Controller\Backend\Page;
use Concrete\Controller\Backend\UserInterface\Page;
use Concrete\Core\Area\Area;
use Concrete\Core\Block\Block;
use Concrete\Core\Http\ResponseFactoryInterface;
use Concrete\Core\Page\EditResponse as PageEditResponse;
use Concrete\Core\Page\Stack\Stack;
use Concrete\Core\Permission\Checker;
class ArrangeBlocks extends Page
{
public function canAccess()
{
return $this->permissions->canEditPageContents();
}
public function arrange()
{
$pc = new PageEditResponse();
$pc->setPage($this->page);
$this->performArrangement($pc);
return $this->app->make(ResponseFactoryInterface::class)->json($pc);
}
/**
* @param \Concrete\Core\Page\EditResponse $pc
*/
protected function performArrangement(PageEditResponse $pc)
{
$e = $pc->getError();
$post = $this->request->request;
$nvc = $this->page->getVersionToModify();
$sourceAreaID = (int) $post->get('sourceArea');
$sourceAreaHandle = $sourceAreaID === 0 ? null : Area::getAreaHandleFromID($sourceAreaID);
$sourceArea = (string) $sourceAreaHandle === '' ? null : Area::get($nvc, $sourceAreaHandle);
if ($sourceArea === null) {
$e->add(t('Unable to find the source area.'));
}
$destinationAreaID = (int) $post->get('area');
if ($destinationAreaID === $sourceAreaID) {
$destinationAreaHandle = $sourceAreaHandle;
$destinationArea = $sourceArea;
} else {
$destinationAreaHandle = $destinationAreaID === 0 ? null : Area::getAreaHandleFromID($destinationAreaID);
$destinationArea = (string) $destinationAreaHandle === '' ? null : Area::get($nvc, $destinationAreaHandle);
if ($destinationArea === null) {
$e->add(t('Unable to find the destination area.'));
}
}
$movingBlockID = (int) $post->get('block');
if ($movingBlockID === 0) {
$e->add(t('Unable to find the block to be moved.'));
}
$sortedBlockIDs = $post->get('blocks', []);
if (is_array($sortedBlockIDs)) {
$sortedBlockIDs = array_values(array_filter(array_map('intval', $sortedBlockIDs)));
} else {
$sortedBlockIDs = [];
}
if (!in_array($movingBlockID, $sortedBlockIDs, true)) {
$e->add(t('Unable to find the block to be moved.'));
}
if ($e->has()) {
return;
}
if ($this->app->make('config')->get('concrete.permissions.model') == 'advanced') {
// first, we check to see if we have permissions to edit the area contents for the source area.
$ap = new Checker($sourceArea);
if (!$ap->canEditAreaContents()) {
$e->add(t('You may not arrange the contents of area %s.', $sourceAreaHandle));
}
// now we get further in. We check to see if we're dealing with both a source AND a destination area.
// if so, we check the area permissions for the destination area.
if ($sourceAreaID !== $destinationAreaID) {
$destAP = new Checker($destinationArea);
if (!$destAP->canEditAreaContents()) {
$e->add(t('You may not arrange the contents of area %s.', $destinationAreaHandle));
}
// we're not done yet. Now we have to check to see whether this user has permission to add
// a block of this type to the destination area.
if ($sourceArea->isGlobalArea()) {
$sourceStack = Stack::getByName($sourceAreaHandle);
$block = Block::getByID($movingBlockID, $sourceStack, STACKS_AREA_NAME);
} else {
$block = Block::getByID($movingBlockID, $nvc, $sourceAreaHandle);
}
if (!$block) {
$e->add(t('Unable to find the block to be moved.'));
} elseif (!$destAP->canAddBlock($block)) {
$e->add(t('You may not add %s to area %s.', t($block->getBlockTypeObject()->getBlockTypeName()), $destinationAreaHandle));
}
}
if ($e->has()) {
return;
}
// now, if we get down here we perform the arrangement
// it will be set to true if we're in simple permissions mode, or if we've passed all the checks
}
if ($destinationArea->isGlobalArea()) {
$destinationStack = Stack::getByName($destinationAreaHandle);
$destinationStackToModify = $destinationStack->getVersionToModify();
$actualDestinationArea = Area::get($destinationStackToModify, STACKS_AREA_NAME);
$actualDestinationAreaID = $actualDestinationArea->getAreaID();
if ($sourceAreaID !== $destinationAreaID) {
$nvc->relateVersionEdits($destinationStackToModify);
// If the source area is global, we need to get the block from there rather than from the view controller
if ($sourceArea->isGlobalArea()) {
$sourceStack = Stack::getByName($sourceAreaHandle);
$sourceStackToModify = $sourceStack->getVersionToModify();
$nvc->relateVersionEdits($sourceStackToModify);
$block = Block::getByID($movingBlockID, $sourceStackToModify, Area::get($sourceStackToModify, STACKS_AREA_NAME));
} else {
$block = Block::getByID($movingBlockID, $nvc, $sourceArea);
}
if (!$block) {
$e->add(t('Unable to find the block to be moved.'));
return;
}
$block->move($destinationStackToModify, $actualDestinationArea);
}
$nvc->relateVersionEdits($destinationStackToModify);
$destinationStackToModify->processArrangement($actualDestinationAreaID, $movingBlockID, $sortedBlockIDs);
} else {
if ($sourceAreaID !== $destinationAreaID && $sourceArea->isGlobalArea()) {
$sourceStack = Stack::getByName($sourceAreaHandle);
$sourceStackToModify = $sourceStack->getVersionToModify();
$block = Block::getByID($movingBlockID, $sourceStackToModify, Area::get($sourceStackToModify, STACKS_AREA_NAME));
if (!$block) {
$e->add(t('Unable to find the block to be moved.'));
return;
}
$nvc->relateVersionEdits($sourceStackToModify);
$block->move($nvc, Area::get($nvc, STACKS_AREA_NAME));
}
$nvc->processArrangement($destinationAreaID, $movingBlockID, $sortedBlockIDs);
}
}
}