<?php
/**
 * This file is to manage deleted inactive users
 *
 * This file is having ManageDeleted class to manage deleted files
 *
 * PHP version 5.0
 *
 * @category	Anova
 * @package		Admin
 * @author 		sridhar_68ag07
 * @copyright 	Copyright (c) 2009 {@link http://www.agriya.com Agriya Infoway}
 * @license		http://www.agriya.com Agriya Infoway Licence
 * @version		SVN: $Id: $
 * @since 		2008-12-26
 *
 **/
require_once('../common/configs/config.inc.php');
$CFG['mods']['include_files'][] = 'common/classes/class_FormHandler.lib.php';
$CFG['mods']['include_files'][] = 'common/configs/config_users.inc.php';
$CFG['mods']['is_include_only']['non_html_header_files'] = true;
/**
 * To include application top file
 */
require_once($CFG['site']['project_path'].'common/application_top.inc.php');
//---------------------------- Class ConfigEdit begins -------------------->>>>>//
/**
 * ManageDeleted
 *
 * @package
 * @author sridhar_68ag07
 * @copyright Copyright (c) 2009
 * @version $Id: $
 * @access public
 **/

set_time_limit(0);
class ManageDeleted extends FormHandler
	{
		public 	function getTimeDiffernce($date1)
			{
				//current date.
				$date2 = date("Y-m-d", time());
				//calculate the difference in seconds.
				$difference = abs(strtotime($date2) - strtotime($date1));
				//calculate the number of days
				$days = round(((($difference/60)/60)/24), 0);
				return $days;
			}
		public function deleteAdvancedSearchTable($user_id)
			{
				$sql = 'DELETE FROM  '.$this->CFG['db']['tbl']['advanced_search'].' WHERE'.
						' user_id='.$this->dbObj->Param('user_id');

				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($user_id));
					if (!$rs)
						trigger_db_error($this->dbObj);
			}
		public function insertUsersAnsLogTable($user_id)
			{
				$sql = 'DELETE FROM '.$this->CFG['db']['tbl']['users_ans_log'].' WHERE'.
						' user_id='.$this->dbObj->Param('user_id');
				$stmt = $this->dbObj->Prepare($sql);
				$rs = $this->dbObj->Execute($stmt, array($user_id));
					if (!$rs)
						trigger_db_error($this->dbObj);
			}
		public function deleteInActiveUsers($table_name)
			{
				$sql =  ' SELECT '.$this->getUserTableFields(array('doj', 'user_id')).' FROM ' .	$table_name .
						' WHERE '.$this->getUserTableField('last_logged').'  IS NULL   AND '.$this->getUserTableField('usr_status').'=\'ToActivate\'';
				$stmt = $this->dbObj->Prepare($sql);
				// execute sql
				$rs = $this->dbObj->Execute($stmt);
				//raise user error... fatal
				if (!$rs)
						trigger_db_error($this->dbObj);
				if($rs)
					{
						while($row = $rs->FetchRow())
							{
							   $diff =  $this->getTimeDiffernce($row['doj']);
							   if($diff >= $this->CFG['admin']['days'])
									{
										$sql = ' DELETE FROM '.$table_name .
											   ' WHERE '.$this->getUserTableField('user_id').' = '.$row['user_id'].'';
										$stmt_del = $this->dbObj->Prepare($sql);
										// execute sql
										$rs_del = $this->dbObj->Execute($stmt_del);
										//raise user error... fatal

										if (!$rs_del)
												trigger_db_error($this->dbObj);
										$this->deleteAdvancedSearchTable($row['user_id']);
										$this->insertUsersAnsLogTable($row['user_id']);

									}
							}
					}

			 }
	 }
//<<<<<-------------- Class ManageDeleted begins ---------------//
//-------------------- Code begins -------------->>>>>//
$ManageDeleted = new ManageDeleted();
$ManageDeleted->deleteInActiveUsers($CFG['db']['tbl']['users']);
?>