Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#25 2010-01-11 22:51:28

ferra1980
New Member
From: Monza
Registered: 2010-01-11
Posts: 2
Website

Re: [plugin] [ORPHAN] cnk_soccer - A football plugin

Hi chriloi, there’s a way to show in next_game tag always match with one specific team? Something like follow_club parameter in the cnk_table?
And also there’s a way to show all matchs in calendar?
Many thanks. Ferra 1980

Offline

#26 2010-02-24 03:24:14

Sono Juventino
Member
From: Skedsmo, Norway
Registered: 2007-12-22
Posts: 81

Re: [plugin] [ORPHAN] cnk_soccer - A football plugin

ferra1980: just write in time and date for the team(s) you want to show in next_game. That’s what I do.

Offline

#27 2010-02-24 18:38:20

ferra1980
New Member
From: Monza
Registered: 2010-01-11
Posts: 2
Website

Re: [plugin] [ORPHAN] cnk_soccer - A football plugin

Thanks Sono Juventino. But did You found a way to show table with all match, played and not played?

Offline

#28 2010-02-25 23:30:54

Sono Juventino
Member
From: Skedsmo, Norway
Registered: 2007-12-22
Posts: 81

Re: [plugin] [ORPHAN] cnk_soccer - A football plugin

No, but I’ve managed to add “Games/games played/etc” to the plugin. I have also changed the order on the table to what I find most suiting.

You may take a look at it here

1.1: At line 80, find the following code (It will be longer, but I’ve translated it)
if (count($col_headers) != 9) $col_headers = explode

1.2: Change the WHOLE line to this: (You may translate the word as you like)
if (count($col_headers) != 10) $col_headers = explode(',','#,club,games,won,drawn,lost,for,against,+/-,points');

2.1: At line 82m find the following code
if (count($col_display) != 9) $col_display = explode

2.2: Change The WHOLE Line with this:
if (count($col_display) != 10) $col_display = explode(',', '1,1,0,0,0,0,0,0,0,1');

3.1: Start at line 146 and change ALL the lines with @if ($col_display[X]) to this

if ($col_display[0]) $out .= '<td>'.($i+1).'</td>';
if ($col_display[1]) $out .= '<td>'.$r['club'].'</td>';
$games_played = $r['game_won'] + $r['game_lost'] + $r['game_drawn'];
if ($col_display[2]) $out .= '<td>' . $games_played . '</td>';
if ($col_display[3]) $out .= '<td>'.$r['game_won'].'</td>';
if ($col_display[4]) $out .= '<td>'.$r['game_draw'].'</td>';
if ($col_display[5]) $out .= '<td>'.$r['game_lost'].'</td>';
if ($col_display[6]) $out .= '<td>'.$r['goals_for'].'</td>';
if ($col_display[7]) $out .= '<td>'.$r['goals_against'].'</td>';
if ($col_display[8]) $out .= '<td>'.$r['goals_sum'].'</td>';
if ($col_display[9]) $out .= '<td>'.$r['points'].'</td>';

(X = Random number)

Fell free to ask if you don’t understand. :D

Last edited by Sono Juventino (2010-02-25 23:34:22)

Offline

#29 2010-09-06 01:48:02

Sono Juventino
Member
From: Skedsmo, Norway
Registered: 2007-12-22
Posts: 81

Re: [plugin] [ORPHAN] cnk_soccer - A football plugin

Ok, I’ve written two functions for this plugin. They are for displaying 5 next games for your club, and for displaying the 5 latest results. I’m not an advanced coder, so they are not optimized. The don’t require any changes to the table structure or such, so they should be easy to add to any exsisting site with this plugin. :)

LINK TO .TXT-FILE WITH CODE AND EXPLANATION

Link to example page with described functions

To display 5 next games:
<txp:cnk_game_list />

To display 5 latest results:
<txp:cnk_game_result />

Please ask here, og send me an email on thomas.m.larsson@gmail.com if you are struggeling

Last edited by Sono Juventino (2010-09-06 02:00:36)

Offline

#30 2011-05-15 14:30:28

magenta
New Member
Registered: 2011-05-08
Posts: 2

Re: [plugin] [ORPHAN] cnk_soccer - A football plugin

Please, tell me how to print in table the country name in brackets

Last edited by magenta (2011-05-15 14:44:22)

Offline

#31 2011-06-18 12:42:48

magenta
New Member
Registered: 2011-05-08
Posts: 2

Re: [plugin] [ORPHAN] cnk_soccer - A football plugin

Hello. afc-torpedo.ru This site with cnk_soccer. Wellcome

Offline

#32 2019-02-26 01:24:30

arellanon
Member
From: San Jose, CA
Registered: 2019-02-26
Posts: 13
Website

Re: [plugin] [ORPHAN] cnk_soccer - A football plugin

Hi: Thank you for this plugin. Can you give advice with installation issues?

I am using php 5.6 and I have version 1.0.2 of the plug in installed.
I go the “soccer” page and click “install” and I quickly get “Installation aborted”
I tried php 7.0 clicked install and got the following error.

Fatal error: Uncaught Error: Call to undefined function mysql_get_server_info() in /home/newsrealsanjose/public_html/textpattern/lib/txplib_misc.php(1870) : eval()’d code:2041 Stack trace: #0 /home/newsrealsanjose/public_html/textpattern/lib/txplib_misc.php(1870) : eval()’d code(2025): cnk_sc_do_install() #1 /home/newsrealsanjose/public_html/textpattern/lib/txplib_misc.php(1870) : eval()’d code(214): cnk_sc_install() #2 /home/newsrealsanjose/public_html/textpattern/lib/txplib_misc.php(1989): cnk_soccer(‘cnk_soccer’, ‘cnk_sc_install’) #3 /home/newsrealsanjose/public_html/textpattern/index.php(258): callback_event(‘cnk_soccer’, ‘cnk_sc_install’, 0) #4 {main} thrown in /home/newsrealsanjose/public_html/textpattern/lib/txplib_misc.php(1870) : eval()’d code on line 2041


Regards, Nick

Offline

#33 2019-02-26 08:20:39

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,127
Website

Re: [plugin] [ORPHAN] cnk_soccer - A football plugin

Hi arellanon and welcome!

That looks like the plugin is not yet updated for newer versions of PHP. It’s been a while since we saw the author here on the forum.

I don’t know what your level of knowledge is, but it may be as simple as replacing functions that use mysql_… in the plugin code with mysqli_… (note the extra i). See here for further information. That was a casualty of a change in PHP. You can edit the plugin code in the Admin › Plugins pane and look up the correct function names on php.net or here. That might resolve the installation problems, but it might also be more complicated than that.


TXP Builders – finely-crafted code, design and txp

Offline

#34 2019-02-26 18:22:34

arellanon
Member
From: San Jose, CA
Registered: 2019-02-26
Posts: 13
Website

Re: [plugin] [ORPHAN] cnk_soccer - A football plugin

I followed the instructions (to my best) and got some progress in that the error now turns to “installation aborted”. Probably means that it read php 7.

if(@txpinterface == 'admin') 
{
	define('CNK_SC_IMG_UPLOAD_CATEGORY', 'clubs');
	define('CNK_SC_IMG_BROWSE_FOLDER', '/images/soccer/clubs/');

	add_privs('cnk_soccer','1,2,3,4');
	add_privs('cnk_soccer_admin','1,2');

	register_tab('content', 'cnk_soccer', "Soccer");
	register_callback('cnk_soccer', 'cnk_soccer');

	$club_array = NULL;
}

// ################################################################
/*
	TAG: next game <txp:cnk_next_game />
*/
// ################################################################
function cnk_next_game($atts) 
{
	global $prefs, $pretext, $s, $id, $title;

	extract(lAtts(array(
		'league_id' => '1',
		'date_pattern' => '%A %e/%m - %Y, Kl. %H:%M',
		'colon_img_filename' => 'images/soccer/colon.gif',
		'delay_hours' => '2'
	), $atts));

	$q = "select UNIX_TIMESTAMP(cnk_soccer_games.kickoff_time) as kickoff, cnk_soccer_clubs1.name as club1, cnk_soccer_clubs1.logo as club1_logo, cnk_soccer_clubs2.name as club2, cnk_soccer_clubs2.logo as club2_logo, cnk_soccer_league.name as league, cnk_soccer_league.season as season from ".safe_pfx('cnk_soccer_games')." as cnk_soccer_games, ".safe_pfx('cnk_soccer_league')." as cnk_soccer_league, ".safe_pfx('cnk_soccer_clubs')." as cnk_soccer_clubs1, ".safe_pfx('cnk_soccer_clubs')." as cnk_soccer_clubs2 where cnk_soccer_clubs1.id = cnk_soccer_games.club1_id AND cnk_soccer_clubs2.id = cnk_soccer_games.club2_id AND cnk_soccer_league.id = cnk_soccer_games.league_id AND cnk_soccer_games.league_id = ".$league_id." AND DATE_ADD(cnk_soccer_games.kickoff_time, INTERVAL ".$delay_hours." HOUR) > NOW() ORDER BY kickoff_time LIMIT 1";

	$game = getRow($q);

	$out = '<div class="cnk_next_game">';

	if ($game) {

		extract($game);

		$out .= '<p><img class="cnk_sc_club_logo_1" src="'.trim($club1_logo, '/').'" alt="'.$club1.' logo" /><img class="cnk_sc_versus" src="'.$colon_img_filename.'" alt="versus" /><img class="cnk_sc_club_logo_2" src="'.trim($club2_logo, '/').'" alt="'.$club2.' logo" /></p>'.

		'<p>'.$club1.' - '.$club2.'</p>'.

		'<p>'.strftime($date_pattern, $kickoff).'</p>';

	}
	else
	{
		$out .= 'No upcoming match!';
	}

	$out .= '</div>';

	return $out;
}

