<?php

//set trigger to table 'cdr'

	include('config.php');
	$data_base_name = $sugar_config['dbconfig']['db_name'];
	
	$query = "DROP TRIGGER IF EXISTS `$data_base_name`.`id_create`;";
	$result = $GLOBALS['db']->query($query);
	
	$query = "CREATE TRIGGER `$data_base_name`.`id_create` BEFORE INSERT ON `$data_base_name`.`cdr` FOR EACH ROW SET NEW.id = UUID() ;";
	$result1 = $GLOBALS['db']->query($query);
	
	$query = "DROP TRIGGER IF EXISTS `$data_base_name`.`ecmcalls_assign`;";
	$result = $GLOBALS['db']->query($query);
	
	$query = "CREATE TRIGGER `$data_base_name`.`ecmcalls_assign` AFTER INSERT ON `$data_base_name`.`cdr`
 				FOR EACH ROW BEGIN
				INSERT INTO `ecmcalls_beans` (`id`, `ecmcall_id`, `bean_id`, `bean_module`, `src`, `date_modified`, `deleted`) SELECT UUID() as `id`, NEW.id as `ecmcall_id`, `ecp`.`bean_id` as `bean_id`, `ecp`.`bean_module` as `bean_module`, `ecp`.`phone` as `src`, NEW.calldate as `date_modified`, '0' as `deleted` FROM `ecmcallsphones` as `ecp` WHERE `ecp`.`bean_module` <> 'EcmCalls' AND ((LPAD(NEW.dst,50,'0') = LPAD(`ecp`.`phone`,50,'0')) OR ((LPAD(NEW.src,50,'0') = LPAD(`ecp`.`phone`,50,'0')) OR (LPAD(NEW.dcontext,50,'0') = LPAD(`ecp`.`phone`,50,'0'))));
 END ;";
	$result2 = $GLOBALS['db']->query($query);

	if(!$result1 || !$result2)	
		echo "Cannnot create triggers to table \"cdr\". The database user must have super privileges to do this!<br>\n";
	else 
		echo "Triggers to table \"cd\" are created.<br>\n";
	

	$query = "DROP TABLE IF EXISTS `ecmcallsphones`;";
	$result = $GLOBALS['db']->query($query);


		
		echo "Creating a table \"EcmCallsPhones\"...<br>\n";
		
		$query = "CREATE TABLE IF NOT EXISTS `ecmcallsphones` (
				  `id` char(36) NOT NULL,
				  `bean_module` varchar(25) NOT NULL,
				  `bean_id` char(36) NOT NULL,
				  `phone` varchar(25) NOT NULL,
				  `note` varchar(50) NOT NULL,
				  `index` int(4) default NULL,
				  PRIMARY KEY  (`id`),
				  KEY `idx_ecmcallsphones_bean_id` (`bean_id`)
				) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
				
		$result = $GLOBALS['db']->query($query);
		
		if($result) {
		    echo "Table \"EcmCallsPhones\" is created.<br>\n";
			$result = true;
		}
		else {
			echo "Error with creating table EcmCallsPhones! Contact with admin for username and password to database!<br>\n";
		}

	
	if($result) {
		
		echo "Table \"EcmCallsPhones\" exists.<br>\n";
		echo "Updating data from Contacts, Accounts, Users to EcmCallsPhones...<br>\n";
		echo "Updating data successfull end!<br>\n";
		
		echo "<br>";
		echo "Creating unique id to \"cdr\" table records if some exists...<br>\n";
		
		
		function getTableEcmCallsCount() {
			$query = "SELECT COUNT(*) FROM `cdr` WHERE `id` IS NULL OR `id`=''";
			$result = $GLOBALS['db']->query($query);
			if($result) {
				$result = $GLOBALS['db']->fetchByAssoc($result);
				if($result) return intval($result['COUNT(*)']); else return 0;
			}
		}
		
		$query = "UPDATE `cdr` SET `id` = '".create_guid()."' WHERE `id` IS NULL OR `id`='' LIMIT 1";
		while(getTableEcmCallsCount()) {
			echo $query.'<br>';
			$result = $GLOBALS['db']->query($query);
			$count++; 
		};
		
		if($count == 0) 
			echo "Table \"cdr\" is empty or all records have set unique id.<br>\n";
		else 
			echo "Unique id is set for $count records.<br>\n";
		
		
		echo "<br>";
		echo "Assigning Users, Contacts and Accounts phones to central calls records if some records exists...<br>\n";
		$file = "modules/EcmCalls/EcmCall.php";
		require_once($file);
		if(EcmCall::autoAssigning())
			echo "Assigning successfull!";
		else
			echo "Assigning fail!";
		
		echo "<br><br>Module EcmCalls is succesfully installed!!!";
	
	}

?>