public function checkIn($userId = null)
{
// If there is no loaded record we can't do much, I'm afraid
if (!$this->getId()) {
throw new RecordNotLoaded("Can't checkin a not loaded DataModel");
}
// If the lock fields are missing we have nothing to do
if (!$this->hasField('locked_by') && !$this->hasField('locked_on')) {
return $this;
}
// If there's no locked_by field we just unlock and return
if (!$this->hasField('locked_by')) {
return $this->unlock();
}
// If the current user and the user who locked the record are the same, unlock it.
if (empty($userId)) {
$userId = $this->container->platform->getUser()->id;
}
$lockedBy = $this->getFieldValue('locked_by');
if (empty($lockedBy) || $lockedBy == $userId) {
return $this->unlock();
}
// Get the component privileges
$platform = $this->container->platform;
$component = $this->container->componentName;
$privileges = array('editown' => $platform->authorise('core.edit.own', $component), 'editstate' => $platform->authorise('core.edit.state', $component), 'admin' => $platform->authorise('core.admin', $component), 'manage' => $platform->authorise('core.manage', $component));
// If we are trackign assets get the item's privileges
if ($this->isAssetsTracked()) {
$assetKey = $this->getAssetKey();
$assetPrivileges = array('editown' => $platform->authorise('core.edit.own', $assetKey), 'editstate' => $platform->authorise('core.edit.state', $assetKey));
foreach ($assetPrivileges as $k => $v) {
$privileges[$k] = $privileges[$k] || $v;
}
}
// If you are a Super User, component manager or allowed to edit the state of records we unlock it
if ($privileges['admin'] || $privileges['manage'] || $privileges['editstate']) {
return $this->unlock();
}
// If you are the owner of the record and have core.edit.own privilege we will unlock it.
$owner = 0;
if ($this->hasField('created_by')) {
$owner = $this->getFieldValue('created_by');
}
if ($privileges['editown'] && $owner == $userId) {
return $this->unlock();
}
// All else failed, you don't have the privilege to unlock this item.
throw new LockedRecord();
}