// ################################################################
/*
	TAG: table <txp:cnk_table />
*/
// ################################################################
function cnk_table($atts) 
{
	global $prefs, $pretext, $s, $id, $title;

	extract(lAtts(array(
		'league_id' => '1',
		'include_ranks' => '',
		'column_headers' => '',
		'column_display' => '',
		'follow_club' => '',
		'follow_club_padding' => 2
	), $atts));

	$rank_includes = explode(',', strtolower(str_replace(' ', '', $include_ranks)));

	$col_headers = explode(',', str_replace(' ', '', $column_headers));

	if (count($col_headers) != 9) $col_headers = explode(',', '#,club,points,goals for,goals against,goals sum,won,draw,lost');

	$col_display = explode(',', str_replace(' ', '', $column_display));

	if (count($col_display) != 9) $col_display = explode(',', '1,1,1,0,0,0,0,0,0');

	$q = "SELECT 
			cl.points AS points, 
			c.name AS club, 
			c.id AS club_id,
			cl.goals_for,
			cl.goals_against,
			(cl.goals_for - cl.goals_against) as goals_sum,
			game_won,
			game_draw,
			game_lost
			FROM 
			".safe_pfx('cnk_soccer_clubs')." AS c LEFT JOIN 
			".safe_pfx('cnk_soccer_club_to_league')." AS cl ON (c.id = cl.club_id)
			WHERE 
			cl.league_id = ".$league_id."
			ORDER BY 
			cl.points DESC,
			(cl.goals_for - cl.goals_against) DESC";

	$ranks = getRows($q);

	// add follow_club and padding clubs
	if ($follow_club)
	{
		for($i=0; $i < count($ranks); $i++)
		{			
				if ($ranks[$i]['club_id'] == $follow_club)
				{
					$p = $i - $follow_club_padding; 

					if ($p < 0) $p = 0;

					while ($p <= $i + $follow_club_padding && $p < count($ranks))
					{
						$ranks[$p]['display'] = 'yes';
						$p++;
					}

					break;
				}
		}
	}

	$out = '<div class="cnk_ranking_table"><table><tr>';

	for($c=0;$c < count($col_headers);$c++)
	{	
		if ($col_display[$c]) $out .= '<td>'.$col_headers[$c].'</td>';
	}

	$out .= '</tr>';

	for($i=0; $i < count($ranks); $i++)
	{
		$r = $ranks[$i];

		if (trim($include_ranks) == '' || in_array(($i+1), $rank_includes) || isset($r['display'])) 
		{
			$out .= '<tr'.($r['club_id']==$follow_club?' class="cnk_sc_fav"':'').'>';

			if ($col_display[0]) $out .= '<td>'.($i+1).'</td>';

			if ($col_display[1]) $out .= '<td>'.$r['club'].'</td>';

			if ($col_display[2]) $out .= '<td>'.$r['points'].'</td>';

			if ($col_display[3]) $out .= '<td>'.$r['goals_for'].'</td>';

			if ($col_display[4]) $out .= '<td>'.$r['goals_against'].'</td>';

			if ($col_display[5]) $out .= '<td>'.($r['goals_sum']>0?'+':'').$r['goals_sum'].'</td>';

			if ($col_display[6]) $out .= '<td>'.$r['game_won'].'</td>';

			if ($col_display[7]) $out .= '<td>'.$r['game_draw'].'</td>';

			if ($col_display[8]) $out .= '<td>'.$r['game_lost'].'</td>';

			$out .= '</tr>';
		}
	}

	$out .= '</table></div>';

	return $out;
}

// ################################################################
/*
	Function: Handles callbacks
*/
// ################################################################
function cnk_soccer($event, $step)
{
	global $vars;
global $DB;

	$vars = array('name', 'season', 'id', 'country', 'logo', 'logo_1', 'logo_2', 'logo_3', 'club_id', 'league_id', 'old_club_id', 'old_league_id', 'points', 'goals_for', 'goals_against', 'game_won', 'game_lost', 'game_draw', 'club1_id', 'club2_id', 'kickoff_time', 'goals_club1', 'goals_club2', 'calc_rank', 'total_rounds', 'round', 'games_per_round');

	if(!$step or !in_array($step, array('cnk_sc_config', 
										'cnk_sc_create_club', 
										'cnk_sc_create_league',
										'cnk_sc_edit_club', 
										'cnk_sc_edit_league',
										'cnk_sc_edit_ranking',
										'cnk_sc_post_league',
										'cnk_sc_post_club',
										'cnk_sc_post_ranking',
										'cnk_sc_save_league',
										'cnk_sc_save_club',
										'cnk_sc_save_ranking',
										'cnk_sc_save_round',
										'cnk_sc_install',
										'cnk_sc_deinstall',
										'cnk_sc_pageby_league',
										'cnk_sc_pageby_club',
										'cnk_sc_pageby_ranking',
										'cnk_sc_pageby_rounds',
										'cnk_sc_multi_edit_league',
										'cnk_sc_multi_edit_club',
										'cnk_sc_multi_edit_ranking',
										'cnk_sc_multi_edit_rounds',
										'cnk_sc_add_round',
										'cnk_sc_edit_round')))
	{
		cnk_sc_config();
	} 
	else
	{
		$step();
	}
}

// ################################################################
/*
	Function: Renders Configuration Page
*/
// ################################################################
function cnk_sc_config($message = '')
{
	pagetop('Configure Soccer Plugin', $message);

	echo '<div style="margin: auto; text-align: center"><ul>';

	if (has_privs('cnk_soccer_admin')) echo '<li><a href="?event=cnk_soccer'.a.'step=cnk_sc_edit_league">League</a></li>';

	if (has_privs('cnk_soccer_admin')) echo '<li><a href="?event=cnk_soccer'.a.'step=cnk_sc_edit_club">Clubs</a></li>';

	if (has_privs('cnk_soccer_admin')) echo '<li><a href="?event=cnk_soccer'.a.'step=cnk_sc_edit_ranking">Table</a></li>';

	echo '<li><a href="?event=cnk_soccer'.a.'step=cnk_sc_add_round">Games</a></li>';

	echo '</ul>';

	echo '<ul style="margin-top: 100px">';		

	if (has_privs('cnk_soccer_admin')) echo '<li><a href="?event=cnk_soccer'.a.'step=cnk_sc_install">Install</a></li>';

	if (has_privs('cnk_soccer_admin')) echo '<li><a href="?event=cnk_soccer'.a.'step=cnk_sc_deinstall">Deinstall</a></li>';

	echo '</ul></div>';
}

// ################################################################
// ################################################################
// ################################################################
/*
	Club Functions
*/
// ################################################################
// ################################################################
// ################################################################

function cnk_sc_post_club()
{
	global $txpcfg,$vars;
	$varray = gpsa($vars);
        global $DB;

	extract(doSlash($varray));

	if ($logo_1)
	{
		$logo = '/images/'.$logo_1;
	}
	else if ($logo_2)
	{
		$logo = CNK_SC_IMG_BROWSE_FOLDER.$logo_2;
	}
	else
	{
		$logo = $logo_3;
	}

	$q = @safe_insert("cnk_soccer_clubs",
	   "name    = '$name',
		country = '$country',
		logo = '$logo'"
	);
	$GLOBALS['ID'] = mysqli_insert_id( );

	if ($q)
	{
		//update lastmod due to link feeds
		update_lastmod();

		$message = 'Club created: '.$name;
	}
	else
	{
		$message = 'Could not create club';
	}

	cnk_sc_edit_club($message);	
}

function cnk_sc_save_club() 
{
	global $txpcfg,$vars;
	$varray = gpsa($vars);

	extract(doSlash($varray));

	$id = assert_int($id);

	if ($logo_1)
	{
		$logo = '/images/'.$logo_1;
	}
	else if ($logo_2)
	{
		$logo = CNK_SC_IMG_BROWSE_FOLDER.$logo_2;
	}
	else
	{
		$logo = $logo_3;
	}

	$rs = @safe_update("cnk_soccer_clubs",
	   "name    = '$name',
		country = '$country',
		logo = '$logo'",
	   "id = $id"
	);

	if ($rs)
	{
		update_lastmod();

		$message = 'Club updated: '.doStrip($name);
	}
	else
	{
		$message = 'Could not save club';
	}

	cnk_sc_edit_club($message);	
}

function club_image_dropdown($filename = '') 
{
	$image_array = array();

	$rs = safe_rows_start("CONCAT(id, ext) as id, name as title", 'txp_image', "category = '".CNK_SC_IMG_UPLOAD_CATEGORY."' order by name asc");

	while ($r = nextRow($rs))
	{
		$image_array[$r['id']] = $r['title'];
	}

	return tda(selectInput('logo_1', $image_array, $filename, true));	
}

function club_folder_dropdown($filename = '') 
{
	$image_array = array();

	if (CNK_SC_IMG_BROWSE_FOLDER != '' && is_dir('..'.CNK_SC_IMG_BROWSE_FOLDER))
	{
		if ($handle = opendir('..'.CNK_SC_IMG_BROWSE_FOLDER)) 
		{
			while (false !== ($file = readdir($handle))) 
			{
				if ($file != "." && $file != "..") 
				{
					$image_array[$file] = $file;
				}
			}

			closedir($handle);
		}

		asort($image_array);
	}

	return tda(selectInput('logo_2', $image_array, $filename, true));	
}

function cnk_sc_edit_club($message = '')
{
	global $vars, $step;

	pagetop('Edit Club', $message);

	extract(gpsa($vars));

	if ($id && $step == 'cnk_sc_edit_club')
	{
		$id = assert_int($id);
		extract(safe_row('*', 'cnk_soccer_clubs', "id = $id"));
	}

	if ($step == 'cnk_sc_save_club' or $step == 'cnk_sc_post_club')
	{
		foreach ($vars as $var)
		{
			$$var = '';
		}
	}

	echo '<p class="prev-next"><a href="?event=cnk_soccer">< Back to Soccer Menu</a></p>'.

	form(

		startTable('edit') .

		tr(
			fLabelCell('Name').
			fInputCell('name', $name, 1, 30)
		).

		tr(
			fLabelCell('Country').
			fInputCell('country', $country, 2, 30)
		).

		tr(
			fLabelCell('1. Select logo from TXP').
			club_image_dropdown($logo)
		).

		tr(
			fLabelCell('2. Select logo from folder').
			club_folder_dropdown($logo)
		).

		tr(
			fLabelCell('3. Enter logo path').
			fInputCell('logo_3', $logo, 2, 30)
		).

		tr(
			td().
			td(
				fInput('submit', '', gTxt('save'), 'publish')
			)
		).

		endTable().

		eInput('cnk_soccer').

		($id ? sInput('cnk_sc_save_club').hInput('id', $id) : sInput('cnk_sc_post_club')).

		hInput('search_method', gps('search_method')).
		hInput('crit', gps('crit'))
	, 'margin-bottom: 25px;');

	echo cnk_sc_list_club();
}

