<?php
namespace App\Controller;

use App\Controller\AppController;
//use Cake\ORM\TableRegistry;

/**
 * Offices Controller
 *
 * @property \App\Model\Table\OfficesTable $Offices
 */
class OfficesController extends AppController
{

    public function importdst() {
        
        $office = $this->Offices->newEntity();
        if ($this->request->is(['patch', 'post', 'put'])) {
            //debug($this->request['data']);die;
        $options = [
            'length' => 0,
            'delimiter' => ';',
            'enclosure' => '"',
            'escape' => '\\',
            'headers' => false,
            'text' => false,
            'excel_bom' => false,
        ];
        $handle = fopen($this->request['data']['file']['tmp_name'], "r");
        $i = 0;
        while ($row = fgetcsv($handle, $options['length'], $options['delimiter'])) {  
            $data = array();         
            if($i == 0) {
                $keys = $row;
            }
            else {
                $j = 0;              
                foreach($keys as $column) {
                    $data[$column] = $row[$j];
                    $j++; 
                }   
                //debug($data);
                //debug($data['d_dst_schluessel']);
                $office = $this->Offices->find('all',['conditions' => array('dst_schluessel' => $data['d_dst_schluessel'])])->first();
                
                $office['d_regierung']  = $data['d_regierung'];
                $office['d_mb']         = $data['d_mb'];
                $office['d_schulamt']   = $data['d_schulamt'];
                //debug($office);die;

                if (!($this->Offices->save($office))) {
                    debug($office);die;
                }
    
            }
            $i++;           
        }
 
    }
        $this->set(compact('office'));
        $this->set('_serialize', ['office']);
 
    }

    public function import() {
        
        $office = $this->Offices->newEntity();
        if ($this->request->is(['patch', 'post', 'put'])) {
            //debug($this->request['data']);
            $this->Offices->deleteAll('id' > 0);
        $options = [
            'length' => 0,
            'delimiter' => ',',
            'enclosure' => '"',
            'escape' => '\\',
            'headers' => false,
            'text' => false,
            'excel_bom' => false,
        ];
        $handle = fopen($this->request['data']['file']['tmp_name'], "r");
        $i = 0;
        while ($row = fgetcsv($handle, $options['length'], $options['delimiter'])) {  
            $data = array();         
            if($i == 0) {
                $keys = $row;
            }
            else {
                $j = 0;
                //debug($row);               
                foreach($keys as $column) {
                    $data[$column] = $row[$j];
                    $j++; 
                }     
                $office = $this->Offices->newEntity();     
                $office = $this->Offices->patchEntity($office, $data);
                //debug($office);
                if (!($this->Offices->save($office))) {
                    debug($office);die;
                }
                //debug($data);
                //die;
            }
            $i++;           
        }
        //debug($data_array);die;
        //$offices = TableRegistry::get('Offices');
        //$entities = $offices->newEntities($data_array);
        //debug($entities);die;
        //$result = $this->Offices->saveMany($entities);
    }
    $this->set(compact('office'));
    $this->set('_serialize', ['office']);
    }

    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
        $offices = $this->paginate($this->Offices);

        $this->set(compact('offices'));
        $this->set('_serialize', ['offices']);
    }

    /**
     * View method
     *
     * @param string|null $id Office id.
     * @return \Cake\Network\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $office = $this->Offices->get($id, [
            'contain' => []
        ]);

        $this->set('office', $office);
        $this->set('_serialize', ['office']);
    }

    /**
     * Add method
     *
     * @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $office = $this->Offices->newEntity();
        if ($this->request->is('post')) {
            $office = $this->Offices->patchEntity($office, $this->request->data);
            if ($this->Offices->save($office)) {
                $this->Flash->success(__('The office has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The office could not be saved. Please, try again.'));
        }
        $this->set(compact('office'));
        $this->set('_serialize', ['office']);
    }

    /**
     * Edit method
     *
     * @param string|null $id Office id.
     * @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $office = $this->Offices->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $office = $this->Offices->patchEntity($office, $this->request->data);
            if ($this->Offices->save($office)) {
                $this->Flash->success(__('The office has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The office could not be saved. Please, try again.'));
        }
        $this->set(compact('office'));
        $this->set('_serialize', ['office']);
    }

    /**
     * Delete method
     *
     * @param string|null $id Office id.
     * @return \Cake\Network\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $office = $this->Offices->get($id);
        if ($this->Offices->delete($office)) {
            $this->Flash->success(__('The office has been deleted.'));
        } else {
            $this->Flash->error(__('The office could not be deleted. Please, try again.'));
        }

        return $this->redirect(['action' => 'index']);
    }
}
