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

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




class commentsGrid extends GridDataManipulator 
{

	public function insertAction() {
		
		$sql = "INSERT INTO ".DB_WARPIT_MYSTERY."._Comments  SET 
					id_project = '".$_POST['id_project']."',
					id_structure =  '".$_POST['id_record']."',
					id_user = '".$_SESSION['UserInfo']['id']."',
					comment_title = '".addslashes($_POST['comment_title'])."',
					comment_text = '".addslashes($_POST['comment_text'])."',
					date_added = NOW() ";	
		
		if ($_POST['send_email'] == 1) $sql .= ", email_to_admin = 1 ";
		
		$this->db->SQLexecute($sql);
		
		echo "/*";
		
		$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 ) {
				
				//print_r(get_defined_constants(true));
				
				// 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_link'] = WARPIT_WWW."mystery/?loadComment=".$_POST['id_project'].",".$_POST['id_record'];
				$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."._Comments as a";
		$_POST['SQL'] .= "	LEFT JOIN   ".DB_WARPIT_MAIN."._user as b ON a.id_user = b.id ";
		$_POST['SQL'] .= "	WHERE ";
		$_POST['SQL'] .= "				id_project = '".$_POST['id_project']."'";
		$_POST['SQL'] .= "		AND 	id_structure =  '".$_POST['id_record']."'";
		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!"}';	
	
}
?>