<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[2018] sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/wordcamp-payments-network.php: WordCamp Payments Dashboard: Add support for exporting requests to a CSV.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/2018">2018</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/2018","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>iandunn</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-10-29 23:42:08 +0000 (Thu, 29 Oct 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>WordCamp Payments Dashboard: Add support for exporting requests to a CSV.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcamppaymentsnetworkwordcamppaymentsnetworkphp">sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/wordcamp-payments-network.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordcamporgpublic_htmlwpcontentpluginswordcamppaymentsnetworkwordcamppaymentsnetworkphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/wordcamp-payments-network.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/wordcamp-payments-network.php   2015-10-29 20:33:57 UTC (rev 2017)
+++ sites/trunk/wordcamp.org/public_html/wp-content/plugins/wordcamp-payments-network/wordcamp-payments-network.php     2015-10-29 23:42:08 UTC (rev 2018)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -24,8 +24,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        wp_schedule_event( time(), 'hourly', 'wordcamp_payments_aggregate' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'wordcamp_payments_aggregate', array( __CLASS__, 'aggregate' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_action( 'admin_enqueue_scripts',  array( __CLASS__, 'enqueue_assets' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 add_action( 'network_admin_menu', array( __CLASS__, 'network_admin_menu' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'init', array( __CLASS__, 'upgrade' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_action( 'init', array( __CLASS__, 'process_export_request' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Diff-based updates to the index.
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'save_post', array( __CLASS__, 'save_post' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -126,6 +128,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'blog_id' => get_current_blog_id(),
</span><span class="cx" style="display: block; padding: 0 10px">                        'post_id' => $request->ID,
</span><span class="cx" style="display: block; padding: 0 10px">                        'created' => get_post_time( 'U', true, $request->ID ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                // todo Sometimes this is empty. Core normally catches this (r8636), but misses in our case because we don't use drafts. #1350-meta might have the side-effect of solving this.
+                       'paid'    => absint( get_post_meta( $request->ID, '_camppayments_date_vendor_paid', true ) ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         'due' => absint( get_post_meta( $request->ID, '_camppayments_due_by', true ) ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'status' => $request->post_status,
</span><span class="cx" style="display: block; padding: 0 10px">                        'method' => get_post_meta( $request->ID, '_camppayments_payment_method', true ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -177,31 +181,262 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Enqueue scripts and stylesheets
+        *
+        * @param string $hook
+        */
+       public static function enqueue_assets( $hook ) {
+               if ( 'index_page_wcp-dashboard' == $hook && 'export' == self::get_current_tab() ) {
+                       wp_enqueue_script( 'jquery-ui-datepicker' );
+                       wp_enqueue_style( 'jquery-ui' );
+                       wp_enqueue_style( 'wp-datepicker-skins' );
+               }
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Renders the Dashboard - Payments screen.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public static function render_dashboard() {
</span><span class="cx" style="display: block; padding: 0 10px">                ?>
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                 <div class="wrap">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        <?php screen_icon( 'tools' ); ?>
</del><span class="cx" style="display: block; padding: 0 10px">                         <h1>WordCamp Payments Dashboard</h1>
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                         <?php settings_errors(); ?>
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                         <h3 class="nav-tab-wrapper"><?php self::render_dashboard_tabs(); ?></h3>
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        <?php self::$list_table->print_inline_css(); ?>
-                       <div id="wcp-list-table">
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 <?php
+                               if ( 'export' == self::get_current_tab() ) {
+                                       self::render_export_tab();
+                               } else {
+                                       self::render_table_tabs();
+                               }
+                       ?>
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                <?php self::$list_table->prepare_items(); ?>
-                               <form id="posts-filter" action="" method="get">
-                                       <input type="hidden" name="page" value="wcp-dashboard" />
-                                       <input type="hidden" name="wcp-section" value="overdue" />
-                                       <?php self::$list_table->display(); ?>
-                               </form>
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         </div> <!-- /wrap -->
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        </div>
</del><span class="cx" style="display: block; padding: 0 10px">                 <?php
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Render the table tabs, like Overview, Pending, etc
+        */
+       protected static function render_table_tabs() {
+               ?>
+
+               <?php self::$list_table->print_inline_css(); ?>
+
+               <div id="wcp-list-table">
+                       <?php self::$list_table->prepare_items(); ?>
+
+                       <form id="posts-filter" action="" method="get">
+                               <input type="hidden" name="page" value="wcp-dashboard" />
+                               <input type="hidden" name="wcp-section" value="overdue" />
+                               <?php self::$list_table->display(); ?>
+                       </form>
+               </div>
+
+               <?php
+       }
+
+       /**
+        * Process export requests
+        */
+       public static function process_export_request() {
+               if ( empty( $_POST['submit'] ) || 'export' != self::get_current_tab() ) {
+                       return;
+               }
+
+               if ( ! current_user_can( 'manage_network' ) || ! check_admin_referer( 'export', 'wcpn_request_export' ) ) {
+                       return;
+               }
+
+               $start_date = strtotime( $_POST['wcpn_export_start_date'] . ' 00:00:00' );
+               $end_date   = strtotime( $_POST['wcpn_export_end_date']   . ' 23:59:59' );
+               $filename   = sanitize_file_name( sprintf( 'wordcamp-payments-%s-to-%s.csv', date( 'Y-m-d', $start_date ), date( 'Y-m-d', $end_date ) ) );
+
+               $report = self::generate_payment_report( $_POST['wcpn_date_type'], $start_date, $end_date );
+
+               if ( is_wp_error( $report ) ) {
+                       add_settings_error( 'wcp-dashboard', $report->get_error_code(), $report->get_error_message() );
+               } else {
+                       header( 'Content-Type: text/csv' );
+                       header( sprintf( 'Content-Disposition: attachment; filename="%s"', $filename ) );
+                       header( 'Cache-control: private' );
+                       header( 'Pragma: private' );
+                       header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
+
+                       echo $report;
+                       die();
+               }
+       }
+
+       /*
+        * Generate and return the raw payment report contents
+        *
+        * @param string $date_type 'paid' | 'created'
+        * @param int $start_date
+        * @param int $end_date
+        *
+        * @return string | WP_Error
+        */
+       protected static function generate_payment_report( $date_type, $start_date, $end_date ) {
+               global $wpdb;
+
+               if ( ! in_array( $date_type, array( 'paid', 'created' ), true ) ) {
+                       return new WP_Error( 'wcpn_bad_date_type', 'Invalid date type.' );
+               }
+
+               if ( ! is_int( $start_date ) || ! is_int( $end_date ) ) {
+                       return new WP_Error( 'wcpn_bad_dates', 'Invalid start or end date.' );
+               }
+
+               $column_headings = array(
+                       'WordCamp', 'ID', 'Title', 'Status', 'Date Vendor was Paid', 'Creation Date', 'Due Date', 'Amount',
+                       'Currency', 'Category', 'Payment Method','Vendor Name', 'Vendor Contact Person', 'Vendor Country',
+                       'Check Payable To', 'URL', 'Supporting Documentation Notes',
+               );
+
+               $table_name = self::get_table_name();
+
+               $request_indexes = $wpdb->get_results( $wpdb->prepare( "
+                       SELECT *
+                       FROM   `{$table_name}`
+                       WHERE  `{$date_type}` BETWEEN %d AND %d",
+                       $start_date,
+                       $end_date
+               ) );
+               $report = fopen( 'php://output', 'w' );
+
+               fputcsv( $report, $column_headings );
+
+               foreach( $request_indexes as $index ) {
+                       fputcsv( $report, self::get_report_row( $index ) );
+               }
+
+               fclose( $report );
+
+               return ob_get_clean();
+       }
+
+       /**
+        * Gather all the request details needed for a row in the export file
+        *
+        * @param stdClass $index
+        *
+        * @return array
+        */
+       protected static function get_report_row( $index ) {
+               switch_to_blog( $index->blog_id );
+
+               $request          = get_post( $index->post_id );
+               $currency         = get_post_meta( $index->post_id, '_camppayments_currency',         true );
+               $category         = get_post_meta( $index->post_id, '_camppayments_payment_category', true );
+               $date_vendor_paid = get_post_meta( $index->post_id, '_camppayments_date_vendor_paid', true );
+
+               if ( $date_vendor_paid ) {
+                       $date_vendor_paid = date( 'Y-m-d', $date_vendor_paid );
+               }
+
+               if ( 'null-select-one' === $currency ) {
+                       $currency = '';
+               }
+
+               if ( 'null' === $category ) {
+                       $category = '';
+               }
+
+               $row = array(
+                       get_wordcamp_name(),
+                       sprintf( '%d-%d', $index->blog_id, $index->post_id ),
+                       $request->post_title,
+                       $index->status,
+                       $date_vendor_paid,
+                       date( 'Y-m-d', $index->created ),
+                       date( 'Y-m-d', $index->due ),
+                       get_post_meta( $index->post_id, '_camppayments_payment_amount', true ),
+                       $currency,
+                       $category,
+                       get_post_meta( $index->post_id, '_camppayments_payment_method', true ),
+                       get_post_meta( $index->post_id, '_camppayments_vendor_name', true ),
+                       get_post_meta( $index->post_id, '_camppayments_vendor_contact_person', true ),
+                       get_post_meta( $index->post_id, '_camppayments_vendor_country', true ),
+                       get_post_meta( $index->post_id, '_camppayments_payable_to', true ),
+                       get_edit_post_link( $index->post_id ),
+                       get_post_meta( $index->post_id, '_camppayments_file_notes', true ),
+               );
+
+               restore_current_blog();
+
+               return $row;
+       }
+
+       /**
+        * Render the Export tab
+        */
+       protected static function render_export_tab() {
+               $today      = date( 'Y-m-d' );
+               $last_month = date( 'Y-m-d', strtotime( 'now - 1 month' ) );
+               ?>
+
+               <script>
+                       /**
+                        * Fallback to the jQueryUI datepicker if the browser doesn't support <input type="date">
+                        */
+                       jQuery( document ).ready( function( $ ) {
+                               var browserTest = document.createElement( 'input' );
+                               browserTest.setAttribute( 'type', 'date' );
+
+                               if ( 'text' === browserTest.type ) {
+                                       $( '#wcpn_export' ).find( 'input[type=date]' ).datepicker( {
+                                               dateFormat : 'yy-mm-dd',
+                                               changeMonth: true,
+                                               changeYear : true
+                                       } );
+                               }
+                       } );
+               </script>
+
+               <form id="wcpn_export" method="POST">
+                       <?php wp_nonce_field( 'export', 'wcpn_request_export' ); ?>
+
+                       <p>
+                               This form will supply a CSV file with payment requests matching the parameters you select below.
+                               For example, all requests that were <code>paid</code> between <code><?php echo esc_html( $last_month ); ?></code> and <code><?php echo esc_html( $today ); ?></code>.
+                       </p>
+
+                       <p>
+                               <label>
+                                       Date type:
+                                       <select name="wcpn_date_type">
+                                               <option value="created">created</option>
+                                               <option value="paid" selected>paid</option>
+                                       </select>
+                               </label>
+                       </p>
+
+                       <p>
+                               <label>
+                                       Start date:
+                                       <input type="date" name="wcpn_export_start_date" class="medium-text" value="<?php echo esc_attr( $last_month ); ?>" />
+                               </label>
+                       </p>
+
+                       <p>
+                               <label>
+                                       End date:
+                                       <input type="date" name="wcpn_export_end_date" class="medium-text" value="<?php echo esc_attr( $today ); ?>" />
+                               </label>
+                       </p>
+
+                       <?php submit_button( 'Export' ); ?>
+               </form>
+
+               <?php
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Loads and initializes the list table object.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public static function pre_render_dashboard() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -216,7 +451,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public static function get_current_tab() {
</span><span class="cx" style="display: block; padding: 0 10px">                $tab = 'overdue';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( isset( $_REQUEST['wcp-section'] ) && in_array( $_REQUEST['wcp-section'], array( 'pending', 'overdue', 'paid', 'incomplete' ) ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( isset( $_REQUEST['wcp-section'] ) && in_array( $_REQUEST['wcp-section'], array( 'pending', 'overdue', 'paid', 'incomplete', 'export' ) ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $tab = $_REQUEST['wcp-section'];
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -233,6 +468,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'pending' => 'Pending',
</span><span class="cx" style="display: block; padding: 0 10px">                        'paid'    => 'Paid',
</span><span class="cx" style="display: block; padding: 0 10px">                        'incomplete' => __( 'Incomplete', 'wordcamporg' ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'export'     => __( 'Export', 'wordcamporg' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $sections as $section_key => $section_caption ) {
</span></span></pre>
</div>
</div>

</body>
</html>