<?php


App::uses('AppController', 'Controller');


/**


 * Portfolios Controller


 *


 * @property Portfolio $Portfolio


 * @property PaginatorComponent $Paginator


 */


class EventsController extends AppController {





/**


 * Components


 *


 * @var array


 */


	public $components = array('Paginator');





/**


 * admin_index method


 *


 * @return void


 */

public function index() {


		$portfolios = $this->Event->find('all', array(

		 	'recursive' => 0,	

			'order' => array(

				'Event.id' => 'desc'

			),

			'conditions' => array(

			),

		));

		$this->set(compact('portfolios'));

	}
	
 


 	public function admin_index() {

		$portfolios = $this->Event->find('all', array(

		 	'recursive' => 0,	

			'order' => array(

				'Event.id' => 'desc'

			),

			'conditions' => array(

			),

		));

		$this->set(compact('portfolios'));

	}


	public function view($id = null) {
 
        $event = $this->Event->find('first', [
            'recursive' => -1,
            'conditions' => [              
               // 'Event.active' => 1,
                'Event.slug' => $id
            ]
        ]);
		  
			
        if (empty($event)) {
          
			throw new NotFoundException('Could not find this Page.');		
			exit;
			
        }		
 	 
	if(!empty($event['Event']['meta_title'])) {	
		$meta_title   = $event['Event']['meta_title'];		
 	
		
	}
	if(!empty($event['Event']['meta_keyword'])) {	
		$meta_keyword = $event['Event']['meta_keyword'];
		
 		
	
	}
	if(!empty($event['Event']['meta_desc'])) {	
		$meta_desc   = $event['Event']['meta_desc'];
		
 		
		
	}
			
	$this->set(compact('event','meta_title','meta_keyword','meta_desc'));		
		
 
    }

/**


 * admin_add method


 *


 * @return void


 */


	public function admin_add() {

		if ($this->request->is(array('post', 'put'))) {

					 $filename = "";

					$this->request->data['Event']['slug'] = $this->generateSlug($this->request->data['Event']['name']);

		if(!empty($this->data['Event']['image']['name']))

                    {

					  $filename = $this->process_image($this->request->data['Event']['image'], 'home-img-'.$this->request->data['Event']['slug'],'events/');


					}

						$this->request->data['Event']['image'] = $filename;

						$filename1 = "";

 					if(!empty($this->data['Event']['pdf']['name']))

                {

					  $filename1 = $this->process_document($this->request->data['Event']['pdf'], 'pdf-'.$this->request->data['Event']['name'],'events/');
	 }

		$this->request->data['Event']['pdf'] = $filename1; 

			$this->Event->create();


			if ($this->Event->save($this->request->data)) {


				$this->Session->setFlash(__('The Event has been saved.'));


				return $this->redirect(array('controller' => 'events', 'action' => 'index' ));


			} else {


				$this->Session->setFlash(__('The Event could not be saved. Please, try again.'));

			}

		}	

	}

/**


 * admin_edit method


 *


 * @throws NotFoundException


 * @param string $id


 * @return void


 */


	public function admin_edit($id = null) {


		if (!$this->Event->exists($id)) {


			throw new NotFoundException(__('Invalid Event'));

		}

		$options = array('conditions' => array('Event.' . $this->Event->primaryKey => $id));


		$portfolio_details = $this->Event->find('first', $options);

		$filename = $portfolio_details['Event']['image'];

		if ($this->request->is(array('post', 'put'))) {

					$this->request->data['Event']['slug'] = $this->generateSlug($this->request->data['Event']['name']);

			 if(!empty($this->data['Event']['image']['name']))


                    {

					    $filename = $this->process_image($this->request->data['Event']['image'], 'img-'.$this->request->data['Event']['slug'],'events/');  

					}

			$this->request->data['Event']['image'] = $filename;

					  $filename1 = $portfolio_details['Event']['pdf'];

				if(!empty($this->data['Event']['pdf']['name']))

                {

					  $filename1 = $this->process_document($this->request->data['Event']['pdf'], 'pdf-'.$this->request->data['Event']['name'],'events/');

	 }

		$this->request->data['Event']['pdf'] = $filename1;	

			if ($this->Event->save($this->request->data)) {

				$this->Session->setFlash(__('The Event has been saved.'));

				return $this->redirect(array('controller' => 'events', 'action' => 'index' ));

			} else {

				$this->Session->setFlash(__('The Event could not be saved. Please, try again.'));

			}

		} else {

			$options = array('conditions' => array('Event.' . $this->Event->primaryKey => $id));

			$this->request->data = $this->Event->find('first', $options);

		}		

}





/**


 * admin_delete method


 *


 * @throws NotFoundException


 * @param string $id


 * @return void


 */


	public function admin_delete($id = null) {


		$this->Event->id = $id;


		if (!$this->Event->exists()) {


			throw new NotFoundException(__('Invalid Event'));


		}


		$this->request->allowMethod('post', 'delete');


		if ($this->Event->delete()) {


			$this->Session->setFlash(__('The Event has been deleted.'));


		} else {


			$this->Session->setFlash(__('The Event could not be deleted. Please, try again.'));


		}


		return $this->redirect(array('action' => 'index'));


	}


}


