Beispiel #1
0
 /**
  * CSV Index.
  *
  * @param string $type Model type. user for file name
  * 
  * @return void
  **/
 function admin_index($type = false)
 {
     if (false == $this->debug) {
         Configure::write('debug', 0);
     }
     if (!$type) {
         echo 'Model name missing';
         exit;
     }
     Configure::load('csver');
     if (is_null(Configure::read('Csver.whitelist'))) {
         echo 'Did you set up the config file? Need whitelist of allowed CSV models.';
         exit;
     }
     $this->type = $type;
     if ('*' != Configure::read('Csver.whitelist') && !in_array($type, Configure::read('Csver.whitelist'))) {
         echo 'Not permitted. Not on whitelist.';
         exit;
     }
     //We don't use Controller::loadModel() due to bug where if successful, doesn't return true.
     //If this fails, will just get a 404 error, so we don't bother any extra checking.
     $model = ClassRegistry::init($type);
     if (method_exists($model, 'csv')) {
         $data = $model->csv();
     } else {
         $data = $this->csv($model);
     }
     $filename = tempnam(TMP, '');
     App::import('Vendor', 'Csver.CsvWriter', array('file' => 'php-csv/csv.php'));
     $file = new CsvWriter($filename);
     //Add header row
     $file->addLine($data['header']);
     foreach ($data['results'] as $line) {
         $file->addLine($line[$type]);
     }
     $this->_output($filename);
 }
Beispiel #2
0
 function testCsvReader_TwoLines()
 {
     $filename = dirname(__FILE__) . '/data/writer-one-line.csv';
     $csv = new CsvWriter($filename);
     $csv->addLine(array('One', 'Two words', 'One "quoted"', 'Single "quote'));
     $csv->addLine(array('Line Number', '"Two "" is here"', 'Is', 'It fine?'));
     $csv->close();
     $file = fopen(dirname(__FILE__) . '/data/writer-one-line.csv', 'r');
     $string1 = utf8_encode(fgets($file));
     $string2 = utf8_encode(fgets($file));
     fclose($file);
     @unlink($filename);
     $expected1 = 'One,Two words,"One ""quoted""","Single ""quote"' . "\r\n";
     $expected2 = 'Line Number,"""Two """" is here""",Is,It fine?' . "\r\n";
     $this->assertEqual($string1, $expected1);
     $this->assertEqual($string2, $expected2);
 }
Beispiel #3
0
<?php

require_once '../csv.php';
$data = array(array('cell one', 'cell two', 'cell three'), array('cell four', 'cell five', 'cell six'), array('cell seven', 'cell eight', 'cell nine'));
// make sure your web server has permission to write to the folder
$file = new CsvWriter(dirname(__FILE__) . '/../tests/data/file.csv');
foreach ($data as $line) {
    $file->addLine($line);
}