<commit_before><?php

/**
 * Integration system for drafts into Post controller
 *
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * This file contains code covered by:
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
 *
 * @version 1.1 beta 1
 *
 */

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

class Drafts_Post_Module implements ElkArte\sources\modules\Module_Interface
{
	protected static $_autosave_enabled = false;
	protected static $_autosave_frequency = 30000;
	protected static $_subject_length = 24;
	protected static $_eventsManager = null;

	protected $_loaded_drafts = array();
	protected $_current_draft = array();

	/**
	 * {@inheritdoc }
	 */
	public static function hooks(\Event_Manager $eventsManager)
	{
		global $modSettings;

		$return = array();
		if (!empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_post_enabled']))
		{
			self::$_eventsManager = $eventsManager;

			self::$_autosave_enabled = !empty($modSettings['drafts_autosave_enabled']);

			if (!empty($modSettings['drafts_autosave_frequency']))
				self::$_autosave_frequency = (int) $modSettings['drafts_autosave_frequency'] * 1000;

			if (!empty($modSettings['draft_subject_length']))
				self::$_subject_length = (int) $modSettings['draft_subject_length'];

			$return = array(
				array('prepare_modifying', array('Drafts_Post_Module', 'prepare_modifying'), array('really_previewing')),
				array('finalize_post_form', array('Drafts_Post_Module', 'finalize_post_form'), array('editorOptions', 'board', 'topic', 'template_layers')),
			);
			if (isset($_REQUEST['save_draft']))
			{
				$return = array_merge($return, array(
					array('prepare_save_post', array('Drafts_Post_Module', 'prepare_save_post'), array()),
					array('before_save_post', array('Drafts_Post_Module', 'before_save_post'), array()),
					array('after_save_post', array('Drafts_Post_Module', 'after_save_post'), array('msgOptions')),
				));
			}
		}

		return $return;
	}

	public function prepare_modifying(&$really_previewing)
	{
		global $context;

		if (!empty($_REQUEST['id_draft']))
			$context['attach_source'] = 1;

		$really_previewing = $really_previewing && !isset($_REQUEST['save_draft']);
	}

	public function finalize_post_form(&$editorOptions, $board, $topic, $template_layers)
	{
		global $context, $user_info, $options, $txt;

		// Are post drafts enabled?
		$context['drafts_save'] = allowedTo('post_draft');
		$context['drafts_autosave'] = $context['drafts_save'] && self::$_autosave_enabled && allowedTo('post_autosave_draft');

		// Build a list of drafts that they can load into the editor
		if (!empty($context['drafts_save']))
		{
			loadLanguage('Drafts');
			if (!empty($_REQUEST['id_draft']))
			{
				$id_draft = (int) $_REQUEST['id_draft'];
				$context['id_draft'] = $id_draft;
			}

			if (!empty($context['drafts_autosave']) && !empty($options['drafts_autosave_enabled']))
			{
				if (!isset($editorOptions['plugin_addons']))
					$editorOptions['plugin_addons'] = array();
				if (!isset($editorOptions['plugin_options']))
					$editorOptions['plugin_options'] = array();

				// @todo remove
				$context['drafts_autosave_frequency'] = self::$_autosave_frequency;

				$editorOptions['plugin_addons'][] = 'draft';
				$editorOptions['plugin_options'][] = '
					draftOptions: {
						sLastNote: \'draft_lastautosave\',
						sSceditorID: \'' . $editorOptions['id'] . '\',
						sType: \'post\',
						iBoard: ' . (empty($board) ? 0 : $board) . ',
						iFreq: ' . self::$_autosave_frequency . ',
						sLastID: \'id_draft\',
						sTextareaID: \'' . $editorOptions['id'] . '\',
						id_draft: ' . (empty($context['id_draft']) ? 0 : $context['id_draft']) . '
					}';

				loadJavascriptFile('drafts.plugin.js', array('defer' => true));
			}
			$context['shortcuts_text'] = $txt['shortcuts_drafts' . (isBrowser('is_firefox') ? '_firefox' : '')];

			if (!isset($editorOptions['buttons']))
				$editorOptions['buttons'] = array();

			$editorOptions['buttons'][] = array(
				'name' => 'save_draft',
				'value' => $txt['draft_save'],
				'options' => 'onclick="return confirm(' . JavaScriptEscape($txt['draft_save_note']) . ') && submitThisOnce(this);" accesskey="d"',
			);

			if (!empty($id_draft))
			{
				$this->_prepareDraftsContext($user_info['id'], $topic);
				self::$_eventsManager->trigger('after_loading_drafts', array('loaded_drafts' => &$this->_loaded_drafts, 'current_draft' => &$this->_current_draft));

				$context['message'] = $editorOptions['value'] = $this->_current_draft['body'];
				$context['subject'] = addcslashes($this->_current_draft['subject'], '"');
			}

			if (!empty($context['drafts']))
				$template_layers->add('load_drafts', 100);
		}
	}

	public function prepare_save_post()
	{
		// Drafts enabled and needed?
		if (isset($_POST['save_draft']) || isset($_POST['id_draft']))
			require_once(SUBSDIR . '/Drafts.subs.php');
	}

	public function before_save_post()
	{
		global $context, $modSettings, $board, $user_info;

		// If drafts are enabled, then pass this off
		if (isset($_POST['save_draft']))
		{
			// Ajax calling
			if (!isset($context['drafts_save']))
				$context['drafts_save'] = !empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_post_enabled']) && allowedTo('post_draft');

			// Can you be, should you be ... here?
			if (!empty($context['drafts_save']) && isset($_POST['id_draft']))
			{
				// Prepare and clean the data, load the draft array
				$draft = array(
					'id_draft' => empty($_POST['id_draft']) ? 0 : (int) $_POST['id_draft'],
					'topic_id' => empty($_REQUEST['topic']) ? 0 : (int) $_REQUEST['topic'],
					'board' => $board,
					'icon' => empty($_POST['icon']) ? 'xx' : preg_replace('~[\./\\\\*:"\'<>]~', '', $_POST['icon']),<commit_msg>id_draft is not set for not yet saved drafts<commit_after><?php

/**
 * Integration system for drafts into Post controller
 *
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * This file contains code covered by:
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
 * license:  	BSD, See included LICENSE.TXT for terms and conditions.
 *
 * @version 1.1 beta 1
 *
 */

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

class Drafts_Post_Module implements ElkArte\sources\modules\Module_Interface
{
	protected static $_autosave_enabled = false;
	protected static $_autosave_frequency = 30000;
	protected static $_subject_length = 24;
	protected static $_eventsManager = null;

	protected $_loaded_drafts = array();
	protected $_current_draft = array();

	/**
	 * {@inheritdoc }
	 */
	public static function hooks(\Event_Manager $eventsManager)
	{
		global $modSettings;

		$return = array();
		if (!empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_post_enabled']))
		{
			self::$_eventsManager = $eventsManager;

			self::$_autosave_enabled = !empty($modSettings['drafts_autosave_enabled']);

			if (!empty($modSettings['drafts_autosave_frequency']))
				self::$_autosave_frequency = (int) $modSettings['drafts_autosave_frequency'] * 1000;

			if (!empty($modSettings['draft_subject_length']))
				self::$_subject_length = (int) $modSettings['draft_subject_length'];

			$return = array(
				array('prepare_modifying', array('Drafts_Post_Module', 'prepare_modifying'), array('really_previewing')),
				array('finalize_post_form', array('Drafts_Post_Module', 'finalize_post_form'), array('editorOptions', 'board', 'topic', 'template_layers')),
			);
			if (isset($_REQUEST['save_draft']))
			{
				$return = array_merge($return, array(
					array('prepare_save_post', array('Drafts_Post_Module', 'prepare_save_post'), array()),
					array('before_save_post', array('Drafts_Post_Module', 'before_save_post'), array()),
					array('after_save_post', array('Drafts_Post_Module', 'after_save_post'), array('msgOptions')),
				));
			}
		}

		return $return;
	}

	public function prepare_modifying(&$really_previewing)
	{
		global $context;

		if (!empty($_REQUEST['id_draft']))
			$context['attach_source'] = 1;

		$really_previewing = $really_previewing && !isset($_REQUEST['save_draft']);
	}

	public function finalize_post_form(&$editorOptions, $board, $topic, $template_layers)
	{
		global $context, $user_info, $options, $txt;

		// Are post drafts enabled?
		$context['drafts_save'] = allowedTo('post_draft');
		$context['drafts_autosave'] = $context['drafts_save'] && self::$_autosave_enabled && allowedTo('post_autosave_draft');

		// Build a list of drafts that they can load into the editor
		if (!empty($context['drafts_save']))
		{
			loadLanguage('Drafts');
			if (!empty($_REQUEST['id_draft']))
			{
				$id_draft = (int) $_REQUEST['id_draft'];
				$context['id_draft'] = $id_draft;
			}

			if (!empty($context['drafts_autosave']) && !empty($options['drafts_autosave_enabled']))
			{
				if (!isset($editorOptions['plugin_addons']))
					$editorOptions['plugin_addons'] = array();
				if (!isset($editorOptions['plugin_options']))
					$editorOptions['plugin_options'] = array();

				// @todo remove
				$context['drafts_autosave_frequency'] = self::$_autosave_frequency;

				$editorOptions['plugin_addons'][] = 'draft';
				$editorOptions['plugin_options'][] = '
					draftOptions: {
						sLastNote: \'draft_lastautosave\',
						sSceditorID: \'' . $editorOptions['id'] . '\',
						sType: \'post\',
						iBoard: ' . (empty($board) ? 0 : $board) . ',
						iFreq: ' . self::$_autosave_frequency . ',
						sLastID: \'id_draft\',
						sTextareaID: \'' . $editorOptions['id'] . '\',
						id_draft: ' . (empty($context['id_draft']) ? 0 : $context['id_draft']) . '
					}';

				loadJavascriptFile('drafts.plugin.js', array('defer' => true));
			}
			$context['shortcuts_text'] = $txt['shortcuts_drafts' . (isBrowser('is_firefox') ? '_firefox' : '')];

			if (!isset($editorOptions['buttons']))
				$editorOptions['buttons'] = array();

			$editorOptions['buttons'][] = array(
				'name' => 'save_draft',
				'value' => $txt['draft_save'],
				'options' => 'onclick="return confirm(' . JavaScriptEscape($txt['draft_save_note']) . ') && submitThisOnce(this);" accesskey="d"',
			);

			if (!empty($id_draft))
			{
				$this->_prepareDraftsContext($user_info['id'], $topic);
				self::$_eventsManager->trigger('after_loading_drafts', array('loaded_drafts' => &$this->_loaded_drafts, 'current_draft' => &$this->_current_draft));

				$context['message'] = $editorOptions['value'] = $this->_current_draft['body'];
				$context['subject'] = addcslashes($this->_current_draft['subject'], '"');
			}

			if (!empty($context['drafts']))
				$template_layers->add('load_drafts', 100);
		}
	}

	public function prepare_save_post()
	{
		// Drafts enabled and needed?
		if (isset($_POST['save_draft']) || isset($_POST['id_draft']))
			require_once(SUBSDIR . '/Drafts.subs.php');
	}

	public function before_save_post()
	{
		global $context, $modSettings, $board, $user_info;

		// If drafts are enabled, then pass this off
		if (isset($_POST['save_draft']))
		{
			// Ajax calling
			if (!isset($context['drafts_save']))
				$context['drafts_save'] = !empty($modSettings['drafts_enabled']) && !empty($modSettings['drafts_post_enabled']) && allowedTo('post_draft');

			// Can you be, should you be ... here?
			if (!empty($context['drafts_save']))
			{
				// Prepare and clean the data, load the draft array
				$draft = array(
					'id_draft' => empty($_POST['id_draft']) ? 0 : (int) $_POST['id_draft'],
					'topic_id' => empty($_REQUEST['topic']) ? 0 : (int) $_REQUEST['topic'],
					'board' => $board,
					'icon' => empty($_POST['icon']) ? 'xx' : preg_replace('~[\./\\\\*:"\'<>]~', '', $_POST['icon']),
{'fraction_non_alphanumeric': 0.18667679837892603, 'fraction_numerical': 0.004559270516717325, 'mean_word_length': 4.855165595650025, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '928573', 'n_tokens_mistral': 4477, 'n_tokens_neox': 3830, 'n_words': 692}