'Name',
'Company' => 'Company',
'Created' => '',
);
protected $rows = array();
protected $shared_strings = array();
function __construct( $doc_prop = array() ) {
$this->doc_prop = array_merge( $this->doc_prop, $doc_prop );
}
function can_xlsx() {
return class_exists( 'ZipArchive', false );
}
function add_row( $fields ) {
$this->rows[] = $fields;
}
function add_rows( $rows ) {
$this->rows = array_merge( $this->rows, $rows );
}
function xml_save() {
$xml_filename = tempnam( sys_get_temp_dir(), 'cf7-export-xml' );
$this->doc_prop['Created'] = gmdate( 'Y-m-d\TH:i:s\Z' );
$doc_prop_fields = array();
foreach ( $this->doc_prop as $prop_key => $prop_value ) {
$doc_prop_fields[] = sprintf(
'<%1$s>%2$s%1$s>',
$prop_key,
filter_var( $prop_value, FILTER_SANITIZE_SPECIAL_CHARS )
);
}
$xml = sprintf(
'
%s
',
implode( "\n", $doc_prop_fields ),
implode( "\n", $this->xml_get_rows() )
);
$save_xml = file_put_contents( $xml_filename, $xml );
if ( $save_xml ) {
return $xml_filename;
}
return new WP_Error(
'export-xml-fail',
__( 'Failed to save the exported Excel XML file.', 'cf7-storage' )
);
}
function xlsx_save() {
if ( ! $this->can_xlsx() ) {
return new WP_Error(
'missing-zip-extension',
__( 'You version of PHP doesn\'t support creating ZIP files which is required for creating XLSX files.', 'cf7-storage' )
);
}
$zip_filename = tempnam( sys_get_temp_dir(), 'cf7-export-xlsx' );
$zip = new ZipArchive();
$create_zip = $zip->open( $zip_filename, ZipArchive::CREATE );
if ( ! $create_zip ) {
return new WP_Error(
'create-zip-fail',
__( 'Failed to create the ZIP file required for XLSX.', 'cf7-storage' )
);
}
/**
* Build the XLSX file and directory tree
*/
$zip->addEmptyDir( 'docProps' );
$zip->addFromString( 'docProps/app.xml', $this->xlsx_get_app_xml() );
$zip->addFromString( 'docProps/core.xml', $this->xlsx_get_core_xml() );
$zip->addEmptyDir( '_rels' );
$zip->addFromString( '_rels/.rels', $this->xlsx_get_rels_xml() );
$zip->addEmptyDir( 'xl/worksheets' );
$zip->addFromString( 'xl/worksheets/sheet1.xml', $this->xlsx_get_sheet_xml() );
$zip->addFromString( 'xl/workbook.xml', $this->xlsx_get_workbook_xml() );
$zip->addFromString( 'xl/sharedStrings.xml', $this->xlsx_get_shared_strings_xml() );
$zip->addEmptyDir( 'xl/_rels' );
$zip->addFromString( 'xl/_rels/workbook.xml.rels', self::xlsx_get_workbook_rels_xml() );
$zip->addFromString( '[Content_Types].xml', $this->xlsx_get_content_types_xml() );
$zip->close();
return $zip_filename;
}
function xlsx_get_shared_string_no( $string ) {
static $string_pos = array();
if ( isset( $this->shared_strings[ $string ] ) ) {
$this->shared_strings[ $string ] += 1;
} else {
$this->shared_strings[ $string ] = 1;
}
if ( ! isset( $string_pos[ $string ] ) ) {
$string_pos[ $string ] = array_search( $string, array_keys( $this->shared_strings ) );
}
return $string_pos[ $string ];
}
function xlsx_cell_name( $row_no, $column_no ) {
$n = $column_no;
for ( $r = ''; $n >= 0; $n = intval( $n / 26 ) - 1 ) {
$r = chr( $n % 26 + 0x41 ) . $r;
}
return $r . ( $row_no + 1 );
}
function xml_get_rows() {
$rows = array();
foreach ( $this->rows as $row ) {
$cells = array();
foreach ( $row as $field_value ) {
$field_value = filter_var( $field_value, FILTER_SANITIZE_SPECIAL_CHARS );
$field_type = 'String';
if ( is_numeric( $field_value ) ) {
$field_type = 'Number';
}
$cells[] = sprintf(
'%s | ',
$field_type,
$field_value
);
}
$rows[] = sprintf(
'%s
',
implode( "\n", $cells )
);
}
return $rows;
}
function xlsx_get_sheet_xml() {
$rows = array();
foreach ( $this->rows as $row_no => $row ) {
$cells = array();
$row = array_values( $row );
foreach ( $row as $col_no => $field_value ) {
$field_type = 's';
if ( is_numeric( $field_value ) ) {
$field_type = 'n';
}
$field_value_no = $this->xlsx_get_shared_string_no( $field_value );
$cells[] = sprintf(
'%d',
$this->xlsx_cell_name( $row_no, $col_no ),
$field_type,
$field_value_no
);
}
$rows[] = sprintf(
'
%s
',
$row_no + 1,
implode( "\n", $cells )
);
}
return sprintf(
'
%s
',
implode( "\n", $rows )
);
}
function xlsx_get_shared_strings_xml() {
$shared_strings = array();
foreach ( $this->shared_strings as $string => $string_count ) {
$shared_strings[] = sprintf(
'%s',
filter_var( $string, FILTER_SANITIZE_SPECIAL_CHARS )
);
}
return sprintf(
'
%s
',
array_sum( $this->shared_strings ),
count( $this->shared_strings ),
implode( "\n", $shared_strings )
);
}
function xlsx_get_workbook_xml() {
return sprintf(
'
'
);
}
function xlsx_get_content_types_xml() {
return sprintf(
'
'
);
}
function xlsx_get_workbook_rels_xml() {
return sprintf(
'
'
);
}
function xlsx_get_app_xml() {
return sprintf(
'
Microsoft Excel
'
);
}
function xlsx_get_core_xml() {
return sprintf(
'
%s
Preseto
',
date( 'Y-m-d\TH:i:s.00\Z' )
);
}
function xlsx_get_rels_xml() {
return sprintf(
'
'
);
}
}