<commit_before><?php

namespace App;

use App\Models\Show;
use App\Models\Notification;
use App\Models\PCGSlotHistory;
use App\Models\Post;
use App\Models\User;
use App\Exceptions\MismatchedProviderException;

class Posts {

	/**
	 * Retrieves requests & reservations for the episode specified
	 * Optionally lists broken posts
	 *
	 * @param int  $show_id
	 * @param int  $only
	 * @param bool $showBroken
	 *
	 * @return Post[]|Post[][]
	 */
	public static function get(int $show_id, int $only = null, bool $showBroken = false){
		$return = [];
		if ($only !== ONLY_RESERVATIONS){
			// If we don't want reservations only, grab requests
			$return[] = Post::find('all', [
				'conditions'=> [
					'requested_by IS NOT NULL AND show_id = ?'.($showBroken === false?' AND broken IS NOT true':''),
					$show_id
				],
				'order' => 'finished_at asc, requested_at asc',
			]);
		}
		if ($only !== ONLY_REQUESTS){
			// If we don't want requests only, grab reservations
			$return[] = Post::find('all', [
				'conditions'=> [
					'requested_by IS NULL AND show_id = ?'.($showBroken === false?' AND broken IS NOT true':''),
					$show_id
				],
				'order' => 'finished_at asc, reserved_at asc',
			]);
		}

		return $only ? $return[0] : $return;
	}

	/**
	 * @return Post[]
	 */
	public static function getRecentPosts():array {
		return DB::$instance->orderByLiteral(Post::ORDER_BY_POSTED_AT, 'DESC')->get('posts', 20);
	}

	/**
	 * Get list of most recent posts
	 *
	 * @param bool $wrap
	 *
	 * @return string
	 */
	public static function getMostRecentList($wrap = WRAP):string {
		$recent_posts = self::getRecentPosts();

		return Twig::$env->render('admin/_most_recent_posts.html.twig', ['recent_posts' => $recent_posts, 'wrap' => $wrap ]);
	}

	/**
	 * POST data validator function used when creating/editing posts
	 *
	 * @param bool         $request Boolean that's true if post is a request and false otherwise
	 * @param array|object $target  Array or object to output the checked data into
	 * @param Post|null    $post    Optional, exsting post to compare new data against
	 */
	public static function checkPostDetails(bool $request, &$target, $post = null){
		$editing = !empty($post);

		$label = (new Input('label','string', [
			Input::IS_OPTIONAL => true,
			Input::IN_RANGE => [3,255],
			Input::CUSTOM_ERROR_MESSAGES => [
				Input::ERROR_RANGE => 'The description must be between @min and @max characters'
			]
		]))->out();
		if ($label !== null){
			if (!$editing || $label !== $post->label){
				CoreUtils::checkStringValidity($label,'The description',INVERSE_PRINTABLE_ASCII_PATTERN);
				$label = preg_replace(new RegExp("''"),'"',$label);
				CoreUtils::set($target, 'label', $label);
			}
		}
		else if (!$editing && $request)
			Response::fail('Description cannot be empty');
		else CoreUtils::set($target, 'label', null);

		if ($request){
			$type = (new Input('type',function($value){
				if (!isset(Post::REQUEST_TYPES[$value]))
					return Input::ERROR_INVALID;
			}, [
				Input::IS_OPTIONAL => true,
				Input::CUSTOM_ERROR_MESSAGES => [
					Input::ERROR_INVALID => 'Request type (@value) is invalid'
				]
			]))->out();
			if ($type === null && !$editing)
				Response::fail('Missing request type');

			if (!$editing || (isset($type) && $type !== $post->type))
				CoreUtils::set($target,'type',$type);

			if (Permission::sufficient('developer')){
				$reserved_at = self::validateReservedAt();
				if (isset($reserved_at)){
					if ($reserved_at !== strtotime($post->reserved_at))
						CoreUtils::set($target,'reserved_at',date('c', $reserved_at));
				}
				else CoreUtils::set($target,'reserved_at',null);
			}
		}

		if (Permission::sufficient('developer')){
			$posted_at = self::validatePostedAt();
			if (isset($posted_at) && ($post === null || $posted_at !== strtotime($post->posted_at)))
				CoreUtils::set($target,'posted_at',date('c', $posted_at));

			$finished_at = self::validateFinishedAt();
			if (isset($finished_at)){
				if ($post !== null && $finished_at !== strtotime($post->finished_at))
					CoreUtils::set($target,'finished_at',date('c', $finished_at));
			}
			else CoreUtils::set($target,'finished_at',null);
		}
	}

