/
reorder.php
103 lines (92 loc) · 3.74 KB
/
reorder.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
<?php
namespace Concrete\Controller\Dialog\Express\Association;
use Concrete\Controller\Backend\UserInterface as BackendInterfaceController;
use Concrete\Controller\Element\Dashboard\Express\Entries\Header;
use Concrete\Core\Application\EditResponse;
use Concrete\Core\Express\Form\Control\SaveHandler\ManySaveHandlerInterface;
class Reorder extends BackendInterfaceController
{
protected $viewPath = '/dialogs/express/association/reorder';
protected function canAccess()
{
$entry = $this->getEntry();
if ($entry) {
$ep = new \Permissions($entry);
return $ep->canViewExpressEntries();
}
return false;
}
protected function getEntry()
{
if ($this->request->request->has('entryID')) {
$entryID = $this->request->request->get('entryID');
} else {
$entryID = $this->request->query->get('entryID');
}
$em = \Database::connection()->getEntityManager();
return $em->getRepository('Concrete\Core\Entity\Express\Entry')
->findOneById($entryID);
}
protected function getControl()
{
if ($this->request->request->has('controlID')) {
$controlID = $this->request->request->get('controlID');
} else {
$controlID = $this->request->query->get('controlID');
}
$em = \Database::connection()->getEntityManager();
return $em->getRepository('Concrete\Core\Entity\Express\Control\AssociationControl')
->findOneById($controlID);
}
public function view()
{
$entry = $this->getEntry();
$control = $this->getControl();
if ($association = $control->getAssociation()) {
$related = $entry->getAssociations();
foreach ($related as $relatedAssociation) {
if ($relatedAssociation->getAssociation()->getID() == $association->getID()) {
$this->set('selectedEntries', $relatedAssociation->getSelectedEntries());
$this->set('control', $control);
$this->set('entry', $entry);
$this->set('formatter', $association->getFormatter());
}
}
}
}
public function submit()
{
$em = \Database::connection()->getEntityManager();
$selectedEntry = $this->getEntry();
$control = $this->getControl();
if ($association = $control->getAssociation()) {
$i = 0;
$handler = $association->getSaveHandler();
/**
* @var $handler ManySaveHandlerInterface
*/
$supportsCustomDisplayOrder = false;
if ($association->getTargetEntity()->supportsCustomDisplayOrder()) {
$supportsCustomDisplayOrder = true;
}
$associatedEntries = $handler->getAssociatedEntriesFromRequest($control, $this->request);
foreach($associatedEntries as $entry) {
if ($supportsCustomDisplayOrder) {
$entry->setEntryDisplayOrder($i);
$em->persist($entry);
} else {
$entryAssociation = $selectedEntry->getAssociation($association);
$associationEntry = $entryAssociation->getAssociationEntry($entry);
$associationEntry->setDisplayOrder($i);
$em->persist($associationEntry);
}
$i++;
}
$em->flush();
$this->flash('success', t('Display order saved successfully.'));
$response = new EditResponse();
$response->setRedirectURL(\URL::to('/dashboard/express/entries/', 'view_entry', $selectedEntry->getId()));
$response->outputJSON();
}
}
}