Jeff PHP framework  0.99
Modular, extensible, OOP, MVC, lightweight php framework designed to ease the programmers in the development of web applications.
export.class.php
Go to the documentation of this file.
00001 <?php
00023 class export {
00024         
00028         private $_registry;
00029         
00033         private $_s;
00034         
00038         private $_fe;
00039 
00043         private $_table;
00044         
00048         private $_pkey;
00049         
00053         private $_sfields;
00054         
00058         private $_fkeys;
00059 
00063         private $_head;
00064 
00068         private $_fields;
00069 
00073         private $_rids;
00074         
00078         private $_order;
00079         
00083         private $_data;
00084 
00116         function __construct($opts=array()) {
00117 
00118                 $this->_registry = registry::instance();
00119 
00120                 $this->_s = gOpt($opts, 'separator', ',');
00121                 $this->_fe = gOpt($opts, 'field_enclosure', '"');
00122                 $this->_table = gOpt($opts, 'table', '');
00123                 $this->_pkey = gOpt($opts, 'pkey', 'id');
00124                 $this->_sfields = gOpt($opts, 'sfields', array());
00125                 $this->_fkeys = gOpt($opts, 'fkeys', array());
00126                 $this->_head = gOpt($opts, 'head', true);
00127                 $this->_fields = gOpt($opts, 'fields', '*');
00128                 $this->_rids = gOpt($opts, 'rids', '*');
00129                 $this->_order = gOpt($opts, 'order', '');
00130                 $this->_data = gOpt($opts, 'data', array());
00131 
00132         }
00133 
00140         public function setTable($table) {
00141                 $this->_table = $table; 
00142         }
00143         
00150         public function setSfields($sfields) {
00151                 $this->_sfields = $sfields;     
00152         }
00153         
00160         public function setFkeys($fkeys) {
00161                 $this->_fkeys = $fkeys; 
00162         }
00163 
00170         public function setSeparator($s) {
00171                 $this->_s = $s;
00172         }
00173         
00180         public function setFieldEnclosure($fe) {
00181                 $this->_fe = $fe;
00182         }
00183 
00190         public function setFields($fields) {
00191                 $this->_fields = $fields;
00192         }
00193 
00200         public function setHead($head) {
00201                 $this->_head = $head;
00202         }
00203         
00210         public function setRids($rids) {
00211                 $this->_rids = $rids;
00212         }
00213 
00220         public function setOrder($order) {
00221                 $this->_order = $order;
00222         }
00223 
00230         public function setData($data) {
00231                 $this->_data = $data;
00232         }
00233 
00244         public function exportData($filename, $extension, $output='stream') {
00245 
00246                 if($extension=='csv') return $this->exportCsv($filename, $output);
00247                 // maybe other extensions in the future 
00248         } 
00249 
00259         private function exportCsv($filename, $output) {
00260                 
00261                 $data = $this->getData();
00262 
00263                 $csv = '';
00264                 foreach($data as $row) {
00265                         $cell = array();
00266                         foreach($row as $v) $cell[] = $this->encloseField($v);
00267                         $csv .= implode($this->_s, $cell)."\r\n";
00268                 }
00269 
00270                 ob_clean();
00271 
00272                 if($output=='stream') { 
00273                         header("Content-Type: plain/text");
00274                         header("Content-Disposition: Attachment; filename=$filename");
00275 
00276                         header("Pragma: no-cache");
00277                         echo $csv;
00278                         exit;
00279                 }
00280                 elseif($output=='file') {
00281                         $fo = fopen($filename, "w");
00282                         fwrite($fo, $csv);
00283                         fclose($fo);
00284                 }
00285 
00286         }
00287 
00297         private function getData() {
00298 
00299                 if($this->_data) return $this->_data;
00300                 if(!$this->_table) return array();
00301 
00302                 $tot_fk = count($this->_fkeys);
00303                 $tot_sf = count($this->_sfields);
00304                 $data = array();
00305                 $table_structure = $this->_registry->db->getTableStructure($this->_table);
00306 
00307                 $head_fields = $this->getHeadFields($table_structure);
00308                 if(count($head_fields) && $this->_head) $data[] = $head_fields;
00309 
00310                 if($this->_rids=='*') $where = '';
00311                 elseif(is_array($this->_rids) && count($this->_rids)) 
00312                         $where = $this->_pkey."='".implode("' OR id='", $this->_rids)."'";
00313                 elseif(is_string($this->_rids) && strlen($this->_rids)>0)       
00314                         $where = $this->_pkey."='".implode("' OR id='", explode(",",$this->_rids))."'";
00315 
00316                 $order = $this->_order ? $this->_order : null;
00317 
00318                 $at = new adminTable($this->_registry, $this->_table);
00319                 $at->setForeignKeys($this->_fkeys);
00320                 $at->setSpecialFields($this->_sfields);
00321                 $results = $this->_registry->db->autoSelect($head_fields, $this->_table, $where, $order);
00322                 foreach($results as $r) { 
00323                         if($tot_fk) $r = $at->parseForeignKeys($r);
00324                         if($tot_sf) $r = $at->parseSpecialFields($r, array("show_pwd"=>true, "mailto"=>false));
00325                         $data[] = $r;
00326                 }
00327 
00328                 return $data;
00329 
00330         }
00331 
00338         private function getHeadFields($table_structure) {
00339                 
00340                 if($this->_head && is_string($this->_fields) && preg_match("#\*#", $this->_fields)) {
00341                         preg_match("#\* -\((.*)\)#", $this->_fields, $matches);
00342                         $excluded_fields = isset($matches[1]) ? explode(",",$matches[1]):array();
00343                         $head_fields = array();
00344                         foreach($table_structure['fields'] as $field=>$info) {
00345                                 if(!in_array($field, $excluded_fields)) $head_fields[] = $field;
00346                         }
00347                 }
00348                 elseif(is_string($this->_fields)) $head_fields = explode(",",$this->_fields);
00349                 elseif(is_array($this->_fields)) $head_fields = $this->_fields;
00350 
00351                 return $head_fields;
00352 
00353         }
00354 
00361         private function encloseField($field) {
00362 
00363                 return $this->_fe.$field.$this->_fe;
00364 
00365         }
00366 
00367 }
00368 
00369 ?>