	/**
	 * Check image URL in POST request
	 *
	 * @param string    $image_url
	 * @param Post|null $Post      Existing post for comparison
	 *
	 * @return ImageProvider<commit_msg>Fix request posted_at timestamp not being used when set<commit_after><?php

namespace App;

use App\Models\Show;
use App\Models\Notification;
use App\Models\PCGSlotHistory;
use App\Models\Post;
use App\Models\User;
use App\Exceptions\MismatchedProviderException;

class Posts {

	/**
	 * Retrieves requests & reservations for the episode specified
	 * Optionally lists broken posts
	 *
	 * @param int  $show_id
	 * @param int  $only
	 * @param bool $showBroken
	 *
	 * @return Post[]|Post[][]
	 */
	public static function get(int $show_id, int $only = null, bool $showBroken = false){
		$return = [];
		if ($only !== ONLY_RESERVATIONS){
			// If we don't want reservations only, grab requests
			$return[] = Post::find('all', [
				'conditions'=> [
					'requested_by IS NOT NULL AND show_id = ?'.($showBroken === false?' AND broken IS NOT true':''),
					$show_id
				],
				'order' => 'finished_at asc, requested_at asc',
			]);
		}
		if ($only !== ONLY_REQUESTS){
			// If we don't want requests only, grab reservations
			$return[] = Post::find('all', [
				'conditions'=> [
					'requested_by IS NULL AND show_id = ?'.($showBroken === false?' AND broken IS NOT true':''),
					$show_id
				],
				'order' => 'finished_at asc, reserved_at asc',
			]);
		}

		return $only ? $return[0] : $return;
	}

	/**
	 * @return Post[]
	 */
	public static function getRecentPosts():array {
		return DB::$instance->orderByLiteral(Post::ORDER_BY_POSTED_AT, 'DESC')->get('posts', 20);
	}

	/**
	 * Get list of most recent posts
	 *
	 * @param bool $wrap
	 *
	 * @return string
	 */
	public static function getMostRecentList($wrap = WRAP):string {
		$recent_posts = self::getRecentPosts();

		return Twig::$env->render('admin/_most_recent_posts.html.twig', ['recent_posts' => $recent_posts, 'wrap' => $wrap ]);
	}

	/**
	 * POST data validator function used when creating/editing posts
	 *
	 * @param bool         $request Boolean that's true if post is a request and false otherwise
	 * @param array|object $target  Array or object to output the checked data into
	 * @param Post|null    $post    Optional, exsting post to compare new data against
	 */
	public static function checkPostDetails(bool $request, &$target, $post = null){
		$editing = !empty($post);

		$label = (new Input('label','string', [
			Input::IS_OPTIONAL => true,
			Input::IN_RANGE => [3,255],
			Input::CUSTOM_ERROR_MESSAGES => [
				Input::ERROR_RANGE => 'The description must be between @min and @max characters'
			]
		]))->out();
		if ($label !== null){
			if (!$editing || $label !== $post->label){
				CoreUtils::checkStringValidity($label,'The description',INVERSE_PRINTABLE_ASCII_PATTERN);
				$label = preg_replace(new RegExp("''"),'"',$label);
				CoreUtils::set($target, 'label', $label);
			}
		}
		else if (!$editing && $request)
			Response::fail('Description cannot be empty');
		else CoreUtils::set($target, 'label', null);

		if ($request){
			$type = (new Input('type',function($value){
				if (!isset(Post::REQUEST_TYPES[$value]))
					return Input::ERROR_INVALID;
			}, [
				Input::IS_OPTIONAL => true,
				Input::CUSTOM_ERROR_MESSAGES => [
					Input::ERROR_INVALID => 'Request type (@value) is invalid'
				]
			]))->out();
			if ($type === null && !$editing)
				Response::fail('Missing request type');

			if (!$editing || (isset($type) && $type !== $post->type))
				CoreUtils::set($target,'type',$type);

			if (Permission::sufficient('developer')){
				$reserved_at = self::validateReservedAt();
				if (isset($reserved_at)){
					if ($reserved_at !== strtotime($post->reserved_at))
						CoreUtils::set($target,'reserved_at',date('c', $reserved_at));
				}
				else CoreUtils::set($target,'reserved_at',null);
			}
		}

		if (Permission::sufficient('developer')){
			$posted_at = self::validatePostedAt();
			if (isset($posted_at) && ($post === null || $posted_at !== strtotime($post->posted_at))){
				$posted_at_column = $request ? 'requested_at' : 'reserved_at';
				CoreUtils::set($target,$posted_at_column,date('c', $posted_at));
			}

			$finished_at = self::validateFinishedAt();
			if (isset($finished_at)){
				if ($post !== null && $finished_at !== strtotime($post->finished_at))
					CoreUtils::set($target,'finished_at',date('c', $finished_at));
			}
			else CoreUtils::set($target,'finished_at',null);
		}
	}

	/**
	 * Check image URL in POST request
	 *
	 * @param string    $image_url
	 * @param Post|null $Post      Existing post for comparison
	 *
	 * @return ImageProvider
{'fraction_non_alphanumeric': 0.17154811715481172, 'fraction_numerical': 0.0015831731312902862, 'mean_word_length': 4.025, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, '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': '6761602', 'n_tokens_mistral': 3310, 'n_tokens_neox': 2794, 'n_words': 699}