/** * generates HTML table of signatures for a single petition * * @param int $id the ID petition for which we are displaying signatures * @param int $start the first signature to be retrieved * @param int $limit number of signatures to be retrieved * @param string $context either 'shortcode' or 'ajax' to distinguish between calls from the initia page load (shortcode) and calls from pagination buttons (ajax) * @param string $dateformat PHP date format provided by shortcode attribute - also relayed in ajax requests * @param string $nextbuttontext provided by shortcode attribute * @param string $prevtbuttontext provided by shortcode attribute * * @return string HTML table containing signatures (or just the table rows if context is ajax) */ public static function table($id, $start, $limit, $context = 'shortcode', $dateformat = 'M d, Y', $nextbuttontext = '>', $prevbuttontext = '<') { include_once 'class.signature.php'; $the_signatures = new dk_speakout_Signature(); $options = get_option('dk_speakout_options'); // get list of columns to display - as defined in settings $columns = unserialize($options['signaturelist_columns']); // get the signatures $signatures = $the_signatures->all($id, $start, $limit, 'signaturelist'); $total = $the_signatures->count($id, 'signaturelist'); $current_signature_number = $total - $start; $signatures_list = ''; // only show signature lists if there are signatures if ($total > 0) { // determine which columns to display $display_city = in_array('sig_city', $columns) ? 1 : 0; $display_state = in_array('sig_state', $columns) ? 1 : 0; $display_postcode = in_array('sig_postcode', $columns) ? 1 : 0; $display_country = in_array('sig_country', $columns) ? 1 : 0; $display_custom = in_array('sig_custom', $columns) ? 1 : 0; $display_date = in_array('sig_date', $columns) ? 1 : 0; if ($context !== 'ajax') { // only include on initial page load (not when paging) $signatures_list = ' <!-- signaturelist --> <table class="dk-speakout-signaturelist dk-speakout-signaturelist-' . $id . '"> <caption>' . $options['signaturelist_header'] . '</caption>'; } $row_count = 0; foreach ($signatures as $signature) { if ($row_count % 2) { $signatures_list .= '<tr class="dk-speakout-even">'; } else { $signatures_list .= '<tr class="dk-speakout-odd">'; } $signatures_list .= '<td class="dk-speakout-signaturelist-count">' . number_format($current_signature_number, 0, '.', ',') . '</td>'; $display_lastname = $signature->last_name; // if we have enabled privacy, only show forst letter of surname if ($options['signaturelist_privacy'] == 'enabled') { $display_lastname = substr($signature->last_name, 0, 1) . "."; } $signatures_list .= '<td class="dk-speakout-signaturelist-name">' . stripslashes($signature->first_name . ' ' . $display_lastname) . '</td>'; // if we display both city and state, combine them into one column $city = $display_city ? $signature->city : ''; $state = $display_state ? $signature->state : ''; if ($display_city && $display_state) { // should we separate with a comma? $delimiter = $city != '' && $state != '' ? ', ' : ''; $signatures_list .= '<td class="dk-speakout-signaturelist-city">' . stripslashes($city . $delimiter . $state) . '</td>'; } else { if ($display_city) { $signatures_list .= '<td class="dk-speakout-signaturelist-city">' . stripslashes($city) . '</td>'; } if ($display_state) { $signatures_list .= '<td class="dk-speakout-signaturelist-state">' . stripslashes($state) . '</td>'; } } if ($display_postcode) { $signatures_list .= '<td class="dk-speakout-signaturelist-postcode">' . stripslashes($signature->postcode) . '</td>'; } if ($display_country) { $signatures_list .= '<td class="dk-speakout-signaturelist-country">' . stripslashes($signature->country) . '</td>'; } if ($display_custom) { $signatures_list .= '<td class="dk-speakout-signaturelist-custom">' . stripslashes($signature->custom_field) . '</td>'; } if ($display_date) { $signatures_list .= '<td class="dk-speakout-signaturelist-date">' . date_i18n($dateformat, strtotime($signature->date)) . '</td>'; } $signatures_list .= '</tr>'; $current_signature_number--; $row_count++; } if ($context !== 'ajax') { // only include on initial page load if ($limit != 0 && $start + $limit < $total) { $colspan = count($columns) + 2; $signatures_list .= ' <tr class="dk-speakout-signaturelist-pagelinks"> <td colspan="' . $colspan . '"> <a class="dk-speakout-signaturelist-prev dk-speakout-signaturelist-disabled" rel="' . $id . ',' . $total . ',' . $limit . ',' . $total . ',0">' . $prevbuttontext . '</a> <a class="dk-speakout-signaturelist-next" rel="' . $id . ',' . ($start + $limit) . ',' . $limit . ',' . $total . ',1">' . $nextbuttontext . '</a> </td> </tr> '; } $signatures_list .= '</table>'; } } return $signatures_list; }
function dk_speakout_signatures_csv() { // check security: ensure user has authority and intention if (!current_user_can('publish_posts')) { wp_die(__('Insufficient privileges: You need to be an editor to do that.', 'dk_speakout')); } check_admin_referer('dk_speakout-download_signatures'); include_once 'class.signature.php'; $signatures = new dk_speakout_Signature(); $petition_id = isset($_REQUEST['pid']) ? $_REQUEST['pid'] : ''; // petition id // retrieve signatures from the database $csv_data = $signatures->all($petition_id, 0, 0, 'csv'); // display error message if query returns no results if (count($csv_data) < 1) { echo '<h1>' . __("No signatures found.", "dk_speakout") . '</h1>'; die; } // construct CSV filename $counter = 0; foreach ($csv_data as $file) { if ($counter < 1) { $filename_title = stripslashes(str_replace(' ', '-', $file->title)); $filename_date = date('Y-m-d', strtotime(current_time('mysql', 0))); $filename = $filename_title . '_' . $filename_date . '.csv'; } $counter++; } // set up CSV file headers header('Content-Type: text/octet-stream; charset=UTF-8'); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Pragma: public'); // supposed to make stuff work over https // get the column headers translated $firstname = __('First Name', 'dk_speakout'); $lastname = __('Last Name', 'dk_speakout'); $email = __('Email Address', 'dk_speakout'); $street = __('Street Address', 'dk_speakout'); $city = __('City', 'dk_speakout'); $state = __('State', 'dk_speakout'); $postcode = __('Post Code', 'dk_speakout'); $country = __('Country', 'dk_speakout'); $date = __('Date Signed', 'dk_speakout'); $confirmed = __('Confirmed', 'dk_speakout'); $petition_title = __('Petition Title', 'dk_speakout'); $petitions_id = __('Petition ID', 'dk_speakout'); $email_optin = __('Email Opt-in', 'dk_speakout'); $custom_message = __('Custom Message', 'dk_speakout'); $language = __('Language', 'dk_speakout'); // If set, use the custom field label as column header instead of "Custom Field" $counter = 0; foreach ($csv_data as $label) { if ($counter < 1) { if ($label->custom_field_label != '') { $custom_field_label = stripslashes($label->custom_field_label); } else { $custom_field_label = __('Custom Field', 'dk_speakout'); } } $counter++; } // construct CSV file header row // must use double quotes and separate with tabs $csv = "{$firstname}\t{$lastname}\t{$email}\t{$street}\t{$city}\t{$state}\t{$postcode}\t{$country}\t{$custom_field_label}\t{$date}\t{$confirmed}\t{$petition_title}\t{$petitions_id}\t{$email_optin}\t{$custom_message}\t{$language}"; $csv .= "\n"; // construct CSV file data rows foreach ($csv_data as $signature) { // convert the 1, 0, or '' values of confirmed to readable format $confirm = $signature->is_confirmed; if ($confirm == 1) { $confirm = __('confirmed', 'dk_speakout'); } elseif ($confirm == 0) { $confirm = __('unconfirmed', 'dk_speakout'); } else { $confirm = '...'; } // convert the 1, 0, or '' values of optin to readable format $optin = $signature->optin; if ($optin == 1) { $optin = __('yes', 'dk_speakout'); } elseif ($optin == 0) { $optin = __('no', 'dk_speakout'); } else { $optin = '...'; } $csv .= stripslashes('"' . $signature->first_name . '" "' . $signature->last_name . '" "' . $signature->email . '" "' . $signature->street_address . '" "' . $signature->city . '" "' . $signature->state . '" "' . $signature->postcode . '" "' . $signature->country . '" "' . $signature->custom_field . '" "' . $signature->date . '" "' . $confirm . '" "' . $signature->title . '" "' . $signature->petitions_id . '" "' . $optin . '" "' . $signature->custom_message . '" "' . $signature->language . '"'); $csv .= "\n"; } // output CSV file in a UTF-8 format that Excel can understand echo chr(255) . chr(254) . mb_convert_encoding($csv, 'UTF-16LE', 'UTF-8'); exit; }