<?php
/*
Officity - Web application platform - Version 6.0 - 2011-07-05

François Dispaux, Boris Verdeyen, Thomas Hermant,
Jérémie Roy, Grégory Meurice, Abdelila Harbi, 
Marc Mignonsin, Jonathan Sanchez, Julien Gonzalez, Pierre Fouchez

Sushee and Officity is © Copyright 2011 Nectil SA.

`/sushee-source/sushee/private/mail_postprocessing.php` is part of Officity.

Sushee is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Officity and Sushee are distributed in the hope that they will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Sushee. If not, see <http://www.gnu.org/licenses/>.
*/
if ($requestName=='UPDATE'){
	if( ($values['Type']=='in' || $former_values['Type']=='in') && isset($values['Junk']) ){
		if( $former_values['Junk']!=$values['Junk']){
			$moduleInfo = moduleInfo('mail');

			include_once(dirname(__FILE__).'/../common/automatic_classifier.class.php');
			$classifier = new automatic_classifier();
			// Marked as Junk or 'Not junk' by human interaction
			$values['JunkDetection']='human';
			$db_conn->Execute('UPDATE `'.$moduleInfo->tableName.'` SET `JunkDetection`="human" WHERE ID=\''.$IDs_array[0].'\';');
			$subject = $former_values['Subject'];
			$plaintext = $former_values['PlainText'];
			$from = $former_values['From'];
			$cc = $former_values['Cc'];
			$to = $former_values['To'];
			$total_mail_str = $from.$subject.$to.$cc.$plaintext;
			if($values['Junk']==1){
				$former_category = 'mail_notspam';
				$category = 'mail_spam';
				$weight = 1;
			}else{
				$former_category = 'mail_spam';
				$category = 'mail_notspam';
				$weight = 2;
			}
			if($former_values['Junk']!=2 && $former_values['JunkDetection']!='computer'){
				$classifier->untrain($total_mail_str,$former_category,$weight);
			}
			$classifier->train($total_mail_str,$category,$weight);
			$classifier->update_categories_informations();
		}
	}
}
return TRUE;
?>