function cnk_sc_list_club($message = '') 
{
	global $step, $cnk_sc_club_list_pageby;

	extract(get_prefs());

	extract(gpsa(array('page', 'sort', 'dir', 'crit', 'search_method')));

	$dir = ($dir == 'desc') ? 'desc' : 'asc';

	switch ($sort)
	{
		case 'id':
			$sort_sql = 'id '.$dir;
		break;

		case 'name':
			$sort_sql = 'name '.$dir.', id asc';
		break;

		case 'country':
			$sort_sql = 'country '.$dir.', name asc';
		break;

		default:
			$sort = 'country';
			$sort_sql = 'country '.$dir.', name asc';
		break;
	}

	$switch_dir = ($dir == 'desc') ? 'asc' : 'desc';

	$criteria = 1;

	if ($search_method and $crit)
	{
		$crit_escaped = doSlash($crit);

		$critsql = array(
			'id'			     => "id = '$crit_escaped'",
			'name'		     => "name like '%$crit_escaped%'",
			'country'	 => "country like '%$crit_escaped%'"
		);

		if (array_key_exists($search_method, $critsql))
		{
			$criteria = $critsql[$search_method];
			$limit = 500;
		}

		else
		{
			$search_method = '';
			$crit = '';
		}
	}

	else
	{
		$search_method = '';
		$crit = '';
	}

	$total = getCount('cnk_soccer_clubs', $criteria);  

	if ($total < 1)
	{
		if ($criteria != 1)
		{
			echo n.cnk_sc_search_form_club($crit, $search_method).
				n.graf(gTxt('no_results_found'), ' style="text-align: center;"');
		}

		else
		{
			echo n.graf('No club recorded', ' style="text-align: center;"');
		}

		return;
	}

	$limit = max(@$cnk_sc_club_list_pageby, 15);

	list($page, $offset, $numPages) = pager($total, $limit, $page);

	echo cnk_sc_search_form_club($crit, $search_method);

	$rs = safe_rows_start('*', 'cnk_soccer_clubs', "$criteria order by $sort_sql limit $offset, $limit");

	if ($rs)
	{
		echo n.n.'<form action="index.php" method="post" name="longform" onsubmit="return verify(\''.gTxt('are_you_sure').'\')">',

			startTable('list').

			n.tr(
				cnk_sc_column_head('ID', 'id', 'cnk_soccer', 'cnk_sc_edit_club', true, $switch_dir, $crit, $search_method, ('id' == $sort) ? $dir : '').
				cnk_sc_column_head('Country', 'country', 'cnk_soccer', 'cnk_sc_edit_club', true, $switch_dir, $crit, $search_method, ('country' == $sort) ? $dir : '').
				cnk_sc_column_head('name', 'name', 'cnk_soccer', 'cnk_sc_edit_club', true, $switch_dir, $crit, $search_method, ('name' == $sort) ? $dir : '').
				cnk_sc_column_head('Logo', 'logo').
				hCell()
			);

			while ($a = nextRow($rs))
			{
				extract($a);				

				$edit_url = '?event=cnk_soccer'.a.'step=cnk_sc_edit_club'.a.'id='.$id.a.'sort='.$sort.
					a.'dir='.$dir.a.'page='.$page.a.'search_method='.$search_method.a.'crit='.$crit;

				echo tr(

					n.td($id, 40).

					td(htmlspecialchars($country), 150).

					td(href(htmlspecialchars($name), $edit_url), 255).

					td('<img src="..'.$logo.'" />', 40).

					td(fInput('checkbox', 'selected[]', $id), 20)
				);
			}

		echo n.n.tr(
			tda(
				select_buttons().
				cnk_sc_multiedit_form_club($page, $sort, $dir, $crit, $search_method)
			, ' colspan="5" style="text-align: right; border: none;"')
		).

		endTable().
		'</form>'.

		n.cnk_sc_nav_form('cnk_soccer', 'cnk_sc_edit_club', $page, $numPages, $sort, $dir, $crit, $search_method).

		cnk_sc_pageby_form('cnk_soccer', 'cnk_sc_pageby_club', $cnk_sc_club_list_pageby).

		'<p class="prev-next"><a href="?event=cnk_soccer">< Back to Soccer Menu</a></p>';
	}
}



function cnk_sc_search_form_club($crit, $method)
{
	$methods =	array(
		'id'				=> gTxt('ID'),
		'name'				=> gTxt('name'),
		'country' 			=> 'Country'
	);

	return search_form('cnk_soccer', 'cnk_sc_edit_club', $crit, $methods, $method, 'name');
}

function cnk_sc_multiedit_form_club($page, $sort, $dir, $crit, $search_method) 
{
	$methods = array(
		'delete' => gTxt('delete')
	);

	return cnk_sc_event_multiedit_form('cnk_soccer', 'cnk_sc_multi_edit_club', $methods, $page, $sort, $dir, $crit, $search_method);
}

function cnk_sc_pageby_club() 
{
	event_change_pageby('cnk_sc_club');
	cnk_sc_edit_club();
}

function cnk_sc_multi_edit_club() 
{
	$deleted = cnk_sc_event_multi_edit('cnk_soccer_clubs', 'id');

	if ($deleted)
	{
		$message = 'Club deleted: '.$deleted;

		return 	cnk_sc_edit_club($message);
	}

	return 	cnk_sc_edit_club();
}

// ################################################################
// ################################################################
// ################################################################
/*
	LEAGUE Functions
*/
// ################################################################
// ################################################################
// ################################################################

function cnk_sc_post_league()
{
	global $txpcfg,$vars;
	$varray = gpsa($vars);
global $DB;

	extract(doSlash($varray));

	$q = @safe_insert("cnk_soccer_league",
	   "name    = '$name',
		season = '$season',
		total_rounds = '$total_rounds',
		games_per_round = '$games_per_round'"
	);

	$GLOBALS['ID'] = mysqli_insert_id( );

	if ($q)
	{
		//update lastmod due to link feeds
		update_lastmod();

		$message = 'League created: '.$name;	
	}
	else
	{
		$message = 'Could not create league';
	}

	cnk_sc_edit_league($message);
}

function cnk_sc_save_league() 
{
	global $txpcfg,$vars;
	$varray = gpsa($vars);

	extract(doSlash($varray));

	$id = assert_int($id);

	$rs = @safe_update("cnk_soccer_league",
	   "name    = '$name',
		season = '$season',
		total_rounds = '$total_rounds',
		games_per_round = '$games_per_round'",
	   "id = $id"
	);

	if ($rs)
	{
		update_lastmod();

		$message = 'League updated: '.doStrip($name);
	}
	else
	{
		$message = 'Could not save league';
	}

	cnk_sc_edit_league($message);
}

function cnk_sc_edit_league($message = '')
{
	global $vars, $step;

	pagetop('Edit League', $message);

	extract(gpsa($vars));

	if ($id && $step == 'cnk_sc_edit_league')
	{
		$id = assert_int($id);
		extract(safe_row('*', 'cnk_soccer_league', "id = $id"));
	}

	if ($step == 'cnk_sc_save_league' or $step == 'cnk_sc_post_league')
	{
		foreach ($vars as $var)
		{
			$$var = '';
		}
	}

	echo '<p class="prev-next"><a href="?event=cnk_soccer">< Back to Soccer Menu</a></p>'.

	form(

		startTable('edit') .

		tr(
			fLabelCell('Name').
			fInputCell('name', $name, 1, 30)
		).

		tr(
			fLabelCell('Season').
			fInputCell('season', $season, 2, 9)
		).

		tr(
			fLabelCell('Total rounds').
			fInputCell('total_rounds', $total_rounds, 3, 2)
		).

		tr(
			fLabelCell('Games per round').
			fInputCell('games_per_round', $games_per_round, 4, 2)
		).

		tr(
			td().
			td(
				fInput('submit', '', gTxt('save'), 'publish')
			)
		).

		endTable().

		eInput('cnk_soccer').

		($id ? sInput('cnk_sc_save_league').hInput('id', $id) : sInput('cnk_sc_post_league')).

		hInput('search_method', gps('search_method')).
		hInput('crit', gps('crit'))
	, 'margin-bottom: 25px;');

	echo cnk_sc_list_league();
}

