2016-03-30 23:37:12 +02:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
namespace SilverStripe\ORM\Versioning;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Admin\CMSPreviewable;
|
|
|
|
use SilverStripe\Assets\Thumbnail;
|
|
|
|
use SilverStripe\Control\Controller;
|
2016-09-30 03:10:23 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\ORM\ManyManyList;
|
|
|
|
use SilverStripe\ORM\SS_List;
|
2016-09-23 01:05:33 +02:00
|
|
|
use SilverStripe\ORM\UnexpectedDataException;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Permission;
|
2016-08-19 00:51:35 +02:00
|
|
|
use BadMethodCallException;
|
|
|
|
use Exception;
|
2016-04-12 00:24:16 +02:00
|
|
|
|
2016-03-30 23:37:12 +02:00
|
|
|
/**
|
|
|
|
* A single line in a changeset
|
|
|
|
*
|
|
|
|
* @property string $Added
|
2016-04-11 22:39:55 +02:00
|
|
|
* @property string $ObjectClass The _base_ data class for the referenced DataObject
|
|
|
|
* @property int $ObjectID The numeric ID for the referenced object
|
2016-03-30 23:37:12 +02:00
|
|
|
* @method ManyManyList ReferencedBy() List of explicit items that require this change
|
|
|
|
* @method ManyManyList References() List of implicit items required by this change
|
|
|
|
* @method ChangeSet ChangeSet()
|
|
|
|
*/
|
2016-04-12 00:24:16 +02:00
|
|
|
class ChangeSetItem extends DataObject implements Thumbnail {
|
2016-03-30 23:37:12 +02:00
|
|
|
|
|
|
|
const EXPLICITLY = 'explicitly';
|
|
|
|
|
|
|
|
const IMPLICITLY = 'implicitly';
|
|
|
|
|
|
|
|
/** Represents an object deleted */
|
|
|
|
const CHANGE_DELETED = 'deleted';
|
|
|
|
|
|
|
|
/** Represents an object which was modified */
|
|
|
|
const CHANGE_MODIFIED = 'modified';
|
|
|
|
|
|
|
|
/** Represents an object added */
|
|
|
|
const CHANGE_CREATED = 'created';
|
|
|
|
|
|
|
|
/** Represents an object which hasn't been changed directly, but owns a modified many_many relationship. */
|
|
|
|
//const CHANGE_MANYMANY = 'manymany';
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
private static $table_name = 'ChangeSetItem';
|
|
|
|
|
2016-03-30 23:37:12 +02:00
|
|
|
/**
|
|
|
|
* Represents that an object has not yet been changed, but
|
|
|
|
* should be included in this changeset as soon as any changes exist
|
|
|
|
*/
|
|
|
|
const CHANGE_NONE = 'none';
|
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'VersionBefore' => 'Int',
|
|
|
|
'VersionAfter' => 'Int',
|
|
|
|
'Added' => "Enum('explicitly, implicitly', 'implicitly')"
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $has_one = array(
|
2016-06-15 06:03:16 +02:00
|
|
|
'ChangeSet' => 'SilverStripe\ORM\Versioning\ChangeSet',
|
|
|
|
'Object' => 'SilverStripe\ORM\DataObject',
|
2016-03-30 23:37:12 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
private static $many_many = array(
|
2016-06-15 06:03:16 +02:00
|
|
|
'ReferencedBy' => 'SilverStripe\ORM\Versioning\ChangeSetItem'
|
2016-03-30 23:37:12 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
private static $belongs_many_many = array(
|
|
|
|
'References' => 'ChangeSetItem.ReferencedBy'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $indexes = array(
|
|
|
|
'ObjectUniquePerChangeSet' => array(
|
|
|
|
'type' => 'unique',
|
|
|
|
'value' => '"ObjectID", "ObjectClass", "ChangeSetID"'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2016-04-11 22:39:55 +02:00
|
|
|
public function onBeforeWrite() {
|
|
|
|
// Make sure ObjectClass refers to the base data class in the case of old or wrong code
|
2016-05-25 07:09:29 +02:00
|
|
|
$this->ObjectClass = $this->getSchema()->baseDataClass($this->ObjectClass);
|
2016-04-11 22:39:55 +02:00
|
|
|
parent::onBeforeWrite();
|
|
|
|
}
|
|
|
|
|
2016-04-12 00:24:16 +02:00
|
|
|
public function getTitle() {
|
|
|
|
// Get title of modified object
|
|
|
|
$object = $this->getObjectLatestVersion();
|
|
|
|
if($object) {
|
|
|
|
return $object->getTitle();
|
|
|
|
}
|
|
|
|
return $this->i18n_singular_name() . ' #' . $this->ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a thumbnail for this object
|
|
|
|
*
|
|
|
|
* @param int $width Preferred width of the thumbnail
|
|
|
|
* @param int $height Preferred height of the thumbnail
|
|
|
|
* @return string URL to the thumbnail, if available
|
|
|
|
*/
|
|
|
|
public function ThumbnailURL($width, $height) {
|
|
|
|
$object = $this->getObjectLatestVersion();
|
|
|
|
if($object instanceof Thumbnail) {
|
|
|
|
return $object->ThumbnailURL($width, $height);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-03-30 23:37:12 +02:00
|
|
|
/**
|
|
|
|
* Get the type of change: none, created, deleted, modified, manymany
|
|
|
|
* @return string
|
2016-09-30 03:10:23 +02:00
|
|
|
* @throws UnexpectedDataException
|
2016-03-30 23:37:12 +02:00
|
|
|
*/
|
|
|
|
public function getChangeType() {
|
2016-09-23 01:05:33 +02:00
|
|
|
if(!class_exists($this->ObjectClass)) {
|
|
|
|
throw new UnexpectedDataException("Invalid Class '{$this->ObjectClass}' in ChangeSetItem #{$this->ID}");
|
|
|
|
}
|
|
|
|
|
2016-03-30 23:37:12 +02:00
|
|
|
// Get change versions
|
|
|
|
if($this->VersionBefore || $this->VersionAfter) {
|
|
|
|
$draftVersion = $this->VersionAfter; // After publishing draft was written to stage
|
|
|
|
$liveVersion = $this->VersionBefore; // The live version before the publish
|
|
|
|
} else {
|
|
|
|
$draftVersion = Versioned::get_versionnumber_by_stage(
|
|
|
|
$this->ObjectClass, Versioned::DRAFT, $this->ObjectID, false
|
|
|
|
);
|
|
|
|
$liveVersion = Versioned::get_versionnumber_by_stage(
|
|
|
|
$this->ObjectClass, Versioned::LIVE, $this->ObjectID, false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version comparisons
|
|
|
|
if ($draftVersion == $liveVersion) {
|
|
|
|
return self::CHANGE_NONE;
|
|
|
|
} elseif (!$liveVersion) {
|
|
|
|
return self::CHANGE_CREATED;
|
|
|
|
} elseif (!$draftVersion) {
|
|
|
|
return self::CHANGE_DELETED;
|
|
|
|
} else {
|
|
|
|
return self::CHANGE_MODIFIED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find version of this object in the given stage
|
|
|
|
*
|
|
|
|
* @param string $stage
|
2016-09-30 03:10:23 +02:00
|
|
|
* @return DataObject|Versioned
|
|
|
|
* @throws UnexpectedDataException
|
2016-03-30 23:37:12 +02:00
|
|
|
*/
|
2016-04-12 00:24:16 +02:00
|
|
|
protected function getObjectInStage($stage) {
|
2016-09-23 01:05:33 +02:00
|
|
|
if(!class_exists($this->ObjectClass)) {
|
|
|
|
throw new UnexpectedDataException("Invalid Class '{$this->ObjectClass}' in ChangeSetItem #{$this->ID}");
|
|
|
|
}
|
|
|
|
|
2016-03-30 23:37:12 +02:00
|
|
|
return Versioned::get_by_stage($this->ObjectClass, $stage)->byID($this->ObjectID);
|
|
|
|
}
|
|
|
|
|
2016-04-12 00:24:16 +02:00
|
|
|
/**
|
|
|
|
* Find latest version of this object
|
2016-09-30 03:10:23 +02:00
|
|
|
* @return DataObject|Versioned
|
|
|
|
* @throws UnexpectedDataException
|
2016-04-12 00:24:16 +02:00
|
|
|
*/
|
|
|
|
protected function getObjectLatestVersion() {
|
2016-09-23 01:05:33 +02:00
|
|
|
if(!class_exists($this->ObjectClass)) {
|
|
|
|
throw new UnexpectedDataException("Invalid Class '{$this->ObjectClass}' in ChangeSetItem #{$this->ID}");
|
|
|
|
}
|
|
|
|
|
2016-04-12 00:24:16 +02:00
|
|
|
return Versioned::get_latest_version($this->ObjectClass, $this->ObjectID);
|
|
|
|
}
|
|
|
|
|
2016-03-30 23:37:12 +02:00
|
|
|
/**
|
|
|
|
* Get all implicit objects for this change
|
|
|
|
*
|
|
|
|
* @return SS_List
|
|
|
|
*/
|
|
|
|
public function findReferenced() {
|
|
|
|
if($this->getChangeType() === ChangeSetItem::CHANGE_DELETED) {
|
|
|
|
// If deleted from stage, need to look at live record
|
2016-09-30 03:10:23 +02:00
|
|
|
$record = $this->getObjectInStage(Versioned::LIVE);
|
|
|
|
if ($record) {
|
|
|
|
return $record->findOwners(false);
|
|
|
|
}
|
2016-03-30 23:37:12 +02:00
|
|
|
} else {
|
|
|
|
// If changed on stage, look at owned objects there
|
2016-09-30 03:10:23 +02:00
|
|
|
$record = $this->getObjectInStage(Versioned::DRAFT);
|
|
|
|
if ($record) {
|
|
|
|
return $record->findOwned()->filterByCallback(function ($owned) {
|
|
|
|
/** @var Versioned|DataObject $owned */
|
|
|
|
return $owned->stagesDiffer(Versioned::DRAFT, Versioned::LIVE);
|
|
|
|
});
|
|
|
|
}
|
2016-03-30 23:37:12 +02:00
|
|
|
}
|
2016-09-30 03:10:23 +02:00
|
|
|
// Empty set
|
|
|
|
return new ArrayList();
|
2016-03-30 23:37:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Publish this item, then close it.
|
|
|
|
*
|
|
|
|
* Note: Unlike Versioned::doPublish() and Versioned::doUnpublish, this action is not recursive.
|
|
|
|
*/
|
|
|
|
public function publish() {
|
2016-09-23 01:05:33 +02:00
|
|
|
if(!class_exists($this->ObjectClass)) {
|
|
|
|
throw new UnexpectedDataException("Invalid Class '{$this->ObjectClass}' in ChangeSetItem #{$this->ID}");
|
|
|
|
}
|
|
|
|
|
2016-03-30 23:37:12 +02:00
|
|
|
// Logical checks prior to publish
|
|
|
|
if(!$this->canPublish()) {
|
|
|
|
throw new Exception("The current member does not have permission to publish this ChangeSetItem.");
|
|
|
|
}
|
|
|
|
if($this->VersionBefore || $this->VersionAfter) {
|
|
|
|
throw new BadMethodCallException("This ChangeSetItem has already been published");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record state changed
|
|
|
|
$this->VersionAfter = Versioned::get_versionnumber_by_stage(
|
|
|
|
$this->ObjectClass, Versioned::DRAFT, $this->ObjectID, false
|
|
|
|
);
|
|
|
|
$this->VersionBefore = Versioned::get_versionnumber_by_stage(
|
|
|
|
$this->ObjectClass, Versioned::LIVE, $this->ObjectID, false
|
|
|
|
);
|
|
|
|
|
|
|
|
switch($this->getChangeType()) {
|
|
|
|
case static::CHANGE_NONE: {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case static::CHANGE_DELETED: {
|
|
|
|
// Non-recursive delete
|
|
|
|
$object = $this->getObjectInStage(Versioned::LIVE);
|
|
|
|
$object->deleteFromStage(Versioned::LIVE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case static::CHANGE_MODIFIED:
|
|
|
|
case static::CHANGE_CREATED: {
|
|
|
|
// Non-recursive publish
|
|
|
|
$object = $this->getObjectInStage(Versioned::DRAFT);
|
2016-04-01 05:27:59 +02:00
|
|
|
$object->publishSingle();
|
2016-03-30 23:37:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Reverts this item, then close it. **/
|
|
|
|
public function revert() {
|
|
|
|
user_error('Not implemented', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canView($member = null) {
|
|
|
|
return $this->can(__FUNCTION__, $member);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canEdit($member = null) {
|
|
|
|
return $this->can(__FUNCTION__, $member);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canCreate($member = null, $context = array()) {
|
|
|
|
return $this->can(__FUNCTION__, $member, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canDelete($member = null) {
|
|
|
|
return $this->can(__FUNCTION__, $member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the BeforeVersion of this changeset can be restored to draft
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canRevert($member) {
|
|
|
|
// Just get the best version as this object may not even exist on either stage anymore.
|
|
|
|
/** @var Versioned|DataObject $object */
|
2016-04-12 00:24:16 +02:00
|
|
|
$object = $this->getObjectLatestVersion();
|
2016-03-30 23:37:12 +02:00
|
|
|
if(!$object) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check change type
|
|
|
|
switch($this->getChangeType()) {
|
|
|
|
case static::CHANGE_CREATED: {
|
|
|
|
// Revert creation by deleting from stage
|
|
|
|
if(!$object->canDelete($member)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
// All other actions are typically editing draft stage
|
|
|
|
if(!$object->canEdit($member)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If object can be published/unpublished let extensions deny
|
|
|
|
return $this->can(__FUNCTION__, $member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this ChangeSetItem can be published
|
|
|
|
*
|
|
|
|
* @param Member $member
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canPublish($member = null) {
|
|
|
|
// Check canMethod to invoke on object
|
|
|
|
switch($this->getChangeType()) {
|
|
|
|
case static::CHANGE_DELETED: {
|
|
|
|
/** @var Versioned|DataObject $object */
|
|
|
|
$object = Versioned::get_by_stage($this->ObjectClass, Versioned::LIVE)->byID($this->ObjectID);
|
|
|
|
if(!$object || !$object->canUnpublish($member)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
/** @var Versioned|DataObject $object */
|
|
|
|
$object = Versioned::get_by_stage($this->ObjectClass, Versioned::DRAFT)->byID($this->ObjectID);
|
|
|
|
if(!$object || !$object->canPublish($member)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If object can be published/unpublished let extensions deny
|
|
|
|
return $this->can(__FUNCTION__, $member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default permissions for this ChangeSetItem
|
|
|
|
*
|
|
|
|
* @param string $perm
|
|
|
|
* @param Member $member
|
|
|
|
* @param array $context
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function can($perm, $member = null, $context = array()) {
|
|
|
|
if(!$member) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allow extensions to bypass default permissions, but only if
|
|
|
|
// each change can be individually published.
|
|
|
|
$extended = $this->extendedCan($perm, $member, $context);
|
|
|
|
if($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default permissions
|
|
|
|
return (bool)Permission::checkMember($member, ChangeSet::config()->required_permission);
|
|
|
|
}
|
2016-04-11 22:54:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ChangeSetItems that reference a passed DataObject
|
|
|
|
*
|
|
|
|
* @param DataObject $object
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
public static function get_for_object($object) {
|
|
|
|
return ChangeSetItem::get()->filter([
|
|
|
|
'ObjectID' => $object->ID,
|
2016-05-25 07:09:29 +02:00
|
|
|
'ObjectClass' => $object->baseClass(),
|
2016-04-11 22:54:38 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ChangeSetItems that reference a passed DataObject
|
|
|
|
*
|
|
|
|
* @param int $objectID The ID of the object
|
|
|
|
* @param string $objectClass The class of the object (or any parent class)
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
public static function get_for_object_by_id($objectID, $objectClass) {
|
|
|
|
return ChangeSetItem::get()->filter([
|
|
|
|
'ObjectID' => $objectID,
|
2016-05-25 07:09:29 +02:00
|
|
|
'ObjectClass' => static::getSchema()->baseDataClass($objectClass)
|
2016-04-11 22:54:38 +02:00
|
|
|
]);
|
|
|
|
}
|
2016-04-27 04:26:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of modes this record can be previewed in.
|
|
|
|
*
|
2016-04-28 01:41:40 +02:00
|
|
|
* {@link https://tools.ietf.org/html/draft-kelly-json-hal-07#section-5}
|
|
|
|
*
|
|
|
|
* @return array Map of links in acceptable HAL format
|
2016-04-27 04:26:39 +02:00
|
|
|
*/
|
|
|
|
public function getPreviewLinks() {
|
|
|
|
$links = [];
|
|
|
|
|
|
|
|
// Preview draft
|
|
|
|
$stage = $this->getObjectInStage(Versioned::DRAFT);
|
2016-04-28 01:41:40 +02:00
|
|
|
if($stage instanceof CMSPreviewable && $stage->canView() && ($link = $stage->PreviewLink())) {
|
|
|
|
$links[Versioned::DRAFT] = [
|
|
|
|
'href' => Controller::join_links($link, '?stage=' . Versioned::DRAFT),
|
|
|
|
'type' => $stage->getMimeType(),
|
|
|
|
];
|
2016-04-27 04:26:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Preview live
|
|
|
|
$live = $this->getObjectInStage(Versioned::LIVE);
|
2016-04-28 01:41:40 +02:00
|
|
|
if($live instanceof CMSPreviewable && $live->canView() && ($link = $live->PreviewLink())) {
|
|
|
|
$links[Versioned::LIVE] = [
|
|
|
|
'href' => Controller::join_links($link, '?stage=' . Versioned::LIVE),
|
|
|
|
'type' => $live->getMimeType(),
|
|
|
|
];
|
2016-04-27 04:26:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $links;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get edit link for this item
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function CMSEditLink()
|
|
|
|
{
|
|
|
|
$link = $this->getObjectInStage(Versioned::DRAFT);
|
|
|
|
if($link instanceof CMSPreviewable) {
|
|
|
|
return $link->CMSEditLink();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-03-30 23:37:12 +02:00
|
|
|
}
|