![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00002 if ( !function_exists('prepare_csv') ){ 00009 function prepare_csv($str){ 00010 return str_replace('"','""',$str); 00011 } 00012 } 00013 00014 if ( !function_exists('fputcsv') ){ 00020 function fputcsv($filePointer,$dataArray,$delimiter=',',$enclosure='"') 00021 { 00022 // Write a line to a file 00023 // $filePointer = the file resource to write to 00024 // $dataArray = the data to write out 00025 // $delimeter = the field separator 00026 00027 00028 // Build the string 00029 $dataArray = array_map('prepare_csv', $dataArray); 00030 $string = $enclosure.implode($enclosure.$delimiter.$enclosure, $dataArray).$enclosure; 00031 00032 00033 // Append new line 00034 $string .= "\n"; 00035 00036 // Write the string to the file 00037 fwrite($filePointer,$string); 00038 } 00039 } 00040 00041 00042 00043 class dataface_actions_export_csv { 00044 00045 function handle(&$params){ 00046 00047 $app =& Dataface_Application::getInstance(); 00048 $query = $app->getQuery(); 00049 $query['-limit'] = 9999999; 00050 $table =& Dataface_Table::loadTable($query['-table']); 00051 if ( isset($query['-relationship']) and @$query['--related'] ){ 00052 $query['-related:start'] = 0; 00053 $query['-related:limit'] = 9999999; 00054 $record =& $app->getRecord(); 00055 $relationship =& $table->getRelationship($query['-relationship']); 00056 00057 $records =& df_get_related_records($query); //$record->getRelatedRecordObjects($query['-relationship']); 00058 00059 $data = array(/*$relationship->_schema['short_columns']*/); 00060 $headings = array(); 00061 foreach ( $relationship->_schema['short_columns'] as $colhead ){ 00062 $f =& $relationship->getField($colhead); 00063 if ( @$f['visibility']['csv'] == 'hidden' ){ 00064 unset($f); 00065 continue; 00066 } 00067 $headings[] = $colhead; 00068 unset($f); 00069 } 00070 $data[] = $headings; 00071 foreach ($records as $record){ 00072 if ( !$record->checkPermission('view') ) continue; 00073 $data[] = $this->related_rec2data($record); 00074 } 00075 } else { 00076 $records =& df_get_records_array($query['-table'], $query,null,null,false); 00077 $data = array(); 00078 $headings = array(); 00079 foreach (array_merge(array_keys($table->fields()), array_keys($table->graftedFields())) as $colhead){ 00080 $f =& $table->getField($colhead); 00081 if ( @$f['visibility']['csv'] == 'hidden' ){ 00082 unset($f); 00083 continue; 00084 } 00085 $headings[] = $colhead; 00086 unset($f); 00087 00088 } 00089 $data[] = $headings; 00090 foreach ($records as $record){ 00091 if ( !$record->checkPermission('view') ) continue; 00092 $data[] = $this->rec2data($record); 00093 } 00094 } 00095 00096 $temp = tmpfile(); 00097 foreach ($data as $row){ 00098 fputcsv($temp, $row,",",'"'); 00099 } 00100 fseek($temp,0); 00101 header("Content-type: text/csv; charset={$app->_conf['oe']}"); 00102 header('Content-disposition: attachment; filename="'.$query['-table'].'_results_'.date('Y_m_d_H_i_s').'.csv"'); 00103 00104 $fstats = fstat($temp); 00105 00106 echo fread($temp, $fstats['size']); 00107 fclose($temp); 00108 exit; 00109 00110 00111 00112 } 00113 00114 function rec2data(&$record){ 00115 $out = array(); 00116 $columns = array_merge(array_keys($record->_table->fields()), array_keys($record->_table->graftedFields())); 00117 00118 foreach ($columns as $key){ 00119 $f =& $record->_table->getField($key); 00120 if ( @$f['visibility']['csv'] == 'hidden' ){ 00121 unset($f); 00122 continue; 00123 } 00124 $out[] = $record->display($key); 00125 unset($f); 00126 } 00127 return $out; 00128 } 00129 00130 function related_rec2data(&$record){ 00131 $out = array(); 00132 $r =& $record->_relationship; 00133 foreach ($r->_schema['short_columns'] as $col){ 00134 $f =& $r->getField($col); 00135 if ( @$f['visibility']['csv'] == 'hidden' ){ 00136 unset($f); 00137 continue; 00138 } 00139 $out[] = $record->display($col); 00140 unset($f); 00141 } 00142 return $out; 00143 00144 } 00145 00146 } 00147 00148 ?>