function cnk_sc_list_league($message = '') 
{
	global $step, $cnk_sc_league_list_pageby;

	extract(get_prefs());

	extract(gpsa(array('page', 'sort', 'dir', 'crit', 'search_method')));

	$dir = ($dir == 'desc') ? 'desc' : 'asc';

	switch ($sort)
	{
		case 'id':
			$sort_sql = 'id '.$dir;
		break;

		case 'name':
			$sort_sql = 'name '.$dir.', id asc';
		break;

		case 'season':
			$sort_sql = 'season '.$dir.', id asc';
		break;

		default:
			$sort = 'name';
			$sort_sql = 'name '.$dir.', id asc';
		break;
	}

	$switch_dir = ($dir == 'desc') ? 'asc' : 'desc';

	$criteria = 1;

	if ($search_method and $crit)
	{
		$crit_escaped = doSlash($crit);

		$critsql = array(
			'id'			     => "id = '$crit_escaped'",
			'name'		     => "name like '%$crit_escaped%'",
			'season'	 => "season like '%$crit_escaped%'"
		);

		if (array_key_exists($search_method, $critsql))
		{
			$criteria = $critsql[$search_method];
			$limit = 500;
		}

		else
		{
			$search_method = '';
			$crit = '';
		}
	}

	else
	{
		$search_method = '';
		$crit = '';
	}

	$total = getCount('cnk_soccer_league', $criteria);  

	if ($total < 1)
	{
		if ($criteria != 1)
		{
			echo n.cnk_sc_search_form_league($crit, $search_method).
				n.graf(gTxt('no_results_found'), ' style="text-align: center;"');
		}

		else
		{
			echo n.graf('No league recorded', ' style="text-align: center;"');
		}

		return;
	}

	$limit = max(@$cnk_sc_league_list_pageby, 15);

	list($page, $offset, $numPages) = pager($total, $limit, $page);

	echo cnk_sc_search_form_league($crit, $search_method);

	$rs = safe_rows_start('*', 'cnk_soccer_league', "$criteria order by $sort_sql limit $offset, $limit");

	if ($rs)
	{
		echo n.n.'<form action="index.php" method="post" name="longform" onsubmit="return verify(\''.gTxt('are_you_sure').'\')">',

			startTable('list').

			n.tr(
				cnk_sc_column_head('ID', 'id', 'cnk_soccer', 'cnk_sc_edit_league', true, $switch_dir, $crit, $search_method, ('id' == $sort) ? $dir : '').
				cnk_sc_column_head('name', 'name', 'cnk_soccer', 'cnk_sc_edit_league', true, $switch_dir, $crit, $search_method, ('name' == $sort) ? $dir : '').
				cnk_sc_column_head('Season', 'season', 'cnk_soccer', 'cnk_sc_edit_league', true, $switch_dir, $crit, $search_method, ('season' == $sort) ? $dir : '').
				cnk_sc_column_head('Total rounds', 'total_rounds').
				cnk_sc_column_head('Games/ Round', 'games_per_round').
				hCell()
			);

			while ($a = nextRow($rs))
			{
				extract($a);				

				$edit_url = '?event=cnk_soccer'.a.'step=cnk_sc_edit_league'.a.'id='.$id.a.'sort='.$sort.
					a.'dir='.$dir.a.'page='.$page.a.'search_method='.$search_method.a.'crit='.$crit;

				echo tr(

					n.td($id, 40).

					td(href(htmlspecialchars($name), $edit_url), 255).

					td(htmlspecialchars($season), 150).

					td(htmlspecialchars($total_rounds), 40).

					td(htmlspecialchars($games_per_round), 40).

					td(fInput('checkbox', 'selected[]', $id), 20)
				);
			}

		echo n.n.tr(
			tda(
				select_buttons().
				cnk_sc_multiedit_form_league($page, $sort, $dir, $crit, $search_method)
			, ' colspan="6" style="text-align: right; border: none;"')
		).

		endTable().
		'</form>'.

		n.cnk_sc_nav_form('cnk_soccer', 'cnk_sc_edit_league', $page, $numPages, $sort, $dir, $crit, $search_method).

		cnk_sc_pageby_form('cnk_soccer', 'cnk_sc_pageby_league', $cnk_sc_league_list_pageby).

		'<p class="prev-next"><a href="?event=cnk_soccer">< Back to Soccer Menu</a></p>';
	}
}



function cnk_sc_search_form_league($crit, $method)
{
	$methods =	array(
		'id'				=> gTxt('ID'),
		'name'				=> gTxt('name'),
		'season' 			=> 'Season'
	);

	return search_form('cnk_soccer', 'cnk_sc_edit_league', $crit, $methods, $method, 'name');
}

function cnk_sc_multiedit_form_league($page, $sort, $dir, $crit, $search_method) 
{
	$methods = array(
		'delete' => gTxt('delete')
	);

	return cnk_sc_event_multiedit_form('cnk_soccer', 'cnk_sc_multi_edit_league', $methods, $page, $sort, $dir, $crit, $search_method);
}

function cnk_sc_pageby_league() 
{
	event_change_pageby('cnk_sc_league');
	cnk_sc_edit_league();
}

function cnk_sc_multi_edit_league() 
{
	$deleted = cnk_sc_event_multi_edit('cnk_soccer_league', 'id');

	if ($deleted)
	{
		$message = 'League deleted: '.$deleted;

		return 	cnk_sc_edit_league($message);
	}

	return 	cnk_sc_edit_league();
}

// ################################################################
// ################################################################
// ################################################################
/*
	Ranking Functions
*/
// ################################################################
// ################################################################
// ################################################################

function cnk_sc_post_ranking()
{
	global $txpcfg,$vars;
	$varray = gpsa($vars);

	extract(doSlash($varray));

	$check = getCount('cnk_soccer_club_to_league', "club_id = '$club_id' AND league_id = '$league_id'");

	if (!$check)
	{
		$q = safe_insert("cnk_soccer_club_to_league",
		   "club_id    = '$club_id',
			league_id = '$league_id'"
		);

		$GLOBALS['ID'] = $club_id;

		if ($q)
		{
			//update lastmod due to link feeds
			update_lastmod();

			$message = 'Rank created: '.$club_id.'_'.$league_id;
		}
		else
		{
			$message = 'Database Error';
		}
	}
	else
	{
		$message = 'Club is already assigned to this league.';
	}

	cnk_sc_edit_ranking($message);	
}

function cnk_sc_save_ranking() 
{
	global $txpcfg,$vars;
	$varray = gpsa($vars);

	extract(doSlash($varray));

	$club_id = assert_int($club_id);
	$league_id = assert_int($league_id);
	$goals_for = assert_int($goals_for);
	$goals_against = assert_int($goals_against);
	$game_won = assert_int($game_won);
	$game_lost = assert_int($game_lost);
	$game_draw = assert_int($game_draw);

	if ($club_id != $old_club_id || $league_id != $old_league_id)
	{
		$check = getCount('cnk_soccer_club_to_league', "club_id = '$club_id' AND league_id = '$league_id'");
	}
	else
	{
		$check = false;
	}

	if (!$check)
	{
		$rs = safe_update("cnk_soccer_club_to_league",
		   "points    = '$points',
		    goals_for = '$goals_for',
		    goals_against = '$goals_against',
		    game_won = '$game_won',
		    game_draw = '$game_draw',
		    game_lost = '$game_lost',
			club_id = '$club_id',
			league_id = '$league_id'",
		   "club_id = $club_id AND league_id = $league_id"
		);

		if ($rs)
		{
			update_lastmod();

			$message = 'Rank updated: '.doStrip($name);
		}
		else
		{
			$message = 'Database Error';
		}
	}
	else
	{
		$message = 'Club is already assigned to this league.';
	}

	cnk_sc_edit_ranking($message);	
}

// TODO: redundant functions

function ranking_club_dropdown($club_id = '', $help = '') 
{
	$club_array = array();

	$rs = safe_rows_start("id, CONCAT(country, ' - ', name) as title", 'cnk_soccer_clubs', "1=1 order by country asc, name asc");

	while ($r = nextRow($rs))
	{
		$club_array[$r['id']] = $r['title'];
	}

	return tda(selectInput('club_id', $club_array, $club_id).$help);	
}

function ranking_league_dropdown($league_id = '', $help = '') 
{
	$league_array = array();

	$rs = safe_rows_start("id, CONCAT(name, ' - ', season) as title", 'cnk_soccer_league', "1=1 order by name asc, season asc");

	while ($r = nextRow($rs))
	{
		$league_array[$r['id']] = $r['title'];
	}

	return tda(selectInput('league_id', $league_array, $league_id).$help);	
}

function cnk_sc_edit_ranking($message = '')
{
	global $vars, $step;

	pagetop('Edit Rank', $message);

	extract(gpsa($vars));

	if ($club_id && $league_id && $step == 'cnk_sc_edit_ranking')
	{
		$club_id = assert_int($club_id);
		$league_id = assert_int($league_id);
		extract(safe_row('*', 'cnk_soccer_club_to_league', "club_id = $club_id AND league_id = $league_id"));
	}

	if ($step == 'cnk_sc_save_ranking' or $step == 'cnk_sc_post_ranking')
	{
		foreach ($vars as $var)
		{
			$$var = '';
		}
	}

	echo '<p class="prev-next"><a href="?event=cnk_soccer">< Back to Soccer Menu</a></p>'.

	form(

		startTable('edit') .

		tr(
			fLabelCell('Club').
			ranking_club_dropdown($club_id, ' ['.eLink('cnk_soccer', 'cnk_sc_edit_club', '', '', gTxt('edit')).']')
		).

		tr(
			fLabelCell('League').
			ranking_league_dropdown($league_id, ' ['.eLink('cnk_soccer', 'cnk_sc_edit_league', '', '', gTxt('edit')).']')
		).

		($club_id?
			tr(
				fLabelCell('Points').
				fInputCell('points', $points, 3, 3)
			).

			tr(
				fLabelCell('Goals for').
				fInputCell('goals_for', $goals_for, 4, 3)
			).

			tr(
				fLabelCell('Goals against').
				fInputCell('goals_against', $goals_against, 5, 3)
			).

			tr(
				fLabelCell('Games won').
				fInputCell('game_won', $game_won, 6, 3)
			).

			tr(
				fLabelCell('Games draw').
				fInputCell('game_draw', $game_draw, 7, 3)
			).

			tr(
				fLabelCell('Games lost').
				fInputCell('game_lost', $game_lost, 8, 3)
			)
		:'').

		tr(
			td().
			td(
				fInput('submit', '', gTxt('save'), 'publish')
			)
		).

		endTable().

		eInput('cnk_soccer').

		($club_id ? sInput('cnk_sc_save_ranking').hInput('old_club_id', $club_id).hInput('old_league_id', $league_id) : sInput('cnk_sc_post_ranking')).

		hInput('search_method', gps('search_method')).
		hInput('crit', gps('crit'))
	, 'margin-bottom: 25px;');

	echo cnk_sc_list_ranking();
}

