<?xml version="1.0"?>
<!DOCTYPE modification SYSTEM "http://www.simplemachines.org/xml/modification">
<modification xmlns="http://www.simplemachines.org/xml/modification" xmlns:smf="http://www.simplemachines.org/">
	<id>yodaofdarkness:tagging</id>
	<version>1.0.2</version>

<file name="$sourcedir/Post.php">
	<operation>
		<search position="replace"><![CDATA[
	$context['tags'] = '';]]></search>
		<add><![CDATA[
	$context['tags'] = array();]]></add>
	</operation>
	
	<operation>
		<search position="replace"><![CDATA[
		$context['tags'] = isset($_REQUEST['tags']) ? preg_replace("([?!\"':;./\\|&%$#<>*\)\(\[\]{}+=@~`_]+)", "", strtolower($_REQUEST['tags'])) : '';]]></search>
		<add><![CDATA[

		$ids = 0;
		$raw_tags = array();

		// Was it by ajax?
		if(isset($_POST['ajax_tags']))
			foreach($_POST['ajax_tags'] as $tag)
				$raw_tags[] = $tag;
		else if(isset($_POST['tags']))
			$raw_tags = explode(',', $_REQUEST['tags']);

		if (!empty($raw_tags)){
			foreach($raw_tags as $tag){
				$tag = preg_replace("([?!\"':;./\\|&%$#<>*\)\(\[\]{}+=@~`_]+)", "", strtolower($tag));

				$context['tags'][] = array(
					'name' => $tag,
					'last' => false
				);
			}

			if(!empty($context['tags']))
				$context['tags'][count($context['tags'])-1]['last'] = true;
		}]]></add>
	</operation>
	
	<operation>
		<search position="replace"><![CDATA[
		while($tag_row = $smcFunc['db_fetch_assoc']($result)){
			$context['tags'][] = $tag_row['tag_name'];
		}]]></search>
		<add><![CDATA[
		while($tag_row = $smcFunc['db_fetch_assoc']($result)){
			$context['tags'][] = array(
				'name' => $tag_row['tag_name'],
				'last' => false,
			);
		}

		if(!empty($context['tags']))
			$context['tags'][count($context['tags'])-1]['last'] = true;]]></add>
	</operation>

	<operation>
		<search position="replace"><![CDATA[
	if(isset($_REQUEST['tags'])){
		$raw_tags = explode(',', $_REQUEST['tags']);]]></search>
		<add><![CDATA[
	if(isset($_REQUEST['tags']) || isset($_POST['ajax_tags'])){

		// Was it by ajax?
		if(isset($_POST['ajax_tags']))
			foreach($_POST['ajax_tags'] as $tag)
				$raw_tags[] = $tag;
		else
			$raw_tags = explode(',', $_REQUEST['tags']);]]></add>
	</operation>
</file>

<file name="$sourcedir/Subs-Editor.php">
	<operation>
		<search position="before"><![CDATA[
		'member' => 'Member',]]></search>
		<add><![CDATA[
		'tag' => 'Tag',]]></add>
	</operation>

	<operation>
		<search position="end" />
		<add><![CDATA[

// I heard you like tags, so I put some tags in your tags!
function AutoSuggest_Search_Tag()
{
	global $user_info, $txt, $smcFunc;

	$original = $_REQUEST['search'];
	$ids = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : 0;
	$_REQUEST['search'] = trim($smcFunc['strtolower']($_REQUEST['search'])) . '*';
	$_REQUEST['search'] = strtr($_REQUEST['search'], array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_', '&#038;' => '&amp;'));

	// Find the tag.
	$request = $smcFunc['db_query']('', '
		SELECT tag_name
		FROM {db_prefix}tags
		WHERE tag_name LIKE {string:search}
		LIMIT ' . (strlen($_REQUEST['search']) <= 2 ? '100' : '800'),
		array(
			'search' => $_REQUEST['search'],
		)
	);
	$xml_data = array(
		'items' => array(
			'identifier' => 'item',
			'children' => array(),
		),
	);
	while ($row = $smcFunc['db_fetch_assoc']($request))
	{
		$row['tag_name'] = strtr($row['tag_name'], array('&amp;' => '&#038;', '&lt;' => '&#060;', '&gt;' => '&#062;', '&quot;' => '&#034;'));

		$listed[$row['tag_name']] = true;

		$xml_data['items']['children'][] = array(
			'attributes' => array(
				'id' => $ids++,
			),
			'value' => $row['tag_name'],
		);
	}
	$smcFunc['db_free_result']($request);

	// Always show their original request first, if it's not already there... they need to be able to add new ones!
	if(!isset($listed[$original]))
		array_unshift($xml_data['items']['children'],
			array(
				'attributes' => array(
					'id' => $ids++,
				),
				'value' => $original,
			)
		);

	return $xml_data;
}]]></add>
	</operation>
</file>

<file name="$themedir/Post.template.php">
	<operation>
		<search position="replace"><![CDATA[';

	// Only show this when it's the first post.
	if($context['is_first_post'])
		echo '
							<tr>
								<td align="right" style="font-weight: bold;">
									', $txt['tags'], ':
								</td>
								<td>
									<input type="text" name="tags"', empty($context['tags']) ? '' : ' value="' . implode(', ', $context['tags']) . '"', ' tabindex="', $context['tabindex']++, '" size="80" maxlength="80" /><br />
									<span class="smalltext">', $txt['tags_instructions'], '</span>
								</td>
							</tr>';

	echo ']]></search>
		<add><![CDATA[';

	// Only show this when it's the first post.
	if($context['is_first_post']){
		echo '
							<tr>
								<td align="right" style="font-weight: bold;">
									', $txt['tags'], ':
								</td>
								<td>
									<input type="text" name="tags" id="tags" tabindex="', $context['tabindex']++, '" size="80" /><br />
									<div id="tag_list_container"></div>
									<span id="tags_instructions" class="smalltext">', $txt['tags_instructions'], '</span>
									<script language="JavaScript" type="text/javascript" src="', $settings['default_theme_url'], '/scripts/tagging.js?rc1"></script>
									<script language="JavaScript" type="text/javascript"><!-- // -->
										var oTagSend = new smf_TagSend({
											sSelf: \'oTagSend\',
											sSessionId: \'', $context['session_id'], '\',
											sTextDeleteItem: \'', $txt['autosuggest_delete_item'], '\',
											sToControlId: \'tags\',
											aPresetTags: [';

		$tc = 0;
		foreach ($context['tags'] as $tag)
			echo '
												{
													sItemId: ', $tc++, ',
													sItemName: ', JavaScriptEscape($tag['name']), '
												}', $tag['last'] ? '' : ',';

	echo '
											],
										});
									// </script>
								</td>
							</tr>';
	}

	echo ']]></add>
	</operation>
</file>

</modification>
