ServiceExcel.php 3.83 KB
<?php
namespace{


    class ServiceExcel
    {

        protected $_object, $_reader, $_sheets, $_supplier, $_fields, $file, $maxRows, $name;
        private $_errors = array();
        const CHUNK_SIZE = 5000;
        /**
         * Переменные статистики
         */

        /**
         * @var fvMemCache
         */
        protected $messenger;
        protected $rate;
        protected $result;

        public    $total,    /* Всего рядов в таблице */
            $good = 0,     /*уже есть, обновляем*/
            $bad = 0,      /*еще нет, создали, сохранили*/
            $ugly = 0;    /*ошибка сохранения */

        /**
         * @param $file - Эксель-файл
         * @param $supplierId - ИД поставщика
         * @param $maxRows
         * @param $id
         */


        function addFile($file)
        {
            $this->_reader = PHPExcel_IOFactory::createReader('CSV');
            if (method_exists($this->_reader, "setReadDataOnly"))
                $this->_reader->setReadDataOnly(true);
            $this->file = $file;
            $getWay = explode('/', $this->file);
            $this->name = end( $getWay );
        }


        public function getCSV_CharsetClean($route){

            $src = file_get_contents($route);

            $fd = @fopen($route, "w");
            fwrite($fd, $src);
            @fclose($fd);


            if (($handle = fopen($route, "r")) !== FALSE) {
                while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
                    $result[] =  $data;

                }
                fclose($handle);

            }

            return $result;
        }

        public function getCSV($route, $s = ';'){
            $result = array();
            $src = file_get_contents($route);

            $dst = iconv('windows-1251', 'UTF-8',  $src);

            $fd = @fopen($route, "w");
            fwrite($fd, $dst);
            @fclose($fd);


            if (($handle = fopen($route, "r")) !== FALSE) {

                while (($data = fgetcsv($handle, 1000, $s)) !== FALSE) {

                        $result[] =  $data;

                }
                fclose($handle);

            }

            return $result;
        }


        static function getEntity() {
            return __CLASS__;
        }

        /**
         *
         * @return array
         */

        function getRows()
        {
            $src = file_get_contents($this->file);

            $dst = iconv('windows-1251', 'UTF-8',  $src);

            #file_put_contents($this->file, $dst);
            $fd = @fopen($this->file, "w");
            fwrite($fd, $dst);
            @fclose($fd);

            $this->_object = $this->_reader->load($this->file);


            foreach ($this->_object->getSheetNames() as $index => $sheetName)
                $this->_sheets[$index] = $sheetName;

            foreach ($this->_sheets as $idx => $sheet) {
                /**
                 * @var PHPExcel_Worksheet $activeSheet
                 */
                $objWorksheet = $this->_object->getSheet($idx);

                $highestRow = $objWorksheet->getHighestRow();
                $highestColumn = $objWorksheet->getHighestColumn();

                $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
                for ($row = 2; $row <= $highestRow; ++$row) {
                    for ($col = 0; $col <= $highestColumnIndex; ++$col) {
                        $val = $objWorksheet->getCellByColumnAndRow($col, $row);

                            $this->result[$row][$col] = $val->getValue();

                    }
                }
            }

            unset($this->_object);

            return $this->result;


        }

        function getErrors()
        {
            return $this->_errors;
        }
    }

}