<?php
App::uses('AppController', 'Controller');
/**
 * AppNews Controller
 *
 * @property AppNews $AppNews
 */
class AppNewsController extends AppController {
	
	
	public $helpers = array(
			'Ajax',
	);

	/**
	 * beforeFilter
	 *
	 * @return void
	 */
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow(
				'notes'
			);
	}
	
/**
 * Notas
 *
 * @return void
 */
	public function notes($app) {
		$this->autoLayout = false;
		$this->AppNews->recursive = 0;
		
		$news = $this->AppNews->find('all', array(
					'conditions' => array(
								'AppNews.app' => $app
							),
					'fields' => array(
								'AppNews.created',
								'AppNews.app',
								'AppNews.note',
								'User.username',
							),
				 	'limit' => 100
				));
		$this->response->body(json_encode($news));
		return $this->response;
	}

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

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

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->AppNews->create();
			if ($this->AppNews->save($this->request->data)) {
				$this->Session->setFlash('Dados Salvos com Sucesso!',
						'default', array(), 'good');
				$this->redirect(array('add'));
			} else {
				$this->Session->setFlash('Falha ao Salvar dados, tente novamente!',
						'default', array(), 'bad');
			}
		}
		$users = $this->AppNews->User->find('list');
		$this->set(compact('users'));
	}

/**
 * admin_edit method
 *
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		$this->AppNews->id = $id;
		if (!$this->AppNews->exists()) {
			throw new NotFoundException(__('Invalid app news'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->AppNews->save($this->request->data)) {
				$this->Session->setFlash('Dados Salvos com Sucesso!',
						'default', array(), 'good');
			} else {
				$this->Session->setFlash('Falha ao Salvar dados, tente novamente!',
						'default', array(), 'bad');
			}
		}
		$this->request->data = $this->AppNews->read(null, $id);
		$users = $this->AppNews->User->find('list');
		$this->set(compact('users'));
	}

/**
 * admin_delete method
 *
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->AppNews->id = $id;
		if (!$this->AppNews->exists()) {
			throw new NotFoundException(__('Invalid app news'));
		}
		if ($this->AppNews->delete()) {
			$this->Session->setFlash(__('App news deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('App news was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
