<?php
    namespace App\Controller;

    use App\Controller\AppController;

    class EmploymentsController extends AppController
    {
        public function analyse()
        {
            if ($this->request->is('post')) {
                $data = $this->request->data;
                //debug($data['Login_Key']);
                $key = $data['Login_Key'];
                if($key == 't5BfdBY4Tj4uPBpQ3773mbkmFuhybLsn') {
                    $file_out = '/var/www/html/asd-control/webroot/files/analyse/analyse.txt';
                    $this->set('file_content',file_get_contents($file_out));
                    $this->set('stage',1);
                }
                else {
                    $employment = $this->Employments->newEntity();
                    $this->set('employment',$employment);
                    $this->set('stage',0);
                }
            }
            else {
                $employment = $this->Employments->newEntity();
                $this->set('employment',$employment);
                $this->set('stage',0);
            }
        }

        public function index()
        {
            $employment = $this->Employments->newEntity();
            if ($this->request->is('post')) {
                //debug($this->request->data);
                $xmldata = $this->request->data['xmldata'];
                $filename = $xmldata['tmp_name'];
                //echo $filename;
                $dataxml = file_get_contents($filename);
                $dataxml = '<xml>' . $dataxml . '</xml>';
                //debug($dataxml);die;
                $xml = simplexml_load_string($dataxml);
                //debug($xml);
                $json = json_encode($xml);
                $array =json_decode($json,TRUE);
                //debug($array);die;
                $rows = ['TYP', 'ARBEITGEBER', 'DIENSTSTELLE','RECHTSVERHAELTNIS','VON','BIS', 'IST_NEBENTAETIGKEIT','MELDENDE_DIENSTSTELLE','UPZ_MIN','UPZ_MAX','BEURLAUBUNG','TEILZEIT','TEILZEITSTUNDEN_SCHULAUFSICHT','TEILZEITSTUNDEN','VIVA_PKZ','DV_ID'];
                $this->set('rows',$rows);
                $rows_eas = ['VON','BIS','STAT_RELEV_WOSTD','STUNDEN','STD_WISS','DIENSTSTELLE','LEH_ID','MELDENDE_DIENSTSTELLE','IST_KENNZAHLENRELEVANT'];
                $this->set('rows_eas',$rows_eas);
                //debug(sizeof($array));die;
                //$array[0]=$array;
                //debug($array['Lehrer']);die;   
                //foreach($array['Lehrer'] as $teacher) {
                //debug($array);
                //debug(sizeof($array));
                //debug(isset($array['Lehrer'][0]));
                if (!isset($array['Lehrer'][0])) {
                    $teacher_array = $array;
                }   
                else {
                    $teacher_array =$array['Lehrer'];
                }
                foreach($teacher_array as $teacher) {
                    //debug($teacher);die;
                    $teacher_nr =$teacher['PKZ'];
                    $part_nr = 0;
                    foreach($teacher['DV'] as $part) {
                        foreach($part as $key => $value) {
                                $data[$teacher_nr][$part_nr][$key] = $value;
                        }
                        $part_nr++;
                    }
                    $part_nr = 0;
                    if(isset($teacher['EAS'])) {
                        foreach($teacher['EAS'] as $part) {
                            foreach($part as $key => $value) {
                                $data_eas[$teacher_nr][$part_nr][$key] = $value;
                            }
                            $part_nr++;
                        }
                    }
                    
                }
                //debug($data);
                //debug($rows);
                //$list = implode($rows,',');
                //debug($list);
                //echo 'Hallo';die;
                //debug($data_eas);die;
                $this->set('data',$data);
                if (isset($data_eas)) {
                    $this->set('data_eas',$data_eas);
                }
            }
            $this->set('employment',$employment);

        }

    }
