MyBB Hacks

Full Version: turn username into a profile link
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello!

I've searched for quite a while but didn't find anything that fits.
I'm using xthreads for different thread descriptions. [My forum is used for a rpg and the fields are for date, time, location of the scene.] Now I would like to add another field for the character names of the attending characters/users. Is there a way the user can put two and more usernames into a field (only one field, the names most likely seperated by comma or something) and those names turn into a normal link that redirects to the profile like with any other username in the forum?
Thanks in advance.
Do you mean by the usernames/character names are the forum users username?
Yes! Each character has its own account.
XThreads itself supports multiple values (comma separated for textbox and new line for textarea).
The problem is validating whether the username(s) entered by the user is a valid username or not, including whether they are duplicate or not (for example: User 1, User 2, User 1).
If you store the username inside the custom field, it won't be updated automatically if the user decides to change the usernames (character names).
So, you need an additional plugin for that.
The alternative way is store the uid(s) rather than the username(s). But still, you still need the validation and an additional plugin to load the usernames based on the uid to turn them into a profile link.
Hm. Okay, so there would be a lot of additional work. Since I have only little knowledge when it comes to coding, I'll stick with a field only for the names without turning them into a link. But thank you for your thoughts about it!
To make it a bit "better", you can try using the select2.js included by MyBB.
Is that the script used in PMs to find a username? If so, how could I use it?
[Sorry, I'm really just a beginner. Frown]
Yep. The script used when sending PM.
The js could be found inside private_send_autocomplete template.
You need to modify it (especially for the ID of the input), and add an ID to your custom field.

Here is a simple example for that (assuming the field is a textbox):
Edit the field:
Allow multiple values for this field: Yes
Use Custom Input HTML: Yes
Input Field HTML:

HTML Code
<input type="text" id="xtun" class="textbox"{NAME_PROP}{MAXLEN_PROP}{WIDTH_PROP_SIZE}{TABINDEX_PROP}{REQUIRED_PROP} value="{VALUE}" style="min-width: 300px;" />
<link rel="stylesheet" href="{$mybb->asset_url}/jscripts/select2/select2.css?ver=1806">
<script type="text/javascript" src="{$mybb->asset_url}/jscripts/select2/select2.min.js?ver=1806"></script>
<script type="text/javascript">
<!--
if(use_xmlhttprequest == "1")
{
	MyBB.select2();
	$("#xtun").select2({
		placeholder: "Username(s), separated by comma. Maximum 3 users.",
		minimumInputLength: 3,
		maximumSelectionSize: 3,
		multiple: true,
		ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
			url: "xmlhttp.php?action=get_users",
			dataType: 'json',
			data: function (term, page) {
				return {
					query: term, // search term
				};
			},
			results: function (data, page) { // parse the results into the format expected by Select2.
				// since we are using custom formatting functions we do not need to alter remote JSON data
				return {results: data};
			}
		},
		initSelection: function(element, callback) {
			var query = $(element).val();
			if (query !== "") {
				var newqueries = [];
				exp_queries = query.split(",");
				$.each(exp_queries, function(index, value ){
					if(value.replace(/\s/g, '') != "")
					{
						var newquery = {
							id: value.replace(/,\s?/g, ", "),
							text: value.replace(/,\s?/g, ", ")
						};
						newqueries.push(newquery);
					}
				});
				callback(newqueries);
			}
		}
	});
}
// -->
</script>

* the above code based on 1.8.6. I don't have MyBB version above 1.8.6. So maybe you need to modify it a bit.

Multiple Value Delimiter: ,

Change the maximumSelectionSize if you are allowing more than 3 usernames.

Thank you!

So I tried to add a field for that in my test forum. For the moment with just the copied code of the private_send_autocomplete templated. I wanted to modify it afterwards so that I already have it if something goes wrong (what most likely will happen). But the first misstep came quicker than thought: I can't save the field. ^^

That's (so far) the code I wanted to put into the field you said:

