Skip to content

Commit 696ecb5

Browse files
committed
connection add and grid view
1 parent 8717d12 commit 696ecb5

13 files changed

Lines changed: 385 additions & 8 deletions

files/lib/data/wsdb/record/connection/RecordConnection.class.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
namespace wcf\data\wsdb\record\connection;
44

5-
use wcf\data\DatabaseObject;
5+
use wcf\data\CollectionDatabaseObject;
6+
use wcf\data\wsdb\record\Record;
67

78
/**
89
* @property-read int $connectionID
910
* @property-read int $databaseID
1011
* @property-read int $recordID
1112
* @property-read int $referencedDatabaseID
1213
* @property-read int $referencedRecordID
14+
* @extends CollectionDatabaseObject<RecordConnectionCollection>
1315
*/
14-
final class RecordConnection extends DatabaseObject
16+
final class RecordConnection extends CollectionDatabaseObject
1517
{
1618
/**
1719
* @inheritDoc
@@ -22,4 +24,14 @@ final class RecordConnection extends DatabaseObject
2224
* @inheritDoc
2325
*/
2426
protected static $databaseTableIndexName = 'connectionID';
27+
28+
public function getRecord(): Record
29+
{
30+
return $this->getCollection()->getRecord($this);
31+
}
32+
33+
public function getReferencedRecord(): Record
34+
{
35+
return $this->getCollection()->getReferencedRecord($this);
36+
}
2537
}

files/lib/data/wsdb/record/connection/RecordConnectionAction.class.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,17 @@ final class RecordConnectionAction extends AbstractDatabaseObjectAction
1313
* @inheritDoc
1414
*/
1515
public $className = RecordConnectionEditor::class;
16+
17+
#[\Override]
18+
public function create(): RecordConnection
19+
{
20+
RecordConnectionEditor::create([
21+
'recordID' => $this->parameters['data']['referencedRecordID'],
22+
'databaseID' => $this->parameters['data']['referencedDatabaseID'],
23+
'referencedRecordID' => $this->parameters['data']['recordID'],
24+
'referencedDatabaseID' => $this->parameters['data']['databaseID'],
25+
]);
26+
27+
return parent::create();
28+
}
1629
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace wcf\data\wsdb\record\connection;
4+
5+
use wcf\data\DatabaseObjectCollection;
6+
use wcf\data\wsdb\record\Record;
7+
use wcf\system\wsdb\cache\runtime\RecordRuntimeCache;
8+
9+
/**
10+
* @extends DatabaseObjectCollection<RecordConnection>
11+
*/
12+
final class RecordConnectionCollection extends DatabaseObjectCollection
13+
{
14+
private bool $recordsLoaded = false;
15+
16+
public function getRecord(RecordConnection $connection): Record
17+
{
18+
$this->loadRecords();
19+
20+
return RecordRuntimeCache::getInstance()->getObject($connection->recordID);
21+
}
22+
23+
public function getReferencedRecord(RecordConnection $connection): Record
24+
{
25+
$this->loadRecords();
26+
27+
return RecordRuntimeCache::getInstance()->getObject($connection->referencedRecordID);
28+
}
29+
30+
private function loadRecords(): void
31+
{
32+
if ($this->recordsLoaded) {
33+
return;
34+
}
35+
$this->recordsLoaded = true;
36+
37+
$recordIDs = [];
38+
foreach ($this->getObjects() as $object) {
39+
$recordIDs[] = $object->recordID;
40+
$recordIDs[] = $object->referencedRecordID;
41+
}
42+
43+
if ($recordIDs !== []) {
44+
RecordRuntimeCache::getInstance()->cacheObjectIDs($recordIDs);
45+
}
46+
}
47+
}

files/lib/data/wsdb/record/connection/RecordConnectionList.class.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace wcf\data\wsdb\record\connection;
44

55
use wcf\data\DatabaseObjectList;
6+
use wcf\system\wsdb\cache\runtime\RecordConnectionRuntimeCache;
67

