<?

class csvimport {
    var $filename;
    var $errorcode = '';
    var $datarows = array();
    var $fieldnames = array(); // Spaltenueberschriften
    var $delimiter = '';

    public function __construct($filename,$fieldnames) {
        $this->filename = $filename;
        $this->fieldnames = $fieldnames;
        return $this;
    }

    private function splitfields($row) {
        if ($this->delimiter == '') { 
            /* Try to guess delimiter if it has not been set before*/
            $delimiters = array(';',"\t",',');
            /* Easy guessing: The delimiter that splits the max number of
               fields wins */
            $max = 0;        
            foreach ($delimiters as $delimiter) {
                $fields = explode($delimiter,$row);
                if (count($fields) > $max) {
                    $max = count($fields);
                    $this->delimiter = $delimiter;
                }
            }
            if ($max < 1) {
                /* Should never happen */
                return false;
            }
        }
        return explode($this->delimiter,$row);
    }

    public function isvalid() {
        if ($this->filename == '') {
            $this->errorcode = 'NOFILENAME_ERROR';
            return false;
        }

        if (count($this->fieldnames) < 1) {
            $this->errorcode = 'NOFIELDNAMES_ERROR';
            return false;
        }

        $lines = array();
        $fin = fopen($this->filename, 'r') or 
            error('Kann Datei "' . $this->filename . '" nicht lesen');
        while ($line = fgets($fin)) {
            array_push($lines,trim($line));
        }
        fclose($fin);
        $numrows = count($lines);
        if (!($fields = $this->splitfields($lines[0]))) {
            $this->errorcode = 'FORMAT_ERROR';
            return false;
        }

        $numcols = count($fields);
        if ($numrows < 2 || $numcols < count($this->fieldnames)) {
            $this->errorcode = 'FORMAT_ERROR';
            return false;
        }

        $r=0;
        foreach($lines as $line) {
            if (! ($fields = $this->splitfields($line))) {
                $this->errorcode = 'EMPTY_ERROR';
                return false;
            } 

            $validfirstrow = true;
            for($c=0;$c < count($this->fieldnames);$c++) {
                $cellvalue = trim(strtolower($fields[$c]));
                if ($cellvalue != trim(strtolower($this->fieldnames[$c]))) {
                    $validfirstrow = false;
                }
            }
            if ($validfirstrow) {
                $r++;
                $this->readdatarows($lines,$r);
                return true;
            }
        }
        $this->errorcode = 'FORMAT_ERROR';
        return false;
    }

    private function readdatarows($lines,$r) {
        $rows = array();
        $row = array();
        for ($i = $r; $i < count($lines); $i++) {
            $fields = $this->splitfields($lines[$i]);
            for ($c=0; $c < count($this->fieldnames); $c++) {
                if (isset($fields[$c])) {
                    $row[$this->fieldnames[$c]] = trim(utf8_encode($fields[$c]));
                } else {
                    $row[$this->fieldnames[$c]] = '';
                }
            }

            /* check if row is just empty values */
            $empty = true;
            for ($c=0; $c < count($this->fieldnames); $c++) {
                if ($row[$this->fieldnames[$c]] != '') {
                    $empty = false;
                    break;
                }
            }
            if (!$empty) {
                array_push($rows,$row);
            }

            $this->datarows = $rows;
        }
    }
}

?>
