-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finished building out the Printing and CSV Download version of the AD…
…P tool
- Loading branch information
1 parent
d38217b
commit 67490c1
Showing
6 changed files
with
283 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
/** | ||
* Fantrax CSV ADP Download | ||
* | ||
* @since 1.1 | ||
*/ | ||
|
||
namespace Fantrax; | ||
|
||
class CSV extends Setup { | ||
|
||
/** | ||
* Setup constructor. | ||
*/ | ||
private function __construct() {} | ||
|
||
/** | ||
* Init | ||
* | ||
* @since 1.0 | ||
*/ | ||
public static function init() { | ||
add_action( 'parse_request', array( __CLASS__, 'render' ) ); | ||
} | ||
|
||
/** | ||
* Renders the page in a csv format for download | ||
* | ||
* @since 1.1 | ||
* | ||
* @param $request | ||
*/ | ||
public static function render( $request ) { | ||
$csv = $_REQUEST[self::$csv_parameter]; | ||
|
||
if( $csv == 'true' ) { | ||
$atts = array( | ||
'sport' => esc_attr( $_REQUEST['sport'] ), | ||
'position' => esc_attr( $_REQUEST['position'] ), | ||
'limit' => esc_attr( $_REQUEST['limit'] ), | ||
'start' => esc_attr( $_REQUEST['start'] ), | ||
'order' => esc_attr( $_REQUEST['order'] ) | ||
); | ||
|
||
$url = self::buildApiUrl( $atts ); | ||
|
||
$data = self::callApi( $url ); | ||
|
||
if( !$data ) { | ||
echo 'The data doesn\'t exist in the api.'; | ||
die(); | ||
} | ||
|
||
self::set_headers(); | ||
|
||
$csv = '"Rank","Player","Position","ADP"' . "\r\n"; | ||
if( !empty( $data ) ) { | ||
$i = ( $atts['start'] == 0 ) ? 1 : $atts['start']; | ||
foreach( $data as $player ) { | ||
if( in_array( $atts['order'], array( 'NAME', 'ADP', 'name', 'adp' ) ) && !empty( $player->ADP ) ) { | ||
$csv .= '"'.$i.'",'; | ||
$csv .= '"'.$player->name.'",'; | ||
$csv .= '"'.$player->pos.'",'; | ||
$csv .= '"'.$player->ADP.'"'; | ||
$csv .= "\r\n"; | ||
$i++; | ||
} elseif( in_array( $atts['order'], array( 'NAME', 'ADP_PPR', 'name', 'adp_ppr' ) ) && !empty( $player->ADP_PPR ) ) { | ||
$csv .= '"'.$i.'"'; | ||
$csv .= '"'.$player->name.'",'; | ||
$csv .= '"'.$player->pos.'",'; | ||
$csv .= '"'.$player->ADP_PPR.'",'; | ||
$csv .= "\r\n"; | ||
$i++; | ||
} | ||
} | ||
} | ||
|
||
print mb_convert_encoding($csv, 'UTF-16LE', 'UTF-8'); | ||
|
||
die(); | ||
} | ||
} | ||
|
||
/** | ||
* Set the headers that are required so the user | ||
* can download the csv file. | ||
* | ||
* @since 1.1 | ||
*/ | ||
private static function set_headers() { | ||
header('Content-Encoding: UTF-8'); | ||
header("Content-type: text/csv; charset=UTF-8"); | ||
header("Content-Disposition: attachment; filename=adp.csv"); | ||
header("Pragma: no-cache"); | ||
header("Expires: 0"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* Print Friendly | ||
* | ||
* @since 1.1 | ||
*/ | ||
|
||
namespace Fantrax; | ||
|
||
class Printing extends Setup { | ||
|
||
/** | ||
* Setup constructor. | ||
*/ | ||
private function __construct() {} | ||
|
||
/** | ||
* Init | ||
* | ||
* @since 1.0 | ||
*/ | ||
public static function init() { | ||
add_action( 'parse_request', array( __CLASS__, 'render' ) ); | ||
} | ||
|
||
/** | ||
* Renders the table in a basic format for printing | ||
* | ||
* @since 1.1 | ||
* | ||
* @param $request | ||
*/ | ||
public static function render( $request ) { | ||
$print = $_REQUEST[self::$print_parameter]; | ||
|
||
if( $print == 'true' ) { | ||
wp_enqueue_style( 'fantrax-adp-style' ); | ||
|
||
$atts = array( | ||
'sport' => esc_attr( $_REQUEST['sport'] ), | ||
'position' => esc_attr( $_REQUEST['position'] ), | ||
'limit' => esc_attr( $_REQUEST['limit'] ), | ||
'start' => esc_attr( $_REQUEST['start'] ), | ||
'order' => esc_attr( $_REQUEST['order'] ) | ||
); | ||
|
||
$url = self::buildApiUrl( $atts ); | ||
|
||
$data = self::callApi( $url ); | ||
|
||
if( !$data ) { | ||
echo 'The data doesn\'t exist in the api.'; | ||
die(); | ||
} | ||
|
||
|
||
require FANTRAX_ABSPATH . 'views/print_output.php'; | ||
die(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
Plugin Name: Fantrax Fantasy ADP | ||
Plugin URI: https://fantrax.com | ||
Description: Allows Fantrax to embed tables with sports data from their api | ||
Version: 1.0 | ||
Version: 1.1 | ||
Author: Tyler Steinhaus | ||
Author URI: https://tylersteinhaus.com | ||
Author email: [email protected] | ||
|
@@ -22,5 +22,10 @@ | |
define( 'FANTRAX_BASENAME', plugin_basename( FANTRAX__FILE__ ) ); | ||
|
||
require FANTRAX_ABSPATH . 'classes/Setup.php'; | ||
require FANTRAX_ABSPATH . 'classes/Printing.php'; | ||
require FANTRAX_ABSPATH . 'classes/CSV.php'; | ||
|
||
add_action( 'init', array( '\Fantrax\Setup', 'init' ) ); | ||
|
||
add_action( 'init', array( '\Fantrax\Setup', 'init' ) ); | ||
add_action( 'init', array( '\Fantrax\Printing', 'init' ) ); | ||
add_action( 'init', array( '\Fantrax\CSV', 'init' ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" | ||
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title><?php wp_title( '' ); ?></title> | ||
<?php wp_head(); ?> | ||
</head> | ||
<body> | ||
<table id="fantrax_fantasyadp" class="fantrax_fantasyadp tablesorter print_table"> | ||
<thead> | ||
<tr> | ||
<th>Rk</th> | ||
<th>Player</th> | ||
<th>POS</th> | ||
<th>ADP</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<?php | ||
if( !empty( $data ) ) { | ||
$i = ( $atts['start'] == 0 ) ? 1 : $atts['start']; | ||
foreach( $data as $player ) { | ||
$class = ( $i % 2 == 0 ) ? 'even' : 'odd'; | ||
if( in_array( $atts['order'], array( 'NAME', 'ADP', 'name', 'adp' ) ) && !empty( $player->ADP ) ) { | ||
?> | ||
<tr class="<?php echo $class; ?>"> | ||
<td><?php echo $i; ?></td> | ||
<td><?php echo $player->name; ?></td> | ||
<td><?php echo $player->pos; ?></td> | ||
<td><?php echo $player->ADP; ?></td> | ||
</tr> | ||
<?php | ||
$i++; | ||
} elseif( in_array( $atts['order'], array( 'NAME', 'ADP_PPR', 'name', 'adp_ppr' ) ) && !empty( $player->ADP_PPR ) ) { | ||
?> | ||
<tr class="<?php echo $class; ?>"> | ||
<td><?php echo $i; ?></td> | ||
<td><?php echo $player->name; ?></td> | ||
<td><?php echo $player->pos; ?></td> | ||
<td><?php echo $player->ADP_PPR; ?></td> | ||
</tr> | ||
<?php | ||
$i++; | ||
} | ||
} | ||
} | ||
?> | ||
</tbody> | ||
</table> | ||
<script type="text/javascript"> | ||
window.print(); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters