<commit_before><?php

/**
 * A representation of a forum thread. A forum thread is 1 topic on the forum 
 * which has multiple posts underneath it.
 *
 * @package forum
 */

class ForumThread extends DataObject {
	
	private static $db = array(
		"Title" => "Varchar(255)",
		"NumViews" => "Int",
		"IsSticky" => "Boolean",
		"IsReadOnly" => "Boolean",
		"IsGlobalSticky" => "Boolean"
	);
	
	private static $has_one = array(
		'Forum' => 'Forum',
		'LastPost' => 'Post'
	);
	
	private static $has_many = array(
		'Posts' => 'Post'
	);
	
	private static $defaults = array(
		'NumViews' => 0,
		'IsSticky' => false,
		'IsReadOnly' => false,
		'IsGlobalSticky' => false
	);
		
	
	/**
	 * Check if the user can create new threads and add responses
	 */
	function canPost($member = null) {
		if(!$member) $member = Member::currentUser();
		return ($this->Forum()->canPost($member) && !$this->IsReadOnly);
	}
	
	/**
	 * Check if user can moderate this thread
	 */
	function canModerate($member = null) {
		if(!$member) $member = Member::currentUser();
		return $this->Forum()->canModerate($member);
	}
	
	/**
	 * Check if user can view the thread
	 */
	function canView($member = null) {
		if(!$member) $member = Member::currentUser();
		return $this->Forum()->canView($member);
	}

	/**
	 * Hook up into moderation.
	 */
	function canEdit($member = null) {
		if(!$member) $member = Member::currentUser();
		return $this->canModerate($member);
	}

	/**
	 * Hook up into moderation - users cannot delete their own posts/threads because 
	 * we will loose history this way.
	 */
	function canDelete($member = null) {
		if(!$member) $member = Member::currentUser();
		return $this->canModerate($member);
	}

	/**
	 * Hook up into canPost check
	 */
	function canCreate($member = null) {
		if(!$member) $member = Member::currentUser();
		return $this->canPost($member);
	}
	
	/** 
	 * Are Forum Signatures on Member profiles allowed
	 * 
	 * @return bool
	 */
	 function getDisplaySignatures() {
	 	return $this->Forum()->Parent()->DisplaySignatures;
	}
	
	/**
	 * Get the latest post from this thread. Nicer way then using an control
	 * from the template
	 *
	 * @return Post
	 */
	function getLatestPost() {
		$post = $this->LastPost();
		if($post && $post->ID > 0) return $post;    //need to check for null post object both with ID of zero

		return $this->updateLastPost();
	}
	
	/**
	 * Return the first post from the thread. Useful to working out the original author
	 *
	 * @return Post
	 */
	function getFirstPost() {
		return DataObject::get_one('Post', "\"ThreadID\" = '$this->ID'", true, "\"ID\" ASC");
	}

	/**<commit_msg>Cache first call of DisplaySignatures. It only needs to be checked once.<commit_after><?php

/**
 * A representation of a forum thread. A forum thread is 1 topic on the forum 
 * which has multiple posts underneath it.
 *
 * @package forum
 */

class ForumThread extends DataObject {
	
	private static $db = array(
		"Title" => "Varchar(255)",
		"NumViews" => "Int",
		"IsSticky" => "Boolean",
		"IsReadOnly" => "Boolean",
		"IsGlobalSticky" => "Boolean"
	);
	
	private static $has_one = array(
		'Forum' => 'Forum',
		'LastPost' => 'Post'
	);
	
	private static $has_many = array(
		'Posts' => 'Post'
	);
	
	private static $defaults = array(
		'NumViews' => 0,
		'IsSticky' => false,
		'IsReadOnly' => false,
		'IsGlobalSticky' => false
	);

	/**
	 * @var null|boolean Per-request cache, whether we should display signatures on a post.
	 */
	private static $_cache_displaysignatures = null;
	
	/**
	 * Check if the user can create new threads and add responses
	 */
	function canPost($member = null) {
		if(!$member) $member = Member::currentUser();
		return ($this->Forum()->canPost($member) && !$this->IsReadOnly);
	}
	
	/**
	 * Check if user can moderate this thread
	 */
	function canModerate($member = null) {
		if(!$member) $member = Member::currentUser();
		return $this->Forum()->canModerate($member);
	}
	
	/**
	 * Check if user can view the thread
	 */
	function canView($member = null) {
		if(!$member) $member = Member::currentUser();
		return $this->Forum()->canView($member);
	}

	/**
	 * Hook up into moderation.
	 */
	function canEdit($member = null) {
		if(!$member) $member = Member::currentUser();
		return $this->canModerate($member);
	}

	/**
	 * Hook up into moderation - users cannot delete their own posts/threads because 
	 * we will loose history this way.
	 */
	function canDelete($member = null) {
		if(!$member) $member = Member::currentUser();
		return $this->canModerate($member);
	}

	/**
	 * Hook up into canPost check
	 */
	function canCreate($member = null) {
		if(!$member) $member = Member::currentUser();
		return $this->canPost($member);
	}
	
	/** 
	 * Are Forum Signatures on Member profiles allowed.
	 * This only needs to be checked once, so we cache the initial value once per-request.
	 * 
	 * @return bool
	 */
	function getDisplaySignatures() {
		if(isset(self::$_cache_displaysignatures) && self::$_cache_displaysignatures !== null) {
			return self::$_cache_displaysignatures;
		}

		$result = $this->Forum()->Parent()->DisplaySignatures;
		self::$_cache_displaysignatures = $result;
		return $result;
	}

	/**
	 * Get the latest post from this thread. Nicer way then using an control
	 * from the template
	 *
	 * @return Post
	 */
	function getLatestPost() {
		$post = $this->LastPost();
		if($post && $post->ID > 0) return $post;    //need to check for null post object both with ID of zero

		return $this->updateLastPost();
	}
	
	/**
	 * Return the first post from the thread. Useful to working out the original author
	 *
	 * @return Post
	 */
	function getFirstPost() {
		return DataObject::get_one('Post', "\"ThreadID\" = '$this->ID'", true, "\"ID\" ASC");
	}

	/**
{'fraction_non_alphanumeric': 0.15839860748476936, 'fraction_numerical': 0.0020887728459530026, 'mean_word_length': 3.556701030927835, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, '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': '5800502', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1789, 'n_words': 547}