<?php
App::uses('AppController', 'Controller');
/**
 * TmItems Controller
 *
 * @property TmItem $TmItem
 */
class TmItemsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

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

		$conditions = array(
			'TmItem.updated is not null',
		);
		
		
		$this->Paginator->settings = array(
				'conditions' => $conditions,
				'order' => array(
						'TmItem.updated' => 'desc',
				),
		);
		
		$this->set('tmItems', $this->paginate());
	}

/**
 * admin_view method
 *
 * @param string $id
 * @return void
 */
	public function admin_view($id_campaign = null) {
	    
	    $this->set('title_for_layout', 'Ítens da Campanha ' .$id_campaign);	 
		$this->TmItem->recursive = 1;
	    
	    $conditions = array(
	            'TmItem.tm_campaign_id' => $id_campaign,
	    );
	    
	    $this->Paginator->settings = array(
	            'conditions' => $conditions,
	            'order' => array(
	                    'TmItem.updated' => 'ASC',
	            ),
	    );
	    
	    $this->set('tmItems', $this->paginate());
	    $this->set('tm_campaign', $id_campaign);
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->TmItem->create();
			if ($this->TmItem->save($this->request->data)) {
				$this->Session->setFlash(__('The tm item has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The tm item could not be saved. Please, try again.'));
			}
		}
		$tmCampaigns = $this->TmItem->TmCampaign->find('list');
		$this->set(compact('tmCampaigns'));
	}

/**
 * admin_edit method
 *
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		$this->TmItem->id = $id;
		if (!$this->TmItem->exists()) {
			throw new NotFoundException(__('Invalid tm item'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->TmItem->save($this->request->data)) {
				$this->Session->setFlash(__('The tm item has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The tm item could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->TmItem->read(null, $id);
		}
		$tmCampaigns = $this->TmItem->TmCampaign->find('list');
		$this->set(compact('tmCampaigns'));
	}

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