function cnk_sc_list_ranking($message = '') 
{
	global $step, $cnk_sc_ranking_list_pageby;

	extract(get_prefs());

	extract(gpsa(array('page', 'sort', 'dir', 'crit', 'search_method')));

	$dir = ($dir == 'desc') ? 'desc' : 'asc';

	switch ($sort)
	{
		case 'club':
			$sort_sql = 'cnk_soccer_clubs.name '.$dir.', cnk_soccer_league.name asc, cnk_soccer_league.season asc';
		break;

		case 'league':
			$sort_sql = 'cnk_soccer_league.name '.$dir.', cnk_soccer_league.season asc, cnk_soccer_clubs.name asc';
		break;

		default:
			$sort = 'club';
			$sort_sql = 'cnk_soccer_clubs.name '.$dir.', cnk_soccer_league.name asc, cnk_soccer_league.season asc';
		break;
	}

	$switch_dir = ($dir == 'desc') ? 'asc' : 'desc';

	$criteria = 1;

	if ($search_method and $crit)
	{
		$crit_escaped = doSlash($crit);

		$critsql = array(
			'club'			     => "cnk_soccer_clubs.name like '%$crit_escaped%'",
			'league'		     => "cnk_soccer_league.name like '%$crit_escaped%'"
		);

		if (array_key_exists($search_method, $critsql))
		{
			$criteria = $critsql[$search_method];
			$limit = 500;
		}

		else
		{
			$search_method = '';
			$crit = '';
		}
	}

	else
	{
		$search_method = '';
		$crit = '';
	}

	$total = getCount('cnk_soccer_club_to_league,cnk_soccer_clubs,cnk_soccer_league', "cnk_soccer_club_to_league.club_id = cnk_soccer_clubs.id AND cnk_soccer_club_to_league.league_id = cnk_soccer_league.id AND ".$criteria);  

	if ($total < 1)
	{
		if ($criteria != 1)
		{
			echo n.cnk_sc_search_form_ranking($crit, $search_method).
				n.graf(gTxt('no_results_found'), ' style="text-align: center;"');
		}

		else
		{
			echo n.graf('No rank recorded', ' style="text-align: center;"');
		}

		return;
	}

	$limit = max(@$cnk_sc_ranking_list_pageby, 15);

	list($page, $offset, $numPages) = pager($total, $limit, $page);

	echo cnk_sc_search_form_ranking($crit, $search_method);

	$rs = safe_rows_start("cnk_soccer_club_to_league.*, cnk_soccer_clubs.name as club, CONCAT(cnk_soccer_league.name, ' - ', cnk_soccer_league.season) as league", 'cnk_soccer_club_to_league,cnk_soccer_clubs,cnk_soccer_league', "cnk_soccer_club_to_league.club_id = cnk_soccer_clubs.id AND cnk_soccer_club_to_league.league_id = cnk_soccer_league.id AND $criteria order by $sort_sql limit $offset, $limit");

	if ($rs)
	{
		echo n.n.'<form action="index.php" method="post" name="longform" onsubmit="return verify(\''.gTxt('are_you_sure').'\')">',

			startTable('list').

			n.tr(
				cnk_sc_column_head('Club', 'name', 'cnk_soccer', 'cnk_sc_edit_ranking', true, $switch_dir, $crit, $search_method, ('club' == $sort) ? $dir : '').
				cnk_sc_column_head('League - Season', 'league', 'cnk_soccer', 'cnk_sc_edit_ranking', true, $switch_dir, $crit, $search_method, ('league' == $sort) ? $dir : '').
				cnk_sc_column_head('Points', 'points').
				cnk_sc_column_head('Goals for', 'goals_for').
				cnk_sc_column_head('Goals against', 'goals_against').
				cnk_sc_column_head('Games won', 'game_won').
				cnk_sc_column_head('Games draw', 'game_draw').
				cnk_sc_column_head('Games lost', 'game_lost').
				hCell()
			);

			while ($a = nextRow($rs))
			{
				extract($a);				

				$edit_url = '?event=cnk_soccer'.a.'step=cnk_sc_edit_ranking'.a.'club_id='.$club_id.a.'league_id='.$league_id.a.'sort='.$sort.
					a.'dir='.$dir.a.'page='.$page.a.'search_method='.$search_method.a.'crit='.$crit;

				echo tr(

					n.td(href(htmlspecialchars($club), $edit_url), 255).

					td($league, 255).

					td('<strong>'.$points.'</strong>', 20).

					td('<strong>'.$goals_for.'</strong>', 20).

					td('<strong>'.$goals_against.'</strong>', 20).

					td('<strong>'.$game_won.'</strong>', 20).

					td('<strong>'.$game_draw.'</strong>', 20).

					td('<strong>'.$game_lost.'</strong>', 20).

					td(fInput('checkbox', 'selected[]', $club_id.'_'.$league_id), 20)
				);
			}

		echo n.n.tr(
			tda(
				select_buttons().
				cnk_sc_multiedit_form_ranking($page, $sort, $dir, $crit, $search_method)
			, ' colspan="4" style="text-align: right; border: none;"')
		).

		endTable().
		'</form>'.

		n.cnk_sc_nav_form('cnk_soccer', 'cnk_sc_edit_ranking', $page, $numPages, $sort, $dir, $crit, $search_method).

		cnk_sc_pageby_form('cnk_soccer', 'cnk_sc_pageby_ranking', $cnk_sc_ranking_list_pageby).

		'<p class="prev-next"><a href="?event=cnk_soccer">< Back to Soccer Menu</a></p>';
	}
}



function cnk_sc_search_form_ranking($crit, $method)
{
	$methods =	array(
		'club'				=> 'Club',
		'league'			=> 'League'
	);

	return search_form('cnk_soccer', 'cnk_sc_edit_ranking', $crit, $methods, $method, 'name');
}

function cnk_sc_multiedit_form_ranking($page, $sort, $dir, $crit, $search_method) 
{
	$methods = array(
		'delete' => gTxt('delete')
	);

	return cnk_sc_event_multiedit_form('cnk_soccer', 'cnk_sc_multi_edit_ranking', $methods, $page, $sort, $dir, $crit, $search_method);
}

function cnk_sc_pageby_ranking() 
{
	event_change_pageby('cnk_sc_ranking');
	cnk_sc_edit_ranking();
}

function cnk_sc_multi_edit_ranking() 
{
	$deleted = cnk_sc_event_multi_edit('cnk_soccer_club_to_league', 'club_id|league_id');

	if ($deleted)
	{
		$message = 'Rank deleted: '.$deleted;

		return 	cnk_sc_edit_ranking($message);
	}

	return 	cnk_sc_edit_ranking();
}

// ################################################################
// ################################################################
// ################################################################
/*
	Round Functions
*/
// ################################################################
// ################################################################
// ################################################################

function cnk_sc_save_round()
{
	global $txpcfg,$vars;
	$varray = gpsa($vars);
	$wrong_games = array();
	$wrong_ids = array();

	extract(doSlash($varray));

	$i = 0;

	while ($i < $games_per_round)
	{
		$i++;

		$club1_id = gps('club1_id_'.$i);
		$club2_id = gps('club2_id_'.$i);
		$kickoff_time = gps('kickoff_time_'.$i);
		$goals_club1 = gps('goals_club1_'.$i); 
		$goals_club2 = gps('goals_club2_'.$i);
		$id = gps('id_'.$i);

		$game = array
		(
			'id' => $id,
			'club1_id' => $club1_id,
			'club2_id' => $club2_id,
			'kickoff_time' => $kickoff_time,
			'goals_club1' => $goals_club1,
			'goals_club2' => $goals_club2		
		);

		if (trim($goals_club1) == '') $goals_club1 = 'NULL';
		if (trim($goals_club2) == '') $goals_club2 = 'NULL';

		if ((is_numeric($goals_club1) || $goals_club1 == 'NULL') && (is_numeric($goals_club2) || $goals_club2 == 'NULL'))
		{						
			// check if clubs are set

			if ($club1_id && $club2_id && $club1_id != $club2_id)
			{
				// check if club is already assigned to a game in this round

				$c = getCount('cnk_soccer_games', "id <> '".$id."' AND league_id = ".$league_id." AND round = ".$round." AND (club1_id in (".$club1_id.", ".$club2_id.") OR club2_id in (".$club1_id.", ".$club2_id."))");

				if (!$c)
				{
					$rs = false;
					$goals = false;

					if (has_privs('cnk_soccer_admin'))
					{
						if ($id)
						{
							// get previous goals
							$goals = safe_row('club1_id, club2_id, goals_club1, goals_club2', 'cnk_soccer_games', 'id = '.$id);

							$rs = safe_update("cnk_soccer_games",
							   "club1_id    = '$club1_id',
								club2_id    = '$club2_id',
								goals_club1    = $goals_club1,
								goals_club2    = $goals_club2,
								kickoff_time    = '$kickoff_time',
								status = '1'",
							   "id = $id"
							);
						}
						else
						{
							$rs = safe_insert("cnk_soccer_games",
							   "club1_id    = '$club1_id',
								club2_id    = '$club2_id',
								goals_club1    = $goals_club1,
								goals_club2    = $goals_club2,
								kickoff_time    = '$kickoff_time',
								league_id = '$league_id',
								status = '1',
								round = '$round'"
							);
						}
					}
					else
					{
						if ($id)
						{
							// get previous goals
							$goals = safe_row('club1_id, club2_id, goals_club1, goals_club2', 'cnk_soccer_games', 'id = '.$id);

							$rs = safe_update("cnk_soccer_games",
							   "goals_club1    = $goals_club1,
								goals_club2    = $goals_club2,
								status = '1'",
							   "id = $id AND status IS NULL"
							);
						}
					}

					if ($rs)
					{				
						// correct stats
						if ($goals && $goals['goals_club1'] != '' && $goals['goals_club2'] != '')
						{
							if ($goals['goals_club1'] > $goals['goals_club2'])
							{
								$rs = safe_update("cnk_soccer_club_to_league",
									"points = points - 3,
									goals_for = goals_for - ".$goals['goals_club1'].",
									goals_against = goals_against - ".$goals['goals_club2'].",
									game_won = game_won - 1",
									"club_id = ".$goals['club1_id']." AND league_id = $league_id");

								$rs = safe_update("cnk_soccer_club_to_league",
									"goals_for = goals_for - ".$goals['goals_club2'].",
									goals_against = goals_against - ".$goals['goals_club1'].",
									game_lost = game_lost - 1",
									"club_id = ".$goals['club2_id']." AND league_id = $league_id");
							} 
							else if ($goals['goals_club1'] < $goals['goals_club2'])
							{
								$rs = safe_update("cnk_soccer_club_to_league",
									"points = points - 3,
									goals_for = goals_for - ".$goals['goals_club2'].",
									goals_against = goals_against - ".$goals['goals_club1'].",
									game_won = game_won - 1",
									"club_id = ".$goals['club2_id']." AND league_id = $league_id");

								$rs = safe_update("cnk_soccer_club_to_league",
									"goals_for = goals_for - ".$goals['goals_club1'].",
									goals_against = goals_against - ".$goals['goals_club2'].",
									game_lost = game_lost - 1",
									"club_id = ".$goals['club1_id']." AND league_id = $league_id");
							}
							else if ($goals['goals_club1'] == $goals['goals_club2'])
							{
								$rs = safe_update("cnk_soccer_club_to_league",
									"points = points - 1,
									goals_for = goals_for - ".$goals['goals_club1'].",
									goals_against = goals_against - ".$goals['goals_club2'].",
									game_draw = game_draw - 1",
									"club_id = ".$goals['club1_id']." AND league_id = $league_id");

								$rs = safe_update("cnk_soccer_club_to_league",
									"points = points - 1,
									goals_for = goals_for - ".$goals['goals_club2'].",
									goals_against = goals_against - ".$goals['goals_club1'].",
									game_draw = game_draw - 1",
									"club_id = ".$goals['club2_id']." AND league_id = $league_id");
							}
						}

						if ($goals_club1 != 'NULL' && $goals_club2 != 'NULL')
						{				
							if ($goals_club1 > $goals_club2)
							{
								$rs = safe_update("cnk_soccer_club_to_league",
									"points = points + 3,
									goals_for = goals_for + ".$goals_club1.",
									goals_against = goals_against + ".$goals_club2.",
									game_won = game_won + 1",
									"club_id = ".$club1_id." AND league_id = $league_id");

								$rs = safe_update("cnk_soccer_club_to_league",
									"goals_for = goals_for + ".$goals_club2.",
									goals_against = goals_against + ".$goals_club1.",
									game_lost = game_lost + 1",
									"club_id = ".$club2_id." AND league_id = $league_id");
							} 
							else if ($goals_club1 < $goals_club2)
							{
								$rs = safe_update("cnk_soccer_club_to_league",
									"points = points + 3,
									goals_for = goals_for + ".$goals_club2.",
									goals_against = goals_against + ".$goals_club1.",
									game_won = game_won + 1",
									"club_id = ".$club2_id." AND league_id = $league_id");

								$rs = safe_update("cnk_soccer_club_to_league",
									"goals_for = goals_for + ".$goals_club1.",
									goals_against = goals_against + ".$goals_club2.",
									game_lost = game_lost + 1",
									"club_id = ".$club1_id." AND league_id = $league_id");
							}
							else if ($goals_club1 == $goals_club2)
							{
								$rs = safe_update("cnk_soccer_club_to_league",
									"points = points + 1,
									goals_for = goals_for + ".$goals_club1.",
									goals_against = goals_against + ".$goals_club2.",
									game_draw = game_draw + 1",
									"club_id = ".$club1_id." AND league_id = $league_id");

								$rs = safe_update("cnk_soccer_club_to_league",
									"points = points + 1,
									goals_for = goals_for + ".$goals_club2.",
									goals_against = goals_against + ".$goals_club1.",
									game_draw = game_draw + 1",
									"club_id = ".$club2_id." AND league_id = $league_id");
							}
						}
					}
				}
				else
				{
					$game['error'] = 'One of the clubs is already assigned to a game in this round.';
					array_push($wrong_games, $game);
					if ($id) $wrong_ids[$id] = true;
				}
			}
			else if ($club1_id || $club2_id || $id || $kickoff_time)
			{
				$game['error'] = 'Something is wrong with your club selections.';
				array_push($wrong_games, $game);
				if ($id) $wrong_ids[$id] = true;
			}
		}
		else
		{
			$game['error'] = 'Goals only accept numeric values.';
			array_push($wrong_games, $game);
			if ($id) $wrong_ids[$id] = true;
		}
	}

	//update lastmod due to link feeds
	update_lastmod();

	cnk_sc_edit_round('', $wrong_games, $wrong_ids);			
}

