<?php

App::uses('AppController', 'Controller');

/**
 * Contacts Controller
 *
 * @property Contact $Contact
 */
class ContactsController extends AppController {

    /**
     * beforeFilter
     *
     * @return void
     */
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow(
                'z_send',
                'z_consultor'
        );
    }

    /**
     * admin_index method
     *
     * @return void
     */
    public function admin_index() {
        $this->Contact->recursive = 0;
        $this->set('contacts', $this->paginate());
    }

    public function z_send() {
        $node = array();
        $node['Node']['meta_og_title'] = 'Contato Bemmelhor Soluções Inteligentes';
        $node['Node']['meta_og_description'] = 'Obrigado por entrar em contato!';
        $node['Node']['meta_keywords'] = 'business';
        $node['Node']['meta_article_session'] = 'Business';
        $node['Node']['slug'] = 'z_send';
        $node['Node']['type'] = 'contacts';
        $node['Node']['banner'] = 'https://bemmelhor.com.br/theme/BmSpeed/img/banner-bemmelhor.png';
        $node['Node']['created'] = '2018-10-10';
        $node['Node']['updated'] = '2018-10-20';

        $this->set(compact('node'));
    }
    public function z_consultor() {
        $node = array();
        $node['Node']['meta_og_title'] = 'Contato Bemmelhor Soluções Inteligentes';
        $node['Node']['meta_og_description'] = 'Obrigado por entrar em contato!';
        $node['Node']['meta_keywords'] = 'business';
        $node['Node']['meta_article_session'] = 'Business';
        $node['Node']['slug'] = 'z_consultor';
        $node['Node']['type'] = 'contacts';
        $node['Node']['banner'] = 'https://bemmelhor.com.br/theme/BmSpeed/img/banner-bemmelhor.png';
        $node['Node']['created'] = '2018-10-10';
        $node['Node']['updated'] = '2018-10-20';

        $this->set(compact('node'));
    }

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

    /**
     * admin_add method
     *
     * @return void
     */
    public function admin_addContact() {
        if ($this->request->is('post')) {
            $this->Contact->create();
            if ($this->Contact->save($this->request->data)) {
                $this->Session->setFlash(__('The contact has been saved'));

                $this->Contact->recursive = 1;
                $this->request->data = $this->Contact->read(null, $this->Contact->id);
                $this->redirect(array('controller' => 'customers', 'action' => 'view', $this->request->data['Customer']['id']));
            } else {
                $this->Session->setFlash(__('The contact could not be saved. Please, try again.'));
            }
        }
    }

    /**
     * admin_add method
     *
     * @return void
     */
    public function admin_add($cid = null) {
        $customerid = $cid;
        if (($cid == null) && (isset($this->request->data['Contact']['_customerid']))) {
            $customerid = $this->request->data['Contact']['_customerid'];
        }

        $customers = $this->Contact->Customer->find('list', array(
            'conditions' => array('Customer.id' => $customerid),
            'limit' => 1,
        ));
        $this->set(compact('customers'));
        $this->set('customerid', $customerid);
    }

    /**
     * admin_edit method
     *
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        $this->Contact->id = $id;
        if (!$this->Contact->exists()) {
            throw new NotFoundException(__('Invalid contact'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Contact->save($this->request->data)) {
                $this->Session->setFlash(__('The contact has been saved'));

                $this->Contact->recursive = 1;
                $this->request->data = $this->Contact->read(null, $this->Contact->id);
                $this->redirect(array('controller' => 'customers', 'action' => 'view', $this->request->data['Customer']['id']));
            } else {
                $this->Session->setFlash(__('The contact could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->Contact->read(null, $id);
        }
        $customers = $this->Contact->Customer->find('list');
        $this->set(compact('customers'));
    }

    /**
     * admin_delete method
     *
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Contact->id = $id;
        if (!$this->Contact->exists()) {
            throw new NotFoundException(__('Invalid contact'));
        }
        $this->Contact->recursive = 1;
        $this->request->data = $this->Contact->read(null, $this->Contact->id);

        if ($this->Contact->delete()) {
            $this->Session->setFlash(__('Contact deleted'));
            $this->redirect(array('controller' => 'customers', 'action' => 'view', $this->request->data['Customer']['id']));
        }
        $this->Session->setFlash(__('Contact was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

}
