<?php



use Website\Controller\Action;

use \Website\Model\Auth;



class IntranetController extends Action

{



    public function clientsOverviewAction(){

        $this->enableLayout();

        $this->setLayout('intranet/intranet');



        $this->view->freemiums = Website\Model\Freemium::getAll();

    }



    public function createFreemiumAction(){

        $this->enableLayout();

        $this->setLayout('intranet/intranet');





        if(!$this->_request->isPost()) {

            $this->view->showForm = true;

            return;

        }



        $agency = new Website\Model\Agency();

        $freemium = new \Website\Model\Freemium();



        $agency->setId($this->getParam('id'));

        $agency->setUid($this->getParam('uid'));



        $agency->setName($this->getParam('name'));

        $agency->setPhone($this->getParam('phone'));

        $agency->setEmail($this->getParam('email'));

        $agency->setAddress($this->getParam('address'));

        $agency->setCp($this->getParam('cp'));



        $agency->setAccountType(3);



        $agency->setShort_description_fr($this->getParam('short_description_fr'));



        $agency->setShort_description_nl($this->getParam('short_description_nl'));



        $agency->setWebsite($this->getParam('website'));



        $agency->setSeerus_key($this->getParam('seerus_key'));

        $agency->setSeerus_secret($this->getParam('seerus_secret'));

        $agency->setDashboard_token($this->getParam('seerus_token'));

        $agency->setSeerus_custom_url($this->getParam('custom_url'));



        $freemium->setAgency_uid($this->getParam('uid'));

        $freemium->setStep(0);

        $freemium->setStep_0_time(date('Y-m-d H:i:s'));



        echo 'Success';





        try{

            $auth = \Website\Model\Auth::create(array(

                'name' => 'freemium',

                'surname' => 'freemium',

                'email' => $this->getParam('login'),

                'password' => $this->getParam('pwd')

            ));



            $auth->setAgency_uid($this->getParam('uid'));





            $auth->save();

            $agency->save();

            $freemium->save();



            $uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/upload/' . $agency->getUid() . '/' ;



            if (!file_exists($uploaddir))

                mkdir($uploaddir, 0777, true);



            foreach($_FILES as $file)

                if(move_uploaded_file($file['tmp_name'], $uploaddir . 'logo.png'))

                    $files[] = $uploaddir .$file['name'];





            $this->view->showForm = false;

            $this->view->message = "success";

        }catch(\Exception $e){

            $this->view->showForm = true;

            $this->view->message = $e->getMessage();

        }

    }





}