PHP Code:
<link rel="stylesheet" href="{$mybb->asset_url}/jscripts/select2/select2.css?ver=1807">
<script type="text/javascript" src="{$mybb->asset_url}/jscripts/select2/select2.min.js?ver=1806"></script>
<script type="text/javascript">
<!--
if(use_xmlhttprequest == "1")
{
	MyBB.select2();
	$("#to").select2({
		placeholder: "{$lang->search_user}",
		minimumInputLength: 2,
		maximumSelectionSize: 8,
		multiple: true,
		ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
			url: "xmlhttp.php?action=get_users",
			dataType: 'json',
			data: function (term, page) {
				return {
					query: term, // search term
				};
			},
			results: function (data, page) { // parse the results into the format expected by Select2.
				// since we are using custom formatting functions we do not need to alter remote JSON data
				return {results: data};
			}
		},
		initSelection: function(element, callback) {
			var query = $(element).val();
			if (query !== "") {
				var newqueries = [];
				exp_queries = query.split(",");
				$.each(exp_queries, function(index, value ){
					if(value.replace(/\s/g, '') != "")
					{
						var newquery = {
							id: value.replace(/,\s?/g, ", "),
							text: value.replace(/,\s?/g, ", ")
						};
						newqueries.push(newquery);
					}
				});
				callback(newqueries);
			}
		}
	});

  $("#bcc").select2({
		placeholder: "{$lang->search_user}",
		minimumInputLength: 2,
		maximumSelectionSize: {$mybb->usergroup['maxpmrecipients']},
		multiple: true,
		ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
			url: "xmlhttp.php?action=get_users",
			dataType: 'json',
			data: function (term, page) {
				return {
					query: term, // search term
				};
			},
			results: function (data, page) { // parse the results into the format expected by Select2.
				// since we are using custom formatting functions we do not need to alter remote JSON data
				return {results: data};
			}
		},
		initSelection: function(element, callback) {
			var query = $(element).val();
			if (query !== "") {
				var newqueries = [];
				exp_queries = query.split(",");
				$.each(exp_queries, function(index, value ){
					if(value.replace(/\s/g, '') != "")
					{
						var newquery = {
							id: value.replace(/,\s?/g, ", "),
							text: value.replace(/,\s?/g, ", ")
						};
						newqueries.push(newquery);
					}
				});
				callback(newqueries);
			}
		}
	});
}
// -->
</script>


I'll get this error:

Quote:No multiple value delimiter defined (tip, you can set this to be a space).

I think I know where the problem is - is it the input formatter? By default there stands {VALUE}. What do I have to change?

Another question. Which ID do you mean? Do I have to put the one of the field into the script or the css-id like in the first row of the code?
(02-15-2017 05:52 AM)foxxie Wrote: [ -> ]I'll get this error:
Quote:No multiple value delimiter defined (tip, you can set this to be a space).

I think I know where the problem is - is it the input formatter? By default there stands {VALUE}. What do I have to change?

(02-15-2017 03:07 AM)RateU Wrote: [ -> ]Multiple Value Delimiter: ,

(02-15-2017 05:52 AM)foxxie Wrote: [ -> ]Another question. Which ID do you mean? Do I have to put the one of the field into the script or the css-id like in the first row of the code?

(02-15-2017 03:07 AM)RateU Wrote: [ -> ]

HTML Code
<input type="text" id="xtun" class="textbox"{NAME_PROP}{MAXLEN_PROP}{WIDTH_PROP_SIZE}{TABINDEX_PROP}{REQUIRED_PROP} value="{VALUE}" style="min-width: 300px;" />



(02-15-2017 05:52 AM)foxxie Wrote: [ -> ]That's (so far) the code I wanted to put into the field you said:

