<?php

App::uses('TextMessagesAppController', 'TextMessages.Controller');

/**
 * CreditHistories Controller
 *
 * @property Credit $Credit
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class TextMessagesController extends TextMessagesAppController {
	/**
	 * Components
	 *
	 * @var array
	 */
	public $components = array('Paginator', 'Session');
	
	/**
	 * admin_index method
	 *
	 * @return void
	*/
	public function admin_index() {
		$this->TextMessage->recursive = 0;
		
		$this->Paginator->settings = array(
				'order' => array(
						'TextMessage.id' => 'desc',
				),
			);
		
		$this->set('textMessages', $this->Paginator->paginate());
	}
	
	/**
	 * admin_view method
	 *
	 * @throws NotFoundException
	 * @param string $id
	 * @return void
	 */
	public function admin_view($id = null) {
		if (!$this->TextMessage->exists($id)) {
			throw new NotFoundException(__('Invalid text message'));
		}
		$options = array('conditions' => array('TextMessage.' . $this->TextMessage->primaryKey => $id));
		$this->set('textMessage', $this->TextMessage->find('first', $options));
		$this->set('currentkey', $id);
	}
	
	/**
	 * admin_add method
	 *
	 * @return void
	 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->TextMessage->create();
			if ($this->TextMessage->save($this->request->data)) {
				$this->Session->setFlash(__('The text message has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The text message could not be saved. Please, try again.'));
			}
		}
	}
}
