<?php
include_once ("_dirinfo.php");

include_once (PATH_TO_ROOT."/mystery/init.php");
include_once (PATH_TO_ROOT."_ajaxManipulators/_class/class.gridData.php");




class commentsGrid extends GridDataManipulator 
{

	public function insertAction() {
		
		$sql = "INSERT INTO ".DB_WARPIT_MYSTERY."._WelcomeBlog  SET 
				
					id_user = '".$_SESSION['UserInfo']['id']."',
					comment_title = '".$_POST['comment_title']."',
					comment_text = '".$_POST['comment_text']."',
					date_added = NOW() ";			
			
		$this->db->SQLexecute($sql);
	
		echo "/*";
		$_POST['id_project'] = 8;
		$sqlTemplate = "SELECT id_mail_group,id_mail_template FROM ".DB_WARPIT_MYSTERY."._Project_EmailGroup WHERE id_project = '".$_POST['id_project']."'";
		$res = $this->db->SQLexecute($sqlTemplate);
		if ($this->db->NumRows($res)) {
			$zad = $this->db->fetchAssoc($res);
			
			// check if their are any persons in group 
			$sql = "SELECT count(*) as n FROM ".DB_WARPIT_USERSPOOL."._group_link WHERE id_group = ".$zad['id_mail_group'];
			//echo $sql."\n\r";
			$res = $this->db->SQLexecute($sql);
			$zadCount = $this->db->fetchAssoc($res);
				 
			if ( $zadCount['n'] > 0 ) {
				
				
				// prepare new mail set
				$sql = "INSERT INTO  ".DB_WARPIT_USERSPOOL."._SendMailSet 
								SET  
									date_queue = NOW(),
									numSend = ".$zadCount['n'].",
									id_template = ".$zad['id_mail_template'].",
									id_group = ".$zad['id_mail_group'];
				//echo $sql."\n\r";	
				$this->db->SQLexecute($sql);
				$mailSet = $this->db->GetLastInsertId();
					
				// copy all persons from defined group into mail queue
				$sql = "INSERT INTO ".DB_WARPIT_USERSPOOL."._MailStatistic (mail_set, interviewer_id ,mail_status, mail_address)						
							SELECT ".$mailSet.", id, 9, email FROM ".DB_WARPIT_USERSPOOL."._Interviewer 
								WHERE id IN ( SELECT id FROM ".DB_WARPIT_USERSPOOL."._group_link WHERE id_group = ".$zad['id_mail_group'].")";				
				//echo $sql."<Br>";
				$this->db->SQLexecute($sql);
				$_POST['mailSet'] = $mailSet;
				$_POST['mailStatus'] = 9;
				$db = $this->db;
				$ignoreDelay = 1;
				
				$additionalPersonalFields['mystery_comment_title'] = $_POST['comment_title'];
				$additionalPersonalFields['mystery_comment_text'] = $_POST['comment_text'];
				$additionalPersonalFields['mystery_comment_user'] = $_SESSION['UserInfo']['Firstname']." ".$_SESSION['UserInfo']['Surname'];
				
				include("../../webuserspool/_ajax/ajax.sendMail.php");			
			}
			
			
		}
		echo "INSERT DATA: ".$sql;
		echo "SEARCH FOR TEMPLATE: ".$sqlTemplate;
		echo "*/";
		return  '{"success":true }';
		
	}
	
	public function selectAction() {
		
		$_POST['SQL'] = "SELECT a.*, CONCAT(Firstname,' ',Surname) as userName FROM  ".DB_WARPIT_MYSTERY."._WelcomeBlog as a";
		$_POST['SQL'] .= "	LEFT JOIN   ".DB_WARPIT_MAIN."._user as b ON a.id_user = b.id ";
		
		echo "/*".$_POST['SQL']."*/";
			
		$this->fetchSelect();
		return parent::selectAction();
		
		
	}
	
}

$db->ChangeDatabase($_POST['database']);
$tableName = $_POST['RMtable'];
$manipulator = new commentsGrid($db,$tableName);


$comm = $_POST['action'].'Action';
if(method_exists($manipulator,$comm))
	echo $manipulator->$comm();
else 
{
	
	Echo '{success:false,error:"Unknown action!"}';	
	
}
?>