Skip to content

Commit

Permalink
Fix comparing reviews not working on specific multi-site setups
Browse files Browse the repository at this point in the history
  • Loading branch information
engram-design committed Nov 12, 2023
1 parent e63c803 commit 4e9d34d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Workflow extends Plugin

public bool $hasCpSection = true;
public bool $hasCpSettings = true;
public string $schemaVersion = '2.4.0';
public string $schemaVersion = '2.5.0';
public string $minVersionRequired = '1.7.0';


Expand Down
3 changes: 3 additions & 0 deletions src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function createTables(): void
'id' => $this->primaryKey(),
'submissionId' => $this->integer(),
'elementId' => $this->integer(),
'elementSiteId' => $this->integer(),
'draftId' => $this->integer(),
'userId' => $this->integer(),
'role' => $this->enum('role', ['editor', 'reviewer', 'publisher']),
Expand All @@ -65,6 +66,7 @@ public function createIndexes(): void
$this->createIndex(null, '{{%workflow_submissions}}', 'ownerSiteId', false);

$this->createIndex(null, '{{%workflow_reviews}}', 'elementId', false);
$this->createIndex(null, '{{%workflow_reviews}}', 'elementSiteId', false);
$this->createIndex(null, '{{%workflow_reviews}}', 'draftId', false);
$this->createIndex(null, '{{%workflow_reviews}}', 'userId', false);
}
Expand All @@ -77,6 +79,7 @@ public function addForeignKeys(): void

$this->addForeignKey(null, '{{%workflow_reviews}}', 'submissionId', '{{%workflow_submissions}}', 'id', 'CASCADE', null);
$this->addForeignKey(null, '{{%workflow_reviews}}', 'elementId', '{{%elements}}', 'id', 'SET NULL', null);
$this->addForeignKey(null, '{{%workflow_reviews}}', 'elementSiteId', '{{%sites}}', 'id', 'CASCADE', null);
$this->addForeignKey(null, '{{%workflow_reviews}}', 'draftId', '{{%drafts}}', 'id', 'SET NULL', null);
$this->addForeignKey(null, '{{%workflow_reviews}}', 'userId', '{{%users}}', 'id', 'CASCADE', null);
}
Expand Down
56 changes: 56 additions & 0 deletions src/migrations/m231112_000000_reviews_siteid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
namespace verbb\workflow\migrations;

use craft\db\Migration;
use craft\db\Query;

use Throwable;

class m231112_000000_reviews_siteid extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
if (!$this->db->columnExists('{{%workflow_reviews}}', 'elementSiteId')) {
$this->addColumn('{{%workflow_reviews}}', 'elementSiteId', $this->integer()->after('elementId'));
}

$this->createIndex(null, '{{%workflow_reviews}}', 'elementSiteId', false);
$this->addForeignKey(null, '{{%workflow_reviews}}', 'elementSiteId', '{{%sites}}', 'id', 'CASCADE', null);

// Populate the site info from submissions
$submissions = (new Query())
->select(['id', 'ownerSiteId'])
->from(['{{%workflow_submissions}}'])
->indexBy('id')
->all();

$reviews = (new Query())
->select(['*'])
->from(['{{%workflow_reviews}}'])
->all();

foreach ($reviews as $review) {
$submissionId = $review['submissionId'] ?? null;

if ($submissionId) {
$this->update('{{%workflow_reviews}}', [
'elementSiteId' => $submissions[$submissionId]['ownerSiteId'] ?? null,
], ['id' => $review['id']]);
}
}

return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
echo "m231112_000000_reviews_siteid cannot be reverted.\n";
return false;
}
}
5 changes: 3 additions & 2 deletions src/models/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static function roles(): array
public ?int $id = null;
public ?int $submissionId = null;
public ?int $elementId = null;
public ?int $elementSiteId = null;
public ?int $draftId = null;
public ?int $userId = null;
public ?string $role = null;
Expand Down Expand Up @@ -105,7 +106,7 @@ public function getSubmission(): ?Submission
}

if ($this->submissionId !== null) {
return $this->_submission = Workflow::$plugin->getSubmissions()->getSubmissionById($this->submissionId);
return $this->_submission = Workflow::$plugin->getSubmissions()->getSubmissionById($this->submissionId, $this->elementSiteId);
}

return null;
Expand All @@ -118,7 +119,7 @@ public function getElement(): ?ElementInterface
}

if ($this->elementId !== null) {
return $this->_element = Craft::$app->getElements()->getElementById($this->elementId);
return $this->_element = Craft::$app->getElements()->getElementById($this->elementId, null, $this->elementSiteId);
}

return null;
Expand Down
2 changes: 2 additions & 0 deletions src/services/Reviews.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function saveReview(Review $review, bool $runValidation = true): bool
$reviewRecord->id = $review->id;
$reviewRecord->submissionId = $review->submissionId;
$reviewRecord->elementId = $review->elementId;
$reviewRecord->elementSiteId = $review->elementSiteId;
$reviewRecord->draftId = $review->draftId;
$reviewRecord->userId = $review->userId;
$reviewRecord->role = $review->role;
Expand Down Expand Up @@ -195,6 +196,7 @@ private function _createReviewQuery(): Query
'id',
'submissionId',
'elementId',
'elementSiteId',
'draftId',
'userId',
'role',
Expand Down
1 change: 1 addition & 0 deletions src/services/Submissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ private function _setReviewFromPost(Submission $submission, ElementInterface $en
$review = new Review();
$review->submissionId = $submission->id;
$review->elementId = $entry->id;
$review->elementSiteId = $entry->siteId;
$review->draftId = $entry->draftId;
$review->userId = $currentUser->id;
$review->setNotes((string)$request->getParam('workflowNotes'));
Expand Down

0 comments on commit 4e9d34d

Please sign in to comment.