<commit_before><?php

/**
 * Augment SiteTree with a new permissions, 'canApprove',
 * and 'canAction'.
 *
 * @package cmsworkflow
 * @subpackage ThreeStep
 * @author <NAME>
 */
class SiteTreeCMSThreeStepWorkflow extends SiteTreeCMSWFDecorator implements PermissionProvider {
	public function extraStatics() {
		return array(
			'db' => array(
				"CanApproveType" =>"Enum('LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')",
				"CanPublishType" =>"Enum('LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')"
			),
			'many_many' => array(
				"ApproverGroups" => "Group",
				"PublisherGroups" => "Group",
			),
			'defaults' => array(
				"CanApproveType" => "Inherit",
				"CanPublishType" => "Inherit",
			)
		);
	}
	
	function getOpenRequest($workflowClass) {
		$wf = DataObject::get_one($workflowClass, "PageID = " . (int)$this->owner->ID . " AND Status NOT IN ('Completed', 'Denied', 'Cancelled')");
		if($wf) return $wf;
		
		return null;
	}
	
	function batchPublish() {
		$page = $this->owner;
		if($request = $page->openWorkflowRequest('WorkflowRequest')) {
			if ($request->Status == 'AwaitingApproval') {
				$request->approve('Batch approval', null, false);
				$request->publish('Batch publish', null, false);
			}
			if ($request->Status == 'Approved') {
				$request->publish('Batch publish', null, false);
			}
		}
	}
	function batchApprove() {
		$page = $this->owner;
		if($request = $page->openWorkflowRequest('WorkflowRequest')) {
			if ($request->Status == 'AwaitingApproval') {
				$request->approve('Batch approval', null, false);
			}
		}
	}
	
	function canDenyRequests() {
		return true;
	}
	
	function canRequestEdit() {
		return true;
	}
	
	function whoCanApprove() {
		return $this->ApproverMembers();
	}
	
	/**
	 * Implement permissions for ThreeStep
	 *
	 * @return void
	 */
	public function updateCMSFields(&$fields) {
		$fields->addFieldsToTab("Root.Access", array(
			new HeaderField(_t('SiteTreeCMSWorkflow.APPROVEHEADER', "Who can approve requests inside the CMS?"), 2),
			$approveTypeField = new OptionsetField(
				"CanApproveType", 
				"",
				array(
					"Inherit" => _t('SiteTree.EDITINHERIT', "Inherit from parent page"),
					"LoggedInUsers" => _t('SiteTree.EDITANYONE', "Anyone who can log-in to the CMS"),
					"OnlyTheseUsers" => _t('SiteTree.EDITONLYTHESE', "Only these people (choose from list)")
				),
				"Inherit"
			),
			$approverGroupsField = new TreeMultiselectField("ApproverGroups", $this->owner->fieldLabel('ApproverGroups'))
		));
		
		$fields->addFieldsToTab("Root.Access", array(
			new HeaderField(_t('SiteTreeCMSWorkflow.PUBLISHAPPROVEDHEADER', "Who can publish approved requests inside the CMS?"), 2),
			$actionTypeField = new OptionsetField(
				"CanPublishType", 
				"",
				array(
					"Inherit" => _t('SiteTree.EDITINHERIT', "Inherit from parent page"),
					"LoggedInUsers" => _t('SiteTree.EDITANYONE', "Anyone who can log-in to the CMS"),
					"OnlyTheseUsers" => _t('SiteTree.EDITONLYTHESE', "Only these people (choose from list)")
				),
				"Inherit"
			),
			$actionerGroupsField = new TreeMultiselectField("PublisherGroups", $this->owner->fieldLabel('PublisherGroups'))
		));
		
		if(!$this->owner->canPublish() || !Permission::check('SITETREE_GRANT_ACCESS')) {
			$fields->replaceField('CanApproveType', $approveTypeField->performReadonlyTransformation());
			$fields->replaceField('ApproverGroups', $approverGroupsField->performReadonlyTransformation());
			$fields->replaceField('CanPublishType', $actionTypeField->performReadonlyTransformation());
			$fields->replaceField('PublisherGroups', $actionerGroupsField->performReadonlyTransformation());
		}
	}
	
