<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\I18n\Time;
use Cake\Event\Event;

/**
 * Apresults Controller
 *
 * @property \App\Model\Table\ApresultsTable $Apresults
 */
class ApresultsController extends AppController
{
    public function beforeFilter(Event $event) {
        $this->Auth->setConfig('checkAuthIn', 'Controller.initialize');
        //$this->Auth->allow('ftp');
    }

    function export() {
        $fields = array('snr','aptype','lieferstatus','erzeugungsdatum','apcount','anzahl');
        $schools = $this->Apresults->find('all',array('fields' => $fields));
        $schools_array = json_decode(json_encode($schools), true);
        $path = WWW_ROOT.'files/ap/export.csv';
        //debug($path);

        $file = fopen($path,"w");
        $title = true;
        $columns = array();
        foreach($schools_array as  $school) {
            //debug(explode(',',$school));
            //die;
            if ($title) {
                foreach ($school as $key => $column) {
                    array_push($columns,$key);
                }
                fputcsv($file,$columns,';',' ');
                $title = false;
            }
            //debug(date('Y-m-d H:i:s',strtotime($school['erzeugungsdatum'])));
            //debug($school['erzeugungsdatum']);
            if(!is_null($school['erzeugungsdatum'])) {
                $school['erzeugungsdatum'] = date('Y-m-d H:i:s',strtotime($school['erzeugungsdatum']));
                //debug($school['erzeugungsdatum']);
            }
            //debug($school['erzeugungsdatum']);
            //die;
            fputcsv($file,$school,';',' ');
        }
        
        fclose($file);

        $ftp_server = "10.190.22.35";
        $ftp_port = "2587";
        $ftp_user = "LfStaD-Log";
        $ftp_pass = "135!Log!086";
        $local_folder = '/test';

        $conn_id = ftp_connect($ftp_server, $ftp_port) or die("Couldn't connect to $ftp_server");

        if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
            echo "Angemeldet als $ftp_user@$ftp_server\n";
        } else {
            echo "Anmeldung als $ftp_user nicht möglich\n";
        }

        ftp_chdir($conn_id, 'ap-csv');

        ftp_put($conn_id, 'export.csv', $path, FTP_BINARY);

        ftp_close($conn_id);

    }

    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
        $apresults = $this->Apresults->find('all',array('conditions' => array('lieferstatus' => '1')));
        $this->set('apresults',$apresults);
    }

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

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

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

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

    /**
     * Edit method
     *
     * @param string|null $id Apresult 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)
    {
        $apresult = $this->Apresults->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $apresult = $this->Apresults->patchEntity($apresult, $this->request->data);
            if ($this->Apresults->save($apresult)) {
                $this->Flash->success(__('The apresult has been saved.'));

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

    /**
     * Delete method
     *
     * @param string|null $id Apresult 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']);
        $apresult = $this->Apresults->get($id);
        if ($this->Apresults->delete($apresult)) {
            $this->Flash->success(__('The apresult has been deleted.'));
        } else {
            $this->Flash->error(__('The apresult could not be deleted. Please, try again.'));
        }

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