PHP Code:
<link rel="stylesheet" href="{$mybb->asset_url}/jscripts/select2/select2.css?ver=1807">
<script type="text/javascript" src="{$mybb->asset_url}/jscripts/select2/select2.min.js?ver=1806"></script>
<script type="text/javascript">
<!--
if(use_xmlhttprequest == "1")
{
	MyBB.select2();
	$("#to").select2({
		placeholder: "{$lang->search_user}",
		minimumInputLength: 2,
		maximumSelectionSize: 8,
		multiple: true,
		ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
			url: "xmlhttp.php?action=get_users",
			dataType: 'json',
			data: function (term, page) {
				return {
					query: term, // search term
				};
			},
			results: function (data, page) { // parse the results into the format expected by Select2.
				// since we are using custom formatting functions we do not need to alter remote JSON data
				return {results: data};
			}
		},
		initSelection: function(element, callback) {
			var query = $(element).val();
			if (query !== "") {
				var newqueries = [];
				exp_queries = query.split(",");
				$.each(exp_queries, function(index, value ){
					if(value.replace(/\s/g, '') != "")
					{
						var newquery = {
							id: value.replace(/,\s?/g, ", "),
							text: value.replace(/,\s?/g, ", ")
						};
						newqueries.push(newquery);
					}
				});
				callback(newqueries);
			}
		}
	});

  $("#bcc").select2({
		placeholder: "{$lang->search_user}",
		minimumInputLength: 2,
		maximumSelectionSize: {$mybb->usergroup['maxpmrecipients']},
		multiple: true,
		ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
			url: "xmlhttp.php?action=get_users",
			dataType: 'json',
			data: function (term, page) {
				return {
					query: term, // search term
				};
			},
			results: function (data, page) { // parse the results into the format expected by Select2.
				// since we are using custom formatting functions we do not need to alter remote JSON data
				return {results: data};
			}
		},
		initSelection: function(element, callback) {
			var query = $(element).val();
			if (query !== "") {
				var newqueries = [];
				exp_queries = query.split(",");
				$.each(exp_queries, function(index, value ){
					if(value.replace(/\s/g, '') != "")
					{
						var newquery = {
							id: value.replace(/,\s?/g, ", "),
							text: value.replace(/,\s?/g, ", ")
						};
						newqueries.push(newquery);
					}
				});
				callback(newqueries);
			}
		}
	});
}
// -->
</script>



(02-15-2017 03:07 AM)RateU Wrote: [ -> ]Input Field HTML:

HTML Code
<input type="text" id="xtun" class="textbox"{NAME_PROP}{MAXLEN_PROP}{WIDTH_PROP_SIZE}{TABINDEX_PROP}{REQUIRED_PROP} value="{VALUE}" style="min-width: 300px;" />
<link rel="stylesheet" href="{$mybb->asset_url}/jscripts/select2/select2.css?ver=1806">
<script type="text/javascript" src="{$mybb->asset_url}/jscripts/select2/select2.min.js?ver=1806"></script>
<script type="text/javascript">
<!--
if(use_xmlhttprequest == "1")
{
	MyBB.select2();
	$("#xtun").select2({
		placeholder: "Username(s), separated by comma. Maximum 3 users.",
		minimumInputLength: 3,
		maximumSelectionSize: 3,
		multiple: true,
		ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
			url: "xmlhttp.php?action=get_users",
			dataType: 'json',
			data: function (term, page) {
				return {
					query: term, // search term
				};
			},
			results: function (data, page) { // parse the results into the format expected by Select2.
				// since we are using custom formatting functions we do not need to alter remote JSON data
				return {results: data};
			}
		},
		initSelection: function(element, callback) {
			var query = $(element).val();
			if (query !== "") {
				var newqueries = [];
				exp_queries = query.split(",");
				$.each(exp_queries, function(index, value ){
					if(value.replace(/\s/g, '') != "")
					{
						var newquery = {
							id: value.replace(/,\s?/g, ", "),
							text: value.replace(/,\s?/g, ", ")
						};
						newqueries.push(newquery);
					}
				});
				callback(newqueries);
			}
		}
	});
}
// -->
</script>

Pages: 1 2
Reference URL's