<?php
class ControllerToolCategoryDescription extends Controller{

    public function index()
    {

        //unset($this->session->data['excel_file']);
        die();
        header('Content-Type: text/html; charset=utf-8');
        set_time_limit(0);
        ini_set('memory_limit', -1);

        require_once DIR_SYSTEM . 'PHPExcel/Classes/PHPExcel.php';

        $file_path = $file_path = DIR_UPLOAD . "-301_last.xlsx";
        $inputFileType = PHPExcel_IOFactory::identify($file_path);

        $flag = 0;
        $valid = false;
        $types = array('Excel2007', 'Excel5');
        foreach ($types as $type) {
            $objReader = PHPExcel_IOFactory::createReader($type);
            if ($objReader->canRead($file_path)) {
                $valid = true;
                break;
            }
        }

        if (!$valid) {
            die("Bad file");
        }

        if (!isset($this->session->data['excel_file'])) {
            $objPHPExcel = new PHPExcel();
            $objReader->setReadDataOnly(true);
            $objPHPExcel = $objReader->load($file_path);
            $rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
            $array_data = array();
            foreach ($rowIterator as $row) {
                $cellIterator = $row->getCellIterator();
                $cellIterator->setIterateOnlyExistingCells(false);
                $rowIndex = $row->getRowIndex();
                $columnIndex = 0;
                //if (!$flag) {
                foreach ($cellIterator as $cell) {
                    $this->session->data['excel_file'][$rowIndex - 1][$columnIndex++] = $cell->getCalculatedValue();
                }
                $flag++;
                //}
            }

            //unset($this->session->data['excel_file'][0]);
        }

        //var_dump(count($this->session->data['excel_file'])); die();

        foreach ($this->session->data['excel_file'] as $key => $rows){ //var_dump($rows); die();
            $url_from = ltrim($rows[0],'/');
            $url_to = ltrim($rows[1],'/'); //var_dump($url_from); die();
            $this->db->query("INSERT INTO ps_301redirect SET url_from = '". $this->db->escape($url_from) ."', url_to = '". $this->db->escape($url_to) ."' ");
            echo $key . '<br>'; //die();
        }

    }

}