<?php


class SupsysticTablesPro_Exporter_Type_Csv implements SupsysticTablesPro_Exporter_Interface
{
    /**
     * Exports specified data.
     * @param array $data
     * @return mixed
     * @throws \SupsysticTablesPro_Exporter_Exception
     */
    public function export(array $data)
    {
        $letters = range('A', 'Z');
		$prefix = array_merge(array(''), $letters);
		$countIndex = 0;
		$letterIndex = 0;
        $excel = new PHPExcel();
        $sheet = $excel->getActiveSheet();

        foreach ($data['table']->rows as $rowIndex => $row) {
            foreach ($row['cells'] as $colIndex => $cell) {
				//echo '<pre>'; print_r($cell); echo '</pre>';
				$cellIndex = $prefix[$countIndex] . $letters[$letterIndex] . ($rowIndex + 1);
                $sheet->setCellValue($cellIndex, addslashes($cell['data']));
				if($letters[$letterIndex] == 'Z') {
					$letterIndex = 0;
					$countIndex++;
				} else {
					$letterIndex++;
				}
            }
			$letterIndex = 0;
			$countIndex = 0;
        }
        //die;

        /** @var PHPExcel_Writer_CSV $writer */
        $writer = PHPExcel_IOFactory::createWriter($excel, 'CSV')
            ->setDelimiter(',')
            ->setEnclosure('')
            ->setLineEnding("\r\n");
        $writer->save('php://output');
    }
}