| 1 |
<?php |
| 2 |
|
| 3 |
abstract class Red_FileIO { |
| 4 |
public static function create( $type ) { |
| 5 |
$exporter = false; |
| 6 |
|
| 7 |
if ( $type === 'rss' ) { |
| 8 |
include_once dirname( dirname( __FILE__ ) ).'/fileio/rss.php'; |
| 9 |
$exporter = new Red_Rss_File(); |
| 10 |
} elseif ( $type === 'csv' ) { |
| 11 |
include_once dirname( dirname( __FILE__ ) ).'/fileio/csv.php'; |
| 12 |
$exporter = new Red_Csv_File(); |
| 13 |
} elseif ( $type === 'apache' ) { |
| 14 |
include_once dirname( dirname( __FILE__ ) ).'/fileio/apache.php'; |
| 15 |
$exporter = new Red_Apache_File(); |
| 16 |
} elseif ( $type === 'nginx' ) { |
| 17 |
include_once dirname( dirname( __FILE__ ) ).'/fileio/nginx.php'; |
| 18 |
$exporter = new Red_Nginx_File(); |
| 19 |
} elseif ( $type === 'json' ) { |
| 20 |
include_once dirname( dirname( __FILE__ ) ).'/fileio/json.php'; |
| 21 |
$exporter = new Red_Json_File(); |
| 22 |
} |
| 23 |
|
| 24 |
return $exporter; |
| 25 |
} |
| 26 |
|
| 27 |
public static function import( $group_id, $file ) { |
| 28 |
$parts = pathinfo( $file['name'] ); |
| 29 |
$extension = isset( $parts['extension'] ) ? $parts['extension'] : ''; |
| 30 |
$extension = strtolower( $extension ); |
| 31 |
|
| 32 |
if ( $extension === 'csv' ) { |
| 33 |
include_once dirname( dirname( __FILE__ ) ).'/fileio/csv.php'; |
| 34 |
$importer = new Red_Csv_File(); |
| 35 |
$data = ''; |
| 36 |
} else if ( $extension === 'json' ) { |
| 37 |
include_once dirname( dirname( __FILE__ ) ).'/fileio/json.php'; |
| 38 |
$importer = new Red_Json_File(); |
| 39 |
$data = @file_get_contents( $file['tmp_name'] ); |
| 40 |
} else { |
| 41 |
include_once dirname( dirname( __FILE__ ) ).'/fileio/apache.php'; |
| 42 |
$importer = new Red_Apache_File(); |
| 43 |
$data = @file_get_contents( $file['tmp_name'] ); |
| 44 |
} |
| 45 |
|
| 46 |
if ( $extension !== 'json' ) { |
| 47 |
$group = Red_Group::get( $group_id ); |
| 48 |
if ( ! $group ) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return $importer->load( $group_id, $file['tmp_name'], $data ); |
| 54 |
} |
| 55 |
|
| 56 |
public function force_download() { |
| 57 |
header( 'Cache-Control: no-cache, must-revalidate' ); |
| 58 |
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); |
| 59 |
} |
| 60 |
|
| 61 |
public static function export( $module_name_or_id, $format ) { |
| 62 |
$groups = $items = false; |
| 63 |
|
| 64 |
if ( $module_name_or_id === 'all' || $module_name_or_id === 0 ) { |
| 65 |
$groups = Red_Group::get_all(); |
| 66 |
$items = Red_Item::get_all(); |
| 67 |
} else { |
| 68 |
$module_name_or_id = is_numeric( $module_name_or_id ) ? $module_name_or_id : Red_Module::get_id_for_name( $module_name_or_id ); |
| 69 |
$module = Red_Module::get( intval( $module_name_or_id, 10 ) ); |
| 70 |
|
| 71 |
if ( $module ) { |
| 72 |
$groups = Red_Group::get_all_for_module( $module->get_id() ); |
| 73 |
$items = Red_Item::get_all_for_module( $module->get_id() ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
$exporter = self::create( $format ); |
| 78 |
if ( $exporter && $items !== false && $groups !== false ) { |
| 79 |
return array( |
| 80 |
'data' => $exporter->get_data( $items, $groups ), |
| 81 |
'total' => count( $items ), |
| 82 |
'exporter' => $exporter, |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
abstract function get_data( array $items, array $groups ); |
| 90 |
abstract function load( $group, $filename, $data ); |
| 91 |
} |
| 92 |
|