78
/**
89
* @extends DatabaseObjectList<RecordConnection>
@@ -13,4 +14,12 @@ final class RecordConnectionList extends DatabaseObjectList
1314
* @inheritDoc
1415
*/
1516
public $className = RecordConnection::class;
17+
18+
#[\Override]
19+
public function readObjects()
20+
{
21+
parent::readObjects();
22+
23+
RecordConnectionRuntimeCache::getInstance()->setObjects($this->getObjects());
24+
}
1625
}

files/lib/form/WsdbConnectionAddForm.class.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use wcf\data\wsdb\record\RecordList;
1111
use wcf\page\IWsdbPage;
1212
use wcf\page\TWsdbRecordPage;
13+
use wcf\page\WsdbConnectionListPage;
1314
use wcf\system\exception\PermissionDeniedException;
1415
use wcf\system\form\builder\container\FormContainer;
1516
use wcf\system\form\builder\data\processor\CustomFormDataProcessor;
@@ -21,6 +22,7 @@
2122
use wcf\system\form\builder\IFormChildNode;
2223
use wcf\system\form\builder\IFormDocument;
2324
use wcf\system\request\LinkHandler;
25+
use wcf\system\WCF;
2426
use wcf\system\wsdb\page\WsdbPageLocationManager;
2527

2628
/**
@@ -53,7 +55,7 @@ public function readParameters()
5355
$this->canViewRecords();
5456
$this->readRecord();
5557

56-
if (!$this->getRecord()->canEdit()) {
58+
if (!$this->getRecord()->canEdit() || !$this->getDatabase()->enableConnection) {
5759
throw new PermissionDeniedException();
5860
}
5961
}
@@ -79,7 +81,7 @@ protected function createForm(): void
7981
FormContainer::create('data')
8082
->appendChildren([
8183
SelectFormField::create('referencedDatabaseID')
82-
->label('Datenbank')
84+
->label('dev.hanashi.wsdb.connection.database')
8385
->options($this->getDatabaseOptions())
8486
->required(),
8587
...$this->getRecordSelects(),
@@ -128,6 +130,22 @@ public function readData()
128130
);
129131
}
130132

133+
#[\Override]
134+
public function assignVariables(): void
135+
{
136+
parent::assignVariables();
137+
138+
WCF::getTPL()->assign([
139+
'connectionListLink' => LinkHandler::getInstance()->getControllerLink(
140+
WsdbConnectionListPage::class,
141+
[
142+
'__database' => $this->getRecord()->getDatabase()->path,
143+
'object' => $this->getRecord(),
144+
]
145+
),
146+
]);
147+
}
148+
131149
/**
132150
* @return IFormChildNode[]
133151
*/
@@ -199,7 +217,11 @@ private function getDatabases(): array
199217
$databaseList->sqlOrderBy = 'name ASC';
200218
$databaseList->readObjects();
201219

202-
$this->databases = $databaseList->getObjects();
220+
$databases = \array_filter(
221+
$databaseList->getObjects(),
222+
static fn (Database $database): bool => $database->enableConnection
223+
);
224+
$this->databases = $databases;
203225
}
204226

205227
return $this->databases;
@@ -234,6 +256,9 @@ private function getRecordsGroupedByDatabases(): array
234256

235257
$groupedRecords = [];
236258
foreach ($recordList as $record) {
259+
if (!$record->canEdit()) {
260+
continue;
261+
}
237262
$groupedRecords[$record->databaseID][$record->recordID] = $record->getTitle();
238263
}
239264
$this->groupedRecords = $groupedRecords;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace wcf\page;
4+
5+
use wcf\system\exception\PermissionDeniedException;
6+
use wcf\system\wsdb\gridView\user\ConnectionGridView;
7+
use wcf\system\wsdb\page\WsdbPageLocationManager;
8+
9+
/**
10+
* @extends AbstractGridViewPage<ConnectionGridView>
11+
*/
12+
final class WsdbConnectionListPage extends AbstractGridViewPage implements IWsdbPage
13+
{
14+
use TWsdbRecordPage;
15+
16+
#[\Override]
17+
public function readParameters()
18+
{
19+
parent::readParameters();
20+
21+
$this->canViewRecords();
22+
23+
$this->readRecord();
24+
25+
if (!$this->getRecord()->canEdit() || !$this->getDatabase()->enableConnection) {
26+
throw new PermissionDeniedException();
27+
}
28+
}
29+
30+
#[\Override]
31+
protected function createGridView(): ConnectionGridView
32+
{
33+
return new ConnectionGridView($this->getRecord()->recordID);
34+
}
35+
36+
#[\Override]
37+
public function readData()
38+
{
39+
parent::readData();
40+
41+
WsdbPageLocationManager::setLocation(
42+
$this->getDatabase(),
43+
$this->getRecord()->getParentCategories(),
44+
$this->getRecord()->getCategory(),
45+
$this->getRecord()
46+
);
47+
}
48+
49+
#[\Override]
50+
protected function getBaseUrlParameters(): array
51+
{
52+
return [
53+
'__database' => $this->getDatabase()->path,
54+
'object' => $this->getRecord(),
55+
];
56+
}
57+
}