function cnk_sc_edit_round($message = '', $wrong_games = array(), $wrong_ids = array())
{
	global $vars, $step;

	pagetop('Edit Round', $message);

	extract(gpsa($vars));

	echo '<p class="prev-next"><a href="?event=cnk_soccer'.a.'step=cnk_sc_add_round">< Back to Rounds</a></p>';

	if ($round)
	{
		$round = assert_int($round);

		echo '<p class="prev-next">Round No. '.$round.'</p>'. // TODO: add league name

		n.n.'<form action="index.php" method="post" name="longform">',

			startTable('list').

				n.tr(
					cnk_sc_column_head('Home Team', 'club_1').
					cnk_sc_column_head('Away Team', 'club_2').
					cnk_sc_column_head('Kickoff (YYYY-MM-DD HH:MM)', 'kickoff_time').
					cnk_sc_column_head('Goals Home', 'goals_club1').
					cnk_sc_column_head('Goals Away', 'goals_club2')
				//	cnk_sc_column_head('Add points', 'goals_club2')
				);

				$games = safe_rows('*', 'cnk_soccer_games', "round = $round ORDER BY KICKOFF_TIME ASC");

				$i = 0;
				$a = 0;

				$games_per_round = has_privs('cnk_soccer_admin')?getThing('SELECT games_per_round FROM '.safe_pfx('cnk_soccer_league').' WHERE id = '.$league_id):count($games);

				while ($i < $games_per_round)
				{				
					if (isset($games[$i]) && !isset($wrong_ids[$games[$i]['id']]))
					{ 
						extract($games[$i]);
						$error = '';
					}
					else if (isset($wrong_games[$a]))
					{
						extract($wrong_games[$a]);
						$a++;
					}
					else
					{
						$club1_id = NULL;
						$club2_id = NULL;
						$kickoff_time = '';
						$goals_club1 = ''; 
						$goals_club2 = '';
					//	$status = '';
						$id = '';
						$error = '';
					}

					$i++;

					echo tr(

						round_club_dropdown($club1_id, 'club1_id_'.$i, $league_id, !has_privs('cnk_soccer_admin')).

						round_club_dropdown($club2_id, 'club2_id_'.$i, $league_id, !has_privs('cnk_soccer_admin')).

						(has_privs('cnk_soccer_admin')?tda(fInput('text', 'kickoff_time_'.$i, $kickoff_time, 'edit', '', '', 16, 1)):tda($kickoff_time.hInput('club1_id_'.$i, $club1_id).hInput('club2_id_'.$i, $club2_id))).

						tda(fInput('text', 'goals_club1_'.$i, $goals_club1, 'edit', '', '', 2, 2)).

						tda(fInput('text', 'goals_club2_'.$i, $goals_club2, 'edit', '', '', 2, 3).hInput('id_'.$i, $id))

					//	td(($status==''?fInput('checkbox', 'add_points_'.$i, '1'):'added').hInput('id_'.$i, $id), 120)
					, $error?' style="background-color:red" title="'.$error.'"':'');
				}

		//	if (has_privs('cnk_soccer_admin'))

				echo tr(
					td().
					td().
					td().
					td().
				//	td().
					td(
						fInput('submit', '', gTxt('save'), 'publish')
					)
				).

			endTable().

		eInput('cnk_soccer').

		sInput('cnk_sc_save_round').

		hInput('round', $round).
		hInput('league_id', $league_id).
		hInput('games_per_round', $games_per_round).

		'</form>';
	}
}

function round_league_dropdown($league_id = '', $help = '') 
{
	$league_array = array();

	$rs = safe_rows_start("id, CONCAT(name, ' - ', season) as title", 'cnk_soccer_league', "1=1 order by name asc, season asc");

	while ($r = nextRow($rs))
	{
		$league_array[$r['id']] = $r['title'];
	}

	return tda(selectInput('league_id', $league_array, $league_id).$help);	
}

function round_club_dropdown($club_id = '', $name, $league_id, $readonly = false) 
{
	global $club_array;

	if ($club_array === NULL)
	{
		$club_array = array();

		$rs = safe_rows_start("cnk_soccer_clubs.id, cnk_soccer_clubs.name as title", 'cnk_soccer_clubs,cnk_soccer_club_to_league', "cnk_soccer_clubs.id = cnk_soccer_club_to_league.club_id AND cnk_soccer_club_to_league.league_id = $league_id order by cnk_soccer_clubs.name asc");

		while ($r = nextRow($rs))
		{
			$club_array[$r['id']] = $r['title'];
		}
	}

	if ($readonly)
	{
		return tda($club_array[$club_id]);
	}
	else
	{
		return tda(selectInput($name, $club_array, $club_id, true));	
	}
}

function cnk_sc_add_round($message = '')
{
	global $vars, $step;

	pagetop('Add round', $message);

	echo '<p class="prev-next"><a href="?event=cnk_soccer">< Back to Soccer Menu</a></p>';

	if (has_privs('cnk_soccer_admin'))
	{
		echo form(

			startTable('edit') .

			tr(
				fLabelCell('League').
				round_league_dropdown('', ' ['.eLink('cnk_soccer', 'cnk_sc_edit_league', '', '', gTxt('edit')).']')
			).

			tr(
				fLabelCell('Round #').
				fInputCell('round', '', 2, 2)
			).

			tr(
				td().
				td(
					fInput('submit', '', 'Add', 'publish')
				)
			).

			endTable().

			eInput('cnk_soccer').

			sInput('cnk_sc_edit_round').

			hInput('search_method', gps('search_method')).
			hInput('crit', gps('crit'))
		, 'margin-bottom: 25px;');
	}

	echo cnk_sc_list_rounds();
}

function cnk_sc_list_rounds($message = '') 
{
	global $step, $cnk_sc_rounds_list_pageby;

	extract(get_prefs());

	extract(gpsa(array('page', 'sort', 'dir', 'crit', 'search_method')));

	$dir = ($dir == 'desc') ? 'desc' : 'asc';

	switch ($sort)
	{
		case 'league':
			$sort_sql = 'cnk_soccer_league.name '.$dir.', cnk_soccer_league.season asc, cnk_soccer_games.round desc';
		break;

		case 'season':
			$sort_sql = 'cnk_soccer_league.season '.$dir.', cnk_soccer_league.name asc, cnk_soccer_games.round desc';
		break;

		case 'round':
			$sort_sql = 'cnk_soccer_games.round '.$dir.', cnk_soccer_league.name asc, cnk_soccer_league.season asc';
		break;

		default:
			$sort = 'league';
			$sort_sql = 'cnk_soccer_league.name '.$dir.', cnk_soccer_league.season asc, cnk_soccer_games.round desc';
		break;
	}

	$switch_dir = ($dir == 'desc') ? 'asc' : 'desc';

	$criteria = 1;

	if ($search_method and $crit)
	{
		$crit_escaped = doSlash($crit);

		$critsql = array(
			'league'		     => "cnk_soccer_league.name like '%$crit_escaped%'",
			'season'	 => "cnk_soccer_league.season like '%$crit_escaped%'",
			'round'		     => "cnk_soccer_games.round = '$crit_escaped'"
		);

		if (array_key_exists($search_method, $critsql))
		{
			$criteria = $critsql[$search_method];
			$limit = 500;
		}

		else
		{
			$search_method = '';
			$crit = '';
		}
	}

	else
	{
		$search_method = '';
		$crit = '';
	}

	$total = getCount('cnk_soccer_games,cnk_soccer_league', "cnk_soccer_league.id = cnk_soccer_games.league_id AND ".$criteria." GROUP BY league_id");

	if ($total < 1)
	{
		if ($criteria != 1)
		{
			echo n.cnk_sc_search_form_league($crit, $search_method).
				n.graf(gTxt('no_results_found'), ' style="text-align: center;"');
		}

		else
		{
			echo n.graf('No round recorded', ' style="text-align: center;"');
		}

		return;
	}

	$limit = max(@$cnk_sc_rounds_list_pageby, 15);

	list($page, $offset, $numPages) = pager($total, $limit, $page);

	echo cnk_sc_search_form_rounds($crit, $search_method);

	$rs = safe_rows_start("cnk_soccer_games.round, cnk_soccer_games.league_id, cnk_soccer_league.name as league, cnk_soccer_league.season as season", 'cnk_soccer_games,cnk_soccer_league', "cnk_soccer_games.league_id = cnk_soccer_league.id AND $criteria group by $sort_sql limit $offset, $limit");

	if ($rs)
	{
		echo n.n.'<form action="index.php" method="post" name="longform" onsubmit="return verify(\''.gTxt('are_you_sure').'\')">',

			startTable('list').

			n.tr(
				cnk_sc_column_head('League', 'league', 'cnk_soccer', 'cnk_sc_add_round', true, $switch_dir, $crit, $search_method, ('league' == $sort) ? $dir : '').
				cnk_sc_column_head('Season', 'season', 'cnk_soccer', 'cnk_sc_add_round', true, $switch_dir, $crit, $search_method, ('season' == $sort) ? $dir : '').
				cnk_sc_column_head('Round #', 'round', 'cnk_soccer', 'cnk_sc_add_round', true, $switch_dir, $crit, $search_method, ('round' == $sort) ? $dir : '').
				hCell().
				hCell()
			);

			while ($a = nextRow($rs))
			{
				extract($a);				

				$edit_url = '?event=cnk_soccer'.a.'step=cnk_sc_edit_round'.a.'round='.$round.a.'league_id='.$league_id.a.'sort='.$sort.
					a.'dir='.$dir.a.'page='.$page.a.'search_method='.$search_method.a.'crit='.$crit;

				echo tr(

					td(htmlspecialchars($league), 255).

					td(htmlspecialchars($season), 150).

					td(htmlspecialchars($round), 150).

					td(href(gTxt('edit'), $edit_url), 40).

					td(has_privs('cnk_soccer_admin')?fInput('checkbox', 'selected[]', $round.'_'.$league_id):'', 20)
				);
			}

		if (has_privs('cnk_soccer_admin'))
		{
			echo n.n.tr(
				tda(
					select_buttons().
					cnk_sc_multiedit_form_rounds($page, $sort, $dir, $crit, $search_method)
				, ' colspan="4" style="text-align: right; border: none;"')
			);
		}

		echo endTable().
		'</form>'.

		n.cnk_sc_nav_form('cnk_soccer', 'cnk_sc_add_round', $page, $numPages, $sort, $dir, $crit, $search_method).

		cnk_sc_pageby_form('cnk_soccer', 'cnk_sc_pageby_rounds', $cnk_sc_rounds_list_pageby).

		'<p class="prev-next"><a href="?event=cnk_soccer">< Back to Soccer Menu</a></p>';
	}
}



function cnk_sc_search_form_rounds($crit, $method)
{
	$methods =	array(
		'league'				=> 'League',
		'round'				=> 'Round',
		'season' 			=> 'Season'
	);

	return search_form('cnk_soccer', 'cnk_sc_add_round', $crit, $methods, $method, 'name');
}

function cnk_sc_multiedit_form_rounds($page, $sort, $dir, $crit, $search_method) 
{
	$out = '';

	$methods = array(
		'delete' => gTxt('delete')
	);

	return cnk_sc_event_multiedit_form('cnk_soccer', 'cnk_sc_multi_edit_rounds', $methods, $page, $sort, $dir, $crit, $search_method);
}

function cnk_sc_pageby_rounds() 
{
	event_change_pageby('cnk_sc_rounds');
	cnk_sc_add_round();
}

function cnk_sc_multi_edit_rounds() 
{
	$deleted = cnk_sc_event_multi_edit('cnk_soccer_games', 'round|league_id');

	if ($deleted && has_privs('cnk_soccer_admin'))
	{
		$message = 'Rounds deleted: '.$deleted;

		return 	cnk_sc_add_round($message);
	}

	return 	cnk_sc_add_round();
}

// ################################################################
// ################################################################
// ################################################################
/*
	Soccer installation / deinstallation
*/
// ################################################################
// ################################################################
// ################################################################
function cnk_sc_install()
{
	pagetop('Soccer Plugin Installation', '');

	echo '<div style="margin:auto; text-align:center">';

	// check if tables already installed
	$test = getThing('SELECT name FROM '.safe_pfx('txp_prefs')." WHERE name = 'cnk_sc_league_list_pageby'");

	if ($test)
	{
		echo '<p>Plugin already installed</p>';
	}
	else
	{	
		if (cnk_sc_do_install())
		{
			echo '<p>Installation was successful</p>';
		}
		else
		{
			echo '<p>Installation aborted</p>';
		}
	}

	echo '</div>';
}

function cnk_sc_do_install()
{
global $DB;
$version = mysqli_get_server_info();
//Use "ENGINE" if version of MySQL > (4.0.18 or 4.1.2)
$tabletype = ( intval($version[0]) >= 5 || preg_match('#^4\.(0\.[2-9]|(1[89]))|(1\.[2-9])#',$version)) 
	? " ENGINE=MyISAM " 
	: " TYPE=MyISAM ";

// On 4.1 or greater use utf8-tables
if (intval($version[0]) >= 5 || preg_match('#^4\.[1-9]#',$version)) 
{
global $DB;
	$tabletype .= " CHARACTER SET = utf8 ";
	$tabletype .= " COLLATE utf8_general_ci ";
	mysqli_query("SET NAMES ".$dbcharset);
}

// insert tables and test data

$sql = "CREATE TABLE ".safe_pfx('cnk_soccer_clubs')." (
  id int(11) NOT NULL auto_increment,
  country varchar(255) NOT NULL default 'default',
  `name` varchar(255) NOT NULL,
  logo varchar(255) default NULL,
  PRIMARY KEY  (id),
  UNIQUE KEY `name` (`name`),
  KEY country (country)
) ".$tabletype.";

CREATE TABLE ".safe_pfx('cnk_soccer_club_to_league')." (
  club_id int(11) NOT NULL,
  league_id int(11) NOT NULL,
  points tinyint(4) NOT NULL default '0',
  goals_for tinyint(4) NOT NULL default '0',
  goals_against tinyint(4) NOT NULL default '0',
  game_won tinyint(4) NOT NULL default '0',
  game_draw tinyint(4) NOT NULL default '0',
  game_lost tinyint(4) NOT NULL default '0',
  PRIMARY KEY  (club_id,league_id)
) ".$tabletype.";

CREATE TABLE ".safe_pfx('cnk_soccer_games')." (
  id int(11) NOT NULL auto_increment,
  league_id int(11) NOT NULL,
  round TINYINT NOT NULL,
  club1_id int(11) NOT NULL,
  club2_id int(11) NOT NULL,
  kickoff_time datetime NOT NULL,
  goals_club1 tinyint(4) default NULL,
  goals_club2 tinyint(4) default NULL,
  `status` char(1) default NULL,
  PRIMARY KEY  (id),
  KEY kickoff_time (kickoff_time),
  KEY league_id (league_id),
  KEY club1_id (club1_id),
  KEY club2_id (club2_id),
  KEY `status` (`status`)
) ".$tabletype.";

CREATE TABLE ".safe_pfx('cnk_soccer_league')." (
  id int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  season varchar(9) default NULL,
  total_rounds TINYINT NOT NULL DEFAULT '36',
  games_per_round TINYINT NOT NULL DEFAULT '10',
  PRIMARY KEY  (id),
  UNIQUE KEY `name` (`name`,`season`)
) ".$tabletype.";";

if (!cnk_sc_batch_query($sql)) return false;

// insert prefs

$sql = "INSERT INTO ".safe_pfx('txp_prefs')." (`prefs_id`, `name`, `val`, `type`, `event`, `html`, `position`) VALUES 
('1', 'cnk_sc_league_list_pageby', '15', '2', 'publish', 'text_input', '0'),
('1', 'cnk_sc_club_list_pageby', '15', '2', 'publish', 'text_input', '0'),
('1', 'cnk_sc_ranking_list_pageby', '15', '2', 'publish', 'text_input', '0'),
('1', 'cnk_sc_rounds_list_pageby', '15', '2', 'publish', 'text_input', '0');";

if (!cnk_sc_batch_query($sql)) return false;

return true;

}

function cnk_sc_deinstall()
{
	pagetop('Soccer Plugin Deinstallation', '');

	echo '<div style="margin:auto; text-align:center">';

	// check if tables already installed
	$test = getThing('SELECT name FROM '.safe_pfx('txp_prefs')." WHERE name = 'cnk_sc_league_list_pageby'");

	if (!$test)
	{
		echo '<p>Plugin not installed</p>';
	}
	else
	{	
		if (gps('do_deinstall'))
		{
			if (cnk_sc_do_deinstall())
			{
				echo '<p>Deinstallation was successful</p>';
			}
			else
			{
				echo '<p>Deinstallation aborted</p>';
			}
		}
		else
		{
			echo '<a href="?event=cnk_soccer'.a.'step=cnk_sc_deinstall'.a.'do_deinstall=1">Yes, I really want to deinstall</a>';
		}
	}

	echo '</div>';
}

function cnk_sc_do_deinstall()
{

// drop tables

$sql = "DROP TABLE ".safe_pfx('cnk_soccer_clubs').";
DROP TABLE ".safe_pfx('cnk_soccer_club_to_league').";
DROP TABLE ".safe_pfx('cnk_soccer_games').";
DROP TABLE ".safe_pfx('cnk_soccer_league');

if (!cnk_sc_batch_query($sql)) return false;

// delete prefs

$sql = "DELETE FROM ".safe_pfx('txp_prefs')." WHERE name IN ('cnk_sc_league_list_pageby', 'cnk_sc_club_list_pageby', 'cnk_sc_ranking_list_pageby', 'cnk_sc_rounds_list_pageby')";

if (!cnk_sc_batch_query($sql)) return false;

return true;

}

function cnk_sc_batch_query ($p_query, $p_transaction_safe = true) 
{
	if ($p_transaction_safe) 
	{
		$p_query = 'START TRANSACTION;' . $p_query . '; COMMIT;';
	}

	$query_split = preg_split ("/[;]+/", $p_query);

	foreach ($query_split as $command_line) 
	{
		$command_line = trim($command_line);

		if ($command_line != '') 
		{
			$query_result = safe_query($command_line);

			if ($query_result === false) 
			{
				break;
			}
		}
	}

	return $query_result;
}

// ################################################################
// ################################################################
// ################################################################
/*
	Custom TXP Functions
*/
// ################################################################
// ################################################################
// ################################################################

function cnk_sc_column_head($value, $sort = '', $event = '', $step = '', $is_link = '', $dir = '', $crit = '', $method = '', $class = '')
{
	return cnk_sc_column_multi_head( array(
				array ('value' => $value, 'sort' => $sort, 'event' => $event, 'is_link' => $is_link,
					   'dir' => $dir, 'crit' => $crit, 'method' => $method, 'step' => $step)
			), $class);
}

function cnk_sc_column_multi_head($head_items, $class='')
{
	$o = n.t.'<th'.($class ? ' class="'.$class.'"' : '').'>';
	$first_item = true;
	foreach ($head_items as $item)
	{
		if (empty($item)) continue;
		extract(lAtts(array(
			'value'		=> '',
			'sort'		=> '',
			'event'		=> '',
			'step'		=> '',
			'is_link'	=> '',
			'dir'		=> '',
			'crit'		=> '',
			'method'	=> '',
		),$item));

		$o .= ($first_item) ? '' : ', '; $first_item = false;

		if ($is_link)
		{
			$o .= '<a href="index.php?';

			$o .= ($step) ? a."step=$step" : '';
			$o .= ($event) ? a."event=$event" : '';
			$o .= ($sort) ? a."sort=$sort" : '';
			$o .= ($dir) ? a."dir=$dir" : '';
			$o .= ($crit) ? a."crit=$crit" : '';
			$o .= ($method) ? a."search_method=$method" : '';

			$o .= '">';
		}

		$o .= gTxt($value);

		if ($is_link)
		{
			$o .= '</a>';
		}
	}
	$o .= '</th>';

	return $o;
}

function cnk_sc_pageby_form($event, $step, $val)
{
	$vals = array(
		15  => 15,
		25  => 25,
		50  => 50,
		100 => 100
	);

	$select_page = selectInput('qty', $vals, $val,'', 1);

	// proper localisation
	$page = str_replace('{page}', $select_page, gTxt('view_per_page'));

	return form(
		'<div style="margin: auto; text-align: center;">'.
			$page.
			eInput($event).
			sInput($step).
			'<noscript> <input type="submit" value="'.gTxt('go').'" class="smallerbox" /></noscript>'.
		'</div>'
	);
}

function cnk_sc_event_multiedit_form($name, $step, $methods = null, $page, $sort, $dir, $crit, $search_method)
{
	$method = ps('edit_method');

	if ($methods === NULL)
	{
		$methods = array(
			'delete' => gTxt('delete')
		);
	}

	return '<label for="withselected">'.gTxt('with_selected').'</label>'.sp.
		selectInput('edit_method', $methods, $method, 1,
			( ($name == 'list') ? ' id="withselected" onchange="poweredit(this); return false;"' : ' id="withselected"' )
		).
		n.eInput($name).
		n.sInput($step).
		n.hInput('page', $page).
		( $sort ? n.hInput('sort', $sort).n.hInput('dir', $dir) : '' ).
		( $crit ? n.hInput('crit', $crit).n.hInput('search_method', $search_method) : '' ).
		n.fInput('submit', '', gTxt('go'), 'smallerbox');
}

function cnk_sc_PrevNextLink($event, $step, $page, $label, $type, $sort = '', $dir = '', $crit = '', $search_method = '')
{
	return '<a href="?event='.$event.a.'step='.$step.a.'page='.$page.
		($sort ? a.'sort='.$sort : '').
		($dir ? a.'dir='.$dir : '').
		($crit ? a.'crit='.$crit : '').
		($search_method ? a.'search_method='.$search_method : '').
		'" class="navlink">'.
		($type == 'prev' ? '&#8249;'.sp.$label : $label.sp.'&#8250;').
		'</a>';
}

function cnk_sc_nav_form($event, $step, $page, $numPages, $sort, $dir, $crit, $search_method)
{
	if ($numPages > 1)
	{
		$option_list = array();

		for ($i = 1; $i <= $numPages; $i++)
		{
			if ($i == $page)
			{
				$option_list[] = '<option value="'.$i.'" selected="selected">'."$i/$numPages".'</option>';
			}

			else
			{
				$option_list[] = '<option value="'.$i.'">'."$i/$numPages".'</option>';
			}
		}

		$nav = array();

		$nav[] = ($page > 1) ?
			cnk_sc_PrevNextLink($event, $step, $page - 1, gTxt('prev'), 'prev', $sort, $dir, $crit, $search_method).sp :
			tag('&#8249; '.gTxt('prev'), 'span', ' class="navlink-disabled"').sp;

		$nav[] = '<select name="page" class="list" onchange="submit(this.form);">';
		$nav[] = n.join(n, $option_list);
		$nav[] = n.'</select>';
		$nav[] = '<noscript> <input type="submit" value="'.gTxt('go').'" class="smallerbox" /></noscript>';

		$nav[] = ($page != $numPages) ?
			sp.cnk_sc_PrevNextLink($event, $step, $page + 1, gTxt('next'), 'next', $sort, $dir, $crit, $search_method) :
			sp.tag(gTxt('next').' &#8250;', 'span', ' class="navlink-disabled"');

		return '<form class="prev-next" method="get" action="index.php">'.
			n.eInput($event).
			n.sInput($step).
			( $sort ? n.hInput('sort', $sort).n.hInput('dir', $dir) : '' ).
			( $crit ? n.hInput('crit', $crit).n.hInput('search_method', $search_method) : '' ).
			join('', $nav).
			'</form>';
	}

	else
	{
		return graf($page.'/'.$numPages, ' class="prev-next"');
	}
}

function cnk_sc_event_multi_edit($table, $id_key) // TODO: replace $id_key with array
{
	$method = ps('edit_method');
	$selected = ps('selected');

	$id_keys = explode('|', $id_key);

	if ($selected)
	{
		if ($method == 'delete')
		{
			foreach ($selected as $id)
			{
				$where = array();
				$id_arr = explode('_', $id);

				for ($i=0; $i < count($id_keys); $i++)
				{
					array_push($where, $id_keys[$i].' = '.$id_arr[$i]);
				}

				if (safe_delete($table, implode(' AND ', $where)))
				{
					$ids[] = $id;
				}
			}

			return join(', ', $ids);
		}
	}

	return '';
}

Regards, Nick

Offline

#35 2019-02-26 22:55:44

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,127
Website

Re: [plugin] [ORPHAN] cnk_soccer - A football plugin

Hi arellanon,

Have a go with this one. All I’ve looked at for the time being is the install and deinstall routine. They now use txp’s own create/remove table and prefs functions but you still have to go through the Content › Soccer tab to install and deinstall. In my tests installation works as do the panels afterwards (PHP 7.2 and Textpattern 4.7.3), though I’ve not entered any details. I don’t know anything about the plugin (or that much about football leagues) so you’ll still need to figure out how to use the rest of the plugin.

Oh, and I also added the public tags to the registry so you don’t get an error further down the line.

EDIT: see below for an updated version:


TXP Builders – finely-crafted code, design and txp

Offline

#36 2019-02-27 04:16:31

arellanon
Member
From: San Jose, CA
Registered: 2019-02-26
Posts: 13
Website

Re: [plugin] [ORPHAN] cnk_soccer - A football plugin

It installed! Using (PHP 7.0 and Textpattern 4.7.3). I upgraded the PHP to 7.2 after installation because I got the following error. I got this when I populated the database using the forms provided.

Fatal error: Uncaught Error: Call to undefined function select_buttons() in /home/newsrealsanjose/public_html/textpattern/lib/txplib_misc.php(1870) : eval()’d code:589 Stack trace: #0 /home/newsrealsanjose/public_html/textpattern/lib/txplib_misc.php(1870) : eval()’d code(460): cnk_sc_list_club() #1 /home/newsrealsanjose/public_html/textpattern/lib/txplib_misc.php(1870) : eval()’d code(348): cnk_sc_edit_club(‘Club updated: R…’) #2 /home/newsrealsanjose/public_html/textpattern/lib/txplib_misc.php(1870) : eval()’d code(221): cnk_sc_save_club() #3 /home/newsrealsanjose/public_html/textpattern/lib/txplib_misc.php(1989): cnk_soccer(‘cnk_soccer’, ‘cnk_sc_save_clu…’) #4 /home/newsrealsanjose/public_html/textpattern/index.php(258): callback_event(‘cnk_soccer’, ‘cnk_sc_save_clu…’, 0) #5 {main} thrown in /home/newsrealsanjose/public_html/textpattern/lib/txplib_misc.php(1870) : eval()’d code on line 589


Regards, Nick

Offline

Board footer

Powered by FluxBB