Advertisement
lamontef

LeagueManager temp shortcode.php

Apr 8th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 30.15 KB | None | 0 0
  1. <?php
  2. /**
  3. * Shortcodes class for the WordPress plugin LeagueManager
  4. *
  5. * @author   Kolja Schleich
  6. * @package  LeagueManager
  7. * @copyright    Copyright 2008-2009
  8. */
  9.  
  10. class LeagueManagerShortcodes extends LeagueManager
  11. {
  12.     /**
  13.      * checks if bridge is active
  14.      *
  15.      * @var boolean
  16.      */
  17.     var $bridge = false;
  18.    
  19.    
  20.     /**
  21.      * initialize shortcodes
  22.      *
  23.      * @param boolean $bridge
  24.      * @return void
  25.      */
  26.     function __construct($bridge = false)
  27.     {
  28.         global $lmLoader;
  29.        
  30.         $this->addShortcodes();
  31.         if ( $bridge ) {
  32.             global $lmBridge;
  33.             $this->bridge =  true;
  34.             $this->lmBridge = $lmBridge;
  35.         }
  36.     }
  37.     function LeagueManagerShortcodes($bridge = false)
  38.     {
  39.         $this->__construct($bridge);
  40.     }
  41.    
  42.  
  43.     /**
  44.      * Adds shortcodes
  45.      *
  46.      * @param none
  47.      * @return void
  48.      */
  49.     function addShortcodes()
  50.     {
  51.         add_shortcode( 'standings', array(&$this, 'showStandings') );
  52.         add_shortcode( 'matches', array(&$this, 'showMatches') );
  53.         add_shortcode( 'match', array(&$this, 'showMatch') );
  54.         add_shortcode( 'championship', array(&$this, 'showChampionship') );
  55.         add_shortcode( 'crosstable', array(&$this, 'showCrosstable') );
  56.         add_shortcode( 'teams', array(&$this, 'showTeams') );
  57.         add_shortcode( 'team', array(&$this, 'showTeam') );
  58.         add_shortcode( 'leaguearchive', array(&$this, 'showArchive') );
  59.  
  60.         add_action( 'leaguemanager_teampage', array(&$this, 'showTeam') );
  61.     }
  62.    
  63.    
  64.     /**
  65.      * Function to display League Standings
  66.      *
  67.      *  [standings league_id="1" mode="extend|compact" template="name"]
  68.      *
  69.      * - league_id is the ID of league
  70.      * - league_name (optional) get league by name and not id
  71.      * - season: display specific season (optional). default is current season
  72.      * - template is the template used for displaying. Replace name appropriately. Templates must be named "standings-template.php" (optional)
  73.      * - group: optional
  74.      *
  75.      * @param array $atts
  76.      * @param boolean $widget (optional)
  77.      * @return the content
  78.      */
  79.     function showStandings( $atts, $widget = false )
  80.     {
  81.         global $wpdb, $leaguemanager;
  82.        
  83.         extract(shortcode_atts(array(
  84.             'league_id' => 0,
  85.             'league_name' => '',
  86.             'logo' => 'true',
  87.             'show_website' => 'true',
  88.             'template' => 'extend',
  89.             'season' => false,
  90.             'group' => false,
  91.             'home' => 0
  92.         ), $atts ));
  93.        
  94.         $search = !empty($league_name) ? $league_name : $league_id;
  95.         $league = $leaguemanager->getLeague( $search );
  96.         if (!$season) {
  97.             $season = $leaguemanager->getSeason( $league );
  98.             $season = $season['name'];
  99.         }
  100.  
  101.         $search = "`league_id` = '".$league->id."' AND `season` = '".$season."'";
  102.         if ( $group ) $search .= " AND `group` = '".$group."'";
  103.         $teams = $leaguemanager->getTeams( $search );
  104.    
  105.         if ( !empty($home) ) {
  106.             $teamlist = array();
  107.             foreach ( $teams AS $offset => $team ) {
  108.                 if ( $team->home == 1 ) {
  109.                     $low = $offset-$home;
  110.                     $high = $offset+$home;
  111.  
  112.                     if ( $low < 0 ) {
  113.                         $high -= $low;
  114.                         $low = 0;
  115.                     } elseif ( $high > count($teams)-1 ) {
  116.                         $low -= $high - count($teams)+1;
  117.                         $high = count($teams)-1;
  118.                     }
  119.  
  120.                     for ( $x = $low; $x <= $high; $x++ ) {
  121.                         if ( !array_key_exists($teams[$x]->rank, $teamlist) )
  122.                             $teamlist[$teams[$x]->rank] = $teams[$x];
  123.                     }
  124.                 }
  125.             }
  126.            
  127.             $teams = array_values($teamlist);
  128.         }
  129.  
  130.         $i = 0; $class = array();
  131.         foreach ( $teams AS $team ) {
  132.             $class = ( in_array('alternate', $class) ) ? array() : array('alternate');
  133.             // Add classes for ascend or descend
  134.             if ( $team->rank <= $league->num_ascend ) $class[] = 'ascend';
  135.             elseif ( count($teams)-$team->rank < $league->num_descend ) $class[] =  'descend';
  136.  
  137.             // Add class for relegation
  138.             if ( $team->rank >  count($teams)-$league->num_descend-$league->num_relegation && $team->rank <= count($teams)-$league->num_descend ) $class[] = 'relegation';
  139.  
  140.             // Add class for home team
  141.             if ( 1 == $team->home ) $class[] = 'homeTeam';
  142.            
  143.             $url = get_permalink();
  144.             $url = add_query_arg( 'team', $team->id, $url );
  145.  
  146.             $teams[$i]->pageURL = $url;
  147.             //if ( $league->team_ranking == 'auto' ) $teams[$i]->rank = $i+1;
  148.             $teams[$i]->class = implode(' ', $class);
  149.             $teams[$i]->logoURL = $leaguemanager->getThumbnailUrl($team->logo);
  150.             if ( 1 == $team->home ) $teams[$i]->title = '<strong>'.$team->title.'</strong>';
  151.  
  152.             if (  $show_website == 'true'  ){
  153.                 if ( $team->website != '' ) $teams[$i]->title = '<a href="http://'.$team->website.'" target="_blank">'.$team->title.'</a>';
  154.             } else {
  155.                 $teams[$i]->title = $team->title;                
  156.             }
  157.            
  158.             $team->points_plus += $team->add_points; // add or substract points
  159.             $teams[$i]->points = sprintf($league->point_format, $team->points_plus, $team->points_minus);
  160.             $teams[$i]->points2 = sprintf($league->point_format2, $team->points2_plus, $team->points2_minus);
  161.             $i++;
  162.         }
  163.        
  164.         $league->show_logo = ( $logo == 'true' ) ? true : false;
  165.         $league->show_website = ( $show_website == 'true' ) ? true : false;
  166.  
  167.         if ( !$widget && $this->checkTemplate('standings-'.$league->sport) )
  168.             $filename = 'standings-'.$league->sport;
  169.         else
  170.             $filename = 'standings-'.$template;
  171.  
  172.         $out = $this->loadTemplate( $filename, array('league' => $league, 'teams' => $teams, 'widget' => $widget) );
  173.            
  174.         return $out;
  175.     }
  176.    
  177.    
  178.     /**
  179.      * Function to display League Matches
  180.      *
  181.      *  [matches league_id="1" mode="all|home|racing" template="name" roster=ID]
  182.      *
  183.      * - league_id is the ID of league
  184.      * - league_name: get league by name and not ID (optional)
  185.      * - mode can be either "all" or "home". For racing it must be "racing". If it is not specified the matches are displayed on a weekly basis
  186.      * - season: display specific season (optional)
  187.      * - template is the template used for displaying. Replace name appropriately. Templates must be named "matches-template.php" (optional)
  188.      * - archive: true or false, check if archive page
  189.      * - roster is the ID of individual team member (currently only works with racing)
  190.      * - match_day: specific match day (integer)
  191.      *
  192.      * @param array $atts
  193.      * @return the content
  194.      */
  195.     function showMatches( $atts )
  196.     {
  197.         global $leaguemanager, $championship;
  198.        
  199.         extract(shortcode_atts(array(
  200.             'league_id' => 0,
  201.             'league_name' => '',
  202.             'team' => 0,
  203.             'template' => '',
  204.             'mode' => '',
  205.             'season' => '',
  206.             'limit' => false,
  207.             'archive' => false,
  208.             'roster' => false,
  209.             'order' => false,
  210.             'match_day' => false,
  211.             'group' => false,
  212.             'time' => false,
  213.             'dateformat' => '',
  214.             'timeformat' => ''
  215.         ), $atts ));
  216.        
  217.         $search = !empty($league_name) ? $league_name : $league_id;
  218.         $league = $leaguemanager->getLeague( $search );
  219.         $league_id = $this->league_id = $league->id;
  220.         $leaguemanager->setLeagueId($league_id);
  221.        
  222.         if ( $league->mode == 'championship' ) $championship->initialize($league->id);
  223.  
  224.         if ( !$group && isset($_GET['group']) ) $group = $_GET['group'];
  225.  
  226.         if ( !isset($_GET['match']) ) {
  227.             $season = $leaguemanager->getSeason($league, $season);
  228.             $league->num_match_days = $season['num_match_days'];
  229.             $season = $season['name'];
  230.             $leaguemanager->setSeason($season);
  231.  
  232.             $league->match_days = ( ( !$match_day && empty($mode) || $mode == 'racing' ) && !$time && $league->num_match_days > 0 ) ? true : false;
  233.             $league->isCurrMatchDay = ( $archive ) ? false : true;
  234.                
  235.             $teams = $leaguemanager->getTeams( "`league_id` = ".$league_id." AND `season` = '".$season."'", "`title` ASC", 'ARRAY' );
  236.  
  237.             $search = "`league_id` = '".$league_id."' AND `season` = '".$season."' AND `final` = ''";
  238.             if ( $mode != 'racing' ) {
  239.                 // Standard is match day based with team dropdown
  240.                 if ( empty($mode) ) {
  241.                     if ( !empty($team) || (isset($_GET['team_id']) && !empty($_GET['team_id'])) )
  242.                         $team_id = !empty($team) ? $team : (int)$_GET['team_id'];
  243.  
  244.                     $match_day = $match_day ? $match_day : $leaguemanager->getMatchDay(true);
  245.  
  246.                     if ( $team_id )
  247.                         $search .= " AND ( `home_team`= {$team_id} OR `away_team` = {$team_id} )";
  248.                     elseif ( $group )
  249.                         $search .= " AND `group` = '".$group."'";
  250.                     //elseif ( $league->mode != 'championship' && !$time )
  251.                         $search .= " AND `match_day` = '".$match_day."'";
  252.                    
  253.                 }
  254.                
  255.                 if ( $time ) {
  256.                     if ( $time == 'next' )
  257.                         $search .= " AND DATEDIFF(NOW(), `date`) <= 0";
  258.                     elseif ( $time == 'prev' )
  259.                         $search .= " AND DATEDIFF(NOW(), `date`) > 0";
  260.                     elseif ( $time == 'prev1' )
  261.                         $search .= " AND (DATEDIFF(NOW(), `date`) > 0) AND (`winner_id` != 0) ";
  262.                 }
  263.  
  264.                 // Only get Home Teams
  265.                 if ( $mode == 'home' )
  266.                     $search .= parent::buildHomeOnlyQuery($league_id);
  267.             } else {
  268.                 if ( isset($_GET['match_day']) && !empty($_GET['match_day']) ) {
  269.                     $match_day = (int)$_GET['match_day'];
  270.                     $search .= " AND `match_day` = '".$match_day."'";
  271.                 } elseif ( $match_day ) {
  272.                     $search .= " AND `match_day` = '".$match_day."'";
  273.                 }
  274.             }
  275.             $matches = $leaguemanager->getMatches( $search, $limit, $order );
  276.  
  277.             foreach ( $matches AS $key => $row ) {
  278.                 $matchdate[$key] = $row->date;
  279.             }
  280.  
  281.             if ($time=='prev1'){ array_multisort( $matchdate, SORT_ASC, $matches ); }
  282.  
  283.             $i = 0;
  284.             foreach ( $matches AS $match ) {
  285.                 $class = ( 'alternate' == $class ) ? '' : 'alternate';
  286.                
  287.                 $matches[$i]->class = $class;
  288.                 $matches[$i]->hadPenalty = $match->hadPenalty = ( isset($match->penalty) && $match->penalty['home'] != '' && $match->penalty['away'] != '' ) ? true : false;
  289.                 $matches[$i]->hadOvertime = $match->hadOvertime = ( isset($match->overtime) && $match->overtime['home'] != '' && $match->overtime['away'] != '' ) ? true : false;
  290.  
  291.                 $url = get_permalink();
  292.                 $url = add_query_arg( 'match', $match->id, $url );
  293.                 $matches[$i]->pageURL = $url;
  294.  
  295.                 if ( $timeformat ) {
  296.                     $matches[$i]->start_time = ( '00' == $match->hour && '00' == $match->minutes ) ? '' : mysql2date($timeformat, $match->date);
  297.                 } else {
  298.                     $matches[$i]->start_time = ( '00' == $match->hour && '00' == $match->minutes ) ? '' : mysql2date(get_option('time_format'), $match->date);
  299.                 }
  300.                 if ( $dateformat ) {
  301.                     $matches[$i]->date = ( substr($match->date, 0, 10) == '0000-00-00' ) ? 'N/A' : mysql2date($dateformat, $match->date);
  302.                 } else {
  303.                     $matches[$i]->date = ( substr($match->date, 0, 10) == '0000-00-00' ) ? 'N/A' : mysql2date(get_option('date_format'), $match->date);
  304.                 }
  305.  
  306. //              $matches[$i]->start_time = ( '00' == $match->hour && '00' == $match->minutes ) ? '' : mysql2date(get_option('time_format'), $match->date);
  307. //              $matches[$i]->date = ( substr($match->date, 0, 10) == '0000-00-00' ) ? 'N/A' : mysql2date(get_option('date_format'), $match->date);
  308.  
  309.                 $matches[$i]->title = ( isset($matches[$i]->title) && !empty($matches[$i]->title) ) ? $match->title : $teams[$match->home_team]['title'].' &#8211; '. $teams[$match->away_team]['title'];
  310.                 $matches[$i]->title = apply_filters( 'leaguemanager_matchtitle_'.$league->sport, $matches[$i]->title, $match, $teams );
  311.                 if ( parent::isHomeTeamMatch( $match->home_team, $match->away_team, $teams ) )
  312.                     $matches[$i]->title = '<strong>'.$matches[$i]->title.'</strong>';
  313.                
  314.                 $matches[$i]->report = ( $match->post_id != 0 ) ? '(<a href="'.get_permalink($match->post_id).'">'.__('Report', 'leaguemanager').'</a>)' : '';
  315.    
  316.                 if ( $match->hadPenalty )
  317.                     $matches[$i]->score = sprintf("%s - %s", $match->penalty['home']+$match->overtime['home'], $match->penalty['away']+$match->overtime['away'])." "._c( 'o.P.|on penalty', 'leaguemanager' );
  318.                 elseif ( $match->hadOvertime )
  319.                 //  $matches[$i]->score = sprintf("%s - %s", $match->overtime['home'], $match->overtime['away'])." "._c( 'AET|after extra time', 'leaguemanager' );
  320.                     $matches[$i]->score = sprintf("%s - %s", $matches[$i]->home_points, $matches[$i]->away_points);
  321.                 elseif ( $match->home_points != NULL && $match->away_points != NULL )
  322.                     $matches[$i]->score = sprintf("%s - %s", $matches[$i]->home_points, $matches[$i]->away_points);
  323.                 else
  324.                     $matches[$i]->score = "-:-";
  325.  
  326.                 $i++;
  327.             }
  328.         }
  329.        
  330.         if ( empty($template) && $this->checkTemplate('matches-'.$league->sport) )
  331.             $filename = 'matches-'.$league->sport;
  332.         else
  333.             $filename = ( !empty($template) ) ? 'matches-'.$template : 'matches';
  334.  
  335.         $out = $this->loadTemplate( $filename, array('league' => $league, 'matches' => $matches, 'teams' => $teams, 'season' => $season, 'roster' => $roster ) );
  336.  
  337.         return $out;
  338.     }
  339.    
  340.    
  341.     /**
  342.      * Function to display single match
  343.      *
  344.      * [match id="1" template="name"]
  345.      *
  346.      * - id is the ID of the match to display
  347.      * - template is the template used for displaying. Replace name appropriately. Templates must be named "match-template.php" (optional)
  348.      *
  349.      * @param array $atts
  350.      * @return the content
  351.      */
  352.     function showMatch( $atts )
  353.     {
  354.         global $leaguemanager, $lmStats;
  355.         extract(shortcode_atts(array(
  356.             'id' => 0,
  357.             'template' => '',
  358.         ), $atts ));
  359.        
  360.         $match = $leaguemanager->getMatch($id);
  361.         $league = $leaguemanager->getLeague($match->league_id);
  362.         $home = $leaguemanager->getTeam($match->home_team);
  363.         $away = $leaguemanager->getTeam($match->away_team);
  364.        
  365.         $match->hadPenalty = ( isset($match->penalty) && $match->penalty['home'] != '' && $match->penalty['away'] != '' ) ? true : false;
  366.         $match->hadOvertime = ( isset($match->overtime) && $match->overtime['home'] != '' && $match->overtime['away'] != '' ) ? true : false;
  367.  
  368.         $match->home_points = ( NULL == $match->home_points ) ? '-' : $match->home_points;
  369.         $match->away_points = ( NULL == $match->away_points ) ? '-' : $match->away_points;
  370.  
  371.         $match->homeTeam = $home->title;
  372.         $match->awayTeam = $away->title;
  373.         $match->title = $match->homeTeam . "&#8211;" . $match->awayTeam;
  374.  
  375.         $match->homeLogo = $home->logo;
  376.         $match->awayLogo = $away->logo;
  377.  
  378.         $match->start_time = ( '00' == $match->hour && '00' == $match->minutes ) ? '' : mysql2date(get_option('time_format'), $match->date);
  379.         $match->date = ( substr($match->date, 0, 10) == '0000-00-00' ) ? 'N/A' : mysql2date(get_option('date_format'), $match->date);
  380.  
  381.         $match->report = ( $match->post_id != 0 ) ? '(<a href="'.get_permalink($match->post_id).'">'.__('Report', 'leaguemanager').'</a>)' : '';
  382.  
  383.         if ( $match->hadPenalty )
  384.             $match->score = sprintf("%s - %s", $match->penalty['home'], $match->penalty['away'])." "._c( 'o.P.|on penalty', 'leaguemanager' );
  385.         elseif ( $match->hadOvertime )
  386.         //  $match->score = sprintf("%s - %s", $match->overtime['home'], $match->overtime['away'])." "._c( 'AET|after extra time', 'leaguemanager' );
  387.             $match->score = sprintf("%s - %s", $match->home_points, $match->away_points);
  388.         else
  389.             $match->score = sprintf("%s - %s", $match->home_points, $match->away_points);
  390.        
  391.         if ( empty($template) && $this->checkTemplate('match-'.$league->sport) )
  392.             $filename = 'match-'.$league->sport;
  393.         else
  394.             $filename = ( !empty($template) ) ? 'match-'.$template : 'match';
  395.  
  396.         $out = $this->loadTemplate( $filename, array('league' => $league, 'match' => $match) );
  397.  
  398.         return $out;
  399.     }
  400.    
  401.    
  402.     /**
  403.      * Function to display Championship
  404.      *
  405.      *  [championship league_id="1" template="name"]
  406.      *
  407.      * - league_id is the ID of league
  408.      * - league_name: get league by name and not ID (optional)
  409.      * - season: display specific season (optional)
  410.      * - template is the template used for displaying. Replace name appropriately. Templates must be named "matches-template.php" (optional)
  411.      *
  412.      * @param array $atts
  413.      * @return the content
  414.      */
  415.     function showChampionship( $atts )
  416.     {
  417.         global $leaguemanager, $championship;
  418.        
  419.         extract(shortcode_atts(array(
  420.             'league_id' => 0,
  421.             'league_name' => '',
  422.             'template' => '',
  423.             'season' => false,
  424.         ), $atts ));
  425.        
  426.         $search = !empty($league_name) ? $league_name : $league_id;
  427.         $league = $leaguemanager->getLeague( $search );
  428.         if ( !$season ) {
  429.             $season = $leaguemanager->getSeason($league);
  430.             $season = $season['name'];
  431.         }
  432.         $league->season = $season;
  433.         $league_id = $this->league_id = $league->id;
  434.        
  435.         $championship->initialize($league->id);
  436.  
  437.         $finals = array();
  438.         foreach ( $championship->getFinals() AS $final ) {
  439.             $class = ( 'alternate' == $class ) ? '' : 'alternate';
  440.             $data['class'] = $class;
  441.            
  442.             $data['key'] = $final['key'];
  443.             $data['name'] = $final['name'];
  444.             $data['num_matches'] = $final['num_matches'];
  445.             $data['colspan'] = ( $championship->getNumTeamsFirstRound()/2 >= 4 ) ? ceil(4/$final['num_matches']) : ceil(($championship->getNumTeamsFirstRound()/2)/$final['num_matches']);
  446.  
  447.             $matches_raw = $leaguemanager->getMatches("`league_id` = '".$league->id."' AND `season` = '".$season."' AND `final` = '".$final['key']."'", false, "`id` ASC");
  448.             $teams = $leaguemanager->getTeams( "`league_id` = '".$league->id."' AND `season` = '".$season."'", "`id` ASC", 'ARRAY' );
  449.             $teams2 = $championship->getFinalTeams($final, 'ARRAY');
  450.            
  451.             $matches = array();
  452.             for ( $i = 1; $i <= $final['num_matches']; $i++ ) {
  453.                 $match = $matches_raw[$i-1];
  454.                 if ( $match ) {
  455.                     if ( is_numeric($match->home_team) && is_numeric($match->away_team) ) {
  456.                         $match->title = $match->title2 = sprintf("%s &#8211; %s", $teams[$match->home_team]['title'], $teams[$match->away_team]['title']);
  457.                     } else {
  458.                         $match->title = sprintf("%s &#8211; %s", $teams2[$match->home_team], $teams2[$match->away_team]);
  459.                         $match->title2 = "&#8211;";
  460.                     }
  461.  
  462.                     $match->hadPenalty = $match->hadPenalty = ( isset($match->penalty) && $match->penalty['home'] != '' && $match->penalty['away'] != '' ) ? true : false;
  463.                     $match->hadOvertime = $match->hadOvertime = ( isset($match->overtime) && $match->overtime['home'] != '' && $match->overtime['away'] != '' ) ? true : false;
  464.  
  465.                     if ( $match->home_points != NULL && $match->away_points != NULL ) {
  466.                         if ( $match->hadPenalty )
  467.                             $match->score = sprintf("%s:%s", $match->penalty['home'], $match->penalty['away'])." "._c( 'o.P.|on penalty', 'leaguemanager' );
  468.                         elseif ( $match->hadOvertime )
  469.                         //  $match->score = sprintf("%s:%s", $match->overtime['home'], $match->overtime['away'])." "._c( 'AET|after extra time', 'leaguemanager' );
  470.                             $match->score = sprintf("%s:%s", $match->home_points, $match->away_points);
  471.                         else
  472.                             $match->score = sprintf("%s:%s", $match->home_points, $match->away_points);
  473.                         if ( $final['key'] == 'final' ) {
  474.                             $data['isFinal'] = true;
  475.                             $data['field_id'] = ( $match->winner_id == $match->home_team ) ? "final_home" : "final_away";
  476.                         } else {
  477.                             $data['isFinal'] = false;
  478.                             }
  479.                     } else {
  480.                         $match->score = "-:-";
  481.                     }
  482.  
  483.                     $match->date = ( substr($match->date, 0, 10) == '0000-00-00' ) ? 'N/A' : mysql2date(get_option('date_format'), $match->date);
  484.                     $match->time = ( '00:00' == $match->hour.":".$match->minutes ) ? 'N/A' : mysql2date(get_option('time_format'), $match->date);
  485.                     if ( empty($match->location) ) $match->location = 'N/A';
  486.  
  487.                     $matches[$i] = $match;
  488.                 }
  489.             }
  490.  
  491.             $data['matches'] = $matches;
  492.             $finals[] = (object)$data;
  493.         }
  494.  
  495.         if ( empty($template) && $this->checkTemplate('championship-'.$league->sport) )
  496.             $filename = 'championship-'.$league->sport;
  497.         else
  498.             $filename = ( !empty($template) ) ? 'championship-'.$template : 'championship';
  499.  
  500.         $out = $this->loadTemplate( $filename, array('league' => $league, 'championship' => $championship, 'finals' => $finals) );
  501.  
  502.         return $out;
  503.     }
  504.  
  505.  
  506.     /**
  507.      * Function to display Team list
  508.      *
  509.      *  [teams league_id=ID template=X season=x]
  510.      *
  511.      * @param array $atts
  512.      * @return the content
  513.      */
  514.     function showTeams( $atts )
  515.     {
  516.         global $leaguemanager;
  517.         extract(shortcode_atts(array(
  518.             'league_id' => 0,
  519.             'template' => '',
  520.             'season' => false,
  521.             'group' => false
  522.         ), $atts ));
  523.  
  524.         $league = $leaguemanager->getLeague($league_id);
  525.         if (empty($season)) {
  526.             $season = $leaguemanager->getSeason($league);
  527.             $season = $season['name'];
  528.         }
  529.  
  530.         $search = "`league_id` = '".$league->id."' AND `season` = '".$season."'";
  531.         if ( $group ) $search .= " AND `group` = '".$group."'";
  532.         $teams = $leaguemanager->getTeams( $search );
  533.  
  534. //      $teams = $leaguemanager->getTeams( "`league_id` = {$league_id} AND `season` = '".$season."'" );
  535.  
  536.         if ( empty($template) && $this->checkTemplate('teams-'.$league->sport) )
  537.             $filename = 'teams-'.$league->sport;
  538.         else
  539.             $filename = ( !empty($template) ) ? 'teams-'.$template : 'teams';
  540.  
  541.         $out = $this->loadTemplate( $filename, array('league' => $league, 'teams' => $teams) );
  542.  
  543.         return $out;
  544.     }
  545.  
  546.  
  547.     /**
  548.      * Function to display Team Info Page
  549.      *
  550.      *  [team id=ID template=X]
  551.      *
  552.      * @param array $atts
  553.      * @return the content
  554.      */
  555.     function showTeam( $atts )
  556.     {
  557.         global $leaguemanager;
  558.         extract(shortcode_atts(array(
  559.             'id' => 0,
  560.             'template' => '',
  561.             'echo' => 0,
  562.         ), $atts ));
  563.  
  564.         $team = $leaguemanager->getTeam( $id );
  565.         $league = $leaguemanager->getLeague( $team->league_id );
  566.  
  567.         // Get next match
  568.         $next_matches = $leaguemanager->getMatches("( `home_team` = {$team->id} OR `away_team` = {$team->id} ) AND DATEDIFF(NOW(), `date`) <= 0");
  569.         $next_match = $next_matches[0];
  570.         if ( $next_match ) {
  571.             if ( $next_match->home_team == $team->id ) {
  572.                 $opponent = $leaguemanager->getTeam($next_match->away_team);
  573.                 $next_match->match = $team->title . " &#8211; " . $opponent->title;
  574.             } else {
  575.                 $opponent = $leaguemanager->getTeam($next_match->home_team);
  576.                 $next_match->match = $opponent->title  . " &#8211; " . $team->title;
  577.             }
  578.         }
  579.  
  580.         // Get last match
  581.         $prev_matches = $leaguemanager->getMatches("( `home_team` = {$team->id} OR `away_team` = {$team->id} ) AND DATEDIFF(NOW(), `date`) > 0", 1, "`date` DESC");
  582.         $prev_match = $prev_matches[0];
  583.         if ( $prev_match ) {
  584.             if ( $prev_match->home_team == $team->id ) {
  585.                 $opponent = $leaguemanager->getTeam($prev_match->away_team);
  586.                 $prev_match->match = $team->title . " &#8211; " . $opponent->title;
  587.             } else {
  588.                 $opponent = $leaguemanager->getTeam($prev_match->home_team);
  589.                 $prev_match->match = $opponent->title  . " &#8211; " . $team->title;
  590.             }
  591.        
  592.             $prev_match->hadOvertime = ( isset($prev_match->overtime) && $prev_match->overtime['home'] != '' && $prev_match->overtime['away'] != '' ) ? true : false;
  593.             $prev_match->hadPenalty = ( isset($prev_match->penalty) && $prev_match->penalty['home'] != '' && $prev_match->penalty['away'] != '' ) ? true : false;
  594.  
  595.             if ( $prev_match->hadPenalty )
  596.                 $prev_match->score = sprintf("%s - %s", $prev_match->penalty['home'], $prev_match->penalty['away'])." "._c( 'o.P.|on penalty', 'leaguemanager' );
  597.             elseif ( $prev_match->hadOvertime )
  598.             //  $prev_match->score = sprintf("%s - %s", $prev_match->overtime['home'], $prev_match->overtime['away'])." "._c( 'AET|after extra time', 'leaguemanager' );
  599.                 $prev_match->score = sprintf("%s - %s", $prev_match->home_points, $prev_match->away_points);
  600.             else
  601.                 $prev_match->score = sprintf("%s - %s", $prev_match->home_points, $prev_match->away_points);
  602.         }
  603.  
  604.  
  605.         if ( empty($template) && $this->checkTemplate('team-'.$league->sport) )
  606.             $filename = 'team-'.$league->sport;
  607.         else
  608.             $filename = ( !empty($template) ) ? 'team-'.$template : 'team';
  609.  
  610.         $out = $this->loadTemplate( $filename, array('league' => $league, 'team' => $team, 'next_match' => $next_match, 'prev_match' => $prev_match) );
  611.  
  612.         if ( $echo )
  613.             echo $out;
  614.         else
  615.             return $out;
  616.     }
  617.  
  618.  
  619.     /**
  620.      * Function to display Crosstable
  621.      *
  622.      * [crosstable league_id="1" mode="popup" template="name"]
  623.      *
  624.      * - league_id is the ID of league to display
  625.      * - league_name: get league by name and not ID (optional)
  626.      * - mode set to "popup" makes the crosstable be displayed in a thickbox popup window.
  627.      * - template is the template used for displaying. Replace name appropriately. Templates must be named "crosstable-template.php" (optional)
  628.      * - season: display crosstable of given season (optional)
  629.      *
  630.      *
  631.      * @param array $atts
  632.      * @return the content
  633.      */
  634.     function showCrosstable( $atts )
  635.     {
  636.         global $leaguemanager;
  637.         extract(shortcode_atts(array(
  638.             'league_id' => 0,
  639.             'league_name' => '',
  640.             'group' => '',
  641.             'template' => '',
  642.             'mode' => '',
  643.             'season' => false
  644.         ), $atts ));
  645.        
  646.         $search = !empty($league_name) ? $league_name : $league_id;
  647.         $league = $leaguemanager->getLeague( $search );
  648.         if (empty($season)) {
  649.             $season = $leaguemanager->getSeason($league);
  650.             $season = $season['name'];
  651.         }
  652.         $teams = $leaguemanager->getTeams( "`league_id` = '".$league->id."' AND `season` = '".$season."' AND `group` = '".$group."'" );
  653.        
  654.         if ( empty($template) && $this->checkTemplate('crosstable-'.$league->sport) )
  655.             $filename = 'crosstable-'.$league->sport;
  656.         else
  657.             $filename = ( !empty($template) ) ? 'crosstable-'.$template : 'crosstable';
  658.  
  659.         $out = $this->loadTemplate( $filename, array('league' => $league, 'teams' => $teams, 'mode' => $mode) );
  660.        
  661.         return $out;
  662.     }
  663.    
  664.    
  665.     /**
  666.      * show Archive
  667.      *
  668.      *  [leaguearchive league_id=ID season=x template=X]
  669.      *
  670.      * - league_id: ID of league
  671.      * - league_name: get league by name and not ID (optional)
  672.      * - template: template to use
  673.      *
  674.      * @param array $atts
  675.      * @return the content
  676.      */
  677.     function showArchive( $atts )
  678.     {
  679.         global $leaguemanager, $championship;
  680.         extract(shortcode_atts(array(
  681.             'league_id' => false,
  682.             'league_name' => '',
  683.             'template' => ''
  684.         ), $atts ));
  685.        
  686.         // get all leagues, needed for dropdown
  687.         $leagues = $leaguemanager->getLeagues();
  688.         $league = false; // Initialize league variable
  689.  
  690.         // Get League by Name
  691.         if (!empty($league_name)) {
  692.             $league = $leaguemanager->getLeague( $league_name );
  693.             $league_id = $league->id;
  694.         }
  695.        
  696.         if ( isset($_GET['season']) && !empty($_GET['season']) )
  697.             $season = $_GET['season'];
  698.         else
  699.             $season = false;
  700.  
  701.         // Get League ID from shortcode or $_GET
  702.         $league_id = ( !$league_id && isset($_GET['league_id']) && !empty($_GET['league_id']) ) ? (int)$_GET['league_id'] : false;
  703.  
  704.         // select first league
  705.         if ( !$league_id )
  706.             $league_id = $leagues[0]->id;
  707.  
  708.         // Get League and first Season if not set
  709.         if ( !$league ) $league = $leaguemanager->getLeague( $league_id );
  710.         if ( !$season ) {
  711.             $season = reset($league->seasons);
  712.             $season = $season['name'];
  713.         }
  714.  
  715.         $league->season = $season;
  716.  
  717.         if ( $league->mode == 'championship' ) $championship->initialize($league->id);
  718.  
  719.         $seasons = array();
  720.         foreach ( $leagues AS $l ) {
  721.             foreach( (array)$l->seasons AS $l_season ) {
  722.                 if ( !in_array($l_season['name'], $seasons) && !empty($l_season['name']) )
  723.                     $seasons[] = $l_season['name'];
  724.             }
  725.         }
  726.         sort($seasons);
  727.  
  728.         if ( empty($template) && $this->checkTemplate('archive-'.$league->sport) )
  729.             $filename = 'archive-'.$league->sport;
  730.         else
  731.             $filename = ( !empty($template) ) ? 'archive-'.$template : 'archive';
  732.  
  733.         $out = $this->loadTemplate( $filename, array('leagues' => $leagues, 'seasons' => $seasons, 'league_id' => $league_id) );
  734.         return $out;
  735.     }
  736.    
  737.    
  738.     /**
  739.      * get specific field for crosstable
  740.      *
  741.      * @param int $curr_team_id
  742.      * @param int $opponent_id
  743.      * @return string
  744.      */
  745.     function getCrosstableField($curr_team_id, $opponent_id)
  746.     {
  747.         global $wpdb, $leaguemanager;
  748.  
  749.         //$match = $leaguemanager->getMatches("(`home_team` = $curr_team_id AND `away_team` = $opponent_id) OR (`home_team` = $opponent_id AND `away_team` = $curr_team_id)");
  750.         $match = $leaguemanager->getMatches("`home_team` = $curr_team_id AND `away_team` = $opponent_id");
  751.         $match = $match[0];
  752.        
  753.         if ( $match ) {
  754.             return $this->getScore($curr_team_id, $opponent_id, $match);
  755.         } else {
  756.             $match = $leaguemanager->getMatches("`home_team` = $opponent_id AND `away_team` = $curr_team_id");
  757.             $match = $match[0];
  758.             return $this->getScore($curr_team_id, $opponent_id, $match);
  759.  
  760.         }
  761.     }
  762.    
  763.  
  764.     /**
  765.      * get score for specific field of crosstable
  766.      *
  767.      * @param int $curr_team_id
  768.      * @param int $opponent_id
  769.      * @return string
  770.      */
  771.     function getScore($curr_team_id, $opponent_id, $match)
  772.     {
  773.         global $wpdb, $leaguemanager;
  774.    
  775.         if ( !empty($match->penalty['home']) && !empty($match->penalty['away']) ) {
  776.             $match->penalty = maybe_unserialize($match->penalty);
  777.             $points = array( 'home' => $match->penalty['home'], 'away' => $match->penalty['away']);
  778.         } elseif ( !empty($match->overtime['home']) && !empty($match->overtime['away']) ) {
  779.             $match->overtime = maybe_unserialize($match->overtime);
  780.         //  $points = array( 'home' => $match->overtime['home'], 'away' => $match->overtime['away']);
  781.             $points = array( 'home' => $match->home_points, 'away' => $match->away_points );
  782.         } else {
  783.             $points = array( 'home' => $match->home_points, 'away' => $match->away_points );
  784.         }
  785.        
  786.         // unplayed match
  787.         if ( NULL == $match->home_points && NULL == $match->away_points )
  788.             $out = "<td class='num'>-:-</td>";
  789.         // match at home
  790.         elseif ( $curr_team_id == $match->home_team )
  791.             $out = "<td class='num'>".sprintf("%s:%s", $points['home'], $points['away'])."</td>";
  792.         // match away
  793.         elseif ( $opponent_id == $match->home_team )
  794.             $out = "<td class='num'>".sprintf("%s:%s", $points['away'], $points['home'])."</td>";
  795.  
  796.         return $out;
  797.     }
  798.  
  799.  
  800.     /**
  801.      * Load template for user display. First the current theme directory is checked for a template
  802.      * before defaulting to the plugin
  803.      *
  804.      * @param string $template Name of the template file (without extension)
  805.      * @param array $vars Array of variables name=>value available to display code (optional)
  806.      * @return the content
  807.      */
  808.     function loadTemplate( $template, $vars = array() )
  809.     {
  810.         global $leaguemanager, $lmStats, $championship;
  811.         extract($vars);
  812.  
  813.         ob_start();
  814.         if ( file_exists( TEMPLATEPATH . "/leaguemanager/$template.php")) {
  815.             include(TEMPLATEPATH . "/leaguemanager/$template.php");
  816.         } elseif ( file_exists(LEAGUEMANAGER_PATH . "/templates/".$template.".php") ) {
  817.             include(LEAGUEMANAGER_PATH . "/templates/".$template.".php");
  818.         } else {
  819.             parent::setMessage( sprintf(__('Could not load template %s.php', 'leaguemanager'), $template), true );
  820.             parent::printMessage();
  821.         }
  822.         $output = ob_get_contents();
  823.         ob_end_clean();
  824.         return $output;
  825.     }
  826.  
  827.    
  828.     /**
  829.      * check if template exists
  830.      *
  831.      * @param string $template
  832.      * @return boolean
  833.      */
  834.     function checkTemplate( $template )
  835.     {
  836.         if ( file_exists( TEMPLATEPATH . "/leaguemanager/$template.php")) {
  837.             return true;
  838.         } elseif ( file_exists(LEAGUEMANAGER_PATH . "/templates/".$template.".php") ) {
  839.             return true;
  840.         }
  841.  
  842.         return false;
  843.     }
  844. }
  845.  
  846. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement