/** * @param $datas */ public function getCsv($data) { $filePath = "../" . $this->filename . '.csv'; $writer = new CsvWriter(array('delimiter' => ';', 'enclosure' => '"', 'encoding' => 'CP1252', 'eol' => "\r\n", 'escape' => "\\", 'bom' => false, 'translit' => null, 'first_row_header' => false, 'trim' => false)); $stream = fopen($filePath, 'wb'); $writer->open($stream); $array = json_decode(json_encode($data), true); $writer->writeRows($array); $writer->getFileContent(); $writer->close(); $maxRead = 1 * 2048 * 2048; $fileSize = filesize($filePath); $fh = fopen($filePath, 'r'); $read = 0; header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . str_replace('../', '', $this->filename) . ".csv" . '"'); while (!feof($fh)) { echo fread($fh, $maxRead); } exit; }
public function testWritingTempStream() { $csvArray = array(array('nom', 'prénom', 'age'), array('Martin', 'Durand', '28'), array('Alain', 'Richard', '36')); $expected = 'nom,prénom,age' . "\n" . 'Martin,Durand,28' . "\n" . 'Alain,Richard,36' . "\n"; $writer = new CsvWriter(array('delimiter' => ',', 'enclosure' => '"', 'encoding' => 'UTF-8', 'eol' => "\n", 'escape' => "\\", 'enclosing_mode' => Dialect::ENCLOSING_MINIMAL, 'escape_double' => true)); $this->assertFalse($writer->isFileOpened()); $this->assertInstanceOf('CSanquer\\ColibriCsv\\CsvWriter', $writer->createTempStream()); $this->assertTrue($writer->isFileOpened()); $this->assertInternalType('resource', $writer->getFileHandler()); $this->assertInstanceOf('CSanquer\\ColibriCsv\\CsvWriter', $writer->writeRows($csvArray)); $this->assertEquals($expected, $writer->getFileContent()); $this->assertInstanceOf('CSanquer\\ColibriCsv\\CsvWriter', $writer->close()); }
/** * Execute the command * * @param InputInterface $input * @param OutputInterface $output * * @return void */ protected function execute(InputInterface $input, OutputInterface $output) { $ep = $this->getService('sugarcrm.entrypoint'); $bm = new BeanManager($ep); $this->module = $input->getOption('module'); // Get the file as a parameter if (empty($this->module)) { $moduleList = array_keys($ep->getBeansList()); $msg = 'You must define the module with --module'; $msg .= PHP_EOL . PHP_EOL . 'List of Available modules: ' . PHP_EOL; $msg .= ' - ' . implode(PHP_EOL . ' - ', $moduleList); throw new \InvalidArgumentException($msg); } ########### FIELDS $moduleFields = $bm->getModuleFields($this->module, $input->getOption('lang'), true); // Change the lists that are arrays as strings foreach ($moduleFields as $key => $moduleProps) { if (array_key_exists('options_list', $moduleProps)) { $optionsList = ''; foreach ($moduleFields[$key]['options_list'] as $optK => $optV) { $optionsList .= "{$optK}<=>{$optV}" . PHP_EOL; } $moduleFields[$key]['options_list'] = $optionsList; } else { $moduleFields[$key]['options_list'] = 'N/A'; } } // create the writer $writer = new CsvWriter(array('delimiter' => ';', 'enclosure' => '"', 'encoding' => 'UTF-8', 'bom' => false, 'first_row_header' => true, 'trim' => true)); $file = $this->module . '-Fields.' . date('Y-m-d') . '.csv'; //Open the csv file to write $writer->open(getcwd() . '/' . $file); $writer->writeRows($moduleFields); $writer->close(); $output->writeln("<comment>All fields for {$this->module} written in {$file}</comment>"); ########### RELATIONSHIPS $moduleRelationships = $bm->getModuleRelationships($this->module); // create the writer $writer = new CsvWriter(array('delimiter' => ';', 'enclosure' => '"', 'encoding' => 'UTF-8', 'bom' => false, 'first_row_header' => true, 'trim' => true)); $file = $this->module . '-Relationships.' . date('Y-m-d') . '.csv'; //Open the csv file to write $writer->open(getcwd() . '/' . $file); $writer->writeRows($moduleRelationships); $writer->close(); $output->writeln("<comment>All relationships for {$this->module} written in {$file}</comment>"); }