Skip to content

Commit

Permalink
Finished building out the Printing and CSV Download version of the AD…
Browse files Browse the repository at this point in the history
…P tool
  • Loading branch information
tjsteinhaus committed Aug 13, 2017
1 parent d38217b commit 67490c1
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 5 deletions.
97 changes: 97 additions & 0 deletions classes/CSV.php
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");
}
}
62 changes: 62 additions & 0 deletions classes/Printing.php
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();
}
}

}
60 changes: 57 additions & 3 deletions classes/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ class Setup {
*/
private static $shortcode = 'fantrax_table';

/**
* Print Friendly URL Parameter
*
* @var string
* @since 1.1
*/
public static $print_parameter = 'printfriendly';

/**
* CSV Download URL Parameter
*
* @var string
* @since 1.1
*/
public static $csv_parameter = 'csvdownload';

/**
* Setup constructor.
*/
Expand Down Expand Up @@ -66,6 +82,8 @@ static public function buildShortcodeOutput( $atts, $content = null ) {
wp_enqueue_style( 'fantrax-adp-style' );

$url = self::buildApiUrl( $atts );
$print_url = self::buildPrintUrl( $atts );
$csv_url = self::buildCsvDownloadUrl( $atts );

$data = self::callApi( $url );

Expand All @@ -87,24 +105,60 @@ static public function buildShortcodeOutput( $atts, $content = null ) {
* @since 1.0
* @author Tyler Steinhaus
*/
static private function buildApiUrl( $parameters ) {
static protected function buildApiUrl( $parameters ) {

$encode_parameters = http_build_query( $parameters );

return self::$apiurl . $encode_parameters;
}

/**
* Build Print URL with given parameters
*
* @since 1.1
* @author Tyler Steinhaus
*/
static private function buildPrintUrl( $parameters ) {
$encode_parameters = http_build_query( $parameters );

$permalink = get_permalink();

if( strpos( $permalink, '?' ) ) {
return $permalink . '&' . self::$print_parameter . '=true&' . $encode_parameters;
} else {
return $permalink . '?' . self::$print_parameter . '=true&' . $encode_parameters;
}
}

/**
* Build Print URL with given parameters
*
* @since 1.1
* @author Tyler Steinhaus
*/
static private function buildCsvDownloadUrl( $parameters ) {
$encode_parameters = http_build_query( $parameters );

$permalink = get_permalink();

if( strpos( $permalink, '?' ) ) {
return $permalink . '&' . self::$csv_parameter . '=true&' . $encode_parameters;
} else {
return $permalink . '?' . self::$csv_parameter . '=true&' . $encode_parameters;
}
}

/**
* Gather the data from the API so we can pass it
* to our shortcodes view.
*
* @since 1.0
* @author Tyler Steinhaus
*/
static private function callApi( $api_url ) {
static protected function callApi( $api_url ) {

$request = wp_remote_get( $api_url );
$body = wp_remote_retrieve_body( $request )
$body = wp_remote_retrieve_body( $request );

if( !empty( $body ) ) {

Expand Down
9 changes: 7 additions & 2 deletions fantrax_fantasyadp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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' ) );
57 changes: 57 additions & 0 deletions views/print_output.php
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>
3 changes: 3 additions & 0 deletions views/table_output.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<div class="adp_download_links">
<a href="<?php echo $print_url; ?>" target="_blank">Print</a> | <a href="<?php echo $csv_url; ?>" target="_blank">Download CSV</a>
</div>
<table id="fantrax_fantasyadp" class="fantrax_fantasyadp tablesorter">
<thead>
<tr>
Expand Down

0 comments on commit 67490c1

Please sign in to comment.