Permalink
Browse files

adding some fail-proofing tweaks to csv tool; downgrade c5 requiremen…

…t to 5.4.0
  • Loading branch information...
1 parent 17ceedd commit 8e2e9e684d084fd268fdf0a967727eb1a459a626 @macek macek committed Apr 26, 2012
Showing with 53 additions and 17 deletions.
  1. +6 −1 controller.php
  2. +34 −11 libraries/csv/export.php
  3. +13 −5 libraries/csv/import.php
View
@@ -5,7 +5,7 @@
class BurritoPackage extends Package {
protected $pkgHandle = 'burrito';
- protected $appVersionRequired = '5.5.0';
+ protected $appVersionRequired = '5.4.0';
protected $pkgVersion = '1.0.0';
public function on_start() {
@@ -20,4 +20,9 @@ public function getPackageName() {
return t("Burrito");
}
+ // Concrete 5.4.0 legacy function
+ public function install(){
+ parent::install();
+ }
+
}
View
@@ -61,6 +61,7 @@
private $filename;
private $h;
private $data;
+ private $_csv_output;
public function __construct() {
$this->db = Loader::db();
@@ -120,23 +121,26 @@ private function output(){
}
private function capture(){
- ob_start();
- // header
- fputcsv($this->h, $this->header($this->db, $this->data[0]));
+ if (is_null($this->_csv_output)) {
+ ob_start();
- // rows
- foreach($this->data as $row){
- fputcsv($this->h, $this->eachRow($this->db, $row));
- }
+ // header
+ fputcsv($this->h, $this->header($this->db, $this->data[0]));
+
+ // rows
+ foreach($this->data as $row){
+ fputcsv($this->h, $this->eachRow($this->db, $row));
+ }
- $csv = ob_get_clean();
+ $this->_csv_output = ob_get_clean();
- if (fclose($this->h) === false) {
- throw new Exception('There was an error closing the output stream');
+ if (fclose($this->h) === false) {
+ throw new Exception('There was an error closing the output stream');
+ }
}
- return $csv;
+ return $this->_csv_output;
}
final public function run() {
@@ -149,5 +153,24 @@ private function capture(){
}
+ protected function backup($path){
+ $th = Loader::helper('text');
+ $filename = $th->uncamelcase(get_class($this)) . '-' . time() . '.csv.gz';
+ $path = getcwd() . $path;
+
+ // open file to write backup
+ if (($h=fopen($path.'/'.$filename, 'w')) !== false) {
+ fwrite($h, gzencode($this->capture(), 9));
+ fclose($h);
+ }
+
+ // could not open file for writing
+ else {
+ throw new Exception("Could not open {$filename} for writing. Check to write permissions on {$path}");
+ }
+
+ return true;
+ }
+
}
View
@@ -82,7 +82,7 @@ private function checkMimeType() {
throw new Exception("You must upload a valid CSV file; a file of type {$_} was given");
}
- fclose($finfo);
+ finfo_close($finfo);
}
private function getHandle(){
@@ -135,19 +135,27 @@ private function cleanse($data) {
foreach($data as $field => $value) {
- if (strlen($value) && method_exists($this, $_="{$this->fields[$field]}Format")) {
- $data[$field] = $this->{$_}($value);
+ if (method_exists($this, $_="{$this->fields[$field]}Format")) {
+ $data[$field] = $this->{$_}(trim($value));
}
}
return $data;
}
- protected function dateFormat($value){
+ protected function intFormat($value) {
+ return is_numeric($value) ? (int)$value : 0;
+ }
+
+ protected function stringFormat($value) {
+ return empty($value) ? null : $value;
+ }
+
+ protected function dateFormat($value) {
return date('Y-m-d', strtotime($value));
}
- protected function datetimeFormat($value){
+ protected function datetimeFormat($value) {
return date('Y-m-d H:i:s', strtotime($value));
}

0 comments on commit 8e2e9e6

Please sign in to comment.