files/lib/system/event/listener/ConnectionWsdbRecordInteractionCollectingListener.class.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use wcf\data\wsdb\record\Record;
77
use wcf\event\wsdb\interaction\user\RecordInteractionCollecting;
88
use wcf\form\WsdbConnectionAddForm;
9+
use wcf\page\WsdbConnectionListPage;
910
use wcf\system\interaction\AbstractInteraction;
1011
use wcf\system\request\LinkHandler;
1112
use wcf\system\WCF;
@@ -16,23 +17,28 @@ final class ConnectionWsdbRecordInteractionCollectingListener
1617
public function __invoke(RecordInteractionCollecting $event): void
1718
{
1819
$event->provider->addInteractionBefore(
19-
new class('connection', static fn (Record $record) => $record->canEdit()) extends AbstractInteraction {
20+
new class('connection', static fn (Record $record) => $record->canEdit() && $record->getDatabase()->enableConnection) extends AbstractInteraction {
2021
#[\Override]
2122
public function render(DatabaseObject $object): string
2223
{
2324
\assert($object instanceof Record);
2425

2526
return \sprintf(
2627
'<a href="%s">%s</a>',
27-
StringUtil::encodeHTML($object->getEditFormLink()),
28+
StringUtil::encodeHTML(
29+
LinkHandler::getInstance()->getControllerLink(WsdbConnectionListPage::class, [
30+
'__database' => $object->getDatabase()->path,
31+
'object' => $object,
32+
])
33+
),
2834
WCF::getLanguage()->get('wsdb.record.connections')
2935
);
3036
}
3137
},
3238
'edit'
3339
);
3440
$event->provider->addInteractionBefore(
35-
new class('connectionAdd', static fn (Record $record) => $record->canEdit()) extends AbstractInteraction {
41+
new class('connectionAdd', static fn (Record $record) => $record->canEdit() && $record->getDatabase()->enableConnection) extends AbstractInteraction {
3642
#[\Override]
3743
public function render(DatabaseObject $object): string
3844
{
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace wcf\system\wsdb\cache\runtime;
4+
5+
use wcf\data\DatabaseObjectDecorator;
6+
use wcf\data\wsdb\record\connection\RecordConnection;
7+
use wcf\data\wsdb\record\connection\RecordConnectionList;
8+
use wcf\system\cache\runtime\AbstractRuntimeCache;
9+
10+
/**
11+
* @extends AbstractRuntimeCache<RecordConnection, RecordConnectionList>
12+
*/
13+
final class RecordConnectionRuntimeCache extends AbstractRuntimeCache
14+
{
15+
/**
16+
* @inheritDoc
17+
*/
18+
protected $listClassName = RecordConnectionList::class;
19+
20+
/**
21+
* @param array<int, RecordConnection|DatabaseObjectDecorator<RecordConnection>> $objects
22+
*/
23+
public function setObjects(array $objects): void
24+
{
25+
foreach ($objects as $object) {
26+
if ($object instanceof DatabaseObjectDecorator) {
27+
$object = $object->getDecoratedObject();
28+
}
29+
30+
$this->objects[$object->getObjectID()] = $object;
31+
$this->objectIDs[$object->getObjectID()] = $object->getObjectID();
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)