Skip to content

Commit

Permalink
feat(copy): added check field relations (which entry type is allowed …
Browse files Browse the repository at this point in the history
…where)
  • Loading branch information
vandres committed Apr 30, 2024
1 parent dc1b7b1 commit e55041e
Show file tree
Hide file tree
Showing 5 changed files with 266 additions and 144 deletions.
6 changes: 5 additions & 1 deletion src/MatrixExtended.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use craft\base\Model;
use craft\base\Plugin;
use vandres\matrixextended\models\Settings;
use vandres\matrixextended\services\MatrixService;
use vandres\matrixextended\web\assets\cp\AssetBundle;

/**
Expand All @@ -18,7 +19,7 @@
*/
class MatrixExtended extends Plugin
{
public string $schemaVersion = '1.0.0';
public string $schemaVersion = '1.2.0';

public bool $hasCpSettings = true;

Expand Down Expand Up @@ -47,6 +48,9 @@ protected function settingsHtml(): ?string
private function setUp()
{
Craft::$app->onInit(function () {
$this->setComponents([
'service' => MatrixService::class,
]);
});
}

Expand Down
15 changes: 10 additions & 5 deletions src/controllers/MatrixExtendedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,21 @@ public function actionCopyEntry(): Response
throw new ForbiddenHttpException('User not authorized to duplicate this element.');
}

Craft::$app->getSession()->set('matrixExtendedReference', [
$entryReference = [
'entryId' => $entryId,
'fieldId' => $fieldId,
'entryTypeId' => $entryTypeId,
'ownerId' => $ownerId,
'ownerElementType' => $ownerElementType,
'siteId' => $siteId,
'namespace' => $namespace,
]);
];
MatrixExtended::getInstance()->service->setReference($entryReference);

$view = $this->getView();

return $this->asJson([
'blockHtml' => "",
'entryReference' => $entryReference,
'headHtml' => $view->getHeadHtml(),
'bodyHtml' => $view->getBodyHtml(),
]);
Expand All @@ -168,7 +169,6 @@ public function actionCopyEntry(): Response
* Get the latest entry reference from the user session and clones that entry at the given position.
*
* @return Response
* @todo check entry restriction (is that entry allowed to be posted into that owner)
*/
public function actionPasteEntry(): Response
{
Expand All @@ -177,7 +177,7 @@ public function actionPasteEntry(): Response
}

// check source
$entryReference = Craft::$app->getSession()->get('matrixExtendedReference');
$entryReference = MatrixExtended::getInstance()->service->getReference();
if (empty($entryReference)) {
throw new BadRequestHttpException("There is nothing to paste");
}
Expand Down Expand Up @@ -222,6 +222,11 @@ public function actionPasteEntry(): Response
throw new ForbiddenHttpException('User not authorized to duplicate this element.');
}

$childParent = MatrixExtended::getInstance()->service->getChildParentRelations() ?? [];
if (!in_array($fieldId, $childParent[$entry->typeId])) {
throw new BadRequestHttpException('That entry type cannot be pasted in that element.');
}

$duplicatedEntry = $elementsService->duplicateElement($entry, [], true, true);

/** @var EntryQuery|ElementCollection $value */
Expand Down
46 changes: 46 additions & 0 deletions src/services/MatrixService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace vandres\matrixextended\services;

use Craft;
use vandres\matrixextended\MatrixExtended;

class MatrixService
{
/**
* Returns a list, which child can have which parent.
*
* e.g.
* ```
* [
* 35 => [
* 18, 4, 51
* ],
* ...
* ]
* ```
*
* @return array
*/
public function getChildParentRelations(): array
{
$relations = [];
$entryTypes = Craft::$app->getEntries()->getAllEntryTypes();

foreach ($entryTypes as $entryType) {
$relations[$entryType->id] = array_map(fn($usage) => $usage->id, $entryType->findUsages());
}

return $relations;
}

public function setReference($reference): void
{
Craft::$app->getSession()->set('matrixExtendedReference' . MatrixExtended::getInstance()->schemaVersion, $reference);
}

public function getReference(): ?array
{
return Craft::$app->getSession()->get('matrixExtendedReference' . MatrixExtended::getInstance()->schemaVersion) ?: null;
}
}
8 changes: 7 additions & 1 deletion src/web/assets/cp/AssetBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@ public function registerAssetFiles($view): void
'Paste',
'Entry reference copied',
'There was an error copying the entry reference',
'There was an error duplicating the entry',
'There was an error pasting the entry',
]);
}

$data = ['settings' => MatrixExtended::getInstance()->getSettings()];
$data = [
'settings' => MatrixExtended::getInstance()->getSettings(),
'childParent' => MatrixExtended::getInstance()->service->getChildParentRelations(),
'entryReference' => MatrixExtended::getInstance()->service->getReference(),
];
$config = Json::encode($data);

$js = <<<JS
Expand Down
Loading

0 comments on commit e55041e

Please sign in to comment.