<?php
App::uses('AppController', 'Controller');
/**
 * ItemMailings Controller
 *
 * @property ItemMailing $ItemMailing
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class ItemMailingsController extends AppController {

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

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_view($mailingid) {
	    
	    /** 
	     * Validando se o mailing pertence ao usuario
	     */
	    $this->loadModel('Mailing');
	    $this->Mailing->create();
	    $this->Mailing->recursive = 1;
	    $Mailings = $this->Mailing->findById($mailingid);
	    
	    if (($Mailings['Mailing']['user_id'] == $this->Auth->user('id')) ||
	                ($this->Auth->user('role_id') == '1')) {
	        
    	    $conditions = array(
    	            'ItemMailing.mailing_id' => $mailingid,
    	            );
    	    
    	    $this->ItemMailing->recursive = 0;
    	    $this->Paginator->settings = array(
    	            'conditions' => $conditions,
    	            'order' => array(
    	                    'ItemMailing.id' => 'asc',
    	            ),
    	    );
    	    $this->set('itemMailings', $this->Paginator->paginate());	 
    	    $this->set('mailing_id', $mailingid);   
	    } else {
	        $this->Session->setFlash(__('Este registro não existe'));
	        return $this->redirect(array(
    	                'controller' => 'mailings',
    	                'action' => 'index',
	                    'admin' => true,
	                    'plugin' => 'mailings',
	                ));
	    }    
	}
}