	/**
	 * Returns a DataObjectSet of all the members that can approve this page
	 */
	public function ApproverMembers() {
		if($this->owner->CanApproveType == 'OnlyTheseUsers'){
			$groups = $this->owner->ApproverGroups();
			$members = new DataObjectSet();
			if($groups) foreach($groups as $group) {
				$members->merge($group->Members());
			}
			
			// Default to ADMINs, if something goes wrong
			if(!$members->Count()) {
				$group = Permission::get_groups_by_permission('ADMIN')->first();
				$members = $group->Members();
			}<commit_msg>MINOR: Hide readonly permission group fields on pages when not relevant,  as they are confusing. (from r88019)<commit_after><?php

/**
 * Augment SiteTree with a new permissions, 'canApprove',
 * and 'canAction'.
 *
 * @package cmsworkflow
 * @subpackage ThreeStep
 * @author <NAME>
 */
class SiteTreeCMSThreeStepWorkflow extends SiteTreeCMSWFDecorator implements PermissionProvider {
	public function extraStatics() {
		return array(
			'db' => array(
				"CanApproveType" =>"Enum('LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')",
				"CanPublishType" =>"Enum('LoggedInUsers, OnlyTheseUsers, Inherit', 'Inherit')"
			),
			'many_many' => array(
				"ApproverGroups" => "Group",
				"PublisherGroups" => "Group",
			),
			'defaults' => array(
				"CanApproveType" => "Inherit",
				"CanPublishType" => "Inherit",
			)
		);
	}
	
	function getOpenRequest($workflowClass) {
		$wf = DataObject::get_one($workflowClass, "PageID = " . (int)$this->owner->ID . " AND Status NOT IN ('Completed', 'Denied', 'Cancelled')");
		if($wf) return $wf;
		
		return null;
	}
	
	function batchPublish() {
		$page = $this->owner;
		if($request = $page->openWorkflowRequest('WorkflowRequest')) {
			if ($request->Status == 'AwaitingApproval') {
				$request->approve('Batch approval', null, false);
				$request->publish('Batch publish', null, false);
			}
			if ($request->Status == 'Approved') {
				$request->publish('Batch publish', null, false);
			}
		}
	}
	function batchApprove() {
		$page = $this->owner;
		if($request = $page->openWorkflowRequest('WorkflowRequest')) {
			if ($request->Status == 'AwaitingApproval') {
				$request->approve('Batch approval', null, false);
			}
		}
	}
	
	function canDenyRequests() {
		return true;
	}
	
	function canRequestEdit() {
		return true;
	}
	
	function whoCanApprove() {
		return $this->ApproverMembers();
	}
	
	/**
	 * Implement permissions for ThreeStep
	 *
	 * @return void
	 */
	public function updateCMSFields(&$fields) {
		$fields->addFieldsToTab("Root.Access", array(
			new HeaderField(_t('SiteTreeCMSWorkflow.APPROVEHEADER', "Who can approve requests inside the CMS?"), 2),
			$approveTypeField = new OptionsetField(
				"CanApproveType", 
				"",
				array(
					"Inherit" => _t('SiteTree.EDITINHERIT', "Inherit from parent page"),
					"LoggedInUsers" => _t('SiteTree.EDITANYONE', "Anyone who can log-in to the CMS"),
					"OnlyTheseUsers" => _t('SiteTree.EDITONLYTHESE', "Only these people (choose from list)")
				),
				"Inherit"
			),
			$approverGroupsField = new TreeMultiselectField("ApproverGroups", $this->owner->fieldLabel('ApproverGroups'))
		));
		
		$fields->addFieldsToTab("Root.Access", array(
			new HeaderField(_t('SiteTreeCMSWorkflow.PUBLISHAPPROVEDHEADER', "Who can publish approved requests inside the CMS?"), 2),
			$actionTypeField = new OptionsetField(
				"CanPublishType", 
				"",
				array(
					"Inherit" => _t('SiteTree.EDITINHERIT', "Inherit from parent page"),
					"LoggedInUsers" => _t('SiteTree.EDITANYONE', "Anyone who can log-in to the CMS"),
					"OnlyTheseUsers" => _t('SiteTree.EDITONLYTHESE', "Only these people (choose from list)")
				),
				"Inherit"
			),
			$actionerGroupsField = new TreeMultiselectField("PublisherGroups", $this->owner->fieldLabel('PublisherGroups'))
		));
		
		if(!$this->owner->canPublish() || !Permission::check('SITETREE_GRANT_ACCESS')) {
			$fields->replaceField('CanApproveType', $approveTypeField->performReadonlyTransformation());
			if($this->owner->CanApproveType == 'OnlyTheseUsers') {
				$fields->replaceField('ApproverGroups', $approverGroupsField->performReadonlyTransformation());
			} else {
				$fields->removeByName('ApproverGroups');
			}
			
			$fields->replaceField('CanPublishType', $actionTypeField->performReadonlyTransformation());
			if($this->owner->CanPublishType == 'OnlyTheseUsers') {
				$fields->replaceField('PublisherGroups', $actionerGroupsField->performReadonlyTransformation());
			} else {
				$fields->removeByName('PublisherGroups');
			}
		}
	}
	
	/**
	 * Returns a DataObjectSet of all the members that can approve this page
	 */
	public function ApproverMembers() {
		if($this->owner->CanApproveType == 'OnlyTheseUsers'){
			$groups = $this->owner->ApproverGroups();
			$members = new DataObjectSet();
			if($groups) foreach($groups as $group) {
				$members->merge($group->Members());
			}
			
			// Default to ADMINs, if something goes wrong
			if(!$members->Count()) {
				$group = Permission::get_groups_by_permission('ADMIN')->first();
				$members = $group->Members();
			}
{'fraction_non_alphanumeric': 0.16085760146755332, 'fraction_numerical': 0.0010318734235267141, 'mean_word_length': 4.524382520582647, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 119, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7581562', 'n_tokens_mistral': 3259, 'n_tokens_neox': 2757, 'n_words': 558}