<?php
App::uses('AppController', 'Controller');
/**
 * Programs Controller
 *
 * @property Program $Program
 * @property PaginatorComponent $Paginator
 */
class ProgramsController extends AppController {

	     public $layout = 'procedure';

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

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Program->recursive = 0;
		$user_id = $this->Session->read('Auth.User.id');
		// if (!$this->Program->exists($id)) {
		// 	throw new NotFoundException(__('Invalid program'));
		// }
		$options = array('conditions' => array('Program.user_id' => $user_id));
		$this->set('programs', $this->Program->find('all', $options));
		$this->set('countprograms', $this->Program->find('count', $options));


		// $this->set('programs', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Program->exists($id)) {
			throw new NotFoundException(__('Invalid program'));
		}
		$options = array('conditions' => array('Program.' . $this->Program->primaryKey => $id));
		$this->set('program', $this->Program->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */	
	
	
	public function add() {
		if ($this->request->is('post')) {
			$this->Program->create();
			if ($this->Program->save($this->request->data)) {
				$this->Flash->success(__('The program has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Flash->error(__('The program could not be saved. Please, try again.'));
			}
		}
		$users = $this->Program->User->find('list');
		$this->set(compact('users'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Program->exists($id)) {
			throw new NotFoundException(__('Invalid program'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Program->save($this->request->data)) {
				$this->Flash->success(__('The program has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Flash->error(__('The program could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Program.' . $this->Program->primaryKey => $id));
			$this->request->data = $this->Program->find('first', $options);
		}
		$users = $this->Program->User->find('list');
		$this->set(compact('users'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Program->id = $id;
		if (!$this->Program->exists()) {
			throw new NotFoundException(__('Invalid program'));
		}
		$this->request->allowMethod('post', 'delete');
		if ($this->Program->delete()) {
			$this->Flash->success(__('The program has been deleted.'));
		} else {
			$this->Flash->error(__('The program could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}


}


