<commit_before><?php

/**
 * Extension for the SiteTree object to add subsites support
 */
class SiteTreeSubsites extends SiteTreeDecorator {
	static $template_variables = array(
		'((Company Name))' => 'Title'
	);
	
	static $template_fields = array(
		"URLSegment",
		"Title",
		"MenuTitle",
		"Content",
		"MetaTitle",
		"MetaDescription",
		"MetaKeywords",
	);

	/**
	 * Set the fields that will be copied from the template.
	 * Note that ParentID and Sort are implied.
	 */
	static function set_template_fields($fieldList) {
		self::$template_fields = $fieldList;
	}

	
	function extraStatics() {
		if(!method_exists('DataObjectDecorator', 'load_extra_statics')) {
			if($this->owner->class != 'SiteTree') return null;
		}
		return array(
			'has_one' => array(
				'Subsite' => 'Subsite', // The subsite that this page belongs to
				'MasterPage' => 'SiteTree',// Optional; the page that is the content master
			),
			'has_many' => array(
				'RelatedPages' => 'RelatedPageLink'
			),
			'many_many' => array(
				'CrossSubsiteLinkTracking' => 'SiteTree' // Stored separately, as the logic for URL rewriting is different
			),
			'belongs_many_many' => array(
				'BackCrossSubsiteLinkTracking' => 'SiteTree'
			)
		);
	}
	
	/**
	 * Check if we're currently looking at the main site.
	 * @return boolean TRUE main site | FALSE sub-site
	 */
	function isMainSite() {
		if($this->owner->SubsiteID == 0) return true;
		return false;
	}
	
	/**
	 * Update any requests to limit the results to the current site
	 */
	function augmentSQL(SQLQuery &$query) {
		if(Subsite::$disable_subsite_filter) return;
		
		if(defined('DB::USE_ANSI_SQL')) 
			$q="\"";
		else $q='`';
		
		// If you're querying by ID, ignore the sub-site - this is a bit ugly...
		if (!$query->where || (!preg_match('/\.(\'|"|`|)ID(\'|"|`|)( ?)=/', $query->where[0]))) {
			$context = DataObject::context_obj();
			if($context && is_numeric($context->SubsiteID)) $subsiteID = (int) $context->SubsiteID;
			else $subsiteID = (int) Subsite::currentSubsiteID();
			
			// The foreach is an ugly way of getting the first key :-)
			foreach($query->from as $tableName => $info) {
				$where = "{$q}$tableName{$q}.{$q}SubsiteID{$q} IN ($subsiteID)";
				
				// The tableName should be SiteTree or SiteTree_Live...
				if(strpos($tableName,'SiteTree') === false) break;
				$query->where[] = $where;
				break;
			}
		}
	}
	
	/**
	 * Call this method before writing; the next write carried out by the system won't
	 * set the CustomContent value
	 */
	function nextWriteDoesntCustomise() {
		$this->nextWriteDoesntCustomise = true;
	}
	
	protected $nextWriteDoesntCustomise = false;
	
	function augmentBeforeWrite() {
		// if the page hasn't been written to a database table yet and no subsite has been set, then give it a subsite
		if((!is_numeric($this->owner->ID) || $this->owner->ID == 0) && !$this->owner->SubsiteID) {
			$this->owner->SubsiteID = Subsite::currentSubsiteID();
		}

		// If the content has been changed, then the page should be marked as 'custom content'
		if(!$this->nextWriteDoesntCustomise && $this->owner->ID && $this->owner->MasterPageID && !$this->owner->CustomContent) {
			$changed = $this->owner->getChangedFields();

			foreach(self::$template_fields as $field) {
				if(isset($changed[$field]) && $changed[$field]) {
					$this->owner->CustomContent = true;
					FormResponse::add("if($('Form_EditForm_CustomContent')) $('Form_EditForm_CustomContent').checked = true;");
					break;
				}
			}
		}
		
		$this->nextWriteDoesntCustomise = false;
	}
	
