Sick of duplicate ads on your site?
Open myAds/addannonces.php and locate in the AddAnnoncesOK function:
$xoopsDB->queryF("INSERT INTO ".$xoopsDB->prefix("ann_annonces")." values ('', '$cat', '$title', '$type', '$description', '$tel', '$price', '$typeprix', '$date', '$email', '$submitter', '$usid', '$town', '$country', '$valid', '$filename', '0')");
... there's more but that's the line you're looking for.
Replace everything under that before the close of the function "}" with:
if($xoopsDB->getAffectedRows()<=0) {
return False;
} else {
return True;
}
Your function AddAnnoncesOK should look like this:
Quote:
function AddAnnoncesOk($lid, $cat, $title, $type, $description, $tel, $price, $typeprix, $date, $email, $submitter, $usid, $town, $country, $valid, $photo, $photo_size, $photo_name, $HTTP_POST_FILES) {
global $xoopsDB, $myts, $photomax, $xoopsConfig, $destination, $xoopsLogger;
include("cache/config.php");
include("include/functions.php");
$title = $myts->makeTboxData4Save($title);
$type = $myts->makeTboxData4Save($type);
$description = $myts->makeTboxData4Save($description);
$tel = $myts->makeTboxData4Save($tel);
$price = $myts->makeTboxData4Save($price);
$typeprix = $myts->makeTboxData4Save($typeprix);
$submitter = $myts->makeTboxData4Save($submitter);
$town = $myts->makeTboxData4Save($town);
$country = $myts->makeTboxData4Save($country);
$filename = '';
if ( !empty($HTTP_POST_FILES['photo']['name']) ) {
include_once(XOOPS_ROOT_PATH."/class/uploader.php");
$upload = new XoopsMediaUploader("images_ann/", array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/pjpg', 'image/x-png'), $photomax);
$upload->setTargetFileName($HTTP_POST_FILES['photo']['name']);
$upload->fetchMedia('photo');
if (!$upload->upload()) {
redirect_header("addannonces2.php?cid=$cat", 3, $upload->getErrors());
return False;
exit();
} else {
$filename = $upload->getSavedFileName();
}
}
$xoopsDB->queryF("INSERT INTO ".$xoopsDB->prefix("ann_annonces")." values ('', '$cat', '$title', '$type', '$description', '$tel', '$price', '$typeprix', '$date', '$email', '$submitter', '$usid', '$town', '$country', '$valid', '$filename', '0')");
if($xoopsDB->getAffectedRows()<=0) {
return False;
} else {
return True;
}
}
Now, in the same file, find the line
switch($op) {
In that section you'll see:
AddAnnoncesOk($lid, $cid, $title, $type, $description, $tel, $price, $typeprix, $date, $email, $submitter, $usid, $town, $country, $valid, $photo, $photo_size, $photo_name, $HTTP_POST_FILES)==True);
replace that line with this:
If(AddAnnoncesOk($lid, $cid, $title, $type, $description, $tel, $price, $typeprix, $date, $email, $submitter, $usid, $town, $country, $valid, $photo, $photo_size, $photo_name, $HTTP_POST_FILES)==True) {
redirect_header("index.php",1,_CLA_ANNADDED);
} else {
redirect_header("addannonces2.php?cid=$cat",3,"Duplicate Classified. Not Added.");
}
You can make a variable for the language file if you want for the duplicate message but since my site is 100% English, my hack doesn't need that.. I just put the text I want right in the php.
You're not done.
You also need to create another index on your ..._ann_annonces table by running this sql statement:
ALTER TABLE `xoops_ann_annonces`
ADD UNIQUE `PREVENTDUPE` ( `title` , `price` , `submitter` )
This adds a unique index on the combination of title, price and submitter. This will prevent the user from submitting the same title with the same price. Ever.
You can play around with your index to suit your needs by just changing the fields listed. If you change the fields and you've already added the index just use this sql to remove the index and rebuild it with your new fields:
ALTER TABLE `xoops_ann_annonces` DROP INDEX `PREVENTDUPE` ,
ADD UNIQUE `PREVENTDUPE` ( `title` , `price` , `submitter` )
Keep in mind that when you try to create the index you may get errors if there are already duplicate ads in your table. This is a pain in the ass of course so you'll need to go through and remove the duplicates as you find them. Keep running the sql statement until it finally goes through, meaning you have no duplicates. ;)
Then when a user submits an ad on your site it'll either go into the table or it will violate the index and fail. If it fails, it won't add the classified. ;)
Hope you find this as useful as I have.