<?php
class FilePage extends Page {
static $db = array(
"CanViewTypeExtended" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers, OnlyTheseMembers, Inherit', 'Inherit')",
);
static $has_one = array(
"ViewerMember" => "Member",
);
public function getSettingsFields(){
global $project;
$fields = parent::getSettingsFields();
Requirements::javascript($project.'/javascript/CMSMain.EditFormMember.js'); //Not sure where to put this :)
$members = Member::map_in_groups();
$field = new DropdownField("ViewerMemberID", "Viewer Members", $members);
$field->setEmptyString('(Select one)');
$fields->addFieldToTab("Root.Settings", $field, 'CanEditType');
$viewersOptionsField = $fields->removeByName('CanViewType');
$viewersOptions = new OptionsetField(
"CanViewTypeExtended",
_t('SiteTree.ACCESSHEADER', "Who can view this page?")
);
$viewersOptionsSource = array();
$viewersOptionsSource["Inherit"] = _t('SiteTree.INHERIT', "Inherit from parent page");
$viewersOptionsSource["Anyone"] = _t('SiteTree.ACCESSANYONE', "Anyone");
$viewersOptionsSource["LoggedInUsers"] = _t('SiteTree.ACCESSLOGGEDIN', "Logged-in users");
$viewersOptionsSource["OnlyTheseUsers"] = _t('SiteTree.ACCESSONLYTHESE', "Only these people (choose from list)");
$viewersOptionsSource["OnlyTheseMembers"] = _t('SiteTree.ACCESSONLYTHESEMEMBERS', "Only these members (choose from list)");
$viewersOptions->setSource($viewersOptionsSource);
$fields->addFieldToTab("Root.Settings", $viewersOptions, 'ViewerGroups');
return $fields;
}
// Override the SiteTree canView function to implement the new variable
public function canView($member = null) {
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) {
$member = Member::currentUserID();
}
// admin override
if($member && Permission::checkMember($member, array("ADMIN", "SITETREE_VIEW_ALL"))) return true;
// Standard mechanism for accepting permission changes from extensions
$extended = $this->extendedCan('canView', $member);
if($extended !== null) return $extended;
// check for empty spec
if(!$this->CanViewTypeExtended || $this->CanViewTypeExtended == 'Anyone') return true;
// check for inherit
if($this->CanViewTypeExtended == 'Inherit') {
if($this->ParentID) return $this->Parent()->canView($member);
else return $this->getSiteConfig()->canView($member);
}
// check for any logged-in users
if($this->CanViewTypeExtended == 'LoggedInUsers' && $member) {
return true;
}
// check for specific groups
if($member && is_numeric($member)) $member = DataObject::get_by_id('Member', $member);
if(
$this->CanViewTypeExtended == 'OnlyTheseUsers'
&& $member
&& $member->inGroups($this->ViewerGroups())
) return true;
//Check for specific Member
if(
$this->CanViewTypeExtended == 'OnlyTheseMembers'
&& $member
&& $member->ID == $this->ViewerMemberID
) return true;
return false;
}
}