Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-07-16 15:01:26

wornout
Member
From: Italy
Registered: 2009-01-20
Posts: 256
Website

Check for duplicate values in custom fields

How can I check for duplicates in custom_fields while saving article?

Last edited by wornout (2012-07-18 12:42:18)

Offline

#2 2012-07-18 12:41:10

wornout
Member
From: Italy
Registered: 2009-01-20
Posts: 256
Website

Re: Check for duplicate values in custom fields

Maybe I was too generic.
I’m asking for possibility to check if a custom field can has a unique value because is used for product code.
If already exist Textpattern alerts user.

Thanks in advance for every suggestions!

Offline

#3 2012-07-18 13:50:07

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Check for duplicate values in custom fields

On the admin-side? On Textpattern 4.5 and current dev branch you can use the new asynchronous API and messager to fire Textpattern’s native alerts. 4.5 also offers few callback helpful callback events for this purpose. Along the lines of:

/**
 * Hook function abc_product_id to callback events article_posted and article_saved
 */

register_callback('abc_product_id', 'article_posted');
register_callback('abc_product_id', 'article_saved');

/**
 * Checks that product_id is unique and fires and message if it isn't
 */

function abc_product_id($event, $step, $pre, $data) {
	global $theme;

	/*
		If no duplicates end here. Checks "custom_1". Note: it will break when/if multiple-custom field
		code lands.
	*/

	if(safe_count('textpattern', "custom_1='".doSlash($data['custom_1'])."'") <= 1) {
		return;
	}

	/*
		If there is more than one article with the same custom field value, issue a warning.
		Uses Textpattern's native messages.
	*/

	send_script_response(
		$theme->announce_async(array(
			gTxt('abc_duplicate_product_id'), E_WARNING
		))
	);
}

Also, v4.5.0 offers way to validate values, which would outright block saving. There is an article_validate event which can be used to add constraints. It requires bit more code so, I won’t write it on the spot on this web form. You would pretty much create new class that extends Constraint, and add it to the stack.

If it needs to work in TXP 4.4.0, well dunno. It has none of the above. I suppose you could fire hackish a JavaScript alert dialog after the form has been sent and saved. Well, you can know if the article has been saved, but fire a warning based on raw post data on “article” event. Along the lines:

register_callback('abc_product_id', 'article');

function abc_product_id() {

	if(!ps('custom_1') || safe_count('textpattern', "custom_1='".doSlash(ps('custom_1'))."'") <= 1) {
		return;
	}

	echo script_js('alert("Dublicate product ID")');
}

Just note that the above, while it works in TXP 4.4.1, it will not work in TXP 4.5 or any other future releases so to speak correctly. Plus it’s hackish. Heck, it doesn’t even know if an article was saved or not.

Please note that both snippets are just examples and untested too. May or may not work. Both expect that you have existing PHP knowledge, or someone that can deploy them for you. If future compatibility is wanted, then both snippets could be used together, well, potentially.

Last edited by Gocom (2012-07-18 13:54:53)

Offline

#4 2012-07-18 14:11:21

etc
Developer
Registered: 2010-11-11
Posts: 5,397
Website GitHub

Re: Check for duplicate values in custom fields

There is another not very satisfactory options: write a snippet of rah_external_output that checks whether a given custom field value exists and attach an appropriate ajax call to submit event of the edit form. This will act before the article is saved, but relies on client-side javascript and hence can be circumvented.

Offline

#5 2012-07-18 14:56:40

etc
Developer
Registered: 2010-11-11
Posts: 5,397
Website GitHub

Re: Check for duplicate values in custom fields

And the most quick and dirty solution would be to create a unique index on your custom field :)

Offline

Board footer

Powered by FluxBB