/
add_container.php
85 lines (80 loc) · 3.13 KB
/
add_container.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
<?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\Block\BlockType\BlockType;
use Concrete\Core\Entity\Page\Container;
use Concrete\Core\Error\UserMessageException;
use Concrete\Core\Http\ResponseFactoryInterface;
use Concrete\Core\Page\Stack\Stack;
use Concrete\Core\Permission\Checker;
use Doctrine\ORM\EntityManager;
class AddContainer extends Page
{
public function addContainer()
{
$em = $this->app->make(EntityManager::class);
$token = $this->app->make('token');
if (!$token->validate()){
throw new UserMessageException($token->getErrorMessage());
}
$numberValidator = $this->app->make('helper/validation/numbers');
$containerID = $this->request->request->get('containerID', $this->request->query->get('containerID'));
$container = $numberValidator->integer($containerID, 1) ? $em->find(Container::class, $containerID) : null;
if (!$container) {
throw new UserMessageException(t('Invalid container.'));
}
$arHandle = $this->request->query->get('arHandle');
$a = Area::get($this->page, $arHandle);
if (!$a) {
throw new UserMessageException(t('Unable to find the requested area'));
}
if ($a->isGlobalArea()) {
$cx = Stack::getByName($arHandle);
if (!$cx) {
throw new UserMessageException(t('Unable to find the requested stack'));
}
$ax = Area::get($this->page, STACKS_AREA_NAME);
} else {
$cx = $this->page;
$ax = $a;
}
/*
$ap = new Checker($ax);
if (!$ap->canAddStackToArea($stack)) {
throw new UserMessageException(t('The stack contains invalid block types.'));
}
*/
$btx = BlockType::getByHandle(BLOCK_HANDLE_CONTAINER_PROXY);
$nvc = $cx->getVersionToModify();
if ($a->isGlobalArea()) {
$xvc = $this->page->getVersionToModify(); // we need to create a new version of THIS page as well.
$xvc->relateVersionEdits($nvc);
}
$nb = $nvc->addBlock($btx, $ax, ['containerID' => $container->getContainerID()]);
$dragAreaBlockID = $this->request->request->get('dragAreaBlockID', $this->request->query->get('dragAreaBlockID'));
if ($numberValidator->integer($dragAreaBlockID, 1)) {
$afterBlock = Block::getByID($dragAreaBlockID) ?: null;
} else {
$afterBlock = null;
}
$nb->moveBlockToDisplayOrderPosition($afterBlock);
return $this->app->make(ResponseFactoryInterface::class)->json([
'aID' => $a->getAreaID(),
'arHandle' => $a->getAreaHandle(),
'cID' => $this->page->getCollectionID(),
'bID' => $nb->getBlockID(),
'error' => false,
]);
}
/**
* {@inheritdoc}
*
* @see \Concrete\Controller\Backend\UserInterface::canAccess()
*/
protected function canAccess()
{
return $this->permissions->canEditPageContents();
}
}