	function onAfterWrite(&$original) {
		// Update any subsite virtual pages that might need updating
		Subsite::$disable_subsite_filter = true;
		
		$linkedPages = DataObject::get("SubsitesVirtualPage", "CopyContentFromID = {$this->owner->ID}");
		if($linkedPages) foreach($linkedPages as $page) {
			$page->copyFrom($page->CopyContentFrom());
			$page->write();
		}
		
		Subsite::$disable_subsite_filter = false;
	}
	
	function onAfterPublish(&$original) {
		// Publish any subsite virtual pages that might need publishing
		Subsite::$disable_subsite_filter = true;
		
		$linkedPages = DataObject::get("SubsitesVirtualPage", "CopyContentFromID = {$this->owner->ID}");
		if($linkedPages) foreach($linkedPages as $page) {
			$page->copyFrom($page->CopyContentFrom());
			$page->doPublish();
		}
		
		Subsite::$disable_subsite_filter = false;
	}

	function updateCMSFields(&$fields) {
		if($this->owner->MasterPageID) {
			$fields->insertFirst(new HeaderField('This page\'s content is copied from a master page: ' . $this->owner->MasterPage()->Title, 2));
		}
		
		// replace readonly link prefix
		$subsite = $this->owner->Subsite();
		if($subsite && $subsite->ID) {
			$baseUrl = 'http://' . $subsite->domain() . '/';
			$fields->removeByName('BaseUrlLabel');
			$fields->addFieldToTab(
				'Root.Content.Metadata',
				new LabelField('BaseUrlLabel',$baseUrl),
				'URLSegment'
			);
		}
		
		$relatedCount = 0;
		$reverse = $this->ReverseRelated();
		if($reverse) $relatedCount += $reverse->Count();
		$normalRelated = $this->NormalRelated();
		if($normalRelated) $relatedCount += $normalRelated->Count();
		
		$tabName = $relatedCount ? 'Related (' . $relatedCount . ')' : 'Related';
		$tab = $fields->findOrMakeTab('Root.Related', $tabName);<commit_msg>BUGFIX restore the state of the subsite filter, rather than just blinding reenabling it (from r87496)<commit_after><?php

/**
 * Extension for the SiteTree object to add subsites support
 */
class SiteTreeSubsites extends SiteTreeDecorator {
	static $template_variables = array(
		'((Company Name))' => 'Title'
	);
	
	static $template_fields = array(
		"URLSegment",
		"Title",
		"MenuTitle",
		"Content",
		"MetaTitle",
		"MetaDescription",
		"MetaKeywords",
	);

	/**
	 * Set the fields that will be copied from the template.
	 * Note that ParentID and Sort are implied.
	 */
	static function set_template_fields($fieldList) {
		self::$template_fields = $fieldList;
	}

	
	function extraStatics() {
		if(!method_exists('DataObjectDecorator', 'load_extra_statics')) {
			if($this->owner->class != 'SiteTree') return null;
		}
		return array(
			'has_one' => array(
				'Subsite' => 'Subsite', // The subsite that this page belongs to
				'MasterPage' => 'SiteTree',// Optional; the page that is the content master
			),
			'has_many' => array(
				'RelatedPages' => 'RelatedPageLink'
			),
			'many_many' => array(
				'CrossSubsiteLinkTracking' => 'SiteTree' // Stored separately, as the logic for URL rewriting is different
			),
			'belongs_many_many' => array(
				'BackCrossSubsiteLinkTracking' => 'SiteTree'
			)
		);
	}
	
	/**
	 * Check if we're currently looking at the main site.
	 * @return boolean TRUE main site | FALSE sub-site
	 */
	function isMainSite() {
		if($this->owner->SubsiteID == 0) return true;
		return false;
	}
	
	/**
	 * Update any requests to limit the results to the current site
	 */
	function augmentSQL(SQLQuery &$query) {
		if(Subsite::$disable_subsite_filter) return;
		
		if(defined('DB::USE_ANSI_SQL')) 
			$q="\"";
		else $q='`';
		
		// If you're querying by ID, ignore the sub-site - this is a bit ugly...
		if (!$query->where || (!preg_match('/\.(\'|"|`|)ID(\'|"|`|)( ?)=/', $query->where[0]))) {
			$context = DataObject::context_obj();
			if($context && is_numeric($context->SubsiteID)) $subsiteID = (int) $context->SubsiteID;
			else $subsiteID = (int) Subsite::currentSubsiteID();
			
			// The foreach is an ugly way of getting the first key :-)
			foreach($query->from as $tableName => $info) {
				$where = "{$q}$tableName{$q}.{$q}SubsiteID{$q} IN ($subsiteID)";
				
				// The tableName should be SiteTree or SiteTree_Live...
				if(strpos($tableName,'SiteTree') === false) break;
				$query->where[] = $where;
				break;
			}
		}
	}
	
	/**
	 * Call this method before writing; the next write carried out by the system won't
	 * set the CustomContent value
	 */
	function nextWriteDoesntCustomise() {
		$this->nextWriteDoesntCustomise = true;
	}
	
	protected $nextWriteDoesntCustomise = false;
	
	function augmentBeforeWrite() {
		// if the page hasn't been written to a database table yet and no subsite has been set, then give it a subsite
		if((!is_numeric($this->owner->ID) || $this->owner->ID == 0) && !$this->owner->SubsiteID) {
			$this->owner->SubsiteID = Subsite::currentSubsiteID();
		}

		// If the content has been changed, then the page should be marked as 'custom content'
		if(!$this->nextWriteDoesntCustomise && $this->owner->ID && $this->owner->MasterPageID && !$this->owner->CustomContent) {
			$changed = $this->owner->getChangedFields();

			foreach(self::$template_fields as $field) {
				if(isset($changed[$field]) && $changed[$field]) {
					$this->owner->CustomContent = true;
					FormResponse::add("if($('Form_EditForm_CustomContent')) $('Form_EditForm_CustomContent').checked = true;");
					break;
				}
			}
		}
		
		$this->nextWriteDoesntCustomise = false;
	}
	
	function onAfterWrite(&$original) {
		// Update any subsite virtual pages that might need updating
		$oldState = Subsite::$disable_subsite_filter;
		Subsite::$disable_subsite_filter = true;
		
		$linkedPages = DataObject::get("SubsitesVirtualPage", "CopyContentFromID = {$this->owner->ID}");
		if($linkedPages) foreach($linkedPages as $page) {
			$page->copyFrom($page->CopyContentFrom());
			$page->write();
		}
		
		Subsite::$disable_subsite_filter = $oldState;
	}
	
	function onAfterPublish(&$original) {
		// Publish any subsite virtual pages that might need publishing
		$oldState = Subsite::$disable_subsite_filter;
		Subsite::$disable_subsite_filter = true;
		
		$linkedPages = DataObject::get("SubsitesVirtualPage", "CopyContentFromID = {$this->owner->ID}");
		if($linkedPages) foreach($linkedPages as $page) {
			$page->copyFrom($page->CopyContentFrom());
			$page->doPublish();
		}
		
		Subsite::$disable_subsite_filter = $oldState;
	}

	function updateCMSFields(&$fields) {
		if($this->owner->MasterPageID) {
			$fields->insertFirst(new HeaderField('This page\'s content is copied from a master page: ' . $this->owner->MasterPage()->Title, 2));
		}
		
		// replace readonly link prefix
		$subsite = $this->owner->Subsite();
		if($subsite && $subsite->ID) {
			$baseUrl = 'http://' . $subsite->domain() . '/';
			$fields->removeByName('BaseUrlLabel');
			$fields->addFieldToTab(
				'Root.Content.Metadata',
				new LabelField('BaseUrlLabel',$baseUrl),
				'URLSegment'
			);
		}
		
		$relatedCount = 0;
		$reverse = $this->ReverseRelated();
		if($reverse) $relatedCount += $reverse->Count();
		$normalRelated = $this->NormalRelated();
		if($normalRelated) $relatedCount += $normalRelated->Count();
		
		$tabName = $relatedCount ? 'Related (' . $relatedCount . ')' : 'Related';
		$tab = $fields->findOrMakeTab('Root.Related', $tabName);
{'fraction_non_alphanumeric': 0.16379310344827586, 'fraction_numerical': 0.0013904338153503894, 'mean_word_length': 4.055763823805061, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 141, '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': '5184274', 'n_tokens_mistral': 3941, 'n_tokens_neox': 3432, 'n_words': 818}