<?php

App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');

/**
 * Bugs Controller
 *
 * @property Bug $Bug
 */
class SiteContactsController extends AppController {

    /**
     * beforeFilter
     *
     * @return void
     */
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('contact');
        $this->Auth->allow('equipaments');
        $this->Auth->allow('success');
    }

    /**
     * admin_index method
     *
     * @return void
     */
    public function admin_index() {
        $this->SiteContact->recursive = 0;

        $this->FilterResults->addFilters(
                array(
                    'filter_name' => array(
                        'SiteContact.name' => array(
                            'operator' => 'like',
                        )
                    ),
        ));

        $this->FilterResults->setPaginate('order', 'SiteContact.id DESC');
        $this->FilterResults->setPaginate('conditions', $this->FilterResults->getConditions());
        $this->FilterResults->setPaginate('limit', 9);

        $this->set('siteContacts', $this->paginate());
    }

    /**
     * admin_view method
     *
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        $this->SiteContact->id = $id;
        if (!$this->SiteContact->exists()) {
            throw new NotFoundException(__('Invalid site contact'));
        }
        $this->set('siteContact', $this->SiteContact->read(null, $id));
        $this->set('currentkey', $id);
    }

    /**
     * admin_add method
     *
     * @return void
     */
    public function contact() {
        if ($this->request->is('post')) {

            $controller = 'site_contacts';
            $action = 'success';
            $param = '';
            $anchor = '';

            if (array_key_exists('SiteContact', $this->request->data)) {
                if (array_key_exists('controller', $this->request->data['SiteContact'])) {
                    $controller = $this->request->data['SiteContact']['controller'];
                }

                if (array_key_exists('action', $this->request->data['SiteContact'])) {
                    $action = $this->request->data['SiteContact']['action'];
                }

                if (array_key_exists('param', $this->request->data['SiteContact'])) {
                    $param = $this->request->data['SiteContact']['param'];
                }

                if (array_key_exists('anchor', $this->request->data['SiteContact'])) {
                    $anchor = $this->request->data['SiteContact']['anchor'];
                }
            }

            $this->SiteContact->create();
            if ($this->SiteContact->save($this->request->data)) {
                $this->sendMail($this->request->data['SiteContact']);
                $this->Session->setFlash('Seus dados foram enviados com sucesso!', 'default', array(), 'good');
            } else {
                $this->Session->setFlash('Opa, algo errado aconteceu. Por favor, tente novamente.', 'default', array(), 'bad');
            }
            $this->redirect(array(
                'controller' => $controller,
                'action' => $action, $param,
                '#' => $anchor
            ));
        }
    }

    /**
     * Retorna uma pagina de contato com sucesso
     */
    public function success() {

    }

    /**
     * admin_add method
     *
     * @return void
     */
    public function equipaments() {
        if ($this->request->is('post')) {
            $this->SiteContact->create();
            if ($this->SiteContact->save($this->request->data)) {
                $this->Session->setFlash(__('Dados enviados com sucesso', 'default', array(), 'good'));
                $this->redirect(array('controller' => 'node', 'action' => 'index'));
            } else {
                $this->Session->setFlash(__('The types contact could not be saved. Please, try again.', 'default', array(), 'bad'));
            }
        }
    }

    /**
     * Envia um email para um endereco
     */
    public function sendMail($contact) {

        $product = array_key_exists('productname', $contact) ? $contact['productname'] : '';
        $message = array_key_exists('message', $contact) ? $contact['message'] : '';

        $Email = new CakeEmail('default');

        // enviando email para o administrador
        $Email->emailFormat('html');
        $Email->from(array(Configure::read('Site.email') => Configure::read('Site.title')));
        $Email->to(Configure::read('Site.email_admin'));
        $Email->subject(__('[%s] Novo contato pelo Portal', Configure::read('Site.title')));
        $Email->template('contactadmin');
        $Email->viewVars(array(
            'siteTitle' => Configure::read('Site.title'),
            'name' => $contact['name'],
            'phone' => $contact['phone'],
            'email' => $contact['email'],
            'urlBase' => Configure::read('Site.url_base'),
            'product' => $product,
            'message' => $message
        ));
        $Email->send();

        // enviando email para o cliente
        $Email->emailFormat('html');
        $Email->from(array(Configure::read('Site.email') => Configure::read('Site.title')));
        $Email->to($contact['email']);
        $Email->subject(__('[%s] Novo contato pelo Portal', Configure::read('Site.title')));
        $Email->template('contactclient');
        $Email->viewVars(array(
            'siteTitle' => Configure::read('Site.title'),
            'name' => $contact['name'],
            'phone' => $contact['phone'],
            'email' => $contact['email'],
            'urlBase' => Configure::read('Site.url_base'),
            'product' => $product,
            'message' => $message
        ));
        $Email->send();
    }

}
