Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
export_csv.php
Go to the documentation of this file.
1 <?php
2 if ( !function_exists('prepare_csv') ){
9  function prepare_csv($str){
10  return str_replace('"','""',$str);
11  }
12 }
13 
14 if ( !function_exists('fputcsv') ){
20  function fputcsv($filePointer,$dataArray,$delimiter=',',$enclosure='"')
21  {
22  // Write a line to a file
23  // $filePointer = the file resource to write to
24  // $dataArray = the data to write out
25  // $delimeter = the field separator
26 
27 
28  // Build the string
29  $dataArray = array_map('prepare_csv', $dataArray);
30  $string = $enclosure.implode($enclosure.$delimiter.$enclosure, $dataArray).$enclosure;
31 
32 
33  // Append new line
34  $string .= "\n";
35 
36  // Write the string to the file
37  fwrite($filePointer,$string);
38  }
39 }
40 
41 
42 
44 
45  function handle(&$params){
46  set_time_limit(0);
47  import('Dataface/RecordReader.php');
49  $query = $app->getQuery();
50  $query['-limit'] = 9999999;
51  $table =& Dataface_Table::loadTable($query['-table']);
52  if ( isset($query['-relationship']) and @$query['--related'] ){
53  $query['-related:start'] = 0;
54  $query['-related:limit'] = 9999999;
55  $record =& $app->getRecord();
56  $relationship =& $table->getRelationship($query['-relationship']);
57 
58  $records =& df_get_related_records($query); //$record->getRelatedRecordObjects($query['-relationship']);
59 
60  $data = array(/*$relationship->_schema['short_columns']*/);
61  $headings = array();
62  foreach ( $relationship->_schema['short_columns'] as $colhead ){
63  $f =& $relationship->getField($colhead);
64  if ( @$f['visibility']['csv'] == 'hidden' ){
65  unset($f);
66  continue;
67  }
68  $headings[] = $colhead;
69  unset($f);
70  }
71  $data[] = $headings;
72  foreach ($records as $record){
73  if ( !$record->checkPermission('view') ) continue;
74  $data[] = $this->related_rec2data($record);
75  }
76  $temp = tmpfile();
77  foreach ($data as $row){
78  fputcsv($temp, $row,",",'"');
79  }
80  } else {
81  $temp = tmpfile();
82  $query['-skip'] = 0;
83  $query['-limit'] = null;
84  $records = new Dataface_RecordReader($query);
85  //$records =& df_get_records_array($query['-table'], $query,null,null,false);
86  //$data = array();
87  $headings = array();
88  foreach (array_merge(array_keys($table->fields()), array_keys($table->graftedFields())) as $colhead){
89  $f =& $table->getField($colhead);
90  if ( @$f['visibility']['csv'] == 'hidden' ){
91  unset($f);
92  continue;
93  }
94  $headings[] = $colhead;
95  unset($f);
96 
97  }
98  //$data[] = $headings;
99  fputcsv($temp, $headings,",",'"');
100  foreach ($records as $record){
101  if ( !$record->checkPermission('view') ) continue;
102  $data = $this->rec2data($record);
103  fputcsv($temp, $data,",",'"');
104  }
105  }
106 
107 
108  fseek($temp,0);
109  header("Content-type: text/csv; charset={$app->_conf['oe']}");
110  header('Content-disposition: attachment; filename="'.$query['-table'].'_results_'.date('Y_m_d_H_i_s').'.csv"');
111 
112  $fstats = fstat($temp);
113  while ( ob_end_clean() );
114  //echo fread($temp, $fstats['size']);
115  fpassthru($temp);
116  fclose($temp);
117  exit;
118 
119 
120 
121  }
122 
123  function rec2data(&$record){
124  $out = array();
125  $columns = array_merge(array_keys($record->_table->fields()), array_keys($record->_table->graftedFields()));
126 
127  foreach ($columns as $key){
128  $f =& $record->_table->getField($key);
129  if ( @$f['visibility']['csv'] == 'hidden' ){
130  unset($f);
131  continue;
132  }
133  $out[] = $record->display($key);
134  unset($f);
135  }
136  return $out;
137  }
138 
139  function related_rec2data(&$record){
140  $out = array();
141  $r =& $record->_relationship;
142  foreach ($r->_schema['short_columns'] as $col){
143  $f =& $r->getField($col);
144  if ( @$f['visibility']['csv'] == 'hidden' ){
145  unset($f);
146  continue;
147  }
148  $out[] = $record->display($col);
149  unset($f);
150  }
151  return $out;
152 
153  }
154 
155 }
156 
157 ?>