-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a16b8b
commit b8ff21d
Showing
6 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,4 @@ | |
|
||
// Avoid creating global variables | ||
call_user_func(function () { | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace SilverStripe\LinkField\Extensions; | ||
|
||
use SilverStripe\ORM\DataExtension; | ||
use SilverStripe\LinkField\Models\Link; | ||
use SilverStripe\LinkField\Models\LinkArea; | ||
|
||
class DataObjectExtension extends DataExtension | ||
{ | ||
public function onAfterWrite(): void | ||
{ | ||
// Using onAfterWrite instead of onBeforeWrite to ensure that $this->owner->ID is not zero when creating new objects | ||
parent::onAfterWrite(); | ||
foreach ($this->owner->hasOne() as $relation => $relationClassName) { | ||
$relationField = $relation . 'ID'; | ||
$relationID = $this->owner->$relationField; | ||
if (!$relationID) { | ||
continue; | ||
} | ||
if (!is_a($relationClassName, Link::class, true) && !is_a($relationClassName, LinkArea::class, true)) { | ||
continue; | ||
} | ||
// skip for the has_one:LinkArea relation on Link | ||
if (is_a($this->owner, Link::class) && $relation === 'LinkArea') { | ||
continue; | ||
} | ||
$relationObj = $relationClassName::get()->byID($relationID); | ||
if ($relationObj === null) { | ||
// could throw an Exception here, though not sure how if user would be able to fix it | ||
continue; | ||
} | ||
$doWrite = false; | ||
if ($relationObj->OwnerID !== $this->owner->ID) { | ||
$relationObj->OwnerID = $this->owner->ID; | ||
$doWrite = true; | ||
} | ||
if ($relationObj->OwnerClassName !== $this->owner->ClassName) { | ||
$relationObj->OwnerClassName = $this->owner->ClassName; | ||
$doWrite = true; | ||
} | ||
if ($doWrite) { | ||
$relationObj->write(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace SilverStripe\LinkField\Extensions; | ||
|
||
use SilverStripe\LinkField\Models\Link; | ||
use SilverStripe\ORM\DataExtension; | ||
use SilverStripe\ORM\DataObject; | ||
|
||
/** | ||
* This is only intended to be added to Link and LinkArea | ||
* Implemented as an extension rather than base class so it doesn't create an extra base table that needs to be joined | ||
*/ | ||
class LinkObjectExtension extends DataExtension | ||
{ | ||
private static array $db = [ | ||
'OwnerID' => 'Int', | ||
'OwnerClassName' => 'Varchar', | ||
]; | ||
|
||
public function canView($member = null) | ||
{ | ||
return $this->canCheck('canView', $member); | ||
} | ||
|
||
public function canCreate($member = null) | ||
{ | ||
return $this->canCheck('canCreate', $member); | ||
} | ||
|
||
public function canEdit($member = null) | ||
{ | ||
return $this->canCheck('canEdit', $member); | ||
} | ||
|
||
public function canDelete($member = null) | ||
{ | ||
return $this->canCheck('canDelete', $member); | ||
} | ||
|
||
private function canCheck(string $canMethod, $member) | ||
{ | ||
if (!$member) { | ||
$member = Security::getCurrentUser(); | ||
} | ||
$extended = $this->extendedCan($canMethod, $member); | ||
if ($extended !== null) { | ||
return $extended; | ||
} | ||
$owner = $this->getOwningDataObject(); | ||
if ($owner) { | ||
return $owner->$canMethod($member); | ||
} | ||
return parent::$canMethod($member); | ||
} | ||
|
||
private function getOwningDataObject(): ?DataObject | ||
{ | ||
// Thismethod is not called getOwner() because of existing Extension::getOwner() method | ||
// | ||
// If this is a Link, and LinkArea is set on it return that | ||
if (is_a($this->owner, Link::class, true)) { | ||
$linkArea = $this->owner->LinkArea(); | ||
if ($linkArea && $linkArea->exists()) { | ||
return $linkArea; | ||
} | ||
} | ||
// Otherwise look for the ownerID + ownerClassName | ||
// These are set in DataObjectExtension::onAfterWrite() | ||
$ownerID = $this->owner->OwnerID; | ||
$ownerClassName = $this->owner->OwnerClassName; | ||
if ($ownerID === 0 || $ownerClassName === '') { | ||
return null; | ||
} | ||
return $ownerClassName::get()->byID($ownerID); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace SilverStripe\LinkField\Models; | ||
|
||
use SilverStripe\LinkField\Models\Link; | ||
use SilverStripe\LinkField\Extensions\LinkObjectExtension; | ||
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\Versioned\Versioned; | ||
|
||
class LinkArea extends DataObject | ||
{ | ||
private static $table_name = 'LinkField_LinkArea'; | ||
|
||
private static array $has_many = [ | ||
'Links' => Link::class, | ||
]; | ||
|
||
private static array $owns = [ | ||
'Links', | ||
]; | ||
|
||
private static array $cascade_deletes = [ | ||
'Links', | ||
]; | ||
|
||
private static array $cascade_duplicates = [ | ||
'Links', | ||
]; | ||
|
||
private static array $extensions = [ | ||
Versioned::class, | ||
LinkObjectExtension::class, | ||
]; | ||
} |