<commit_before><?php

/**
 * Allow for the saving, retrieving, deleting and settings for the drafts
 *
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * @version 1.0 Beta 2
 *
 */

if (!defined('ELK'))
	die('No access...');

/**
 * Draft controller.
 * This class handles requests that allow for the saving,
 * retrieving, deleting and settings for the drafts functionality.
 */
class Draft_Controller extends Action_Controller
{
	/**
	 * Default method, just forwards, if we ever get here.
	 *
	 * @see Action_Controller::action_index()
	 */
	public function action_index()
	{
		// Where do you want to go today? :P
		$this->action_showProfileDrafts();
	}

	/**
	 * This method is executed before any action handler.
	 * Loads language, common needed stuffs.
	 */
	public function pre_dispatch()
	{
		// Language and helper functions
		loadLanguage('Drafts');
		require_once(SUBSDIR . '/Drafts.subs.php');
	}

	/**
	 * Show all drafts of a given type by the current user
	 * Uses the showdraft template
	 * Allows for the deleting and loading/editing of drafts
	 */
	public function action_showProfileDrafts()
	{
		global $txt, $scripturl, $modSettings, $context, $user_info;

		$memID = currentMemberID();

		// Safe is safe.
		if ($memID != $user_info['id'])
			fatal_lang_error('no_access', false);

		require_once(SUBSDIR . '/Drafts.subs.php');

		// Some initial context.
		$context['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
		$context['current_member'] = $memID;

		// If just deleting a draft, do it and then redirect back.
		if (!empty($_REQUEST['delete']))
		{
			checkSession('get');

			$id_delete = (int) $_REQUEST['delete'];
			deleteDrafts($id_delete, $memID);
			redirectexit('action=profile;u=' . $memID . ';area=showdrafts;start=' . $context['start']);
		}

		// Default to 10.
		if (empty($_REQUEST['viewscount']) || !is_numeric($_REQUEST['viewscount']))
			$_REQUEST['viewscount'] = 10;

		// Get things started
		$msgCount = draftsCount($memID, 0);
		$maxIndex = (int) $modSettings['defaultMaxMessages'];

		// Make sure the starting place makes sense and construct our friend the page index.
		$context['page_index'] = constructPageIndex($scripturl . '?action=profile;u=' . $memID . ';area=showdrafts', $context['start'], $msgCount, $maxIndex);
		$context['current_page'] = $context['start'] / $maxIndex;

		// Reverse the query if we're past 50% of the pages for better performance.
		$start = $context['start'];
		$reverse = $start > $msgCount / 2;
		if ($reverse)
		{
			$maxIndex = $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] + 1 && $msgCount > $context['start'] ? $msgCount - $context['start'] : (int) $modSettings['defaultMaxMessages'];
			$start = $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] + 1 || $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] ? 0 : $msgCount - $context['start'] - $modSettings['defaultMaxMessages'];
		}

		// Find this user's drafts
		$limit = $start . ', ' . $maxIndex;
		$order = 'ud.id_draft ' . ($reverse ? 'ASC' : 'DESC');
		$user_drafts = load_user_drafts($memID, 0, false, $order, $limit);

		// Start counting at the number of the first message displayed.
		$counter = $reverse ? $context['start'] + $maxIndex + 1 : $context['start'];
		$context['posts'] = array();
		foreach ($user_drafts as $row)
		{
			// Censor....
			if (empty($row['body']))
				$row['body'] = '';

			$row['subject'] = Util::htmltrim($row['subject']);
			if (empty($row['subject']))
				$row['subject'] = $txt['drafts_none'];

			censorText($row['body']);
			censorText($row['subject']);

			// BBC-ilize the message.
			$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], 'draft' . $row['id_draft']);

			// And the array...
			$context['drafts'][$counter += $reverse ? -1 : 1] = array(
				'body' => $row['body'],
				'counter' => $counter,
				'alternate' => $counter % 2,
				'board' => array(
					'name' => $row['bname'],
					'id' => $row['id_board'],
					'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['bname'] . '</a>',
				),
				'topic' => array(
					'id' => $row['id_topic'],
					'link' => empty($row['id_topic']) ? $row['subject'] : '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
				),
				'subject' => $row['subject'],
				'time' => standardTime($row['poster_time']),
				'html_time' => htmlTime($row['poster_time']),
				'timestamp' => forum_time(true, $row['poster_time']),
				'icon' => $row['icon'],
				'id_draft' => $row['id_draft'],
				'locked' => $row['locked'],
				'sticky' => $row['is_sticky'],
				'age' => floor((time() - $row['poster_time']) / 86400),
				'remaining' => (!empty($modSettings['drafts_keep_days']) ? round($modSettings['drafts_keep_days'] - ((time() - $row['poster_time']) / 86400)) : 0),
				'buttons' => array(
					'remove' => array(
						'href' => $scripturl . '?action=profile;u=' . $context['member']['id'] . ';area=showdrafts;delete=' . $row['id_draft'] . ';start=' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id'],
						'text' => $txt['draft_delete'],
						'custom' => 'onclick="return confirm(' . JavaScriptEscape($txt['draft_remove'] . '?') . ');"',
					),
					'reply' => array(
						'href' => $scripturl . '?action=post;' . (empty($row['id_topic']) ? 'board=' . $row['id_board'] : 'topic=' . $row['id_topic']) . '.0;id_draft=' . $row['id_draft'],
						'text' => $txt['draft_edit'],
					),
				)
			);
		}

		// If the drafts were retrieved in reverse order, get them right again.
		if ($reverse)
			$context['drafts'] = array_reverse($context['drafts'], true);

		// Menu tab
		$context[$context['profile_menu_name']]['tab_data'] = array(
			'title' => $txt['drafts_show'],
			'class' => 'talk',
			'description' => $txt['drafts_show_desc'],
		);

		$context['sub_template'] = 'showDrafts';
	}

	/**
	 * Show all PM drafts of the current user
	 * Uses the showpmdraft template
	 * Allows for the deleting and loading/editing of PM drafts
	 */
	public function action_showPMDrafts()
	{
		global $txt, $user_info, $scripturl, $modSettings, $context;

		require_once(SUBSDIR . '/Profile.subs.php');<commit_msg>Wrong index for a button<commit_after><?php

/**
 * Allow for the saving, retrieving, deleting and settings for the drafts
 *
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * @version 1.0 Beta 2
 *
 */

if (!defined('ELK'))
	die('No access...');

/**
 * Draft controller.
 * This class handles requests that allow for the saving,
 * retrieving, deleting and settings for the drafts functionality.
 */
class Draft_Controller extends Action_Controller
{
	/**
	 * Default method, just forwards, if we ever get here.
	 *
	 * @see Action_Controller::action_index()
	 */
	public function action_index()
	{
		// Where do you want to go today? :P
		$this->action_showProfileDrafts();
	}

	/**
	 * This method is executed before any action handler.
	 * Loads language, common needed stuffs.
	 */
	public function pre_dispatch()
	{
		// Language and helper functions
		loadLanguage('Drafts');
		require_once(SUBSDIR . '/Drafts.subs.php');
	}

	/**
	 * Show all drafts of a given type by the current user
	 * Uses the showdraft template
	 * Allows for the deleting and loading/editing of drafts
	 */
	public function action_showProfileDrafts()
	{
		global $txt, $scripturl, $modSettings, $context, $user_info;

		$memID = currentMemberID();

		// Safe is safe.
		if ($memID != $user_info['id'])
			fatal_lang_error('no_access', false);

		require_once(SUBSDIR . '/Drafts.subs.php');

		// Some initial context.
		$context['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
		$context['current_member'] = $memID;

		// If just deleting a draft, do it and then redirect back.
		if (!empty($_REQUEST['delete']))
		{
			checkSession('get');

			$id_delete = (int) $_REQUEST['delete'];
			deleteDrafts($id_delete, $memID);
			redirectexit('action=profile;u=' . $memID . ';area=showdrafts;start=' . $context['start']);
		}

		// Default to 10.
		if (empty($_REQUEST['viewscount']) || !is_numeric($_REQUEST['viewscount']))
			$_REQUEST['viewscount'] = 10;

		// Get things started
		$msgCount = draftsCount($memID, 0);
		$maxIndex = (int) $modSettings['defaultMaxMessages'];

		// Make sure the starting place makes sense and construct our friend the page index.
		$context['page_index'] = constructPageIndex($scripturl . '?action=profile;u=' . $memID . ';area=showdrafts', $context['start'], $msgCount, $maxIndex);
		$context['current_page'] = $context['start'] / $maxIndex;

		// Reverse the query if we're past 50% of the pages for better performance.
		$start = $context['start'];
		$reverse = $start > $msgCount / 2;
		if ($reverse)
		{
			$maxIndex = $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] + 1 && $msgCount > $context['start'] ? $msgCount - $context['start'] : (int) $modSettings['defaultMaxMessages'];
			$start = $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] + 1 || $msgCount < $context['start'] + $modSettings['defaultMaxMessages'] ? 0 : $msgCount - $context['start'] - $modSettings['defaultMaxMessages'];
		}

		// Find this user's drafts
		$limit = $start . ', ' . $maxIndex;
		$order = 'ud.id_draft ' . ($reverse ? 'ASC' : 'DESC');
		$user_drafts = load_user_drafts($memID, 0, false, $order, $limit);

		// Start counting at the number of the first message displayed.
		$counter = $reverse ? $context['start'] + $maxIndex + 1 : $context['start'];
		$context['posts'] = array();
		foreach ($user_drafts as $row)
		{
			// Censor....
			if (empty($row['body']))
				$row['body'] = '';

			$row['subject'] = Util::htmltrim($row['subject']);
			if (empty($row['subject']))
				$row['subject'] = $txt['drafts_none'];

			censorText($row['body']);
			censorText($row['subject']);

			// BBC-ilize the message.
			$row['body'] = parse_bbc($row['body'], $row['smileys_enabled'], 'draft' . $row['id_draft']);

			// And the array...
			$context['drafts'][$counter += $reverse ? -1 : 1] = array(
				'body' => $row['body'],
				'counter' => $counter,
				'alternate' => $counter % 2,
				'board' => array(
					'name' => $row['bname'],
					'id' => $row['id_board'],
					'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['bname'] . '</a>',
				),
				'topic' => array(
					'id' => $row['id_topic'],
					'link' => empty($row['id_topic']) ? $row['subject'] : '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
				),
				'subject' => $row['subject'],
				'time' => standardTime($row['poster_time']),
				'html_time' => htmlTime($row['poster_time']),
				'timestamp' => forum_time(true, $row['poster_time']),
				'icon' => $row['icon'],
				'id_draft' => $row['id_draft'],
				'locked' => $row['locked'],
				'sticky' => $row['is_sticky'],
				'age' => floor((time() - $row['poster_time']) / 86400),
				'remaining' => (!empty($modSettings['drafts_keep_days']) ? round($modSettings['drafts_keep_days'] - ((time() - $row['poster_time']) / 86400)) : 0),
				'buttons' => array(
					'remove' => array(
						'href' => $scripturl . '?action=profile;u=' . $context['member']['id'] . ';area=showdrafts;delete=' . $row['id_draft'] . ';start=' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id'],
						'text' => $txt['draft_delete'],
						'custom' => 'onclick="return confirm(' . JavaScriptEscape($txt['draft_remove'] . '?') . ');"',
					),
					'edit' => array(
						'href' => $scripturl . '?action=post;' . (empty($row['id_topic']) ? 'board=' . $row['id_board'] : 'topic=' . $row['id_topic']) . '.0;id_draft=' . $row['id_draft'],
						'text' => $txt['draft_edit'],
					),
				)
			);
		}

		// If the drafts were retrieved in reverse order, get them right again.
		if ($reverse)
			$context['drafts'] = array_reverse($context['drafts'], true);

		// Menu tab
		$context[$context['profile_menu_name']]['tab_data'] = array(
			'title' => $txt['drafts_show'],
			'class' => 'talk',
			'description' => $txt['drafts_show_desc'],
		);

		$context['sub_template'] = 'showDrafts';
	}

	/**
	 * Show all PM drafts of the current user
	 * Uses the showpmdraft template
	 * Allows for the deleting and loading/editing of PM drafts
	 */
	public function action_showPMDrafts()
	{
		global $txt, $user_info, $scripturl, $modSettings, $context;

		require_once(SUBSDIR . '/Profile.subs.php');
{'fraction_non_alphanumeric': 0.20528648306426084, 'fraction_numerical': 0.005539727761949984, 'mean_word_length': 3.915208090237262, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 79, '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': '355663', 'n_tokens_mistral': 4814, 'n_tokens_neox': 4207, 'n_words': 983}