<?php
/**
 * Edit all matchs for bet tables
 *
 * @package PHP Competition System
 * @author  Eric GALLENE <okrome@free.fr>
 * @link	http://www.phpcompet.com
 * @version $Id: edit_betlist.php,v 1.00 2006/10/26 19:47:13
 */

require_once './core/functions.php';
require_once './core/tables.php';

// check for localnet
localnet_or_die();
// multiuser permission check
permission_or_die(PERM_WRITE);
$noError = true;

if ($save) {

	$SELECT = 'SELECT '.TBL_BETTABLE.'.title, '.TBL_BETTABLE.'.nbr_prono,  '.TBL_BETTABLE.'.date_deb,  '.TBL_BETTABLE.'.date_fin
	FROM '.TBL_BETTABLE.'
	WHERE '.TBL_BETTABLE.'.id = '.$id;				 
	$bettable = runSQL($SELECT);

	$DELETE = runSQL('DELETE FROM '.TBL_BETLIST.' WHERE id_bettable=\''.$id.'\'');

	for ($x=1; $x<$bettable[0]['nbr_prono']+1; $x++) {
	
	
	$INSERT = 'REPLACE '.TBL_BETLIST.' SET 
	id_bettable=\''.$id.'\', 
	id_match=\''.${'bet'.$x}.'\'';
	$bet = runSQL($INSERT); 
	}

	redirect('pronostics.php');
}

// get data (id may be empty on a empty database)
if (!empty($id))
{
	$SELECT = 'SELECT '.TBL_BETTABLE.'.title, '.TBL_BETTABLE.'.nbr_prono,  '.TBL_BETTABLE.'.date_deb,  '.TBL_BETTABLE.'.date_fin
	FROM '.TBL_BETTABLE.'
	WHERE '.TBL_BETTABLE.'.id = '.$id;				 
	$bettable = runSQL($SELECT);
	
	$SELECT = 'SELECT '.TBL_BETLIST.'.id_match FROM '.TBL_BETLIST.' WHERE '.TBL_BETLIST.'.id_bettable = '.$id;
	$mybet = runSQL($SELECT);
		
	$SELECT = 'SELECT '.TBL_MATCHS.'.id, C1.shortname as dom, C2.shortname as ext, CO.shortname as compet 
				FROM '.TBL_MATCHS.' 
				JOIN '.TBL_DAYS.' ON '.TBL_MATCHS.'.id_day = '.TBL_DAYS.'.id
				JOIN '.TBL_COMPETITIONS.' AS CO ON CO.id = '.TBL_DAYS.'.id_compet
				JOIN '.TBL_CLUBS.' AS C1 ON '.TBL_MATCHS.'.id_candidate_dom = C1.id
				JOIN '.TBL_CLUBS.' AS C2 ON '.TBL_MATCHS.'.id_candidate_ext = C2.id 
				WHERE '.TBL_MATCHS.'.date_day <= \''.$bettable[0]['date_fin'].'\' AND '.TBL_MATCHS.'.date_day >= \''.$bettable[0]['date_deb'].'\';'; 
	$res   = runSQL($SELECT);
	$numres = count($res);
	
	
    // is it editable/ copyable?
    $editable = localnet();
    $copyable = localnet();
    
    // multi-user permissions
    if ($config['multiuser'])
    {
        $smarty->assign('editable', check_permission(PERM_WRITE, $_COOKIE['PCSuserid']));
		if ($config['activecomments']) {
	    $smarty->assign('commentable', check_permission(PERM_MKCOMMENT, $_COOKIE['PCSuserid']));
		}
		if ($config['activegallery']) {
	    $smarty->assign('seegallery', check_permission(PERM_GALLERY, $_COOKIE['PCSuserid']));
		}
    }

}

$smarty->assign('id', $id);
$smarty->assign('bettable', $bettable[0]);
$smarty->assign('mybet', $mybet);

// prepare templates
tpl_page('detailview', $bettable[0]['title']);
tpl_edit_betlist($res);

// caching enabled?
if ($config['http_caching'])
{
    require_once('./core/httpcache.php');
    httpCacheCaptureStart();
}

// display templates
smarty_display('header.tpl');
if (!$config['http_caching']) flush();
smarty_display('filters_pronostics.tpl');
smarty_display('edit_betlist.tpl', $id);
smarty_display('footer.tpl');
// caching enabled?
if ($config['http_caching'])
{
    httpCacheOutput('edit_betlist'.$id, httpCacheCaptureEnd());
}

?>
